TypeError: ‘b\... has type str, but expected one of: bytes

报错信息:

Traceback (most recent call last):
  File "D:\ProgramSoftware\Anaconda3\envs\tensorflow1.0\lib\threading.py", line 914, in _bootstrap_inner
    self.run()
  File "D:\ProgramSoftware\Anaconda3\envs\tensorflow1.0\lib\threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "build_tfrecord.py", line 210, in _process_image_files
    sequence_example = _to_sequence_example(image, decoder, vocab)
  File "build_tfrecord.py", line 157, in _to_sequence_example
    "image/data": _bytes_feature(encoded_image),
  File "build_tfrecord.py", line 125, in _bytes_feature
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[str(value)]))
TypeError: 'b\'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x00\\x00\\xff\\xdb\\ has type str, but expected one of: bytes

解决方法:

将以下代码:

def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[str(value)]))

修改为: 

def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value.encode('utf-8') if type(value)==str else value]))

 

 

你可能感兴趣的:(Python,TensorFlow)