Resize but no crop (or flip)


#1

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 https://forums.fast.ai/t/resize-instead-of-crop/28680 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.

Thanks!


(Jeremy Howard (Admin)) #2

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


#3

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.


(Arunoda Susiripala) #4

I assume this is the default behavior:

Is it possible to do crops like these:


#5

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


(Jeremy Howard (Admin)) #6

https://docs.fast.ai/vision.transform.html#Data-augmentation


#7

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.


(Jeremy Howard (Admin)) #8

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.


#9

Thanks @jeremy, this is what I get

/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py 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
660

RuntimeError: Traceback (most recent call last):
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, 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/torch_core.py”, line 91, in data_collate
return torch.utils.data.dataloader.default_collate(to_data(batch))
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, 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/dataloader.py”, line 232, in
return [default_collate(samples) for samples in transposed]
File “/anaconda/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, 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


(Arunoda Susiripala) #10

Oh interesting.I’ll have a look this.


(Jeremy Howard (Admin)) #11

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


(Marc Rostock) #12

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.


#13

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

Thanks @jeremy and @sgugger!