How to avoid cropping of Images?

I am trying to build a classifier for heatmaps. Here is a sample image:

But when I use data.show_batch(rows = 3), here is the result I get:

As we can see, almost 3 columns are getting cut out. Since each of the columns have a specific meaning, cutting them decreases accuracy. How can I fix this?

Here is the data bunch I am using:
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.20, size = 224 , num_workers=5)

1 Like

If all the images are of same size in your dataset, then remove the size parameter from the Databunch declaration and it should most probably work. Try it out.


The size=224 parameter calls the resize transform behind the scenes.

By default, a single int, (such as in size=224, will resize AND crop the image to (224, 224).
If you want to just resize the image and squish it to (224,224), without cropping anything out, use size=(224,224)

Alternatively, if you’re images are already the same size, you can get rid of the size parameter entirely, as suggested by @navidpanchi.

Let me know if this helps! :slight_smile:

See below for references & source link


Pytorch’s transforms.Resize(size) equivalent is implemented without an explicit transform function in fastai. It’s done via the arguments size and resize_method .

The size argument can be either a single int 224 , or a tuple of int s (224,400) . The default behavior is to crop the image to a square when a single int is passed and to squish it in the case of a tuple , so that:

  • if size=224 is passed, it will resize and then crop to (224,224)
  • if size=(224,400) is passed, it will squish it to (224,400)
  • if size=(224,224) is passed, it will squish (not crop!) it to (224,224)

For more info, check out this source in the docs :slight_smile: :


Yup, it worked. Just a pointer to anyone else who may encounter this. I had to set num_workers = 0 and reduce the batch size since without it I was encountering CUDA Out of memory errors. Thanks!

@Epoching Thanks for the detailed answer! Yes, removing size made it work:)

Glad to hear :slight_smile: