如何制作将分割数据集封装成tfrecords

import os
from PIL import Image
import tensorflow as tf

IMAGE_SIZE = 224

#数据集的地址,最后的反斜杠别漏掉了
train_label_path=r'/home/user/python/tensorflow_1/FCN.tensorflow-master/Data_zoo/MIT_SceneParsing/ADEChallengeData2016/annotations/validation/'
train_path = r'/home/user/python/tensorflow_1/FCN.tensorflow-master/Data_zoo/MIT_SceneParsing/ADEChallengeData2016/images/validation/'

writer = tf.python_io.TFRecordWriter('fenge_test.tfrecords') #输出成tfrecord文件

#编码方式
def _bytes_feature(value):
    return tf.train.Feature(bytes_list = tf.train.BytesList(value = [value]))

train_label = os.listdir(train_label_path)
train= os.listdir(train_path)
# for img_name in os.listdir(data_path):
#     print(img_name)
#确保名字统一
train_label.sort()
train.sort()

num=1
for ln,lm  in zip(train, train_label):
    img_path = train_path + ln
    label_path = train_label_path+lm
    img = Image.open(img_path)
    lab = Image.open(label_path)
    try:
        #cheke image whether 3input
        r, g, b = img.split()
        #用来验证是否一致
        # aa=os.path.splitext(ln.split('.')[0])[0]
        # bb=os.path.splitext(lm.split('.')[0])[0]
        # print(num,aa==bb)
        # num += 1
        img = img.resize((224, 224))
        lab = lab.resize((224, 224))
        img_raw = img.tobytes()
        lab_raw = lab.tobytes()
        example = tf.train.Example(features=tf.train.Features(feature={"label": _bytes_feature(lab_raw),
                                                                       "img_raw": _bytes_feature(img_raw)
                                                                       }))
        writer.write(example.SerializeToString())
    except ValueError:
        continue
writer.close()
print("finish to write data to tfrecord file!")

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值