How to apply aug_transforms to validation set while training

Hello, I am building an image classifier, but all my images comes with a good angle, but I don’t think real world data in my case won’t be at that angle, I added aug_transforms (focus on warp) to my datablock but it only effect my training data, to 1/ present the real world data somehow and 2/ reduce overfitting a bit.
But the validation set which is the replacement of real world data, is not effected with this transformation, so all the images are with a perfect angle, so while am training I want to show the loss of the training set (effected by aug_transforms) and loss of valid set ((effected by aug_transforms))
an idea is to take all images in valid set and create a warp copy of them and save, but I don’t like this approach since it’s slow and require a space in the disk.
do you have any approach ?

but if your training data is both warped and good angel images, isn’t the validation data(that comes with a good angle) is warped from the models perspective. Then the model can work with both.
And this thread can help you with Test Time Augmentation Fastai2 Test Time Augmentation

i just think it’s cheating if my validation data is all in one angle

Yes I think you are right

  1. if certain transforms don’t make sense for your data then don’t use them, like for example warp for a satellite photo wouldn’t make sense since they’re always from the same angle but rotation or flips might make sense.

  2. transforms don’t use disc space as you say, it does a random transform in memory every time it grabs an image. You can grab the same image over and over and get different variations on the transforms you’ve asked for but it’s done in memory, it’s fast, and it results in a better model as long as you’re using sensible transforms for your data.

Thanks, but I already know that, my question is how can I apply the transformation to the validation set

Each transform has a split_idx that denotes behaviors, and the validation set starts out with split_idx of 1 always. The only way you’d be able to do something similar is by looking into the TTA @kelwa recommended, and it’s source code here: https://github.com/fastai/fastai/blob/master/fastai/learner.py#L554

As the general idea is using our training transforms when doing predictions

Taking inspiration from TTA, I believe something along the lines of the following should work:

dls[1].dataset.set_split_idx(0)

And then do

dls[1].show_batch()

to see if it worked, essentially overriding the internal split_idx to denote the training portion of transforms

Sorry I misunderstood you, I thought you were saying your real world data was all at the same angle, not your training data

1 Like

The big flag with this though is the augmentations are exactly the same as the training data’s. If you wanted to differentiate between the two (which I think you would?) You would want to override dls[1].after_item and dls[1].after_batch with some new transforms and cut out anything you don’t want. (be careful to only grab the actual augmentations if you’re taking stuff out)

Hey, I know the answer is a bit older. But maybe someone reads this way later as I did.

For me that didn’t work, because set_split_idx returns a contextlib._GeneratorContextManager object.

But with dls[1].dataset.set_split_idx(0): dls[1].show_batch() should work then.

1 Like