I've noticed some strange behaviour in preprocess_input
, a function used to preprocess images to normalise values correctly for the specific pre-trained network you are using.
After several hours of debugging, it appears that when a tensor is used as an input, the input tensor is unmodified, and it returns the processed input as a new tensor:
tensor = tf.ones(3)*100
print(tensor)
tensor2 = tf.keras.applications.mobilenet_v2.preprocess_input (tensor)
print(tensor)
print(tensor2)
returns
tf.Tensor([100. 100. 100.], shape=(3,), dtype=float32)
tf.Tensor([100. 100. 100.], shape=(3,), dtype=float32)
tf.Tensor([-0.21568626 -0.21568626 -0.21568626], shape=(3,), dtype=float32)
However when doing the exact same thing but with a numpy array as input, apart from returning the processed version as a new array, the original array is changed to be the same as the new array:
array = np.ones(3)*100
print(array)
array2 = tf.keras.applications.mobilenet_v2.preprocess_input (array)
print(array)
print(array2)
array+=1
print(array)
print(array2)
returns
[100. 100. 100.]
[-0.21568627 -0.21568627 -0.21568627] # <== input has changed!!!
[-0.21568627 -0.21568627 -0.21568627]
[0.78431373 0.78431373 0.78431373]
[0.78431373 0.78431373 0.78431373] # <== further changes to input change output
Three questions:
- Why is behaviour not uniform?
- Why is it considered beneficial for the original array to be changed?
- Why does preprocess_input both return the new values and also modify in-place - isn't it usually one or the other, doing both is confusing...