Data.resize()

Hi mates,

I was just explaining the Dogbreeds notebook to a friend, and I noticed something which made me to question my understanding of the resize() utility method provided by fastai.

The code, at the end of the data loading function, says:

return data if sz > 300 else data.resize(340, 'tmp')

Now, my understanding was that:

  1. If you let the loader rescale you pics, and they are big, it will take forever.
  2. In order to prevent that, resize() does crop the pics if asked to.

Now in the code above, we are saying: “Return the pics if they are big, else crop them”.

Which seems to be a plain nonsense, since it should be the contrary: the pics have to be cropped if they are bigger than a threshold.

Am I wrong?

1 Like

Your understanding is right, you’re just misreading that one line of code.

If you look at the notebook, sz is the size image your model will expect. If you hand it a larger (or smaller) image, the transformer will transform it on the fly. So therefore, if your model is expecting a small image, this tells it to resize it beforehand to reduce the work that has to be done by the transformer (or more likely, the amount of data that has to be transferred).

1 Like

I don’t understand it too.

My understanding is that if the size of img is less than 300. we use the data directly.
However if the size is less than 300, we resize to make it larger?

if sz > 300:
return data
elif 0 < sz <=300:
return data.resize(340, ‘tmp’)

It’s kind of a compromise.

The larger the extent of a resizing operation by the dataloader, the longer it takes. Remember that the dogbreeds dataset contains images which are substantially larger than the the typical sized liked by pretrained models (224, 299, etc…).
So, putting that line of code in words along with its rationale, it would be:

“If we specify a size over 300x300 px, you can feed it to the loader, and let it accomplish the proper resize, since it would not take too long (hopefully).
If we specified a size under that threshold, then probably it would take too long for the loader to resize them properly: in this case, just center-crop them to 340x340, and hope that the important information were not in the vicinity of the edges.”