1

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 :

  1. Does tf.keras.layers.Normalization and the above code normalizes the inputs per channel as I need ?

  2. 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 .

AAA
  • 305
  • 1
  • 7
  • 1
    You can try this solution [here](https://stackoverflow.com/questions/62376835/how-to-normalize-my-image-data-in-tensorflow-keras) – Viettel Solutions Dec 24 '21 at 03:14
  • @ViettelSolutions , the problem is that my image is a tensor and cant be converted to numpy array so np.mean() or np.std() will not work , beside I am standardizing the image on the whole dataset channel wise not per image , meaning that I have calculated the mean and std over all the images in the dataset for each channel separately so I now got the mean and std and I need to apply them separately for each channel and I dont know how to index a tensor so that I can select a certain channel and standardize it using the the formula in the above code in my question. – AAA Dec 24 '21 at 19:51
  • When you used as layers, it works with everything as a whole – Edwin Cheong Dec 25 '21 at 16:44
  • you can just convert a tensor to numpy via .numpy() command – Edwin Cheong Dec 25 '21 at 16:44

1 Answers1

0

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)
Edwin Cheong
  • 879
  • 2
  • 7
  • 12
  • Thanks for the help , but I got the following error : NotImplementedError: Cannot convert a symbolic Tensor (stack:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported – AAA Dec 24 '21 at 19:43
  • This is because the code is converting the tensor to numpy array in the call to the function. – AAA Dec 24 '21 at 19:44
  • Please see update 1 in my question . – AAA Dec 24 '21 at 23:19