tensorflow2 tfrecord_Tfrecord「建议收藏」

tensorflow2 tfrecord_Tfrecord「建议收藏」1生成tfrecord文件(1)数据文件格式(2)代码importtensorflowastffromPILimportImageimportosdefmake_tfrecords(data_pa

1 生成tfrecord文件

(1)数据文件格式

tensorflow2 tfrecord_Tfrecord「建议收藏」

tensorflow2 tfrecord_Tfrecord「建议收藏」

tensorflow2 tfrecord_Tfrecord「建议收藏」

(2)代码

import tensorflow as tf
from PIL import Image
import os


def make_tfrecords(data_path, classes, tfrecord_path):
    """生成tfrecord文件"""
    writer = tf.python_io.TFRecordWriter(tfrecord_path)
    for index, name in enumerate(classes):
        class_path = data_path + name + '/'
        for img_name in os.listdir(class_path):
            img_path = class_path + img_name
            img = Image.open(img_path)
            img = img.resize((256, 256))
            # 将图片转化为二进制格式
            img_data = img.tobytes()
            # example对象对label和image数据进行封装
            example = tf.train.Example(features=tf.train.Features(feature={
                "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
                'img_data': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_data]))}))
            # 序列化为字符串
            writer.write(example.SerializeToString())
    writer.close()
    return


if __name__ == '__main__':
    data_path = 'data/'
    classes = {'archery', 'riding'}
    make_tfrecords(data_path, classes, "train.tfrecords")
    pass

(3) 结果

tensorflow2 tfrecord_Tfrecord「建议收藏」

2 读取tfrecord文件并显示图片

(1)代码

import tensorflow as tf
from PIL import Image


def display_tfrecord_file(filename):
    filename_queue = tf.train.string_input_producer([filename])  # 读入流中
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)  # 返回文件名和文件
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'label': tf.FixedLenFeature([], tf.int64),
                                           'img_data': tf.FixedLenFeature([], tf.string),
                                       })  # 取出包含image和label的feature对象
    image = tf.decode_raw(features['img_data'], tf.uint8)
    image = tf.reshape(image, [256, 256, 3])
    label = tf.cast(features['label'], tf.int32)
    with tf.Session() as sess:
        init_op = tf.initialize_all_variables()
        sess.run(init_op)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        for i in range(9):
            image_data, label_info = sess.run([image, label])
            img = Image.fromarray(image_data, 'RGB')
            img.save("display/" + str(i) + '_Label_' + str(label_info) + '.jpg')
        coord.request_stop()
        coord.join(threads)
    pass


if __name__ == '__main__':
    display_tfrecord_file("train.tfrecords")
    pass

(2)结果

tensorflow2 tfrecord_Tfrecord「建议收藏」

今天的文章tensorflow2 tfrecord_Tfrecord「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/78903.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注