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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TensorFlow学习笔记(二十四)自制TFRecord数据集 读取、显示及代码详解

發(fā)布時(shí)間:2024/1/23 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TensorFlow学习笔记(二十四)自制TFRecord数据集 读取、显示及代码详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在跑通了官網(wǎng)的mnist和cifar10數(shù)據(jù)之后,筆者嘗試著制作自己的數(shù)據(jù)集,并保存,讀入,顯示。 TensorFlow可以支持cifar10的數(shù)據(jù)格式, 也提供了標(biāo)準(zhǔn)的TFRecord 格式,而關(guān)于 tensorflow 讀取數(shù)據(jù), 官網(wǎng)提供了3中方法

1 Feeding: 在tensorflow程序運(yùn)行的每一步, 用python代碼在線提供數(shù)據(jù)
2 Reader : 在一個(gè)計(jì)算圖(tf.graph)的開始前,將文件讀入到流(queue)中
3 在聲明tf.variable變量或numpy數(shù)組時(shí)保存數(shù)據(jù)。受限于內(nèi)存大小,適用于數(shù)據(jù)較小的情況

在本文,主要介紹第二種方法,利用tf.record標(biāo)準(zhǔn)接口來讀入文件

準(zhǔn)備圖片數(shù)據(jù)

筆者找了2類狗的圖片, 哈士奇和吉娃娃, 全部 resize成128 * 128大小
如下圖, 保存地址為D:\Python\data\dog

每類中有10張圖片

現(xiàn)在利用這2 類 20張圖片制作TFRecord文件

制作TFRECORD文件

1 先聊一下tfrecord, 這是一種將圖像數(shù)據(jù)和標(biāo)簽放在一起的二進(jìn)制文件,能更好的利用內(nèi)存,在tensorflow中快速的復(fù)制,移動(dòng),讀取,存儲(chǔ) 等等..

這里注意,tfrecord會(huì)根據(jù)你選擇輸入文件的類,自動(dòng)給每一類打上同樣的標(biāo)簽
如在本例中,只有0,1 兩類

2 先上“制作TFRecord文件”的代碼,注釋附詳解

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import os import tensorflow as tf from PIL import Image? #注意Image,后面會(huì)用到 import matplotlib.pyplot as plt import numpy as np cwd='D:\Python\data\dog\\' classes={'husky','chihuahua'} #人為 設(shè)定 2 類 writer= tf.python_io.TFRecordWriter("dog_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 #每一個(gè)圖片的地址 ????????img=Image.open(img_path) ????????img= img.resize((128,128)) ????????img_raw=img.tobytes()#將圖片轉(zhuǎn)化為二進(jìn)制格式 ????????example = tf.train.Example(features=tf.train.Features(feature={ ????????????"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])), ????????????'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) ????????})) #example對象對label和image數(shù)據(jù)進(jìn)行封裝 ????????writer.write(example.SerializeToString())? #序列化為字符串 writer.close()

運(yùn)行完這段代碼后,會(huì)生成dog_train.tfrecords 文件,如下圖

tf.train.Example 協(xié)議內(nèi)存塊包含了Features字段,通過feature將圖片的二進(jìn)制數(shù)據(jù)和label進(jìn)行統(tǒng)一封裝, 然后將example協(xié)議內(nèi)存塊轉(zhuǎn)化為字符串, tf.python_io.TFRecordWriter 寫入到TFRecords文件中。

讀取TFRECORD文件

在制作完tfrecord文件后, 將該文件讀入到數(shù)據(jù)流中。
代碼如下

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def read_and_decode(filename): # 讀入dog_train.tfrecords ????filename_queue = tf.train.string_input_producer([filename])#生成一個(gè)queue隊(duì)列 ????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), ???????????????????????????????????????})#將image數(shù)據(jù)和label取出來 ????img = tf.decode_raw(features['img_raw'], tf.uint8) ????img = tf.reshape(img, [128, 128, 3])? #reshape為128*128的3通道圖片 ????img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #在流中拋出img張量 ????label = tf.cast(features['label'], tf.int32) #在流中拋出label張量 ????return img, label

