簡體   English   中英

tf.image.decode_png引發InvalidArgumentError

[英]tf.image.decode_png raise InvalidArgumentError

我的圖像如下所示:

培養:

-1.png

-2.png

-3.png

...

-m.png

我創建了一個file_list,其中包含這些圖像的路徑。

我的代碼:

  filename_queue = tf.train.string_input_producer(file_list) 
  reader = tf.WholeFileReader()
  key, value = reader.read(filename_queue)
  image_png_grey = tf.image.decode_png(value, channels=1)
  image_std = tf.image.per_image_standardization(image_png_grey)
  img_resize = tf.image.resize_images(image_std,[28,28],method=tf.image.ResizeMethod.AREA)

  init_op = tf.global_variables_initializer()
  with tf.Session() as sess:
    sess.run(init_op)

  # Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(len(file_list)):
      image, i_key = sess.run([img_resize, key])

和控制台:

2017-09-14 11:34:37.434681: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine andcould speed up CPU computations.
2017-09-14 11:34:37.434701: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-09-14 11:34:37.434706: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-09-14 11:34:37.434709: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-09-14 11:40:43.080336: W tensorflow/core/kernels/queue_base.cc:295] _0_input_producer: Skipping cancelled enqueue attempt with queue not closed
Traceback (most recent call last):
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1327, in _do_call
    return fn(*args)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1306, in _run_fn
    status, run_metadata)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/contextlib.py", line 88, in __exit__
    next(self.gen)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid PNG header, data size 33
         [[Node: DecodePng = DecodePng[channels=1, dtype=DT_UINT8, _device="/job:localhost/replica:0/task:0/cpu:0"](ReaderReadV2:1)]]
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "convert_2_records.py", line 87, in <module>
    main(sys.argv[1:])
  File "convert_2_records.py", line 68, in main
    image, i_key = sess.run([img_resize, key])
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
    options, run_metadata)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid PNG header, data size 33
         [[Node: DecodePng = DecodePng[channels=1, dtype=DT_UINT8, _device="/job:localhost/replica:0/task:0/cpu:0"](ReaderReadV2:1)]]
 
Caused by op 'DecodePng', defined at:
  File "convert_2_records.py", line 87, in <module>
    main(sys.argv[1:])
  File "convert_2_records.py", line 55, in main
    image_png_grey = tf.image.decode_png(value, channels=1)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/ops/gen_image_ops.py", line 393, in decode_png
    channels=channels, dtype=dtype, name=name)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/andrew/anaconda3/envs/tf3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
 
InvalidArgumentError (see above for traceback): Invalid PNG header, data size 33
         [[Node: DecodePng = DecodePng[channels=1, dtype=DT_UINT8, _device="/job:localhost/replica:0/task:0/cpu:0"](ReaderReadV2:1)]]

所有圖像都是通過Keras ImageDataGenerator的相同過程生成的。 經過幾次迭代后,超過80%的數據會發生此錯誤。 我不知道代碼有什么問題。 是生成器還是tf_records的代碼。

這是因為png圖像文件錯誤。 您可以使用以下代碼進行簽出。

try:
   image = Image.open(f)
except Exception as e:
   print('Error:', e)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM