Convert a JpegImageFile to a tensor to train a ResNet

Hello,
l have a jpeg image of (3,224,244). l need to put it in a variable image but it needs to be convert to a tensor (1,3,244,224) to train a Resnet152.
l did the following :

from PIL import Image
img_path="/data/v_PlayingSitar_g25_c04_0256.jpeg"
#image = torch.Tensor(1,3, 224, 224).normal_()
image = Image.open(img_path)
image = Variable(image)

l got the following error :
RuntimeError: Variable data has to be a tensor, but got JpegImageFile

Thank you

You need to convert the PIL image to torch.Tensor. You can do this using torchvision.transforms.ToTensor, like this:

from PIL import Image
from torchvision.transforms import ToTensor

img_path="/data/v_PlayingSitar_g25_c04_0256.jpeg"
image = Image.open(img_path)
image = ToTensor()(image).unsqueeze(0) # unsqueeze to add artificial first dimension
image = Variable(image)

You can find much more information in the tutorials:
http://pytorch.org/tutorials/
Here’s one specifically about data loading:
http://pytorch.org/tutorials/beginner/data_loading_tutorial.html#sphx-glr-beginner-data-loading-tutorial-py

2 Likes

Thank you @jytug, l got the following error after running

image = ToTensor(image).unsqueeze(0)
TypeError: object() takes no parameters

I have edited the answer, it should be correct now

1 Like

I got
TypeError: invalid shape (3,224,224) for image data

from PIL import Image
import cv2
for ind in labels[labels[‘LABEL’]==1][‘filename’][-5:]:
path = ind
imgr = cv2.imread(path)
imgr = cv2.resize(imgr,dsize=(224,224))
trans = transforms.ToTensor()
print(imgr.shape) #prints(224,224,3)
plt.imshow(trans(imgr)) #displayes error** invalid shape (3,224,224) for image data

matplotlib.pyplot.imshow expects a numpy array as [height, width, channels], so you would have to permute the transformed tensor and transform it back to a numpy array via:

x = trans(imgr)
x = x.permute(1, 2, 0).numpy()
plt.imshow(x)