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
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
I got this from fastai docs. Adding related to question stuff you can check everything here Related to augs
class AlbumentationsTransform(RandTransform):
"A transform handler for multiple `Albumentation` transforms"
split_idx,order=None,2
def __init__(self, train_aug, valid_aug): store_attr()
def before_call(self, b, split_idx):
self.idx = split_idx
def encodes(self, img: PILImage):
if self.idx == 0:
aug_img = self.train_aug(image=np.array(img))['image']
else:
aug_img = self.valid_aug(image=np.array(img))['image']
return PILImage.create(aug_img)
Basically this is all you need
For setting diff augs use
def get_train_aug(): return albumentations.Compose([
albumentations.RandomResizedCrop(224,224),
albumentations.Transpose(p=0.5),
])
def get_valid_aug(): return albumentations.Compose([
albumentations.CenterCrop(224,224, p=1.),
albumentations.Resize(224,224)
], p=1.)
And then
item_tfms = [Resize(256), AlbumentationsTransform(get_train_aug(), get_valid_aug())]
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat, item_tfms=item_tfms)
dls.train.show_batch(max_n=4)
Enjoy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With