name 'file' is not defined 和 TypeError: a bytes-like object is required, not 'str'

Python2升级Python3 TFRecord问题解决
本文解决从Python2升级到Python3环境中TFRecord的两个常见问题:file未定义错误及类型错误,通过使用open函数替代file,并在byte特征函数中加入兼容性转换,确保图像文件路径正确处理。

升级环境 python2 到 python3  ,在做TFRecord 时遇见两个问题 

报  name 'file' is not defined 

file函数改为open函数

TypeError: ' xxx.jpg' has type str, but expected one of: bytes

byte feature 函数 中 加一句

  value = tf.compat.as_bytes(value)

即  后 可正常生成

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

 

参考 https://blog.youkuaiyun.com/Lisa_Ren_123/article/details/80199256

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值