Python 编辑tensorflow inceptionV3 retraining-example.py以获得多个分类

Python 编辑tensorflow inceptionV3 retraining-example.py以获得多个分类,python,python-3.x,machine-learning,tensorflow,Python,Python 3.x,Machine Learning,Tensorflow,TLDR:无法确定如何使用重新训练的接收v3进行多个图像预测 你好,善良的人们:)我花了几天时间搜索了许多stackoverflow帖子和文档,但我找不到这个问题的答案。非常感谢您在这方面的帮助 我已经在新图片上对tensorflow inceptionV3模型进行了重新培训,它能够按照中的说明并使用以下命令处理新图片: bazel build tensorflow/examples/label_image:label_image && \ bazel-bin/tensorflo

TLDR:无法确定如何使用重新训练的接收v3进行多个图像预测

你好,善良的人们:)我花了几天时间搜索了许多stackoverflow帖子和文档,但我找不到这个问题的答案。非常感谢您在这方面的帮助

我已经在新图片上对tensorflow inceptionV3模型进行了重新培训,它能够按照中的说明并使用以下命令处理新图片:

bazel build tensorflow/examples/label_image:label_image && \
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--output_layer=final_result \
--image= IMAGE_DIRECTORY_TO_CLASSIFY
然而,我需要对多个图像(如数据集)进行分类,我一直在认真思考如何进行分类。我在上找到了以下示例

关于如何使用重新训练的模型,但同样,关于如何为多个分类修改模型的详细信息很少

从MNIST教程中收集到的信息来看,我需要在sess.run()对象中输入feed_dict,但由于我不理解如何在这种上下文中实现它,所以我被困在那里

我们将非常感谢您的任何帮助!:)

编辑:

运行Styrke的脚本进行一些修改,我得到了这个

    waffle@waffleServer:~/git$ python tensorflowMassPred.py  I
       tensorflow/stream_executor/dso_loader.cc:108] successfully opened
       CUDA library libcublas.so locally I
       tensorflow/stream_executor/dso_loader.cc:108] successfully opened
       CUDA library libcudnn.so locally I
       tensorflow/stream_executor/dso_loader.cc:108] successfully opened
       CUDA library libcufft.so locally I
       tensorflow/stream_executor/dso_loader.cc:108] successfully opened
       CUDA library libcuda.so locally I
       tensorflow/stream_executor/dso_loader.cc:108] successfully opened
       CUDA library libcurand.so locally
       /home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py:1197:
       VisibleDeprecationWarning: converting an array with ndim > 0 to an
       index will result in an error in the future  
       result_shape.insert(dim, 1) I
       tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:924] successful
       NUMA node read from SysFS had negative value (-1), but there must be
       at least one NUMA node, so returning NUMA node zero I
       tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0
       with properties:  name: GeForce GTX 660 major: 3 minor: 0
       memoryClockRate (GHz) 1.0975 pciBusID 0000:01:00.0 Total memory:
       2.00GiB Free memory: 1.78GiB I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0  I
       tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0:   Y  I
       tensorflow/core/common_runtime/gpu/gpu_device.cc:806] Creating
       TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 660, pci
       bus id: 0000:01:00.0) W tensorflow/core/framework/op_def_util.cc:332]
       Op BatchNormWithGlobalNormalization is deprecated. It will cease to
       work in GraphDef version 9. Use tf.nn.batch_normalization(). E
       tensorflow/core/common_runtime/executor.cc:334] Executor failed to
       create kernel. Invalid argument: NodeDef mentions attr 'T' not in
       Op<name=MaxPool; signature=input:float -> output:float;
       attr=ksize:list(int),min=4; attr=strides:list(int),min=4;
       attr=padding:string,allowed=["SAME", "VALID"];
       attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]>;
       NodeDef: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3,
       3, 1], padding="VALID", strides=[1, 2, 2, 1],
       _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)
         [[Node: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3,
       3, 1], padding="VALID", strides=[1, 2, 2, 1],
       _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)]]
       Traceback (most recent call last):   File
       "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py",
       line 715, in _do_call
           return fn(*args)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py",
       line 697, in _run_fn
           status, run_metadata)   File "/home/waffle/anaconda3/lib/python3.5/contextlib.py", line 66, in
       __exit__
           next(self.gen)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors.py",
       line 450, in raise_exception_on_not_ok_status
           pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors.InvalidArgumentError: NodeDef
       mentions attr 'T' not in Op<name=MaxPool; signature=input:float ->
       output:float; attr=ksize:list(int),min=4;
       attr=strides:list(int),min=4; attr=padding:string,allowed=["SAME",
       "VALID"]; attr=data_format:string,default="NHWC",allowed=["NHWC",
       "NCHW"]>; NodeDef: pool = MaxPool[T=DT_FLOAT, data_format="NHWC",
       ksize=[1, 3, 3, 1], padding="VALID", strides=[1, 2, 2, 1],
       _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)
         [[Node: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3,
       3, 1], padding="VALID", strides=[1, 2, 2, 1],
       _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)]]

       During handling of the above exception, another exception occurred:

       Traceback (most recent call last):   File "tensorflowMassPred.py",
       line 116, in <module>
           run_inference_on_image()   File "tensorflowMassPred.py", line 98, in run_inference_on_image
           {'DecodeJpeg/contents:0': image_data})   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py",
       line 372, in run
           run_metadata_ptr)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py",
       line 636, in _run
           feed_dict_string, options, run_metadata)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py",
       line 708, in _do_run
           target_list, options, run_metadata)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py",
       line 728, in _do_call
           raise type(e)(node_def, op, message) tensorflow.python.framework.errors.InvalidArgumentError: NodeDef
       mentions attr 'T' not in Op<name=MaxPool; signature=input:float ->
       output:float; attr=ksize:list(int),min=4;
       attr=strides:list(int),min=4; attr=padding:string,allowed=["SAME",
       "VALID"]; attr=data_format:string,default="NHWC",allowed=["NHWC",
       "NCHW"]>; NodeDef: pool = MaxPool[T=DT_FLOAT, data_format="NHWC",
       ksize=[1, 3, 3, 1], padding="VALID", strides=[1, 2, 2, 1],
       _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)
         [[Node: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3,
       3, 1], padding="VALID", strides=[1, 2, 2, 1],
       _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)]]
       Caused by op 'pool', defined at:   File "tensorflowMassPred.py", line
       116, in <module>
           run_inference_on_image()   File "tensorflowMassPred.py", line 87, in run_inference_on_image
           create_graph()   File "tensorflowMassPred.py", line 68, in create_graph
           _ = tf.import_graph_def(graph_def, name='')   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/importer.py",
       line 274, in import_graph_def
           op_def=op_def)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py",
       line 2260, in create_op
           original_op=self._default_original_op, op_def=op_def)   File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py",
       line 1230, in __init__
           self._traceback = _extract_stack()

