Simple single image prediction

(Chris Fotache) #1

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

(Jeremy Howard (Admin)) #2

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.

(Chris Fotache) #3

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

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