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.
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.
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'),
])
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.
Recommended Posts