【发布时间】:2022-09-22 21:22:33
【问题描述】:
我注意到preprocess_input 中有一些奇怪的行为,该函数用于预处理图像以正确标准化您正在使用的特定预训练网络的值。
经过几个小时的调试,似乎当一个张量用作输入时,输入张量是未修改的,它将处理后的输入作为一个新的张量返回:
tensor = tf.ones(3)*100
print(tensor)
tensor2 = tf.keras.applications.mobilenet_v2.preprocess_input (tensor)
print(tensor)
print(tensor2)
返回
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)
但是,当执行完全相同的操作但使用 numpy 数组作为输入时,除了将处理后的版本作为新数组返回之外,原数组更改为与新数组相同:
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)
返回
[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
三个问题:
- 为什么行为不一致?
- 为什么认为更改原始数组是有益的?
- 为什么 preprocess_input 既返回新值又就地修改 - 通常不是其中之一,两者都做令人困惑...
标签: python tensorflow keras tensorflow2.0 tf.keras