分享

读取tf.record 10-11

 木俊 2018-09-14
import tensorflow as tf
import cv2

filename = "train.tfrecords"
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),
'image' : tf.FixedLenFeature([], tf.string),
})

img = tf.decode_raw(features['image'], tf.uint8)
img = tf.reshape(img, [300, 300,3])
#notice shape应该保持一致

img = tf.cast(img, tf.float32) * (1. / 128) - 0.5
label = tf.cast(features['label'], tf.int32)

///////////////////////////////////////////
函数封装
///////////////////////////////////////////
import tensorflow as tf
import cv2
def read_decode(filename):
#filename = "train.tfrecords"
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),
'image' : tf.FixedLenFeature([], tf.string),
})

img = tf.decode_raw(features['image'], tf.uint8)
img = tf.reshape(img, [500, 500,3])

img = tf.cast(img, tf.float32) * (1. / 128) - 0.5
label = tf.cast(features['label'], tf.int32)
return img,label
//////////////////////////////////////////////////
使用案例
//////////////////////////////////////////////////
import tensorflow as tf
import cv2
import aaa

filename = "train.tfrecords"
img,label = aaa.read_decode(filename)

img_batch,label_batch = tf.train.shuffle_batch([img,label],batch_size=1,
capacity=10,
min_after_dequeue=1)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
threads = tf.train.start_queue_runners(sess=sess)
for _ in range(10):
val = sess.run(img_batch)
label = sess.run(label_batch)
val.resize((500,500,3))
cv2.imshow("cool",val)
print(label)
////////////////////////////////////////////////////////////

1

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多