数据预处理---Tfrecord

1.将img转为tfrecord文件

import os
import tensorflow as tf
from PIL import Image


path = './dataset/'
classes = {'daisy','dandelion','roses','sunflowers','tulips'}
writer = tf.python_io.TFRecordWriter("flowers_train.tfrecords")

for index, name in enumerate(classes):
    class_path = path + name + '/'
    for img_name in os.listdir(class_path):
        img_path = class_path + img_name
        img = Image.open(img_path)
        img = img.resize((224, 224))
        img_raw = img.tobytes()  # 将图片转化为二进制格式
        example = tf.train.Example(features=tf.train.Features(feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'data': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
        }))  
        writer.write(example.SerializeToString())  

writer.close()

2.将tfrecord转为tensor

import tensorflow as tf

def read_and_decode(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),
                                           'data': tf.FixedLenFeature([], tf.string),
                                       })  

    img = tf.decode_raw(features['data'], tf.uint8)
    img = tf.reshape(img, [224, 224, 3])  
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5  
    print(img)
    label = tf.cast(features['label'], tf.int32)  
    print(label)
    return img, label

if __name__=='__main__':
    read_and_decode('flowers_train.tfrecords')

 result:

Tensor("sub:0", shape=(224, 224, 3), dtype=float32)
Tensor("Cast_1:0", shape=(), dtype=int32)

注:通过以上两步即可将img输入给网络模型

3. tfrecord转为img

import tensorflow as tf
from PIL import Image
path='./result/'

filename_queue = tf.train.string_input_producer(["flowers_train.tfrecords"])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,features={'label': tf.FixedLenFeature([], tf.int64),
            'data' : tf.FixedLenFeature([], tf.string),})

image = tf.decode_raw(features['data'], tf.uint8)
image = tf.reshape(image, [224, 224, 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)
    #todo:按照类别文件夹读取,并保存。
    for i in range(3670):
        example, l = sess.run([image,label])
        img=Image.fromarray(example, 'RGB')
        img.save(path+str(i)+'_''Label_'+str(l)+'.jpg')
        print(example, l)
    coord.request_stop()
    coord.join(threads)

备注:数据集获取请点击这里

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值