TensorFlow读取tfrecords数据

因为要用到TensorFlow,自然少不了数据的读取,这里我自己写了一个tfrecords的数据的读取函数

"""
Created on Wed Jun 28 13:56:35 2017

@author:liao
"""
import tensorflow as tf
import numpy as np
from PIL import Image
import pprint as pp
import os

#cwd = os.getcwd()
#file_path=[cwd+os.sep+'emotion-tfrecord'+os.sep+'emotion_image_data.tfrecords']
#fileNameQue = tf.train.string_input_producer(file_path)#从文件名列表,生成文件名队列
#reader = tf.TFRecordReader()#用tfrecords专用的reader去读
#key,value = reader.read(fileNameQue)
#features = tf.parse_single_example(value,features={ 'label': tf.FixedLenFeature([], tf.int64),
#                                           'img' : tf.FixedLenFeature([], tf.string),})
#
#img = tf.decode_raw(features["img"], tf.uint8)
#image_raw=tf.reshape(img,[48,48])#如果后面要接卷积神经网络的话,就化成这样的形式
#labels = tf.cast(features["label"], tf.int32)
#
##使用样本队列,让本队列自动打包,发送
#num_preprocess_threads = 16
#image, label = tf.train.shuffle_batch([image_raw,labels], batch_size = batch_size, num_threads=num_preprocess_threads, min_after_dequeue = 1, capacity = 10)
#
#
#init = tf.global_variables_initializer()#注意这里一定要有这初始化


#with tf.Session() as sess:
#
#    sess.run(init)
#    #线程协调器,控制打包读取的线程
#    coord = tf.train.Coordinator()
#    #让读取队列开始工作
#    threads = tf.train.start_queue_runners(coord=coord)
#    #从队列读取图像和标签
#    images,labels=sess.run([image,label])
#    print(images,labels)
#
#
#    coord.request_stop()
#    coord.join(threads)
def read_tfrecords(file_path,batch_size):
    fileNameQue = tf.train.string_input_producer([file_path])
    reader = tf.TFRecordReader()#用tfrecords专用的reader去读
    key,value = reader.read(fileNameQue)    
    features = tf.parse_single_example(value,features={ 'label': tf.FixedLenFeature([], tf.int64),
                                           'img' : tf.FixedLenFeature([], tf.string),})

    img = tf.decode_raw(features["img"], tf.uint8)
    image_raw=tf.reshape(img,[1,48,48])#如果后面要接卷积神经网络的话,就化成这样的形式[depth,height,width]
    image_raw = tf.transpose(image_raw, (1,2,0))# 
    image_raw = tf.cast(image_raw, tf.float32)
#    归一化操作
    image_raw= tf.image.per_image_standardization(image_raw)
#    
    labels = tf.cast(features["label"], tf.int32)
    #使用样本队列,让本队列自动打包,发送
    num_preprocess_threads = 16
    image, label = tf.train.shuffle_batch([image_raw,labels], batch_size = batch_size, num_threads=num_preprocess_threads, min_after_dequeue = 1, capacity = 1024)
    return image, tf.reshape(label, [batch_size])
屏蔽掉的部分是测试程序。有关数TensorFlow的数据的读取过程可以参考 点击打开链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值