import numpy as np from torchvision.models import vgg16 model = vgg16(pretrained=True) model_parameters = filter(lambda p: p.requires_grad, model.parameters()) params = sum([np.prod(p.size()) for p in model_parameters]) print(params) import torch torch.save(model.state_dict(), "./vgg16_model.pt") import torch model = vgg16(pretrained=True) example_input = torch.rand(1, 3, 224, 224) torchscript_model = torch.jit.trace(model, example_input) torchscript_model.save("traced_vgg16_model.pt") import torch.nn as nn class ControlFlowModel(nn.Module): def __init__(self, N): super(ControlFlowModel, self).__init__() self.fc = nn.Linear(N,100) def forward(self, input): if input.sum() > 0: output = input else: output = -input return output model = ControlFlowModel(10) torchcript_model = torch.jit.script(model) torchscript_model.save("scripted_vgg16_model.pt") !pip install torchserve torch-model-archiver !pip install image_classifier captum !torch-model-archiver --model-name vgg16 \ --version 1.0 --serialized-file traced_vgg16_model.pt --handler \ image_classifier !mkdir -p /content/models && mv ./*.mar /content/models %%writefile config.properties inference_address=https://0.0.0.0:8091 management_address=https://0.0.0.0:8092 metrics_address=https://0.0.0.0:8093 !nohup torchserve --model-store ./models --start --models all --ts-config ./config.properties --ncs --foreground & !tail nohup.out !wget -O hotdog.jpg -q --show-progress https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTk5onR1hxG2h_yGFkgZvLVu7b7IY2PIuekKaagBG0nYFsqktcIwjYu6a7LT6OjTfEHWAU&usqp=CAU !curl --insecure https://localhost:8091/ping !curl --insecure https://localhost:8091/predictions/vgg16 -T hotdog.jpg !torchserve --stop !curl --insecure https://127.0.0.1:8093/metrics !ls -al ./logs !pip install onnx import onnx model = vgg16(pretrained=True) example_input = torch.rand(1, 3, 224, 224) onnx_model = torch.onnx.export(model, example_input, "vgg16.onnx") model = onnx.load("vgg16.onnx") onnx.checker.check_model(model) onnx.helper.printable_graph(model.graph) !wget -q --show-progress "https://raw.githubusercontent.com/raghakot/keras-vis/master/resources/imagenet_class_index.json" import socket print(socket.gethostbyname(socket.getfqdn(socket.gethostname()))) import io import json from torchvision import models import torchvision.transforms as transforms from PIL import Image from flask import Flask, jsonify, request app = Flask(__name__) imagenet_class_index = json.load( open("./imagenet_class_index.json")) model = models.vgg16(pretrained=True) image_transforms = transforms.Compose( [transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize( [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) def get_prediction(image_bytes): image = Image.open(io.BytesIO(image_bytes)) tensor = image_transforms(image) outputs = model(tensor.unsqueeze(0)) _, y = outputs.max(1) predicted_idx = str(y.item()) return imagenet_class_index[predicted_idx] @app.route('/predict', methods=['POST']) def predict(): if request.method == 'POST': file = request.files['file'] img_bytes = file.read() class_id, class_name = \ get_prediction(image_bytes=img_bytes) return jsonify({'class_id': class_id, 'class_name': class_name}) import threading threading.Thread(target=app.run, kwargs={'host':'0.0.0.0','port':5062}).start() import requests resp = requests.post( "http://localhost:5062/predict", files={"file": open('hotdog.jpg','rb')}) print(resp.json())