How to Classify Single Image using Loaded Net

I use something like this. I think it’s a mashup of code from various tutorials/examples so my apologies if I should be crediting someone…

uses PIL and torchvision.transforms

imsize = 256
loader = transforms.Compose([transforms.Scale(imsize), transforms.ToTensor()])

def image_loader(image_name):
    """load image, returns cuda tensor"""
    image = Image.open(image_name)
    image = loader(image).float()
    image = Variable(image, requires_grad=True)
    image = image.unsqueeze(0)  #this is for VGG, may not be needed for ResNet
    return image.cuda()  #assumes that you're using GPU

image = image_loader(PATH TO IMAGE)

your_trained_net(image)

hope that helps

12 Likes