2016-10-10 35 views
0

私はtf.image.decode_jpegをテストしていますが、InvalidArgumentErrorがあります。 Python3をPython2のように動作させるために、encoding = "latin-1"を追加しました..tf.image.decode_jpeg raise InvalidArgumentError

私の質問: この問題を解決するにはどうすればよいですか?

import tensorflow as tf 

with open("/root/PycharmProjects/mscoco/train2014/COCO_train2014_000000291797.jpg", "r", encoding="latin-1") as f: 
    image = f.read() 

sess = tf.Session() 
result = sess.run(tf.image.decode_jpeg(image, channels=3)) 

これはエラーです。フェリックスへ

Not a JPEG file: starts with 0xc3 0xbf 
Not a JPEG file: starts with 0xc3 0xbf 
Traceback (most recent call last): 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 965, in _do_call 
    return fn(*args) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 947, in _run_fn 
    status, run_metadata) 
    File "/usr/lib64/python3.4/contextlib.py", line 66, in __exit__ 
    next(self.gen) 
    File "/usr/lib/python3.4/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: Invalid JPEG data, size 165886 
    [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=0, fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](DecodeJpeg/contents)]] 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/root/PycharmProjects/mytf/models/im2txt/im2txt/data/test.py", line 7, in <module> 
    result = sess.run(tf.image.decode_jpeg(image)) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 710, in run 
    run_metadata_ptr) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 908, in _run 
    feed_dict_string, options, run_metadata) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 958, in _do_run 
    target_list, options, run_metadata) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 978, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors.InvalidArgumentError: Invalid JPEG data, size 165886 
    [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=0, fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](DecodeJpeg/contents)]] 
Caused by op 'DecodeJpeg', defined at: 
    File "/root/PycharmProjects/mytf/models/im2txt/im2txt/data/test.py", line 7, in <module> 
    result = sess.run(tf.image.decode_jpeg(image)) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/ops/gen_image_ops.py", line 283, in decode_jpeg 
    name=name) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op 
    op_def=op_def) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2317, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 1239, in __init__ 
    self._traceback = _extract_stack() 


Process finished with exit code 1 

おかげで、私はこの使用してプレースホルダ書き換えしようとした tf.read_file

を使用して私の問題を解決: を、これは私のコードです:

import tensorflow as tf 

path = "/root/PycharmProjects/mscoco/train2014/COCO_train2014_000000291797.jpg" 

with open(path, "r", encoding="latin-1") as f: 
    image = f.read() 

encoded_jpeg = tf.placeholder(dtype=tf.string) 
decoded_jpeg = tf.image.decode_jpeg(encoded_jpeg, channels=3) 

sess = tf.InteractiveSession() 

sess.run(decoded_jpeg, feed_dict={encoded_jpeg: image}) 

そしてI誰かがそれを修正する方法を教えてくれる?今回は、テンソルをテンソルに供給できないため、tf.read_file()を使用できません。

Not a JPEG file: starts with 0xc3 0xbf 
Traceback (most recent call last): 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 965, in _do_call 
    return fn(*args) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 947, in _run_fn 
    status, run_metadata) 
    File "/usr/lib64/python3.4/contextlib.py", line 66, in __exit__ 
    next(self.gen) 
    File "/usr/lib/python3.4/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: Invalid JPEG data, size 165886 
    [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0)]] 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/root/PycharmProjects/mytf/models/im2txt/im2txt/data/test.py", line 13, in <module> 
    sess.run(decoded_jpeg, feed_dict={encoded_jpeg: image}) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 710, in run 
    run_metadata_ptr) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 908, in _run 
    feed_dict_string, options, run_metadata) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 958, in _do_run 
    target_list, options, run_metadata) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 978, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors.InvalidArgumentError: Invalid JPEG data, size 165886 
    [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0)]] 
Caused by op 'DecodeJpeg', defined at: 
    File "/root/PycharmProjects/mytf/models/im2txt/im2txt/data/test.py", line 9, in <module> 
    decoded_jpeg = tf.image.decode_jpeg(encoded_jpeg, channels=3) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/ops/gen_image_ops.py", line 283, in decode_jpeg 
    name=name) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op 
    op_def=op_def) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2317, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 1239, in __init__ 
    self._traceback = _extract_stack() 
+0

だから、あなたはこのような何かをすべきか?あなたの回答に明記されていなかったので、チェックしたかっただけです。 – Steven

+0

この問題を解決するにはどうすればよいですか? – chenfei

答えて

2

decode_jpgへの入力は、ファイルの内容を文字列テンソル(ここAPIを参照)、ではないのpythonのread()の結果です。これは、符号化部を追加することなく実行

file_contents = tf.read_file(filename) 
image = tf.image.decode_jpeg(image, channels=3) 
+0

うわー。私の問題を解決しました。ありがとうございました! – chenfei

+0

tf.placeholderを使用しようとしましたが、encoded_jpegのフィードはどのようにすべきですか? – chenfei

関連する問題