對(duì)python制作自己的數(shù)據(jù)集實(shí)例講解
一、數(shù)據(jù)集介紹
點(diǎn)擊打開(kāi)鏈接17_Category_Flower 是一個(gè)不同種類(lèi)鮮花的圖像數(shù)據(jù),包含 17 不同種類(lèi)的鮮花,每類(lèi) 80 張?jiān)擃?lèi)鮮花的圖片,鮮花種類(lèi)是英國(guó)地區(qū)常見(jiàn)鮮花。下載數(shù)據(jù)后解壓文件,然后將不同的花剪切到對(duì)應(yīng)的文件夾,如下圖所示:

每個(gè)文件夾下面有80個(gè)圖片文件。
二、使用的工具
首先是在tensorflow框架下,然后介紹一下用到的兩個(gè)庫(kù),一個(gè)是os,一個(gè)是PIL。PIL(Python Imaging Library)是 Python 中最常用的圖像處理庫(kù),而Image類(lèi)又是 PIL庫(kù)中一個(gè)非常重要的類(lèi),通過(guò)這個(gè)類(lèi)來(lái)創(chuàng)建實(shí)例可以有直接載入圖像文件,讀取處理過(guò)的圖像和通過(guò)抓取的方法得到的圖像這三種方法。
三、代碼實(shí)現(xiàn)
我們是通過(guò)TFRecords來(lái)創(chuàng)建數(shù)據(jù)集的,TFRecords其實(shí)是一種二進(jìn)制文件,雖然它不如其他格式好理解,但是它能更好的利用內(nèi)存,更方便復(fù)制和移動(dòng),并且不需要單獨(dú)的標(biāo)簽文件(label)。
1、制作TFRecords文件
import os
import tensorflow as tf
from PIL import Image # 注意Image,后面會(huì)用到
import matplotlib.pyplot as plt
import numpy as np
cwd = 'D:\PyCharm Community Edition 2017.2.3\Work\google_net\jpg\\'
classes = {'daffodil', 'snowdrop', 'lilyvalley', 'bluebell', 'crocus', 'iris', 'tigerlily', 'tulip', 'fritiuary',
'sunflower', 'daisy', 'coltsfoot', 'dandelion', 'cowslip', 'buttercup', 'windflower', 'pansy'} # 花為 設(shè)定 17 類(lèi)
writer = tf.python_io.TFRecordWriter("flower_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((224, 224))
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對(duì)象對(duì)label和image數(shù)據(jù)進(jìn)行封裝
writer.write(example.SerializeToString()) # 序列化為字符串
writer.close()

首先將文件移動(dòng)到對(duì)應(yīng)的路徑:
D:\PyCharm Community Edition 2017.2.3\Work\google_net\jpg
然后對(duì)每個(gè)文件下的圖片進(jìn)行讀寫(xiě)和相應(yīng)的大小驚醒改變,具體過(guò)程是使用tf.train.Example來(lái)定義我們要填入的數(shù)據(jù)格式,其中l(wèi)abel即為標(biāo)簽,也就是最外層的文件夾名字,img_raw為易經(jīng)理二進(jìn)制化的圖片。然后使用tf.python_io.TFRecordWriter來(lái)寫(xiě)入?;镜模粋€(gè)Example中包含F(xiàn)eatures,F(xiàn)eatures里包含F(xiàn)eature(這里沒(méi)s)的字典。最后,F(xiàn)eature里包含有一個(gè) FloatList, 或者ByteList,或者Int64List。就這樣,我們把相關(guān)的信息都存到了一個(gè)文件中,所以前面才說(shuō)不用單獨(dú)的label文件。而且讀取也很方便。
執(zhí)行完以上代碼就會(huì)出現(xiàn)如下圖所示的TF文件

2、讀取TFRECORD文件
制作完文件后,將該文件讀入到數(shù)據(jù)流中,具體代碼如下:
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取出來(lái)
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 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ù)一一對(duì)應(yīng)。
3、顯示tfrecord格式的圖片
為了知道TF 文件的具體內(nèi)容,或者是怕圖片對(duì)應(yīng)的label出錯(cuò),可以將數(shù)據(jù)流以圖片的形式讀出來(lái)并保存以便查看,具體的代碼如下:
filename_queue = tf.train.string_input_producer(["flower_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對(duì)象
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [224, 224, 3])
label = tf.cast(features['label'], tf.int32)
label = tf.one_hot(label, 17, 1, 0)
with tf.Session() as sess: # 開(kāi)始一個(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(100):
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)
執(zhí)行以上代碼后,當(dāng)前項(xiàng)目對(duì)應(yīng)的文件夾下會(huì)生成100張圖片,還有對(duì)應(yīng)的label,如下圖所示:

