how to test a batch of images through a pytorch model

I have a pytorch model and I can test a sample image by following scripts. I want to know how can I send a batch of images to this model. I am pretty noob at pytorch and tensorflow.

load one image

    img_trans = test_transform( img ) 
    img_trans = torch.unsqueeze(img_trans, dim=0)
    img_var = Variable(img_trans).cuda()
    score = model(img_var).data.cpu().numpy()

Also I want to use gpu for this task as well.

You could load a few images and create a batch of these tensors using e.g. torch.stack.
E.g. assuming each image has a shape of [3, 224, 224] and you are loading 5 images, the code would look similar to this:

x = []
# load 5 images
for _ in range(5):
    img = torch.randn(3, 224, 224) # load your actual image here and transform it to a tensor
    x.append(img)
x = torch.stack(x)
print(x.shape)
# > torch.Size([5, 3, 224, 224])

out = model(x)

Of course you could also use a Dataset etc. so I would generally recommend to take a look at the tutorials.

Also, remove the Variable usage, as it’s deprecated since PyTorch 0.4.