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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Tensorflow—tfrecord数据集生成与使用

發布時間:2024/4/18 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow—tfrecord数据集生成与使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考內容:

數據讀取的官方教程:Tensorflow導入數據以及使用數據?

tfrecord數據集生成:

數據準備:圖片數據+圖片目錄與label一一對應的的txt

先讀取圖片信息的txt文件,得到每個圖片的路徑以及它們的標簽,然后對這個圖片作一些預處理,最后將圖片以及它對應的標簽序列化,并建立圖片和標簽的索引(即以下代碼的”img_raw”, “label”)。詳見代碼。?

import random import tensorflow as tf from PIL import Imagedef create_record(records_path, data_path, img_txt):# 聲明一個TFRecordWriterwriter = tf.python_io.TFRecordWriter(records_path)# 讀取圖片信息,并且將讀入的圖片順序打亂img_list = []with open(img_txt, 'r') as fr:img_list = fr.readlines()random.shuffle(img_list)cnt = 0# 遍歷每一張圖片信息for img_info in img_list:# 圖片相對路徑img_name = img_info.split(' ')[0]# 圖片類別img_cls = int(img_info.split(' ')[1])img_path = data_path + img_nameimg = Image.open(img_path)# 對圖片進行預處理(縮放,減去均值,二值化等等)img = img.resize((128, 128))img_raw = img.tobytes()# 聲明將要寫入tfrecord的key值(即圖片,標簽)example = tf.train.Example(features=tf.train.Features(feature={"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[img_cls])),'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))}))# 將信息寫入指定路徑writer.write(example.SerializeToString())# 打印一些提示信息~cnt += 1if cnt % 1000 == 0:print "processed %d images" % cntwriter.close()# 指定你想要生成tfrecord名稱,圖片文件夾路徑,含有圖片信息的txt文件 records_path = '/the/name/of/your/haha.tfrecords' data_path = '/the/root/of/your/image_folder/' img_txt = '/image/labels/list.txt' create_record(records_path, data_path, img_txt)

?tfrecord數據集使用:

目前為止,使用TFrecord最方便的方式是用TensorFlow的Dataset ApI。在這里,勸大家千萬千萬不要用queue的方式讀取數據(麻煩且已經過時)。?
??首先,我們定義好_parse_function,這個函數是用來指定TFrecord中索引的(即上文中的”img_raw”, “label”)。然后我們定義一個TFRecordDataset,并借助_parse_function來讀取數據。最后,為了得到每一輪的訓練數據,我們只需要再額外聲明一個iterator,每次調用get_next()就可以啦。

# 定義如何解析TFrecord數據 def _parse_function(example_proto):features = tf.parse_single_example(example_proto,features={'label': tf.FixedLenFeature([], tf.int64),'img_raw': tf.FixedLenFeature([], tf.string)})# 取出我們需要的數據(標簽,圖片)label = features['label']img = features['img_raw']img = tf.decode_raw(img, tf.uint8)# 對標簽以及圖片作預處理img = tf.reshape(img, [128, 128, 3])img = tf.cast(img, tf.float32) * (1. / 255) - 0.5label = tf.cast(label, tf.int32)return img, label# 得到獲取data batch的迭代器 def data_iterator(tfrecords):# 聲明TFRecordDatasetdataset = tf.contrib.data.TFRecordDataset(tfrecords)dataset = dataset.map(_parse_function)# 打亂順序,無限重復訓練數據,定義好batch sizedataset = dataset.shuffle(buffer_size=1000).repeat().batch(128)# 定義one_shot_iterator。官方上有許多類型的iterrator,這種是最簡單的iterator = dataset.make_one_shot_iterator()return iterator# 指定TFrecords路徑,得到training iterator。 train_tfrecords = '/your/path/to/haha.tfrecords' train_iterator = data_iterator(train_tfrecords)# 使用方式舉例 with tf.Session(config= tfconfig) as sess:tf.initialize_all_variables().run()train_batch = train_iterator.get_next()for step in xrange(50000):train_x, train_y = sess.run(train_batch)


?

?

總結

以上是生活随笔為你收集整理的Tensorflow—tfrecord数据集生成与使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。