How to cast fastai Image to a Pytorch Tensor?

Hello everyone!

I am trying to extract features of an image from ResNet50 model. Kindly refer the image below

I have removed the top layer of classification and the ReLU, BatchNorm1D and Dropout layers just preceding that so that I can obtain a 512 length feature vector for my images.

But when I do that and try to make a forward pass, I need to convert the datatype of the image from a fastai.vision.image.Image to a tensor. Otherwise I cannot make the forward pass as seen in the screenshot below.


Can someone please explain a way to cast a fastai Image to a PyTorch tensor?

Thanks for your time.

With Regards,
Vinayak.

try x.px or x.data

1 Like

Thanks Julian! It worked…