Resize but no crop (or flip)

I am trying to figure out how to load an image data bunch with resizing but no crop or any flips.

A similar question has been asked in fastai users and I don’t see an answer where cropping is omitted. Apologies for duplicate - I am hoping there are more people looking at V3 forums right now.

I’ve tried various things like:

  • Custom get_transform() with no flip
  • Editing the result of get_transform and removing the Crop transforms
  • Using no transforms

Having no Crop transforms (or no transforms) results in PyTorch data loader errors with tensor size mismatches.

Do I have to manually resize the images and then use no transforms? I can do that but hoping for an easier way.


What are you looking to do instead of crop? Pad? Or squish?


Thanks for quick response @jeremy!

I am trying to classify charts and the right edge of the chart is especially important for this exercise. So the primary goal is to not crop the right edge (and also no flips). Otherwise, some skew & rotate is OK.

I assume this is the default behavior:

Is it possible to do crops like these:

I tried to find a way to do crop with padding with the transform parameters but wasn’t successful.

I ultimately did something along the lines of @arunoda and it worked great (with white padding which works just fine for my application)

magick mogrify -resize 224x224 -background white -gravity center -extent 224x224 foo.png



I tried exactly that - do_crop=False, padding_mode='zeros' in ImageDataBunch.from_folder along with get_transforms(do_flip=False, max_zoom=1) but I got errors in PyTorch dataloader.

So I went with native imagemagick resize with padding.

Without seeing your error I can’t really help - sorry. But remember the docs are all runnable jupyter notebooks so you can try it out yourself there.

Thanks @jeremy, this is what I get

/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/ in _process_next_batch(self, batch)
656 self._put_indices()
657 if isinstance(batch, ExceptionWrapper):
–> 658 raise batch.exc_type(batch.exc_msg)
659 return batch

RuntimeError: Traceback (most recent call last):
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/”, line 138, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File “/anaconda/envs/py36/lib/python3.6/site-packages/fastai/”, line 91, in data_collate
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/”, line 232, in default_collate
return [default_collate(samples) for samples in transposed]
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/”, line 232, in
return [default_collate(samples) for samples in transposed]
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/”, line 209, in default_collate
return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 353 and 356 in dimension 3 at /opt/conda/conda-bld/pytorch-nightly_1540496378087/work/aten/src/TH/generic/THTensorMoreMath.cpp:1317

Oh interesting.I’ll have a look this.

Sorry I should have been more specific. We need your code as well. Here’s some tips to save you some time next time:

If you only want resize and no transforms (i.e. you already have squared images but want to train on smaller size) you have to use a little trick to make that work. Not sure this is the best way, maybe someone has a better way?

Basically you have to create a dummy tranform that does nothing in order for the size parameter to work.

1 Like

Per other thread, @sgugger has added a resize_method parameter that helps with this.

Thanks @jeremy and @sgugger!

Yes, the resize_method should do the trick.

data = (ImageList.from_folder(path)
.transform(get_transforms(), size=64, resize_method=ResizeMethod.SQUISH)

def get_data(bs,size):
    data = (src.label_from_func(lambda x: f'{path_to_good_images}/{}')
           .transform(get_transforms(max_zoom=1.), size=size, resize_method=ResizeMethod.SQUISH,tfm_y=True)
           .databunch(bs=bs).normalize(imagenet_stats, do_y=True))

    data.c = 3
    return data
data_gen = get_data(bs,size)

data_gen.show_batch(4) is showing orignal image as distorted too and i want to stop the flip w.r.t y-axis.

this is output (right side is target image)

Screenshot from 2020-07-17 10-48-21
This is actual target image ( which should appear at right ) what i am doing wrong ?

for fastai v4.