Source code for ts.torch_handler.image_classifier
"""
Module for image classification default handler
"""
import torch
import torch.nn.functional as F
from torchvision import transforms
from ts.handler_utils.timer import timed
from ..utils.util import map_class_to_label
from .vision_handler import VisionHandler
[docs]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]),
]
)
@timed
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)