SSD Tensorflow:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start

时间:2021-06-29 20:53:05

 

我们在运行SSD Tensorflow版本的时候,会出现以下问题,我运行的命令为:

DATASET_DIR=VOC2012/
OUTPUT_DIR=tfrecords
python tf_convert_data.py \
    --dataset_name=pascalvoc \
    --dataset_dir=${DATASET_DIR} \
    --output_name=voc_2012_train \
    --output_dir=${OUTPUT_DIR}

通过百度是搜不到github上面的issues上面的答案的,我这里把问题的解决方法写出来,希望帮助大家玩SSD。错误信息为:

 Converting image 1/5138Traceback (most recent call last):
  File "tf_convert_data.py", line 59, in <module>
    tf.app.run()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "tf_convert_data.py", line 54, in main
    pascalvoc_to_tfrecords.run(FLAGS.dataset_dir, FLAGS.output_dir, FLAGS.output_name)
  File "/home/xyxt/Downloads/SSD-Tensorflow/datasets/pascalvoc_to_tfrecords.py", line 218, in run
    _add_to_tfrecord(dataset_dir, img_name, tfrecord_writer)
  File "/home/xyxt/Downloads/SSD-Tensorflow/datasets/pascalvoc_to_tfrecords.py", line 177, in _add_to_tfrecord
    _process_image(dataset_dir, name)
  File "/home/xyxt/Downloads/SSD-Tensorflow/datasets/pascalvoc_to_tfrecords.py", line 83, in _process_image
    image_data = tf.gfile.FastGFile(filename, 'r').read()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/lib/io/file_io.py", line 126, in read
    pywrap_tensorflow.ReadFromStream(self._read_buf, length, status))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/lib/io/file_io.py", line 94, in _prepare_value
    return compat.as_str_any(val)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/compat.py", line 106, in as_str_any
    return as_str(value)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/compat.py", line 84, in as_text
    return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

解决办法为:

pascalvoc_to_tfrecords.py
image_data = tf.gfile.FastGFile(filename, 'r').read()
改成:
image_data = tf.gfile.FastGFile(filename, 'rb').read()

就可以了。


参考文献

[1].can't create dataset: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte . https://github.com/balancap/SSD-Tensorflow/issues/108