日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

打包tfrecord文件,并读取

發(fā)布時間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 打包tfrecord文件,并读取 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Tfrecord文件是tensorflow專門設計的一種訓練樣本儲存格式,將訓練樣本打包成tfrecord格式后能夠加快文件的讀取效率。所以訓練網(wǎng)絡的第一步就是將自己的訓練集樣本打包生成tfrecord格式。本文主要介紹兩種tfrecord打包方式,這兩種方式的主要區(qū)別在于生成的tfrecord文件大小不同。

方式一:利用常用圖像處理庫讀取圖像并解碼,轉換成二進制文件進行存儲,網(wǎng)絡上找到的基本上都是這種方式。

寫入tfrecord文件

def data_to_tfrecord(images, labels, filename): # images中存儲的是所有圖像路徑的一個列表""" Save data into TFRecord """ # labels是images中每個圖像對應的標簽if os.path.isfile(filename): # filename是tfrecord文件名稱print("%s exists" % filename)returnprint("Converting data into %s ..." % filename)writer = tf.python_io.TFRecordWriter(filename)for index, img_file in zip(labels, images):img1 = Image.open(img_file) # 通過PIL包中的Images函數(shù)讀取、解碼圖片width, height = img1.size # 獲取圖像的寬、高參數(shù)img_raw = img1.tobytes() # 將圖像轉換成二進制序列l(wèi)abel = int(index) # 圖片對應的標簽example = tf.train.Example(features=tf.train.Features(feature={'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), # 保存標簽'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), # 保存二進制序列'img_width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])), # 保存圖像的寬度'img_height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height])) # 保存圖像的高}))writer.write(example.SerializeToString()) # Serialize To Stringwriter.close()


讀取tfrecord文件


import numpy as np import tensorflow as tf import tensorlayer as tldef read_and_decode(filename):""" Return tensor to read from TFRecord """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), # 從tfrecord文件中讀取各種信息'img_raw': tf.FixedLenFeature([], tf.string),'img_width': tf.FixedLenFeature([], tf.int64),'img_height': tf.FixedLenFeature([], tf.int64)})# You can do more image distortion here for training datawidth = tf.cast(features['img_width'], tf.int32) # 轉型height = tf.cast(features['img_height'], tf.int32)img = tf.decode_raw(features['img_raw'], tf.uint8) # 從二進制文件轉成uint8img = tf.reshape(img, [height, width, 3]) # 對圖像進行reshape,注意在tfrecord文件中存儲的是一序列,并沒有形狀img = tf.image.resize_images(img, [32, 32]) # 將圖像統(tǒng)一到同一尺寸# img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5label = tf.cast(features['label'], tf.int32)return img, label# Example to visualize data img, label = read_and_decode("train.tfrecord") img_batch, label_batch = tf.train.shuffle_batch([img, label],batch_size=4,capacity=5000,min_after_dequeue=100,num_threads=1) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape)init = tf.initialize_all_variables() with tf.Session() as sess:sess.run(init)coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)for i in range(3): # number of mini-batch (step)print("Step %d" % i)val, l = sess.run([img_batch, label_batch])# exit()print(val.shape, l)tl.visualize.images2d(val, second=1, saveable=False, name='batch'+str(i), dtype=np.uint8, fig_idx=2020121)coord.request_stop()coord.join(threads)sess.close()


方式二:利用tf.gfile.FastGFile讀取圖像信息(貌似并沒有解碼),轉換成二進制文件存儲。

這個方法是我在看tensorflow在github的slim框架中的生成tfrecord文件所使用的方法。

寫入tfrecord文件

def data_to_tfrecord(images, labels, filename):""" Save data into TFRecord """if os.path.isfile(filename):print("%s exists" % filename)returnprint("Converting data into %s ..." % filename)writer = tf.python_io.TFRecordWriter(filename)for index, img_file in zip(labels, images):img1 = Image.open(img_file)width, height = img1.size# img_raw = img1.tobytes()img_raw = tf.gfile.FastGFile(img_file, 'rb').read() # 與方式一不同的是使用的FastGFile函數(shù)label = int(index)example = tf.train.Example(features=tf.train.Features(feature={'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),'img_width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])),'img_height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height]))}))writer.write(example.SerializeToString()) # Serialize To Stringwriter.close()

讀取tfrecord文件

import numpy as np import tensorflow as tf import tensorlayer as tldef read_and_decode(filename):""" Return tensor to read from TFRecord """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_raw': tf.FixedLenFeature([], tf.string),'img_width': tf.FixedLenFeature([], tf.int64),'img_height': tf.FixedLenFeature([], tf.int64)})# You can do more image distortion here for training datawidth = tf.cast(features['img_width'], tf.int32)height = tf.cast(features['img_height'], tf.int32)# img = tf.decode_raw(features['img_raw'], tf.uint8)img = tf.image.decode_jpeg(features['img_raw']) # 與方式一的不同點在于需要用decode_jpeg解碼img = tf.reshape(img, [height, width, 3])img = tf.image.resize_images(img, [32, 32])# img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5label = tf.cast(features['label'], tf.int32)return img, label# Example to visualize data img, label = read_and_decode("train") img_batch, label_batch = tf.train.shuffle_batch([img, label],batch_size=4,capacity=5000,min_after_dequeue=100,num_threads=1) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape)init = tf.initialize_all_variables() with tf.Session() as sess:sess.run(init)coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(sess=sess, coord=coord)for i in range(3): # number of mini-batch (step)print("Step %d" % i)val, l = sess.run([img_batch, label_batch])# exit()print(val.shape, l)tl.visualize.images2d(val, second=1, saveable=False, name='batch'+str(i), dtype=np.uint8, fig_idx=2020121)coord.request_stop()coord.join(threads)sess.close()


兩種方式的區(qū)別

兩種方式雖然在代碼上就那么一兩行的區(qū)別,當對于生成的tfrecord文件還是有很大的區(qū)別的。我用的同樣的圖像樣本集,約200M左右,用方式一生成的tfrecord文件約900M,用方式二生成的tfrecord文件約200M。很明顯在占用內(nèi)存方面有著很大的區(qū)別。據(jù)我個人猜測,方案一將圖像解碼后在轉成二進制文件的,方案二并沒有解碼而是直接轉成二進制文件進行存儲,所以在讀取時需要進行圖像解碼。這僅是我個人猜測,如果有懂的大神,還望賜教。

總結

以上是生活随笔為你收集整理的打包tfrecord文件,并读取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。