注意,feature的屬性“l(fā)abel”和“img_raw”名稱要和制作時(shí)統(tǒng)一 ,返回的img數(shù)據(jù)和label數(shù)據(jù)一一對應(yīng)。返回的img和label是2個(gè) tf 張量,print出來 如下圖

顯示tfrecord格式的圖片

有些時(shí)候我們希望檢查分類是否有誤,或者在之后的網(wǎng)絡(luò)訓(xùn)練過程中可以監(jiān)視,輸出圖片,來觀察分類等操作的結(jié)果,那么我們就可以session回話中,將tfrecord的圖片從流中讀取出來,再保存。 緊跟著一開始的代碼寫:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 filename_queue = tf.train.string_input_producer(["dog_train.tfrecords"]) #讀入流中 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), ???????????????????????????????????})? #取出包含image和label的feature對象 image = tf.decode_raw(features['img_raw'], tf.uint8) image = tf.reshape(image, [128, 128, 3]) label = tf.cast(features['label'], tf.int32) with tf.Session() as sess: #開始一個(gè)會(huì)話 ????init_op = tf.initialize_all_variables() ????sess.run(init_op) ????coord=tf.train.Coordinator() ????threads= tf.train.start_queue_runners(coord=coord) ????for i in range(20): ????????example, l = sess.run([image,label])#在會(huì)話中取出image和label ????????img=Image.fromarray(example, 'RGB')#這里Image是之前提到的 ????????img.save(cwd+str(i)+'_''Label_'+str(l)+'.jpg')#存下圖片 ????????print(example, l) ????coord.request_stop() ????coord.join(threads)

代碼運(yùn)行完后, 從tfrecord中取出的文件被保存了。如下圖:

在這里我們可以看到,圖片文件名的第一個(gè)數(shù)字表示在流中的順序(這里沒有用shuffle), 第二個(gè)數(shù)字則是 每個(gè)圖片的label,吉娃娃都為0,哈士奇都為1。 由此可見,我們一開始制作tfrecord文件時(shí),圖片分類正確。


下面給出一些常見圖片處理方式:

# -*- coding: utf-8 -*-


import matplotlib.pyplot as plt
import tensorflow as tf? ?
import numpy as np
import os

os.getcwd()

image_raw_data = tf.gfile.FastGFile("E:\\testData\\images\\cat.jpg",'rb').read()

