forked from pytorch/serve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_classifier.py
40 lines (33 loc) · 1.11 KB
/
image_classifier.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
"""
Module for image classification default handler
"""
import torch
import torch.nn.functional as F
from torchvision import transforms
from .vision_handler import VisionHandler
from ..utils.util import map_class_to_label
class ImageClassifier(VisionHandler):
"""
ImageClassifier handler class. This handler takes an image
and returns the name of object in that image.
"""
topk = 5
# These are the standard Imagenet dimensions
# and statistics
image_processing = 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])
])
def set_max_result_classes(self, topk):
self.topk = topk
def get_max_result_classes(self):
return self.topk
def postprocess(self, data):
ps = F.softmax(data, dim=1)
probs, classes = torch.topk(ps, self.topk, dim=1)
probs = probs.tolist()
classes = classes.tolist()
return map_class_to_label(probs, self.mapping, classes)