Preprocessing Layer in CNN models for TensorFlow2

A technique to make CNN models less prone to errors and more accessible for developers

Michal Lukac
Towards Data Science

--

source: pexels.com

One of the most common mistakes which are novice machine learning practitioners doing is forget to normalize input images. No wonder! Every model requires different input normalization when you are doing Transfer Learning. VGG, for example, requires to subtract this vector [123.68, 116.779, 103.939] from the RGB image. MobileNetV2 requires inputs from interval <-1,1>. PyTorch models often use different normalization methods. This tip can save you a lot of time and bugs in the future. Instead of doing normalization before inputting images to the model, you can simply add this layer inside your model (computation graph).

With old TensorFlow 1 (rest in peace) you could simply add preprocessing operation to the graph and freeze this model. However, in TensorFlow 2+ you need to create your own preprocessing layer.

So first define our preprocess method (this one is for MobileNetV2):

Then create your custom layer inheriting from tf.keras.layers.Layer and use the function in the call method on the input:

When creating a model then insert the layer before calling the base model of a pre-trained model (functional API of tf.keras):

And that’s it!

From now, your model always accepts just RGB [0, 255] images and the normalization of input is done inside the model. If you saved such a model in Keras .h5 format then do not forget to specify custom objects during the model loading. I always recommend saving models in the SavedModel or TFLITE format because everything is frozen in the model and ready to deploy!

With preprocessing layer you:

  • Saved a lot of hours of other people debugging your model.
  • You don’t need to tell your customers to normalize images in any way when using your model. This is often painful when deploying different versions/architectures of your models to computer vision app on mobile devices.

See you next time!

Michal

--

--

Co-Founder & Machine Learning developer at Ximilar. Writting about AI, Computer Vision and Value Stock Investing.