制作数据集时报错AttributeError: 'str' object has no attribute 'write'

weixin_46105101 2020-02-15 01:39:38
代码如下
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

#cwd='./data/train/'
cwd='./data/test/'
classes={'mask','nomask'} #人为设定2类
#writer= tf.python_io.TFRecordWriter("mask_and_nomask_test.tfrecords") #要生成的文件
writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords") #要生成的文件

for index,name in enumerate(classes):
class_path=cwd+name+'/'
for img_name in os.listdir(class_path):
img_path=class_path+img_name #每一个图片的地址

img=Image.open(img_path)
img= img.resize((128,128))
print(np.shape(img))
img_raw=img.tobytes()
example = tf.train.Example(features=tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}))
writer.write(example.SerializeToString())

writer.close()


报错如下
writer.write(example.SerializeToString()) #序列化为字符串

AttributeError: 'str' object has no attribute 'write'


谢谢各位大佬
...全文
5285 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
crifan 2020-02-17
  • 打赏
  • 举报
回复 1
多写了个等于号
把:
writer= tf.compat.v1.python_io.TFRecordWriter("mask_and_nomask_test.tfrecords")

写成:
writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords")


导致:
此处的writer不是,原本希望的TFRecordWriter()所返回的变量,而只是一个普通的字符串了
因为:
writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords")

等价于:
tf.compat.v1.python_io.TFRecordWriter = ("mask_and_nomask_test.tfrecords")
writer = ("mask_and_nomask_test.tfrecords")

等价于:
tf.compat.v1.python_io.TFRecordWriter = "mask_and_nomask_test.tfrecords"
writer = "mask_and_nomask_test.tfrecords"


即,此时:writer变量只是个str字符串
所以,此处报错:
writer,这个str字符串变量,没有(TFRecordWriter才有的)write这个属性

所以解决办法是:
去掉,自己的笔误,即去掉多写的那个等于号=
writer= tf.compat.v1.python_io.TFRecordWriter("mask_and_nomask_test.tfrecords")

即可。

其含义是:
tf.data.experimental.TFRecordWriter | TensorFlow Core v2.1.0
tf.io.TFRecordWriter | TensorFlow Core v2.1.0

调用了
* 函数:tf.compat.v1.python_io.TFRecordWriter
* 参数是:"mask_and_nomask_test.tfrecords"
返回了对应的类TFRecordWriter,其才有write函数
后续的
writer.write(xxx)

才能正常调用。
crifan 2020-02-15
  • 打赏
  • 举报
回复
如楼上所说,看起来是估计是你笔误,多写了个 =

我当时还纳闷呢:
为何你代码写成:
x=y=z
的形式,具体是啥意思呢。估计就是多了个=了。
冰风漫天 2020-02-15
  • 打赏
  • 举报
回复
是不是writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords") 应该写writer= tf.compat.v1.python_io.TFRecordWriter("mask_and_nomask_test.tfrecords") 中间多敲了=号
crifan 2020-02-15
  • 打赏
  • 举报
回复
> writer.write(example.SerializeToString())
> AttributeError: 'str' object has no attribute 'write'
说明你此处的writer.write中的writer是个str字符串

> writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords") #要生成的文件
那就说明你这句话 返回的 writer的str

只能帮你到这里了。
至于为何返回的是str字符串,只能你自己分析了。
冰风漫天 2020-02-15
  • 打赏
  • 举报
回复
writer= tf.compat.v1.python_io.TFRecordWriter=("mask_and_nomask_test.tfrecords") 你的writer是字符串

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