InceptionV3 和使用 tensorflow 的迁移学习
InceptionV3 and transfer learning with tensorflow
我想在 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 图不接受图像批处理作为输入。我错了吗?
你没有看错。这似乎是一个非常合理的功能请求,所以我打开了 a ticket for it on github。按照更新。
实际上,如果你提取正确的东西,它就可以用于迁移学习。将[N, 299, 299, 3]
形状的一批图像喂入为ResizeBilinear:0
然后使用pool_3:0
张量是没有问题的。之后的重塑会中断,但您可以重塑自己(无论如何之后您都会有自己的图层)。如果您想将原始分类器与批次一起使用,您可以在 pool_3:0
之上添加自己的整形,然后添加 softmax 层,重复使用原始 softmax 的 weights/biases 张量。
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)
应该这样做:
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})
etarion 提出了一个很好的观点。但是,我们不必自己重塑它;相反,我们可以更改 reshape
作为输入的 shape
的值。即,
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
.
我想在 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 图不接受图像批处理作为输入。我错了吗?
你没有看错。这似乎是一个非常合理的功能请求,所以我打开了 a ticket for it on github。按照更新。
实际上,如果你提取正确的东西,它就可以用于迁移学习。将[N, 299, 299, 3]
形状的一批图像喂入为ResizeBilinear:0
然后使用pool_3:0
张量是没有问题的。之后的重塑会中断,但您可以重塑自己(无论如何之后您都会有自己的图层)。如果您想将原始分类器与批次一起使用,您可以在 pool_3:0
之上添加自己的整形,然后添加 softmax 层,重复使用原始 softmax 的 weights/biases 张量。
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)
应该这样做:
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})
etarion 提出了一个很好的观点。但是,我们不必自己重塑它;相反,我们可以更改 reshape
作为输入的 shape
的值。即,
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
.