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

Running pre-trained VGG-16 in tensorflow on images in parallel

我正在使用预训练的 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: setting an array element with a sequence.

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

好的,这是一个愚蠢的问题,但我认为其他 tensorflow 新手可能 运行 遇到同样的问题。

不清楚的话,feed_dictnumpy数组。所以你不构造一个显式的批处理张量。只需传入多个 numpy 数组,tensorflow 就会处理它。注意我定义输入张量的地方:

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

所以 tensorflow 已经知道输入可以说是 "batch"。变量 images(来自 get_16_images())已经正是我所需要的。

您可以通过检查 CPU 分配来确认这是并行的。 Python CPU 该批次的分配激增至 650%。