【问题标题】:InceptionV3 and transfer learning with tensorflowInceptionV3 和使用 tensorflow 进行迁移学习
【发布时间】:2016-05-18 09:35:39
【问题描述】:

我想在 tensorflow 示例中从给定的 inceptionV3 进行迁移学习。按照分类图像示例以及此处给出的运算符和张量名称https://github.com/AKSHAYUBHAT/VisualSearchServer/blob/master/notebooks/notebook_network.ipynb,我可以创建我的图表。但是,当我将一批大小为 (100, 299, 299, 3) 的图像放入预先计算的初始图中时,我在 pool_3 层得到以下形状错误:

ValueError: Cannot reshape a tensor with 204800 elements to shape [1, 2048] (2048 elements)

这个 inceptionV3 图表似乎不接受图像批处理作为输入。我错了吗?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    etarion 提出了一个非常好的观点。但是,我们不必自己重塑它;相反,我们可以更改shape 的值,reshape 将其作为输入。即,

    input_tensor_name = 'import/input:0'
    shape_tensor_name = 'import/InceptionV3/Predictions/Shape:0'
    output_tensor_name= 'import/InceptionV3/Predictions/Reshape_1:0'
    
    output_tensor = tf.import_graph_def(
        graph.as_graph_def(),
        input_map={input_tensor_name: image_batch,
                   shape_tensor_name: [batch_size, num_class]},
        return_elements=[output_tensor_name])
    

    这些张量名称基于inception_v3_2016_08_28_frozen.pb

    【讨论】:

      【解决方案2】:

      应该这样做:

          with g.as_default():
           inputs = tf.placeholder(tf.float32, shape=[batch_size, 299, 299, 3],
                                      name='input')
      
              with slim.arg_scope(inception.inception_v3_arg_scope()):
      
                  logits, end_points = inception.inception_v3( inputs, 
                  num_classes=FLAGS.num_classes, is_training=False)
                  variables_to_restore = lim.get_variables_to_restore(exclude=exclude)
              sess = tf.Session()
      
              saver = tf_saver.Saver(variables_to_restore)
      

      那么你应该可以调用操作了:

              sess.run("pool_3:0",feed_dict={'ResizeBilinear:0':images})
      

      【讨论】:

        【解决方案3】:

        如果你提取正确的东西,它实际上适用于迁移学习。将一批[N, 299, 299, 3] 形状的图像作为ResizeBilinear:0 输入,然后使用pool_3:0 张量是没有问题的。打破的是之后的重塑,但你可以重塑自己(无论如何你都会有自己的层)。如果您想批量使用原始分类器,您可以在 pool_3:0 之上添加自己的整形,然后添加 softmax 层,重用原始 softmax 的权重/偏差张量。

        TLDR:double_img 是两个形状为 (2, 299, 299, 3) 的图像的堆栈,这样可以:

        pooled_2 = sess.graph.get_tensor_by_name("pool_3:0").eval(session=sess, feed_dict={'ResizeBilinear:0':double_img})
        pooled_2.shape
        # => (2, 1, 1, 2048)
        

        【讨论】:

        【解决方案4】:

        你没有错。这似乎是一个非常合理的功能请求,所以我打开了a ticket for it on github。关注更新。

        【讨论】:

          猜你喜欢
          • 2020-08-06
          • 2019-03-08
          • 2018-04-12
          • 1970-01-01
          • 2020-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-03
          相关资源
          最近更新 更多