For the pytorch models I found this tutorial explaining how to classify an image. I tried to apply the same prcedure for an inception model. However the model fails for every image I load in
Code:
# some people need these three lines to make it work
#from torchvision.models.inception import model_urls
#name = 'inception_v3_google'
#model_urls[name] = model_urls[name].replace('https://', 'http://')
from torch.autograd import Variable
import torchvision
import requests
from torchvision import models, transforms
from PIL import Image
import io
from PIL import Image
LABELS_URL = 'https://s3.amazonaws.com/outcome-blog/imagenet/labels.json'
# cat
IMG_URL1 = 'http://farm2.static.flickr.com/1029/762542019_4f197a0de5.jpg'
# dog
IMG_URL2 = 'http://farm3.static.flickr.com/2314/2518519714_98b01968ee.jpg'
# lion
IMG_URL3 = 'http://farm1.static.flickr.com/62/218998565_62930f10fc.jpg'
labels = {int(key):value for (key, value)
in requests.get(LABELS_URL).json().items()}
model = torchvision.models.inception_v3(pretrained=True)
model.training = False
model.transform_input = False
def predict_url_img(url):
response = requests.get(url)
img_pil = Image.open(io.BytesIO(response.content))
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
preprocess = transforms.Compose([
transforms.Scale(256),
transforms.CenterCrop(299),
transforms.ToTensor(),
normalize
])
img_tensor = preprocess(img_pil)
img_tensor.unsqueeze_(0)
img_variable = Variable(img_tensor)
fc_out = model(img_variable)
print("prediction:", labels[fc_out.data.numpy().argmax()])
predict_url_img(IMG_URL1)
predict_url_img(IMG_URL2)
predict_url_img(IMG_URL3)
As output I get this:
('prediction:', u"plunger, plumber's helper")
('prediction:', u'plastic bag')
('prediction:', u"plunger, plumber's helper")
I found out that one needs to call
model.eval()
before applying the model. Because of the batch normalisations and also dropout layers, the model bahaves differently for training and testing.