Keras
image preprocessing
machine learning
deep learning
neural networks

Keras rescale1./255 vs preprocessing_functionpreprocess_input - which one to use?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Keras provides a flexible and powerful API for building neural network models. When preparing image data for training deep learning models, handling input data becomes critical for performance. Two commonly used methods in Keras for preprocessing input data are `rescale=1./255` and setting `preprocessing_function=preprocess_input`. Both methods are used to normalize image data, but they serve slightly different purposes and are often used in different contexts. Understanding their differences and how to apply them can significantly affect the performance and accuracy of a model.

Image Preprocessing Techniques in Keras

When training a model with Keras, images often need to be scaled appropriately for better convergence and performance. Image data typically comes in the form of pixel values ranging from 0 to 255. Deep learning models, however, tend to perform better when input data is normalized to a certain range. Let's explore the two main approaches that can be employed in Keras:

1. Using `rescale=1./255`

Description:

  • The `rescale` parameter is used in `ImageDataGenerator` for basic image normalization.
  • Setting `rescale=1./255` scales pixel values from the range `[0, 255]` to `[0, 1]`.

Technical explanation:

  • Rescaling is a straightforward operation: each pixel value `x` is divided by 255.
  • This operation assumes no further preprocessing is needed and is generally useful when you're not using pre-trained models.

Use case:

  • Simple models not based on pre-trained architectures.
  • When you need uniform preprocessing for custom datasets without additional transformations.

Example:

  • This method utilizes a `preprocessing_function` for more complex normalization routines specific to certain architectures.
  • The `preprocess_input` function is typically imported from `keras.applications` and adjusts image data to match the input requirements of pre-trained networks.
  • Pre-trained models like VGG, ResNet, Inception, etc., expect input to be preprocessed in a way they were trained on.
  • The `preprocess_input` function applies various transformations (like zero-centering based on the ImageNet mean) tailored to a specific architecture.
  • When leveraging pre-trained models for transfer learning, especially those trained on ImageNet.
  • This function is vital to ensure that the input data matches the data distributions seen during the model's pre-training phase.
  • Performance:
  • Flexibility:
  • Integration with Keras APIs:
  • Data Consistency:
  • Complexity:

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.