with tf.Session() as sess:
??? img_data = tf.image.decode_jpeg(image_raw_data)
?? ?
??? # 輸出解碼之后的三維矩陣。
??? #print(img_data.eval())
??? #print(img_data.get_shape())
??? img_data.set_shape([1797, 2673, 3])
??? print(img_data.get_shape())
?
#### 2. 打印圖片?? ?
with tf.Session() as sess:
??? plt.imshow(img_data.eval())
??? plt.show()
#### 3. 重新調(diào)整圖片大小?? ?
with tf.Session() as sess:?? ?
??? resized = tf.image.resize_images(img_data, [300, 300], method=0)
?? ?
??? # TensorFlow的函數(shù)處理圖片后存儲(chǔ)的數(shù)據(jù)是float32格式的,需要轉(zhuǎn)換成uint8才能正確打印圖片。
??? print("Digital type: ", resized.dtype)
??? cat = np.asarray(resized.eval(), dtype='uint8')
??? # tf.image.convert_image_dtype(rgb_image, tf.float32)
??? plt.imshow(cat)
??? plt.show()?? ?
?? ?
#### 4. 裁剪和填充圖片?? ?
with tf.Session() as sess:?? ?
??? croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
??? padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
??? plt.imshow(croped.eval())
??? plt.show()
??? plt.imshow(padded.eval())
??? plt.show()?? ?
?? ?
#### 5. 截取中間50%的圖片?? ?
with tf.Session() as sess:? ?
??? central_cropped = tf.image.central_crop(img_data, 0.5)
??? plt.imshow(central_cropped.eval())
??? plt.show()?? ?
?? ?
#### 6. 翻轉(zhuǎn)圖片?? ?
with tf.Session() as sess:
??? # 上下翻轉(zhuǎn)
??? #flipped1 = tf.image.flip_up_down(img_data)
??? # 左右翻轉(zhuǎn)
??? #flipped2 = tf.image.flip_left_right(img_data)
?? ?
??? #對角線翻轉(zhuǎn)
??? transposed = tf.image.transpose_image(img_data)
??? plt.imshow(transposed.eval())
??? plt.show()
?? ?
??? # 以一定概率上下翻轉(zhuǎn)圖片。
??? #flipped = tf.image.random_flip_up_down(img_data)
??? # 以一定概率左右翻轉(zhuǎn)圖片。
??? #flipped = tf.image.random_flip_left_right(img_data)?? ?
?? ?
#### 7. 圖片色彩調(diào)整?? ?
with tf.Session() as sess:??? ?
??? # 將圖片的亮度-0.5。
??? #adjusted = tf.image.adjust_brightness(img_data, -0.5)
?? ?
??? # 將圖片的亮度-0.5
??? #adjusted = tf.image.adjust_brightness(img_data, 0.5)
?? ?
??? # 在[-max_delta, max_delta)的范圍隨機(jī)調(diào)整圖片的亮度。
??? adjusted = tf.image.random_brightness(img_data, max_delta=0.5)
?? ?
??? # 將圖片的對比度-5
??? #adjusted = tf.image.adjust_contrast(img_data, -5)
?? ?
??? # 將圖片的對比度+5
??? #adjusted = tf.image.adjust_contrast(img_data, 5)
?? ?
??? # 在[lower, upper]的范圍隨機(jī)調(diào)整圖的對比度。
??? #adjusted = tf.image.random_contrast(img_data, lower, upper)

??? plt.imshow(adjusted.eval())
??? plt.show()?? ?
?
#### 8. 添加色相和飽和度?? ?
with tf.Session() as sess:??????? ?
??? adjusted = tf.image.adjust_hue(img_data, 0.1)
??? #adjusted = tf.image.adjust_hue(img_data, 0.3)
??? #adjusted = tf.image.adjust_hue(img_data, 0.6)
??? #adjusted = tf.image.adjust_hue(img_data, 0.9)
?? ?
??? # 在[-max_delta, max_delta]的范圍隨機(jī)調(diào)整圖片的色相。max_delta的取值在[0, 0.5]之間。
??? #adjusted = tf.image.random_hue(image, max_delta)
?? ?
??? # 將圖片的飽和度-5。
??? #adjusted = tf.image.adjust_saturation(img_data, -5)
??? # 將圖片的飽和度+5。
??? #adjusted = tf.image.adjust_saturation(img_data, 5)
??? # 在[lower, upper]的范圍隨機(jī)調(diào)整圖的飽和度。
??? #adjusted = tf.image.random_saturation(img_data, lower, upper)
?? ?
??? # 將代表一張圖片的三維矩陣中的數(shù)字均值變?yōu)?,方差變?yōu)?。
??? #adjusted = tf.image.per_image_whitening(img_data)
?? ?
??? plt.imshow(adjusted.eval())
??? plt.show()?? ?
?? ?
#### 9. 添加標(biāo)注框并裁減。?? ?
with tf.Session() as sess:??????? ?

??? boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])

??? begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
??????? tf.shape(img_data), bounding_boxes=boxes)


??? batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0)
??? image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
?? ?
??? distorted_image = tf.slice(img_data, begin, size)
??? plt.imshow(distorted_image.eval())
??? plt.show()?? ?
???

總結(jié)

以上是生活随笔為你收集整理的TensorFlow学习笔记(二十四)自制TFRecord数据集 读取、显示及代码详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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