看看你的链接脚本:

with tf.Session() as sess:

    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image_data})
    predictions = np.squeeze(predictions)

    top_k = predictions.argsort()[-5:][::-1] # Getting top 5 predictions
在此代码段中,
image\u data
是您要提供给模型的新图像,它在前面加载了几行:

image_data = tf.gfile.FastGFile(imagePath, 'rb').read()

因此,我的直觉是改变
在图像上运行\u推断\u
以接受
imagePath
作为参数,并在数据集中的每个图像上使用和执行该操作。

原始jpeg数据似乎直接输入到一个操作,该操作一次只接受一个图像作为输入。为了一次处理多个图像,您可能需要定义更多的
decode\u jpeg
ops。如果有可能做到这一点,那么我目前不知道如何做到

下一个最好的方法很简单,可能是在TensorFlow会话中使用循环对所有图像逐一进行分类。通过这种方式,您至少可以避免重新加载图形,并为每个要分类的图像启动新的TF会话,如果您必须经常这样做的话,这两种操作都会花费相当多的时间

这里我更改了
run\u expression\u on\u image()
函数的定义,因此它应该对
imagePath
变量指定的目录中的所有图像进行分类。我还没有测试这段代码,所以可能有一些小问题需要解决

def run_inference_on_image():
    answer = []

    if not tf.gfile.IsDirectory(imagePath):
        tf.logging.fatal('imagePath directory does not exist %s', imagePath)
        return answer

    # Get a list of all files in imagePath directory
    image_list = tf.gfile.ListDirectory(imagePath)

    # Creates graph from saved GraphDef.
    create_graph()

    with tf.Session() as sess:

        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

        for i in image_list:
            image_data = tf.gfile.FastGFile(i, 'rb').read()
            predictions = sess.run(softmax_tensor,
                                   {'DecodeJpeg/contents:0': image_data})
            predictions = np.squeeze(predictions)

            top_k = predictions.argsort()[-5:][::-1]  # Getting top 5 predictions
            f = open(labelsFullPath, 'rb')
            lines = f.readlines()
            labels = [str(w).replace("\n", "") for w in lines]
            for node_id in top_k:
                human_string = labels[node_id]
                score = predictions[node_id]
                print('%s (score = %.5f)' % (human_string, score))

            answer.append(labels[top_k[0]])
    return answer

我也有同样的问题。我遵循所有可能的解决方案,最终找到了一个适合我的解决方案。当用于重新训练模型的Tensorflow版本与所使用的版本不同时,会发生此错误

解决方案是将Tensorflow更新为最新版本。由于我使用pip安装Tensorflow,因此只需运行以下命令:

sudo pip install tensorflow --upgrade 

而且效果非常好。

嗨,谢谢你的回复!对不起,我不明白你的意思。你是说加载所有图片,然后循环sess.run()?不幸的是,这不是我想要的。它类似于在普通分类器中逐个拟合行(如xgboost),速度非常慢(8k图像需要31小时)。我正在寻找一种解决方案,将整个X图像输入到feed_dict和分类器中,并一次性输出所有图像的预测。您已经得到了一个张量
\u输入
,其大小应为
[批次大小、高度、宽度、通道]
。接下来我要尝试的是,进入这样一个4D数组,然后在
sess.run
调用中用
DecodeJpeg/contents:0
替换
inputs:0
ResizeBilinear:0
可能也很有用。您好,非常感谢您的回复-我在运行这个脚本时遇到了一些问题,我已经更新了我的答案。如果你能再帮我一次,那就太好了!:)@哇,有一个问题似乎与你的新问题有关。它还没有解决,但我会在它结束之前奖励你:)
sudo pip install tensorflow --upgrade