在這里我們可以看到,前80個(gè)圖片文件的label是1,后20個(gè)圖片的label是2。 由此可見(jiàn),我們一開(kāi)始制作tfrecord文件時(shí),圖片分類(lèi)正確。
完整代碼如下:
import os
import tensorflow as tf
from PIL import Image # 注意Image,后面會(huì)用到
import matplotlib.pyplot as plt
import numpy as np
cwd = 'D:\PyCharm Community Edition 2017.2.3\Work\google_net\jpg\\'
classes = {'daffodil', 'snowdrop', 'lilyvalley', 'bluebell', 'crocus', 'iris', 'tigerlily', 'tulip', 'fritiuary',
'sunflower', 'daisy', 'coltsfoot', 'dandelion', 'cowslip', 'buttercup', 'windflower', 'pansy'} # 花為 設(shè)定 17 類(lèi)
writer = tf.python_io.TFRecordWriter("flower_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((224, 224))
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對(duì)象對(duì)label和image數(shù)據(jù)進(jìn)行封裝
writer.write(example.SerializeToString()) # 序列化為字符串
writer.close()
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取出來(lái)
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 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
filename_queue = tf.train.string_input_producer(["flower_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對(duì)象
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [224, 224, 3])
label = tf.cast(features['label'], tf.int32)
label = tf.one_hot(label, 17, 1, 0)
with tf.Session() as sess: # 開(kāi)始一個(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(100):
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)
本人也是剛剛學(xué)習(xí)深度學(xué)習(xí),能力有限,不足之處請(qǐng)見(jiàn)諒,歡迎大牛一起討論,共同進(jìn)步!
以上這篇對(duì)python制作自己的數(shù)據(jù)集實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python merge、concat合并數(shù)據(jù)集的實(shí)例講解
- python 實(shí)現(xiàn)對(duì)數(shù)據(jù)集的歸一化的方法(0-1之間)
- python:pandas合并csv文件的方法(圖書(shū)數(shù)據(jù)集成)
- python 劃分?jǐn)?shù)據(jù)集為訓(xùn)練集和測(cè)試集的方法
- Python sklearn KFold 生成交叉驗(yàn)證數(shù)據(jù)集的方法
- 對(duì)python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解
- Python數(shù)據(jù)集切分實(shí)例
- Python讀取數(shù)據(jù)集并消除數(shù)據(jù)中的空行方法
- python 篩選數(shù)據(jù)集中列中value長(zhǎng)度大于20的數(shù)據(jù)集方法
- python調(diào)用攝像頭拍攝數(shù)據(jù)集
相關(guān)文章
python中numpy數(shù)組的csv文件寫(xiě)入與讀取
本文主要介紹了python中numpy數(shù)組的csv文件寫(xiě)入與讀取,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python利用Seaborn繪制多標(biāo)簽的混淆矩陣
混淆矩陣也稱誤差矩陣,是表示精度評(píng)價(jià)的一種標(biāo)準(zhǔn)格式,用n行n列的矩陣形式來(lái)表示。本文將利用Seaborn繪制多標(biāo)簽的混淆矩陣,感興趣的可以學(xué)習(xí)一下2022-07-07
淺談Pandas dataframe數(shù)據(jù)處理方法的速度比較
這篇文章主要介紹了淺談Pandas dataframe數(shù)據(jù)處理方法的速度比較,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
使用Python測(cè)試Ping主機(jī)IP和某端口是否開(kāi)放的實(shí)例
今天小編就為大家分享一篇使用Python測(cè)試Ping主機(jī)IP和某端口是否開(kāi)放的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python中的計(jì)時(shí)器timeit的使用方法
本篇文章主要介紹了python中的計(jì)時(shí)器timeit的使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Python調(diào)用服務(wù)接口的實(shí)例
今天小編就為大家分享一篇Python調(diào)用服務(wù)接口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python爬蟲(chóng)獲取國(guó)外大橋排行榜數(shù)據(jù)清單
這篇文章主要介紹了Python爬蟲(chóng)獲取國(guó)外大橋排行榜數(shù)據(jù)清單,文章通過(guò)PyQuery?解析框架展開(kāi)全文詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-05-05
淺談Python實(shí)時(shí)檢測(cè)CPU和GPU的功耗
本文主要介紹了淺談Python實(shí)時(shí)檢測(cè)CPU和GPU的功耗,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

