使用tf.data读取tfrecors数据集3

下面的代码是制作数据集,从之前生成的txt图片读取数据名,然后制作

import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#这里是数据集图片的文件夹
cwd = r'/home/hehe/python/load_cifar10/datadir/'

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

#这里是之前生成的shuffle.txt
filename=r'/home/hehe/python/dataset/list_val.txt'


def _int64_feature(value):
    return tf.train.Feature(int64_list = tf.train.Int64List(value = [value]))

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

f = open(filename)
lines = f.read().splitlines()
for ln in lines:
    fname, lab = ln.split(' ')
    img_path=cwd+fname
    img = Image.open(img_path)

    img = img.resize((64, 64))

    img_raw = img.tobytes()
    example = tf.train.Example(features=tf.train.Features(feature={"label": _int64_feature(int(lab)),
                                                                    "img_raw": _bytes_feature(img_raw)
                                                                                   }))

    # print('Image:',img, 'label:',int(lab))
    writer.write(example.SerializeToString())  # 序列化为字符串
writer.close()
print("finish to write data to tfrecord file!")

猜你喜欢

转载自blog.csdn.net/qwe2508/article/details/80558774