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.
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 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.
/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
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.