Creating Custom transform

Hi,
I am trying to implement a custom transform for blurring the image using opencv.
I defined the blur_ function as:
def blur_(img):
kernel = np.ones((5,5),np.float32)/25
img=np.array(img)
dst = cv2.filter2D(img,-1,kernel)
return PILImage.create(dst)
I created the custom transform by defining the blur_transform class:
class blur_transform(Transform):
def init(self, aug): self.aug = aug)
def encodes(self, img: PILImage):
aug_img = self.aug(img)
return aug_img

Instantiated the custom transform:
blr_tfm = blur_transform(blur_)

Defined the datablock as under:

dblock=DataBlock(blocks=(ImageBlock,CategoryBlock),
get_items=get_image_files,
get_y=label_func,
item_tfms=[Resize(224),blr_tfm],
batch_tfms=[aug_transforms()],
splitter=RandomSplitter(seed=42))
To convert to dataloaders, when I run the following:
dls=dblock.dataloaders(source/‘images’)
I get the following output:
Could not do one pass in your dataloader, there is something wrong in it
Where I am wrong?

1 Like

Cannot tell from the code you posed, could you create a notebook in colab or something so we can have a closer look at it?

Hey I reformated your post to make it readable…

Hi,
I am trying to implement a custom transform for blurring the image using opencv.
I defined the blur_ function as:

def blur_(img):
  kernel = np.ones((5,5),np.float32)/25
  img = np.array(img)
  dst = cv2.filter2D(img,-1,kernel)
  return PILImage.create(dst)

I created the custom transform by defining the blur_transform class:

class BlurTfm(Transform):
  def __init__(self, aug): 
    self.aug = aug
  def encodes(self, img: PILImage):
    aug_img = self.aug(img)
    return aug_img

Instantiated the custom transform:
blr_tfm = BlurTfm(blur_)

Defined the datablock as under:

dblock = DataBlock(blocks=(ImageBlock,CategoryBlock),
                   get_items = get_image_files,
                   get_y = label_func,
                   item_tfms = [Resize(224),blr_tfm],
                   batch_tfms = [aug_transforms()],
                   splitter = RandomSplitter(seed=42))

To convert to dataloaders, when I run the following:
dls = dblock.dataloaders(source/'images')

I get the following output:
**Could not do one pass in your dataloader, there is something wrong in it**
Where I am wrong?

would you mind doing a dblock .summary() to see where is the problem?
for me it works:

Thanks a lot

Hi,
I ran dblock.summary() which was very helpful in understanding the flow. Thanks a lot for your help.

Regards