tf.image.decode_png引发InvalidArgumentError

时间:2017-09-14 03:58:16

标签: python tensorflow

我的图片看起来像这样:

列车:

- 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的代码。

1 个答案:

答案 0 :(得分:0)

这是因为png图像文件错误。您可以使用以下代码签出。

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