Image normalization in TensorFlow

About this article

I used rescale when rescaling pixel values ​​with ImageDataGenerator of keras, but I couldn't use it when I wanted to normalize in the range of [-1 ~ 1]. I will leave it.

Arguments --rescale

Typically

from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
datagen = ImageDataGenerator(rescale=1./255)

I think that it is often used like. Takes the product of the original pixel value and the value given to rescale. This is useful for normalizing [0 ~ 255] to [0 ~ 1]. ImageDataGenerator applies this function before any other augmentation.

Arguments --preprocessing_function

First, let's check how to use it.

from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
datagen = ImageDataGenerator(
    preprocessing_function=preprocess_input,
    )

preprocess_fucntion: Function applied to each input. This function is executed before any other changes are made. This function takes a 3D Numpy tensor as an argument and needs to be defined to output a tensor of the same shape.

When using the tf.keras.applications model, the model determines the range of normalization values, such as [0 ~ 1] or [-1 ~ 1]. Even though it is in the range of [-1 ~ 1], learning often works well even if it is trained in [0 ~ 1](not very good, but ...).

So each model has a handy function called preprocess_input. The following is an example of mobilenet v2.

from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
datagen = ImageDataGenerator(
    preprocessing_function=preprocess_input,
    )

By writing like this, it will normalize to the value suitable for the model. It can also be used alone. You can check the values ​​before and after conversion with the following code.

import numpy as np
import tensorflow as tf 
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing import image
 
model = tf.keras.applications.MobileNetV2()
 
img_path = 'XXXXX.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
print(x.max())
print(x.min())
x = preprocess_input(x)
print(x.max())
print(x.min())

tf.keras.layers.experimental.preprocessing.Rescaling()

Another method of normalization is tf.keras.layers.experimental.preprocessing.Rescaling (). The arguments are scale and offset. The scale value is multiplied by the input value. The value of offset is the image to add.

When [0, 255]-> [-1, 1], ** Rescaling (scale = 1./127.5, offset = -1) ** is okay.



base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                          include_top=False,
                                          weights='imagenet')
        
model = tf.keras.models.Sequential([
        tf.keras.layers.experimental.preprocessing.Rescaling(scale=1./127.5, offset=-1),
        base_model,
        tf.keras.layers.GlobalAveragePooling2D(),
        tf.keras.layers.Dense(2, activation='softmax'),
        ])


At the end

I don't usually use TF, so I thought I didn't know much about it. After that, let's be strict about what value range should be normalized when inputting an image to the created model. If the scope of normalization is different, the predictions will change considerably. You need to be especially careful when embedding an AI model in a web application.

References

Recommended Posts

Image normalization in TensorFlow
Clipping and normalization in TensorFlow
Image format in Python
Easy image classification with TensorFlow
Tweet with image in Python
Random seeds fixed in TensorFlow
Image Processing Collection in Python
Image addition memo in reportlab
Ensure reproducibility with tf.keras in Tensorflow 2.3
Try Embedding Visualization added in TensorFlow 0.12
SLIC Superpixel segmentation in scikit image
Implemented image segmentation in python (Union-Find)
Detect mosaic points in the image
[Image connection] Arrange images in tiles
Install tensorflow in Docker (LINUX) (memo)
Summary of various operations in Tensorflow
install tensorflow in anaconda + python3.5 environment
How to run TensorFlow 1.0 code in 2.0
View image after Data Augmentation in PyTorch
Use tensorflow in an environment without root
Put TensorFlow in P2 instance with pip3
Using TensorFlow in Cloud9 Integrated Development Environment-GetStarted-
Image recognition model using deep learning in 2016
Cut out A4 print in the image
Face image inference using Flask and TensorFlow
tensorflow does not enter in windows + anaconda.
How to adjust image contrast in Python
Easy image processing in Python with Pillow
Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
Partially read parameters in old TensorFlow 1.x
Implemented DQN in TensorFlow (I wanted to ...)