Apply transformations on selected images

Is there a way to apply transformations to only selected images?

To be more precise, I’d like in sequence of code below:

data = src.transform(tfms)

to apply the transformations only to images having names with specific patterns. Another application would be to make data augmentation only for some part of the ImageList.

Of course a workaround would be to create additional images upfront of the process.

Thanks!

3 Likes

+1. I have the same need. I want to apply certain types of transforms to images based on their file name. That would be a welcome addition to the options of the transform method.

Use “filters” on src:

https://docs.fast.ai/data_block.html#ItemList.filter_by_func

Note: it changes your “src” so save/duplicate it first.

Doesn’t filter_by_func and co filter data that are read into the input list? What we want to do is apply transforms selectively on all input images, not filter when reading in the data.

I’d recommend looking at the __get_item__ method in the LabelList class. You could write a custom LabelList which overloads __get_item__ and only applies transforms if the input passes some filter. Then, you can monkey patch that custom class into the usual data block flow like this:

src = (ImageList.from_folder(path)
        .split_by_folder())
src.train._label_list = CustomLabelListClass
data = (src.label_from_folder()
         .databunch())

It’s not elegant, but I’ve been using this technique to alter how augmentation is applied.

1 Like