在图像上并行运行预训练的VGG-16张量流

时间:2017-08-29 06:12:52

标签: python tensorflow parallel-processing vgg-net

我正在使用预先训练好的VGG-16网络将图像转换为特征。我可以顺序完成这个。但是,我想并行执行此操作,并且我不确定如何正确构建批处理。

具体来说,假设我加载了16个以numpy数组(即16x224x224x3)保存的图像。我想并行转换这些。这是我到目前为止所做的:

 checkpoint_file = './vgg_16.ckpt'
 input_tensor = tf.placeholder(tf.float32, shape=(None,224,224,3), name='input_image')
 scaled_input_tensor = tf.scalar_mul((1.0/255), input_tensor)
 scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5)
 scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0)

 arg_scope = vgg_arg_scope()
 with slim.arg_scope(arg_scope):
   _, end_points = vgg_16(scaled_input_tensor, is_training=False)

 sess = tf.Session()
 saver = tf.train.Saver()
 saver.restore(sess, checkpoint_file)

 images = get_16_images() #arbitrary function, images is 16x224x224x3
 images =  tf.convert_to_tensor(images)

 batch = tf.train.batch(im, 2, num_threads=6, enqueue_many=True, allow_smaller_final_batch=True)

 sess.run(end_points['vgg_16/fc7'], feed_dict={input_tensor: batch}) #Error

我最终收到错误:

  

*** ValueError:使用序列设置数组元素。

有人可以帮我吗?批处理教程似乎专注于在读取数据时创建批处理,但我已经读过数据并且只是想创建一个批处理来并行化网络对不同图像的计算。

1 个答案:

答案 0 :(得分:1)

好的,这是一个愚蠢的问题,但我认为其他张量流新手可能会遇到同样的问题。

如果不清楚,feed_dict接收 numpy数组。因此,您不构造显式批量张量。只需传入多个numpy数组,tensorflow就可以处理它。注意我在哪里定义了输入张量:

input_tensor = tf.placeholder(tf.float32, shape=(None,224,224,3), name='input_image')

因此,tensorflow已经知道输入将是“批处理”可以这么说。变量images(来自get_16_images())已经完全符合我的需要。

您可以通过检查CPU分配来确认这是并行的。该批处理的Python CPU分配率高达650%。

相关问题