When I have an image , I can standardize the image channel-wise as follows :
image[:, :, 0] = ((image[:, :, 0]-mean_1))/std_1
image[:, :, 1] = ((image[:, :, 1]-mean_2))/std_2
image[:, :, 2] = ((image[:, :, 2]-mean_3))/std_3
Where mean_1 and std_1 are the first channel mean and standard deviation . Same for mean_2, std_2 ,mean_3 and std_3. But right now the image is a tensor and has the following info :
(460, 700, 3) <dtype: 'float32'>
<class 'tensorflow.python.framework.ops.Tensor'>
I am new to tensorflow and I don't know how to convert the above formulas to a code that perform the same task on the tensor image
?
Edit : The means and the stds are calculated over all the dataset images by me. So I have their values.
Update 1 : I have tried to solve this problem using tf.keras.layers.Normalization impeded into my model :
inputs = keras.Input(shape=(460,700,3))
norm_layer = Normalization(mean=[200.827,160.252,195.008],
variance=[np.square(33.154),
np.square(45.877),
np.square(29.523)])
inputs=norm_layer(inputs)
This raises new two questions :
Does tf.keras.layers.Normalization and the above code normalizes the inputs per channel as I need ?
Using the above code , does tf.keras.layers.Normalization will work on test and validation data or training data only ? I need it to work on all the datasets.
Please help me guys :( I am so confused .
Update 1: Fix to show how to use with preprocessing layer
import tensorflow as tf
import numpy as np
# Create random img
img = tf.Variable(np.random.randint(0, 255, (10, 224, 224 ,3)), dtype=tf.uint8)
# Create prerprocessing layer
# Note: Works with tensorflow 2.6 and up
norm_layer = tf.keras.layers.Normalization(mean=[0.485, 0.456, 0.406], variance=[np.square(33.154), np.square(45.877), np.square(29.523)])
# Apply norm_layer to your image
# You need not add it to your model
norm_img = norm_layer(img)
# or
# Use via numpy but the output is a tensor since your running a preprocesisng layer
# norm_img = norm_layer(img.numpy())
# Run model prediction
predictions = model.predict(norm_img)