当前位置: 代码迷 >> 综合 >> TypeError: ‘b\... has type str, but expected one of: bytes
  详细解决方案

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

热度:74   发布时间:2024-02-05 00:58:13.0

报错信息:

Traceback (most recent call last):File "D:\ProgramSoftware\Anaconda3\envs\tensorflow1.0\lib\threading.py", line 914, in _bootstrap_innerself.run()File "D:\ProgramSoftware\Anaconda3\envs\tensorflow1.0\lib\threading.py", line 862, in runself._target(*self._args, **self._kwargs)File "build_tfrecord.py", line 210, in _process_image_filessequence_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_featurereturn 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]))

 

 

  相关解决方案