Shanmugaraj Kamaraj
Shanmugaraj Kamaraj

Reputation: 33

Image augumentation in Fastai

I would like to use the image data set using Fastai.

tfms = get_transforms(max_rotate=25)
data = ImageDataBunch.from_folder("/content/drive/My Drive/Colab Notebooks/images", valid_pct=0.2, size=224)
data.normalize()

I successfully loaded the images, but now I don't know how to do the augmentation and save the generated images using Fastai.

The augmentations can be changing brightness, turning the images horizontally, etc. For example, changing brightness can be done with:

tfms = [brightness(change=(0.1, 0.9), p=1.0),]

I get confused when reading the blogs. I am stuck and I don't know how to proceed further. I would like to apply this change to every image and save them to a separate directory.

Any help, thanks

Upvotes: 0

Views: 393

Answers (1)

Pablo
Pablo

Reputation: 1491

I think you are doing things almost right, but you are confused about how they should work.

From one example I have (essentially doing the same as the first lesson in Fastai):

transforms = get_transforms(max_rotate=20, max_zoom=1.3, max_lighting=0.4, max_warp=0.4, p_affine=1., p_lighting=1.)

Don't forget to pass these transforms to your data loaders!

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=transforms, size=224, bs=64).normalize(imagenet_stats)

This will enable these data operations (rotations, zoom, etc), but these operatiosn are not used to create and save new images! Instead, they are applied every time you get a batch of images, effectively giving you unlimited variations of your data. Saving a few of these variations would make your dataset heavier and would not help you in any way.

Note that these operations are used in training, but normally not in inference (when you need to predict on real, unseen data). (There is an advanced technique that makes inference on a mini-batch of transformations of your original image.)

Upvotes: 2

Related Questions