generated from ashleve/lightning-hydra-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
42 lines (33 loc) · 1.29 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Any
from cog import BasePredictor, Input, Path
import json
import torch
import timm
import numpy as np
from timm.data.transforms_factory import transforms_imagenet_eval
from PIL import Image
class Predictor(BasePredictor):
def setup(self):
"""Load the model into memory to make running multiple predictions efficient"""
self.model = timm.create_model('efficientnet_b3a', pretrained=True)
self.model.eval()
self.transform = transforms_imagenet_eval()
with open("imagenet_1k.json", "r") as f:
self.labels = list(json.load(f).values())
# Define the arguments and types the model takes as input
def predict(self, image: Path = Input(description="Image to classify")) -> Any:
"""Run a single prediction on the model"""
# Preprocess the image
img = Image.open(image).convert('RGB')
img = self.transform(img)
# Run the prediction
with torch.no_grad():
labels = self.model(img[None, ...])
labels = labels[0] # we'll only do this for one image
# top 5 preds
topk = labels.topk(5)[1]
output = {
# "labels": labels.cpu().numpy(),
"topk": [self.labels[x] for x in topk.cpu().numpy().tolist()],
}
return output