InvalidArgumentError (see above for traceback): Invalid JPEG data, size xxx

【出现如下形式的报错】借用github上的错误代码

File "retrain.py", line 808, in main
jpeg_data_tensor, bottleneck_tensor)
File "retrain.py", line 440, in cache_bottlenecks
jpeg_data_tensor, bottleneck_tensor)
File "retrain.py", line 392, in get_or_create_bottleneck
create_bottleneck_file(bottleneck_path, image_lists, label_name, index, image_dir, category, sess, jpeg_data_tensor, bottleneck_tensor)
File "retrain.py", line 356, in create_bottleneck_file
bottleneck_values = run_bottleneck_on_image(sess, image_data, jpeg_data_tensor, bottleneck_tensor)
File "retrain.py", line 275, in run_bottleneck_on_image
{image_data_tensor: image_data})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 982, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1032, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1052, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid JPEG data, size 147471
[[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"]]

Caused by op u'DecodeJpeg', defined at:
File "retrain.py", line 1062, in 
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "retrain.py", line 779, in main
create_inception_graph())
File "retrain.py", line 256, in create_inception_graph
RESIZED_INPUT_TENSOR_NAME]))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 308, in import_graph_def
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1228, in init
self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Invalid JPEG data, size 147471
[[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"]]

【原因】有jpg文件损坏,并不是因为数据中有png等其他格式的图片,这些格式的图片可能没有进入处理流程

【解决】关键是找出哪些jpg文件引起了报错,仔细检查源码,找出读取照片文件的代码,加入print代码把文件名打印出来,然后重新运行程序,找出报错文件,删除掉或单独进行修复。一般出错的文件较少,我的做法是直接删除

【示例】

def create_bottleneck_file(bottleneck_path, image_lists, label_name, index,
                           image_dir, category, sess, jpeg_data_tensor,
                           decoded_image_tensor, resized_input_tensor,
                           bottleneck_tensor):
  """Create a single bottleneck file."""
  tf.logging.debug('Creating bottleneck at ' + bottleneck_path)
  image_path = get_image_path(image_lists, label_name, index,
                              image_dir, category)
  if not tf.gfile.Exists(image_path):
    tf.logging.fatal('File does not exist %s', image_path)
  image_data = tf.gfile.GFile(image_path, 'rb').read()
  
  # 加入这行代码,成功找到有问题的图片,然后把它拿出来,程序就可以继续运行了
  print('image#############', image_path) # 定位有问题的图片

  try:
    bottleneck_values = run_bottleneck_on_image(
        sess, image_data, jpeg_data_tensor, decoded_image_tensor,
        resized_input_tensor, bottleneck_tensor)
  except Exception as e:
    raise RuntimeError('Error during processing file %s (%s)' % (image_path,
                                                                 str(e)))
  bottleneck_string = ','.join(str(x) for x in bottleneck_values)
  with tf.gfile.GFile(bottleneck_path, 'w') as bottleneck_file:
    bottleneck_file.write(bottleneck_string)

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页