Datablock handling of train images of different size

How does one handle creating a databunch when training images are of different size? Is there an automatic resizing functionality?

One would want to resize all images to a fixed dimension, but I’m not able to find any information to do that on-the fly with databunch. Currently I’m doing:

data = (ImageList.from_df(train_df, train_dir) .split_by_rand_pct(0.2) .label_from_df() .transform(get_transforms()) .databunch() .normalize(imagenet_stats))

I’m getting this warning and it shows the sizes of all my images.

It’s not possible to collate samples of your dataset together in a batch

From the vision.transform documentation:

data = ImageDataBunch.from_name_re(path_img, fnames, pat, size=224, bs=bs)

You can pass in a size parameter when creating the Databunch. When using the “full” datablock API (as in your example), the parameter is passed to the transform function:

transform(tfms=tfms, size=size, resize_method=ResizeMethod.SQUISH)

This also allows you to specify the method for resizing (squish in the call above)

1 Like

Thanks! That worked:

data = (ImageList.from_df(train_df, train_dir)
    .split_by_rand_pct(0.2)
    .label_from_df()
    .transform(tfms=tfms, size=512, resize_method=ResizeMethod.SQUISH)
    .databunch(bs=bs, num_workers=4).normalize(imagenet_stats))
1 Like