Adding custom transforms in fastai

Hi,

I want to add custom transforms/augs/preprocessing. I tried adding a function in transform.py but it seems to not working when I want to use it with data loader. Function looks something like this:

def _custom_fun(x):
image = cv2.cvtColor(np.float32(x), cv2.COLOR_BGR2RGB)
//some more processing
return x
custom_fun = TfmPixel(_custom_fun)

then,
tfms = get_transforms(do_flip=True, xtra_tfms=[circle_crop()])
data = (train.split_by_rand_pct(0.2, seed=42)
.label_from_df()
.add_test(test)
.transform(tfms, size=224)
.databunch(path=Path(’.’), bs=64).normalize(imagenet_stats))

It gives following error:
It’s not possible to apply those transforms to your dataset:
OpenCV(4.1.0) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function ‘cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = (cv::impl::::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]’

Invalid number of channels in input image:
‘VScn::contains(scn)’
where
‘scn’ is 482

I think you should return a PIL.Image object instead of numpy matrix or cv2 image object. I mean your return statement should be “return Image(x)”

2 Likes

Thanks !