Resizing images for progressive img resizing

I am trying to do progressive image resizing on a multilabel classification problem.

I have a function to get data loader as follows:

def get_dls(size, bs):
    tfms = L([Resize(size), FlipItem(p = 0.2), Rotate(max_deg=15)])
    return ImageDataLoaders.from_df(labels, trn_path, label_delim=';', batch_tfms=tfms, bs = bs, seed = 42)

However, it does not resize the images. I can do it using item_tfms however that will do it on CPU and it will be slow (I have a lot of images).

How can I resize it under batch_tfms?

1 Like

You’d need to resize everything beforehand and then you can resize it in a batch transform. IE resize everything completely separately and then remove your Resize transform completely from the item_tfms. Everything needs to be the same size for it to work on a batch_tfm

Hey Mueller,
Every image is (256,256). Now I’m trying to vary sizes from 64 to 256 hence I’m directly putting it in batch_tfms.

Then just put Resize in the batch_tfms like you have above. What do you mean it won’t resize the image? You mean one_batch() doesn’t return the size you want?

Yes, when I do dls.show_batch() the image quality is good, even if I do size 32. Will it not resize for show_batch?

Hi @dipam7, I am using a very similar function to yours and everything seems to be working for me.
See below.
Please disregard the fold logic, as it has nothing to do with your question. I am loading images from a pd.DataFrame in which the training set has been split in multiple folds.

def get_dls(bs, size, fold, df):
    df_fold = df.copy()
    df_fold = df_fold.loc[df_fold.fold==fold].reset_index()
    
    dblock = DataBlock(blocks = (ImageBlock, CategoryBlock),
                   get_x = get_x, 
                   get_y = get_y,
                   splitter=IndexSplitter(df_fold.loc[df_fold.which=='valid'].index),
                   item_tfms=Resize(700),
                   batch_tfms=aug_transforms(size=size, max_rotate=30., min_scale=0.75, flip_vert=True, do_flip=True))
    dls = dblock.dataloaders(df_fold, bs=bs)
    assert (len(dls.train_ds) + len(dls.valid_ds)) == len(df_fold)
    return dls

As you can see there is a clear difference in resolution between the first and the second example below.


3 Likes

Thank you for the response, I saw a similar example in Fastbook however I did not use aug_transform since it has a lot of other transforms that I did not want and I would have had to turn all of them off. I’ll give it a try though. I have used item_tfms = Resize(size) for now. Let’s see if I can figure out why it’s not working.

1 Like

I see, please keep us posted!