I have implemented a code to perform image preprocessing tasks. And it is a fully functioning method but it works with one image at a time. But I don't know how to make the function compatible with the tf dataset as I used OpenCV to perform the image processing task. And, how can I add this layer to models.Sequential()
to match with the Sequential()
requirements type?
The code where I want to add my custom function is -
model = models.Sequential([
resize_and_rescale,
data_augmentation,
custom_function_want_to_add_here(),
layers.Conv2D(32, (3,3,),activation = 'relu', input_shape = input_shape),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, kernel_size = (3,3,),activation = 'relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, kernel_size = (3,3,),activation = 'relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3,),activation = 'relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3,),activation = 'relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3,),activation = 'relu'),
layers.MaxPooling2D((2,2)),
layers.Flatten(),
layers.Dense(64, activation = 'relu'),
layers.Dense(n_classes,activation='softmax'),
])
Two options come to mind:
put your custom function to a custom TensorFlow layer
put the image preprocessing outside of the model
layers.Conv2D(32, ...)
)Since your custom logic requires calling a function from the OpenCV library (if I understood correctly) I would probably recommend the second approach. It will be easier to make the function work with the tf.data.Dataset
than it would be to make it work inside the model.