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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Tensorflow-制作与使用tfrecord数据集

發(fā)布時間:2023/12/20 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow-制作与使用tfrecord数据集 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

引言

??本次博文目的是記錄下tfrecord數(shù)據(jù)集的制作與使用方式。(踩了無數(shù)坑OTZ)
??這里貼上一個數(shù)據(jù)讀取的官方教程:Tensorflow導入數(shù)據(jù)以及使用數(shù)據(jù)
??接下來舉個例子說明怎么用tfrecord,假設我要做個圖片分類的任務。首先,我這里有一個txt文件,包含著所有圖片的路徑以及它們的標簽。還有一個包含許多圖片的文件夾。類似下圖這樣:

??準備好了數(shù)據(jù)后,就可以制作與使用TFrecored啦~

制作TFrecord

??當然是先寫個制作TFrecord的函數(shù)啦。我們先讀取圖片信息的txt文件,得到每個圖片的路徑以及它們的標簽,然后對這個圖片作一些預處理,最后將圖片以及它對應的標簽序列化,并建立圖片和標簽的索引(即以下代碼的”img_raw”, “l(fā)abel”)。詳見代碼。

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的方式讀取數(shù)據(jù)(麻煩且已經(jīng)過時)。
??首先,我們定義好_parse_function,這個函數(shù)是用來指定TFrecord中索引的(即上文中的”img_raw”, “l(fā)abel”)。然后我們定義一個TFRecordDataset,并借助_parse_function來讀取數(shù)據(jù)。最后,為了得到每一輪的訓練數(shù)據(jù),我們只需要再額外聲明一個iterator,每次調(diào)用get_next()就可以啦。

# 定義如何解析TFrecord數(shù)據(jù) def _parse_function(example_proto):features = tf.parse_single_example(example_proto,features={'label': tf.FixedLenFeature([], tf.int64),'img_raw': tf.FixedLenFeature([], tf.string)})# 取出我們需要的數(shù)據(jù)(標簽,圖片)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)# 打亂順序,無限重復訓練數(shù)據(jù),定義好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的Slim模塊

??這篇文章本該到此結(jié)束的。但是我仍想說TensorFlow真的有點難用(也可能是我太弱哈哈)。主要原因是它的API太多,更新速度太快。不過,我們也能迅速學習到許多東西(畢竟它的支持者有很多,這就給我們提供了許多實例以及講解博文),比如這個關于Slim的學習例子。
??接下來聊聊Slim這個模塊,它是2016年出的新模塊,目的是減少構(gòu)建網(wǎng)絡的代碼量。個人覺得真是很好用,強烈推薦一試!!!(不信可以去上面網(wǎng)址里看看)好的,下面貼一段代碼,展示下slim的使用方式,作為本篇的結(jié)尾吧~

slim = tf.contrib.slimdef MyNet(inputs, num_classes=7, is_training=True, keep_prob=0.5, scope='MyNet'):net = tf.reshape(inputs, [-1, 128, 128, 3])with slim.arg_scope([slim.conv2d, slim.fully_connected],activation_fn=tf.nn.relu,weights_regularizer=slim.l2_regularizer(0.0005)):with slim.arg_scope([slim.conv2d],stride=1,padding='SAME',weights_initializer=tf.contrib.layers.xavier_initializer_conv2d()):net = slim.stack(net, slim.conv2d, [(8, [3, 3])], scope='conv1')net = slim.max_pool2d(net, [2, 2], scope='pool1')net = slim.stack(net, slim.conv2d, [(16, [3, 3]), (24, [3, 3])], scope='conv2')net = slim.max_pool2d(net, [2, 2], scope='pool2')net = slim.stack(net, slim.conv2d, [(24, [3, 3]), (24, [3, 3]), (36, [3, 3]), (36, [3, 3])], scope='conv3')net = slim.flatten(net)with slim.arg_scope([slim.fully_connected],weights_initializer=tf.random_normal_initializer(stddev=0.01)):net = slim.fully_connected(net, 2048, scope='fc6')net = slim.dropout(net, keep_prob, scope='dropout6')net = slim.fully_connected(net, 2048, scope='fc7')net = slim.dropout(net, keep_prob, scope='dropout7')net = slim.fully_connected(net, num_classes, activation_fn=None, scope='fc8')return netinput_data = tf.placeholder(tf.float32, [None, 128, 128, 3]) output_logits = MyNet(input_data)

總結(jié)

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

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