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
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)