<reponame>gyan42/ml-serving-playground

import torch
import torchvision.transforms as Transforms

# Read the categories
with open("imagenet_classes.txt", "r") as f:
    categories = [s.strip() for s in f.readlines()]

model = torch.hub.load('pytorch/vision:v0.6.0', 'resnext50_32x4d', pretrained=True)
# or
# model = torch.hub.load('pytorch/vision:v0.6.0', 'resnext101_32x8d', pretrained=True)
model.eval()

def resnext(input_image):
    preprocess = Transforms.Compose([
        Transforms.Resize(256),
        Transforms.CenterCrop(224),
        Transforms.ToTensor(),
        Transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    input_tensor = preprocess(input_image)
    input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model

    # move the input and model to GPU for speed if available
    if torch.cuda.is_available():
        input_batch = input_batch.to('cuda')
        model.to('cuda')

    with torch.no_grad():
        output = model(input_batch)
    # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
    # print(output[0])
    # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
    probabilities = torch.nn.functional.softmax(output[0], dim=0)
    # print(probabilities)

    # Show top categories per image
    top5_prob, top5_catid = torch.topk(probabilities, 5)

    predictions = {}
    for i in range(top5_prob.size(0)):
        # print(categories[top5_catid[i]], top5_prob[i].item())
        predictions[categories[top5_catid[i]]] = top5_prob[i].item()
    return predictions
import torch
import torchvision.transforms as Transforms

# Read the categories
with open("imagenet_classes.txt", "r") as f:
    categories = [s.strip() for s in f.readlines()]

model = torch.hub.load('pytorch/vision:v0.6.0', 'resnext50_32x4d', pretrained=True)
# or
# model = torch.hub.load('pytorch/vision:v0.6.0', 'resnext101_32x8d', pretrained=True)
model.eval()

def resnext(input_image):
    preprocess = Transforms.Compose([
        Transforms.Resize(256),
        Transforms.CenterCrop(224),
        Transforms.ToTensor(),
        Transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    input_tensor = preprocess(input_image)
    input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model

    # move the input and model to GPU for speed if available
    if torch.cuda.is_available():
        input_batch = input_batch.to('cuda')
        model.to('cuda')

    with torch.no_grad():
        output = model(input_batch)
    # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
    # print(output[0])
    # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
    probabilities = torch.nn.functional.softmax(output[0], dim=0)
    # print(probabilities)

    # Show top categories per image
    top5_prob, top5_catid = torch.topk(probabilities, 5)

    predictions = {}
    for i in range(top5_prob.size(0)):
        # print(categories[top5_catid[i]], top5_prob[i].item())
        predictions[categories[top5_catid[i]]] = top5_prob[i].item()
    return predictions
# Read the categories # or # model = torch.hub.load('pytorch/vision:v0.6.0', 'resnext101_32x8d', pretrained=True) # create a mini-batch as expected by the model # move the input and model to GPU for speed if available # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes # print(output[0]) # The output has unnormalized scores. To get probabilities, you can run a softmax on it. # print(probabilities) # Show top categories per image # print(categories[top5_catid[i]], top5_prob[i].item())