21个TensorFlow项目转换tfrecord:TypeError: 'RGB' has type str, but expected one of: bytes(法二)

博主在Python3.5环境运行《21个TensorFlow项目》第三章data_convert.py将图片转换为tfrecord格式时出错。先是因Python3 range返回非list报错,修改代码后又出现编码和类型错误。查找资料后修改多处代码,最后再次运行该脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在看21个TensorFlow项目一书中,由于我环境是Python3.5,项目中环境应该是Python2。运行第三章data_prepare文件夹下data_convert.py将图片转换为tfrecord格式时出现

TypeError: 'range' object does not support item assignment

此处错误是因为Python3 range返回的不是list,修改:tfrecord.py第340行将

shuffled_index = range(len(filenames))

修改为

shuffled_index = list(range(len(filenames)))

 

再次运行data_convert.py时出现下列错误:

UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illega

TypeError:tf.train.Feature TypeError: 'RGB' has type str, but expected one of: bytes

TypeError: 'water' has type str, but expected one of: bytes

查找了相关资料错误原因可见https://blog.youkuaiyun.com/qq_29921623/article/details/80047339

需要修改下列地方:

tfrecord.py第160行改为  with open(filename, 'rb') as f:

tfrecord.py第94和96行修改为  colorspace = b'RGB'     image_format = b'JPEG'

tfrecord.py第104行修改为  'image/class/text': _bytes_feature(str.encode(text)),

tfrecord.py第106行修改为   'image/filename':_bytes_feature(os.path.basename(str.encode(filename))),

 

再次运行data_convert.py  (python data_convert.py -t pic/ --train-shards 2 --validation-shards 2 --num-threads 2 --dataset-name satellite)

D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino: In function 'void onDataReceived()': D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:31:16: error: 'class SPIClass' has no member named 'available' D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:14:22: error: lvalue required as unary '&' operand D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:40:47: note: in expansion of macro 'START_MARKER' D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:15:20: error: lvalue required as unary '&' operand D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:85:47: note: in expansion of macro 'END_MARKER' D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:15:20: error: lvalue required as unary '&' operand D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:95:43: note: in expansion of macro 'END_MARKER' D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino: In function 'void setup()': D:\小车例程\9DOFDemo\9DOF_Demo\9DOF_Demo.ino:111:9: error: 'class SPIClass' has no member named 'onData' exit status 1 Compilation error: 'class SPIClass' has no member named 'available' arduino出现以上错误 Traceback (most recent call last): File "C:\Users\hdhfg\PyCharmMiscProject\main.py", line 223, in <module> reconstructor = ImageReconstructor(port='COM3', baudrate=115200) File "C:\Users\hdhfg\PyCharmMiscProject\main.py", line 14, in __init__ self.serial_port = serial.Serial(port, baudrate, timeout=1) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\hdhfg\PyCharmMiscProject\.venv\Lib\site-packages\serial\serialwin32.py", line 33, in __init__ super(Serial, self).__init__(*args, **kwargs) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "C:\Users\hdhfg\PyCharmMiscProject\.venv\Lib\site-packages\serial\serialutil.py", line 244, in __init__ self.open() ~~~~~~~~~^^ File "C:\Users\hdhfg\PyCharmMiscProject\.venv\Lib\site-packages\serial\serialwin32.py", line 64, in open raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, '拒绝访问。', None, 5) python出现以上错误 TypeError:can't convert int' object to str implicitly openmv出现以上错误 修改代码
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值