我试图重新训练(读取微调)MobileNet图像分类器。Tensorflow:同一图像的不同激活值
由张量流here(来自tutorial)给出的再训练脚本仅更新新添加的完全连接图层的权重。我修改了这个脚本来更新预训练模型的所有图层的权重。我正在使用MobileNet架构,深度倍数为0.25,输入尺寸为128.
但是,在重新训练时,我观察到一个奇怪的现象,如果我将某个特定图像作为批量推理的输入与其他图像进行比较,一些图层后的值与图像单独传递时的值不同。同样来自不同批次的相同图像的激活值也是不同的。示例 - 对于两批 - batch_1 : [img1, img2, img3]; batch_2 : [img1, img4, img5]
。 img1的激活与两个批次都不同。
这里是我使用的推理码 -
for tf.Session(graph=tf.get_default_graph()) as sess:
image_path = '/tmp/images/10dsf00003.jpg'
id_ = gfile.FastGFile(image_path, 'rb').read()
#The line below loads the jpeg using tf.decode_jpeg and does some preprocessing
id = sess.run(decoded_image_tensor, {jpeg_data_tensor: id_})
input_image_tensor = graph.get_tensor_by_name('input')
layerXname='MobilenetV1/MobilenetV1/Conv2d_1_depthwise/Relu:0' #Name of the layer whose activations to inspect.
layerX = graph.get_tensor_by_name(layerXname)
layerXactivations=sess.run(layerX, {input_image_tensor: id})
上面的代码与在最后一行以下变化一次,因为它是,一旦执行:
layerXactivations_batch=sess.run(layerX, {input_image_tensor: np.asarray([np.squeeze(id), np.squeeze(id), np.squeeze(id)])})
以下是一些图中的节点:
[u'input', u'MobilenetV1/Conv2d_0/weights', u'MobilenetV1/Conv2d_0/weights/read', u'MobilenetV1/MobilenetV1/Conv2d_0/convolution', u'MobilenetV1/Conv2d_0/BatchNorm/beta', u'MobilenetV1/Conv2d_0/BatchNorm/beta/read', u'MobilenetV1/Conv2d_0/BatchNorm/gamma', u'MobilenetV1/Conv2d_0/BatchNorm/gamma/read', u'MobilenetV1/Conv2d_0/BatchNorm/moving_mean', u'MobilenetV1/Conv2d_0/BatchNorm/moving_mean/read', u'MobilenetV1/Conv2d_0/BatchNorm/moving_variance', u'MobilenetV1/Conv2d_0/BatchNorm/moving_variance/read', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/add/y', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/add', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/Rsqrt', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/mul', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/mul_1', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/mul_2', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/sub', u'MobilenetV1/MobilenetV1/Conv2d_0/BatchNorm/batchnorm/add_1', u'MobilenetV1/MobilenetV1/Conv2d_0/Relu6', u'MobilenetV1/Conv2d_1_depthwise/depthwise_weights', u'MobilenetV1/Conv2d_1_depthwise/depthwise_weights/read', ... ...]
现在,当layerXname = 'MobilenetV1/MobilenetV1/Conv2d_0/convolution'
上述两种激活方式都是相同的。 (即 layerxactivations和layerxactivations_batch [0]相同)。 但是在此图层之后,所有图层都具有不同的激活值。我觉得'MobilenetV1/MobilenetV1/Conv2d_0/convolution'层对于批量输入和单个图像的batchNorm操作有所不同。还是由其他问题引起的问题?
任何帮助/指针,将不胜感激。
谢谢@jorgemf!正如我在我的问题中怀疑的问题是使用batchNorm并将'is_training'设置为'False'工作。但这不是正确的方法。理想情况下,应该在训练时将'is_training'加载为'True',然后在推断时将'is_training'加载为False。但是因为我自己并没有自己写蝙蝠科,而是从MobileNet代码中加载图形;我还没有弄清楚如何做到上述。你可以参考这里 - https://stackoverflow.com/questions/39353503/tensorflow-tf-slim-model-with-is-training-true-and-false或https://ruishu.io/2016/12/27/batchnorm/ – Krist
@Krist不要忘了标记答案是有效的,如果它帮助你。 – jorgemf