How does get_transforms work?

anybody know how does method really work. it does not seem to add more data to the existing dataset. so it just convert some portion of the existing data? how big? Thanks.

1 Like

Have you read the documentation? And it’s applied over the entire dataset

read through it, mentioned ‘we do small random transformations’
what is small means? what percentage is being transform? that is my questions … why not ADD to the original dataset. From what i see the number of dataset remains the same.

say if you have 1000 dataset, after the tranformation still 1000, say 10% is being transform. Then 100 is ‘new’ tranform dataset. This means we ‘lose’ the original 100 dataset. I don’t think this is a good practice. It is better to ADD on top of the original dataset. In that way you don’t lose any valuable original information.

The transformations are applied on the fly when the DataLoader builds the batch. Most of the tranformations have probabilities of occurrence which means only a percentage of the images is transformed (let’s say 10% of the1000 images in the dataset as you mentionned in your message), and the rest (90%) stay inchanged.

Like you mentionned here above, we are basically throwing 10% of our images when we train our model for 1 epoch.

But remerber that we train our model for a certain number of epochs (each single epoch corresponds to the whole datase). So, let’s say we train it for 20 epochs that means we will have 20,000 images (20 x 1000 images) to be used out of which 2000 are transformed images (20 x 100 images). Thereforefore, there is a good chance that our model has seen almost all the original images plus the extra 2000 transformed images.

If we do not want to do the transformations on the fly, we can create the transformed images and store them on disk before training. I guess we can also combine them with the original images (by chosing a certain" transformed/original" ratio) and train our model. On the other hand, when the transformations are done on the fly the randomness is handled by the fastai library.


thank you Farid. How do you know that ? from looking at the code?

so, it is assume that the algorithm has kind of ‘k-fold’ built in? meaning for every epoch it will transform a different subset of the dataset, so at the end (after k epoch) they have seen all the data?

This means if we should not use this for epoch=1 … now i am intrique to see the actual code

by following the fastai-v1 and fast-v2-dev source code. Also by watching Jeremy’s Part -2 videos and Fastai v2 code walk-thru videos.

To undestand that is going on wrt the transformations, you can check out the source code file and have a look at the DeviceDataLoader class and especially the function __iter__(self), it calls proc_batch(self,b:Tensor)

 def proc_batch(self,b:Tensor)->Tensor:
        "Process batch `b` of `TensorImage`."
        b = to_device(b, self.device)
        for f in listify(self.tfms): b = f(b)
        return b

    def __iter__(self):
        "Process and returns items from `DataLoader`."
        for b in self.dl: yield self.proc_batch(b)

You can see that in proc_batch we iterate through all the transforms self.tfms and apply all of them to the batch recursively (b = f(b))

For your second question (K-fold), AFAIK fastai library does not implement the K-fold Cross Validation. Both your train dataset and validation dataset are fixed. The training is done using the same training dataset.

If you are intrested in implementing the K-Fold in fastai, check out this post

As for epoch=1, training a model needs to be done with more epochs. The number of epochs depends on may factors including checking you training loss, your validation loss, your accuracy, cost of training, etc…

PS: if you are intersted in studing the source code, you can check it on github or even better clone the fastai repo (v1 and/or v2) and use Visual Studio Code if you haven’t already installed it yet or use vim like jeremy does.

Have a great week-end!


I think an easy solution would be to prepare dataset 2 times. In the first epoch do not perform any transformations, then save results (‘stage-1’)). Purge everything (easiest: reset kernel) and prepare dataset now performing transformations and load state after 1-st epoch (learn.load(‘stage-1’)).

Hi Farid,

What percent of data is transformed in the fly as we have not explicitly specified it.
( b = f(b) ) i understand it is done recursively but what percentage.

Thanking you,

The randomness is introduced in this Transform class that mostly all transform functions inherit from:

The random probabilities for image transforms using get_transforms are specified in its definition using p_affine keyword argument.