tf 制作tfrecord笔记

来源:互联网 发布:linux中echo命令详解 编辑:程序博客网 时间:2024/06/04 19:46
import osimport tensorflow as tfcwd = 'C:\\Users\\lenovo\Desktop\\datasets\\flower_photos\\'classes = {'roses', 'sunflowers'}  # 类别tfRecordfilename = "C:\\Users\\lenovo\Desktop\\datasets\\flower_photos\\flowers_use_train.tfrecords"writer = tf.python_io.TFRecordWriter(tfRecordfilename)  # 要生成的文件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))        img_raw = img.tobytes()  # 将图片转化为二进制格式 tostring()转字符串        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]))        }))  # example对象对labelimage数据进行封装        writer.write(example.SerializeToString())  # 序列化为字符串writer.close()
原创粉丝点击