A three-class photo classifier that answers one question about a photo scraped
from a restaurant listing: is this a pizza, and if so, is it Neapolitan?
Bulk-labelling photos for a map of pizzerias: every place gets a verdict from a
vote over its photos rather than from any single prediction, and predictions
below 0.80 confidence are discarded before the vote. It is a filter for a map
legend, not a judgement about a restaurant.
1import torch
2from torchvision import models, transforms
3from PIL import Image
4
5ckpt = torch.load('best.pt', map_location='cpu')
6net = models.resnet18()
7net.fc = torch.nn.Linear(net.fc.in_features, len(ckpt['classes']))
8net.load_state_dict(ckpt['state_dict'])
9net.eval()
10
11tf = transforms.Compose([
12 transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(),
13 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
14])
15with torch.no_grad():
16 p = net(tf(Image.open('photo.jpg').convert('RGB'))[None]).softmax(-1)[0]
17print(dict(zip(ckpt['classes'], p.tolist())))
MIT.