Simple single image prediction

Training the models works fine and fast! But could you add a simple prediction method for a single image?

You can just call the pytorch model directly in the usual way. Remember to call eval on it first. Let me know if that doesn’t solve your problem.

2 Likes

I used the Mnist example, but when trying to predict single images, I end up with the same class every time (not that I’m very new to PyTorch, I picked it up because of Fast.ai).

data = image_data_from_folder(MNIST_PATH, size=224)
learn = ConvLearner(data, tvm.resnet34, metrics=accuracy)
learn.load(“mnisttest”)
model = learn.model
model.eval()
model = torch.nn.DataParallel(model).cuda()
trans = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
#im = Image.open("…/data/mnist_sample/train/3/7.png")
im = Image.open("…/data/mnist_sample/train/7/15.png")
im = im.convert(‘RGB’)
tens = trans(im)
tens = tens.unsqueeze(0)
inp = Variable(tens)
pred = model(inp)
pred
Out[9]:
tensor([[ 8376.8799, -7820.1201]], device=‘cuda:0’, grad_fn=<ThAddmmBackward>)