Tensorflow:同一图像的不同激活值

2024-05-16 10:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图重新训练(读finetune)一个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})

上面的代码按原样执行一次,最后一行有以下更改:

^{pr2}$

以下是图中的一些节点:

[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\u batch[0]相同)。 但在这一层之后,所有层都有不同的激活值。我觉得'MobilenetV1/MobilenetV1/Conv2d_0/convolction'层之后的batchNorm操作对于批处理输入和单个图像的行为是不同的。还是其他原因引起的?在

如有任何帮助/建议,我们将不胜感激。在


Tags: 图像imageidreadinputbatchgraphtensor
2条回答

这是由于批量标准化。在

你怎么推断的。您是从检查点文件加载它还是使用冻结的Protobuf模型。如果使用冻结模型,则不同格式的输入可以得到类似的结果。在

签出this。对于不同的应用程序,也提出了类似的问题。在

当您构建mobilenet时,有一个参数称为is_training。如果不将其设置为false,则dropout层和批处理规范化层将在不同的迭代中给出不同的结果。批处理规范化可能对值的更改很小,但由于删除了一些输入值,因此会对值进行很大的更改。在

看看mobilnet的签名:

def mobilenet_v1(inputs,
                 num_classes=1000,
                 dropout_keep_prob=0.999,
                 is_training=True,
                 min_depth=8,
                 depth_multiplier=1.0,
                 conv_defs=None,
                 prediction_fn=tf.contrib.layers.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='MobilenetV1'):
  """Mobilenet v1 model for classification.

  Args:
    inputs: a tensor of shape [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    dropout_keep_prob: the percentage of activation values that are retained.
    is_training: whether is training or not.
    min_depth: Minimum depth value (number of channels) for all convolution ops.
      Enforced when depth_multiplier < 1, and not an active constraint when
      depth_multiplier >= 1.
    depth_multiplier: Float multiplier for the depth (number of channels)
      for all convolution ops. The value must be greater than zero. Typical
      usage will be to set this value in (0, 1) to reduce the number of
      parameters or computation cost of the model.
    conv_defs: A list of ConvDef namedtuples specifying the net architecture.
    prediction_fn: a function to get predictions out of logits.
    spatial_squeeze: if True, logits is of shape is [B, C], if false logits is
        of shape [B, 1, 1, C], where B is batch_size and C is number of classes.
    reuse: whether or not the network and its variables should be reused. To be
      able to reuse 'scope' must be given.
    scope: Optional variable_scope.

  Returns:
    logits: the pre-softmax activations, a tensor of size
      [batch_size, num_classes]
    end_points: a dictionary from components of the network to the corresponding
      activation.

  Raises:
    ValueError: Input rank is invalid.
  """

相关问题 更多 >