python - tensorflow.python.framework.errors_impl.InvalidArgumentError

标签 python tensorflow image-recognition

在终端窗口上,当图像传递到 tensorflow 以进行图像对象识别时,它运行良好:

python run.py http://image_url.jpg

但是,对于包含 imageURL 流的 JSON 数据,它失败并出现以下主要错误:

InvalidArgumentError: Invalid JPEG data or crop window, data size 15022
 [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_DecodeJpeg/contents_0_0)]]
Caused by op u'DecodeJpeg'

遇到另一个错误:

ValueError:GraphDef 不能大于 2GB。

下面是我的 tensorflow 源代码作为一个函数(它再次使用作为参数传递的单个 ImageUrl 运行):

import tensorflow as tf
import sys
import os
import urllib2

def tensorflow_pred(imageUrl):

    #suppress TF log-info messages - remove to display TF logs 
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    response = urllib2.urlopen(imageUrl)

    image_data = response.read()

    # Loads label file, strips off carriage return
    label_lines = [line.rstrip() for line 
                    in tf.gfile.GFile("./retrained_labels.txt")]

    # Unpersists graph from file
    with tf.gfile.FastGFile("./retrained_graph.pb", 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')

    with tf.Session() as sess:
        # Feed the image_data as input to the graph and get first prediction
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

        predictions = sess.run(softmax_tensor, \
                {'DecodeJpeg/contents:0': image_data})

        # Sort to show labels of first prediction in order of confidence
        top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

        for node_id in top_k:
            classification = label_lines[node_id]
            score = predictions[0][node_id]
            if (score >=0.5):
                return ('%s (score = %.5f)' % (classification, score))

最佳答案

我创建了一个workaround对于这个问题。

关于python - tensorflow.python.framework.errors_impl.InvalidArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49414902/

相关文章:

python - 运行时错误: Disconnected graph for GANs because input can't be obtained

如果还不是可迭代的,则将 Pythonic 转换为单例可迭代

python - 如何找到字典项目中元素最多的列表?

python - typeerror参数 'image'必须为iplimage

windows - Tensorflow:此平台不支持轮子

python - Q : TensorFlow 1. 0.1 OpKernel 未知操作错误

Java Image 获取桌面图像并创建宏

python - 如何从中心裁剪 OpenCV 图像

几个时期后的 tensorflow-GPU OOM 问题