tensorflow製作tfrecord格式資料集

2021-08-17 19:32:38 字數 4675 閱讀 1108

#encoding=utf-8

import os

import tensorflow as tf

from pil import image

cwd = os.getcwd()

classes =

#製作二進位制資料

defcreate_record

(): writer = tf.python_io.tfrecordwriter("train.tfrecords")

for index, name in enumerate(classes):

class_path = cwd +"/"+ name+"/"

for img_name in os.listdir(class_path):

img_path = class_path + img_name

img = image.open(img_path)

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

img_raw = img.tobytes() #將轉化為原生bytes

print index,img_raw

example = tf.train.example(

features=tf.train.features(feature=))

writer.write(example.serializetostring())

writer.close()

data = create_record()

#讀取二進位制資料

defread_and_decode

(filename):

# 建立檔案佇列,不限讀取的數量

filename_queue = tf.train.string_input_producer([filename])

# create a reader from file queue

reader = tf.tfrecordreader()

# reader從檔案佇列中讀入乙個序列化的樣本

_, serialized_example = reader.read(filename_queue)

# get feature from serialized example

# 解析符號化的樣本

features = tf.parse_single_example(

serialized_example,

features=

)label = features['label']

img = features['img_raw']

img = tf.decode_raw(img, tf.uint8)

img = tf.reshape(img, [64, 64, 3])

img = tf.cast(img, tf.float32) * (1. / 255) - 0.5

label = tf.cast(label, tf.int32)

return img, label

if __name__ == '__main__':

if0: data = create_record("train.tfrecords")

else:

img, label = read_and_decode("train.tfrecords")

print

"tengxing",img,label

#使用shuffle_batch可以隨機打亂輸入 next_batch挨著往下取

# shuffle_batch才能實現[img,label]的同步,也即特徵和label的同步,不然可能輸入的特徵和label不匹配

# 比如只有這樣使用,才能使img和label一一對應,每次提取乙個image和對應的label

# shuffle_batch返回的值就是randomshufflequeue.dequeue_many()的結果

# shuffle_batch構建了乙個randomshufflequeue,並不斷地把單個的[img,label],送入佇列中

img_batch, label_batch = tf.train.shuffle_batch([img, label],

batch_size=4, capacity=2000,

min_after_dequeue=1000)

# 初始化所有的op

init = tf.initialize_all_variables()

with tf.session() as sess:

sess.run(init)

# 啟動佇列

threads = tf.train.start_queue_runners(sess=sess)

for i in range(5):

print img_batch.shape,label_batch

val, l = sess.run([img_batch, label_batch])

# l = to_categorical(l, 12)

print(val.shape, l)

#製作二進位制資料

defcreate_record

(): cwd = os.getcwd()

classes =

writer = tf.python_io.tfrecordwriter("train.tfrecords")

for index, name in enumerate(classes):

class_path = cwd +"/"+ name+"/"

for img_name in os.listdir(class_path):

img_path = class_path + img_name

img = image.open(img_path)

img = img.resize((28, 28))

img_raw = img.tobytes() #將轉化為原生bytes

#print index,img_raw

example = tf.train.example(

features=tf.train.features(

feature=))

writer.write(example.serializetostring())

writer.close()

#讀取二進位制資料

defread_and_decode

(filename):

# 建立檔案佇列,不限讀取的數量

filename_queue = tf.train.string_input_producer([filename])

# create a reader from file queue

reader = tf.tfrecordreader()

# reader從檔案佇列中讀入乙個序列化的樣本

_, serialized_example = reader.read(filename_queue)

# get feature from serialized example

# 解析符號化的樣本

features = tf.parse_single_example(

serialized_example,

features=

)label = features['label']

img = features['img_raw']

img = tf.decode_raw(img, tf.uint8)

img = tf.reshape(img, [64, 64, 3])

img = tf.cast(img, tf.float32) * (1. / 255) - 0.5

label = tf.cast(label, tf.int32)

return img, label

with tf.session() as sess:

sess.run(init)

# 啟動佇列

threads = tf.train

.start_queue_runners(sess=sess)

for i in range(5):

print img_batch.shape,label_batch

val, l = sess.run([img_batch, label_batch])

# l = to_categorical(l, 12)

print(val.shape, l)

使用的時候記得使用val和l,不然會報型別錯誤:typeerror: the value of a feed cannot be a tf.tensor object. acceptable feed values include python scalars, strings, lists, or numpy ndarrays.

算交叉熵時候:cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits,labels)算交叉熵 

最後評估的時候用tf.nn.in_top_k(logits,labels,1)選logits最大的數的索引和label比較

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))算交叉熵,所以label必須轉成one-hot向量

tensorflow高效讀取資料之tfrecord

tensorflow提供了一種統一的格式來儲存資料,這個格式就是tfrecord,接下來介紹如何使用tfrecord來統一輸入資料的格式。tfrcord檔案中的資料都是通過tf.train.example protocol buffer的格式來儲存的,以下 給出了tf.train.example的定...

Tensorflow中製作tfrecord資料集

tensorflow提供統一的格式來儲存資料,tfrecord。使得在之後的系統中可以更加方便的處理,實際問題的資料往往有很多格式和屬性,用tfrecord格式可以統一不同原始資料格式,並且更加有效的管理不同屬性。import tensorflow as tf import os import cv...

php mysql製作 php MYSQL製作分頁

header content type text html charset utf 8 傳入頁碼 p get p 每頁數量 pagesize 1 鏈結資料庫 host localhost user root pass db test conn mysqli connect host,user,pas...