Input image size for class prediction

Hello,

Using fastai, I trained a ResNet model on my image dataset and got a high classification accuracy.
Now, I would like to use this trained model on new images.

During CNN training, I had scaled the images down to 400x400 pixels by using
data = ImageDataBunch.from_folder(datasetPath, valid_pct=0.2, size=400, bs=32)

Now, for the classification of new test images (inference), I believe I need to rescale them to this size too. However, after loading my trained model with load_learner, I am not sure how I can define this image transformation when taking the image as input. I have the following code:

my_new_image = open_image("image-1.jpg")
learner.predict(my_new_image)

I am wondering if there is a way to do the image rescaling for these methods (similar to the size=400 in ImageDataBunch). Otherwise, I am afraid I may get inaccurate results (because the input images are much bigger).

Thank you very much for your help during these trying times! :slight_smile:

Hey

The way I did it was:

tfms = get_transforms(do_flip=False, p_affine=0, p_lighting=0)[0]
img = open_image('temp.png')
img = img.apply_tfms(tfms=tfms, size=(224, 336), resize_method=3
prediction = learn.predict(img)

I’m just starting out as well so anyone can correct me if I messed up.

1 Like

In fastai, you can use the pil2tensor function to convert your image to a PyTorch tensor, then resize it to your desired dimensions. Here’s a quick example:
from fastai.vision import open_image, pil2tensor, Image

Open your new image

my_new_image = open_image(“image-1.jpg”)

Resize the image to 400x400 pixels

my_new_image = Image(pil2tensor(my_new_image.data, np.float32).resize((400,400)))

Now you can make predictions with your trained model

learner.predict(my_new_image)

This should help maintain consistency between your training and inference data. Also, consider using a jpg compress tool to make the images occupy less place.