简体   繁体   English

Tensorflow:Inception v3批处理

[英]Tensorflow: Inception v3 batched processing

I'm extracting bottleneck tensors with Inception v3. 我正在使用Inception v3提取瓶颈张量。 My problem: I can feed only one image at a time: 我的问题:一次只能输入一张图片:

sess.graph.get_tensor_by_name("pool_3:0").eval(session=sess, feed_dict={'DecodeJpeg:0':single_image})

Batched Processing of multiple images would speed-up things quite a bit, I guess. 我猜想,对多个图像进行批处理将大大加快处理速度。 A solution is suggested here , but I cannot get it to work (tested with tensorflow v0.10.0 and 0.11.0rc0, inception model downloaded from http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz ). 这里建议一个解决方案,但我无法使其工作(使用tensorflow v0.10.0和0.11.0rc0进行了测试,初始模型是从http://download.tensorflow.org/models/image/imagenet/inception-2015-12下载的-05.tgz )。

import tensorflow as tf
from tensorflow.python.platform import gfile
import cv2
import numpy as np

def create_graph():
    with gfile.FastGFile('classify_image_graph_def.pb', 'rb') as f:
        graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')

create_graph()

img = cv2.imread("some_img.jpg")
img = cv2.resize(img, (299,299), interpolation = cv2.INTER_CUBIC) 
# replicate image 10 times
img = np.array(10*[img]).astype('float')

with tf.Session() as sess:
    pooled_2 =  sess.graph.get_tensor_by_name("pool_3:0").eval(session=sess, feed_dict={'ResizeBilinear:0':img})

This gives me the (plausible) error message: 这给了我(可能的)错误消息:

Traceback (most recent call last):
  File "extract_bottlenecks_minimal.py", line 26, in <module>
    pooled_2 = sess.graph.get_tensor_by_name("pool_3:0").eval(session=sess, feed_dict={'ResizeBilinear:0':img})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 559, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3761, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 894, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (10, 299, 299, 3) for Tensor u'ResizeBilinear:0', which has shape '(1, 299, 299, 3)'

I also found this issue claiming that inception works with image batches, but this code needs to set up the whole network every time I feed it input (I would like to setup the the network only once). 我还发现了这个问题,声称初始操作适用于图像批处理,但是每次输入输入内容时,此代码都需要设置整个网络 (我只想设置一次网络)。

Thanks for taking interest - any help is greatly appreciated ;-) 感谢您的关注-非常感谢您的帮助;-)

(1) Please try using the much more recent Inception v3 model, which you can find here: (1)请尝试使用更新得多的Inception v3模型,您可以在这里找到:

http://download.tensorflow.org/models/image/imagenet/inception-v3-2016-03-01.tar.gz http://download.tensorflow.org/models/image/imagenet/inception-v3-2016-03-01.tar.gz

This was the version released subsequently to the bug I opened in Feb, which should support a batch dimension on the evaluation path. 这是我2月份打开的错误之后发布的版本,该错误应该支持评估路径上的批处理维度。 The imagenet_eval script should work with this. imagenet_eval脚本应与此一起使用。

(2) You might need to decode the JPEG and resize to 299x299 before you feed into the network. (2)在馈入网络之前,您可能需要解码JPEG并将其大小调整为299x299。 I haven't looked at whether the pretrained one accepts a variable batch size to the decode, but it should at least accept it to the network itself. 我没有研究预训练的人是否接受可变批量大小来进行解码,但是它至少应该接受网络本身。

For more information, see the image recognition tutorial. 有关更多信息,请参见图像识别教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM