import json import logging import sys import torch import torch.nn as nn import torch.nn.functional as F logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler(sys.stdout)) # Based on https://github.com/pytorch/examples/blob/master/mnist/main.py class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=1) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") def model_fn(model_dir): model = Net().to(device) model.eval() return model # data preprocessing def input_fn(request_body, request_content_type): assert request_content_type == "application/json" data = json.loads(request_body)["inputs"] data = torch.tensor(data, dtype=torch.float32, device=device) return data # inference def predict_fn(input_object, model): with torch.no_grad(): prediction = model(input_object) return prediction # postprocess def output_fn(predictions, content_type): assert content_type == "application/json" res = predictions.cpu().numpy().tolist() return json.dumps(res)