Search code examples
fast-ai

Applying transforms to fastai v2 vision


In fastai v2 i am trying to add image augmentations

So

tfms = aug_transforms(do_flip = True,
                                 flip_vert=True, 
                                 max_lighting=0.1, 
                                 )
data = ImageDataLoaders.from_df(df,bs=5,item_tfms=tfms,folder=path_to_data)

this give output

Could not do one pass in your dataloader, there is something wrong in it

And when i do

data.show_batch()

it give

RuntimeError: "check_uniform_bounds" not implemented for 'Byte'

How to resolve


Solution

  • I didn't try the do_flip transformation, but what worked for me was to apply them not as item_tfms but as batch_tfms:

    item_tfms = [ Resize((200, 150), method='squish')]
     
    batch_tfms = [Brightness(max_lighting = 0.3, p = 0.4),
        Contrast(max_lighting = 0.6, p = 0.4),
        Saturation(max_lighting = 0.75, p = 0.4)]
     
    db = DataBlock(blocks = (ImageBlock, CategoryBlock),
                      get_items = get_image_files,
                      splitter = RandomSplitter(valid_pct=0.2, seed=42),
                      item_tfms=item_tfms,
                      batch_tfms=batch_tfms,
                      get_y = parent_label)
    

    You can then feed the DataBlock into a DataLoader like in the fastbook tutorial