一害驹、數(shù)據(jù)集準(zhǔn)備
我們做目標(biāo)檢測(cè)的深度學(xué)習(xí)時(shí)透绩,大家都知道要有訓(xùn)練(train)集芥挣,驗(yàn)證(valid)集和測(cè)試(test)集兆龙,數(shù)據(jù)集的格式也有很多種位迂,我們這里使用的是pascal_voc數(shù)據(jù)格式。如下圖是voc2007數(shù)據(jù)集文件夾格式- Annotations文件夾
該文件下存放的是xml格式的標(biāo)簽文件详瑞,每個(gè)xml文件都對(duì)應(yīng)于JPEGImages文件夾的一張圖片。 - JPEGImages文件夾
該文件夾下存放的是數(shù)據(jù)集圖片臣缀,包括訓(xùn)練和測(cè)試圖片坝橡,一般都是jpg格式的照片,如果有必要的話(huà)可以將其他格式的照片轉(zhuǎn)換成jpg或者用PNG格式精置,這個(gè)在后續(xù)會(huì)有提到 - ImageSets文件夾
該文件夾下存放了三個(gè)文件夾计寇,分別是Layout、Main脂倦、Segmentation番宁。在這里我們只用存放圖像數(shù)據(jù)的Main文件夾,其他兩個(gè)暫且不管赖阻。 - SegmentationClass文件和SegmentationObject文件蝶押。
這兩個(gè)文件都是與圖像分割相關(guān),跟咱們這個(gè)沒(méi)有太大關(guān)系火欧,先不管棋电。
1. Annotations文件夾
Annotations文件夾中存放的是xml格式的標(biāo)簽文件,每一個(gè)xml文件都對(duì)應(yīng)于JPEGImages文件夾中的一張圖片苇侵。xml文件的解析如下所示(這是我自己做的數(shù)據(jù)集的注釋文件赶盔,想看原始的可以去下載PASCAL VOC2007原始數(shù)據(jù)集(http://host.robots.ox.ac.uk/pascal/VOC/voc2007/)):
<annotation>
<folder>faster-RCNN-test1</folder>
<filename>000001.jpg</filename> #文件名
<path>G:\UAV\faster-RCNN-test1\000001.jpg</path> #命名這個(gè)文件的文件夾名,不重要
<source> #圖像來(lái)源榆浓,不重要
<database>Unknown</database>
</source>
<size> #圖像尺寸于未,包括長(zhǎng)、寬和通道數(shù)
<width>4608</width>
<height>3456</height>
<depth>3</depth>
</size>
<segmented>0</segmented> #是否用于分割陡鹃,在目標(biāo)識(shí)別中01無(wú)所謂
<object> #檢測(cè)到的物體
<name>succulent_root</name> #物體類(lèi)別
<pose>Unspecified</pose> #拍攝角度
<truncated>0</truncated> #是否被截?cái)啵?表示完整
<difficult>0</difficult> #目標(biāo)是否難以識(shí)別烘浦,0表示容易識(shí)別
<bndbox> #bounding-box,包含左下角和右上角xy坐標(biāo)
<xmin>2136</xmin>
<ymin>2031</ymin>
<xmax>2302</xmax>
<ymax>2207</ymax>
</bndbox>
</object>
<object> #檢測(cè)到幾個(gè)物體萍鲸,其他與第一個(gè)物體同樣
<name>pots</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1844</xmin>
<ymin>1748</ymin>
<xmax>2547</xmax>
<ymax>2400</ymax>
</bndbox>
</object>
<object>
<name>tag</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>2719</xmin>
<ymin>1152</ymin>
<xmax>3743</xmax>
<ymax>1724</ymax>
</bndbox>
</object>
</annotation>
2. JPEGImages文件夾
- JPEGImages 內(nèi)部存放了PASCAL VOC所提供的所有的圖片谎倔,包括了訓(xùn)練圖片、驗(yàn)證圖片和測(cè)試圖片
- 這些圖像的像素尺寸大小不一猿推,但是橫向圖的尺寸大約在500375左右片习,縱向圖的尺寸大約在375500左右捌肴,基本不會(huì)偏差超過(guò)100。(在之后的訓(xùn)練中藕咏,第一步就是將這些圖片都resize到300300或是500500状知,所有原始圖片不能離這個(gè)標(biāo)準(zhǔn)過(guò)遠(yuǎn)。
3. ImageSets文件夾
ImageSets存放的是每一種類(lèi)型的challenge對(duì)應(yīng)的圖像數(shù)據(jù)孽查。
我們只需要準(zhǔn)備三個(gè)文件夾即可饥悴,即剛才重點(diǎn)介紹的3個(gè)文件夾,Annotation盲再,JPEGImages和ImageSets文件夾西设。
- 準(zhǔn)備訓(xùn)練所需的圖片,圖片命名成VOC2007格式答朋,這樣可以免去許多麻煩贷揽,下面是一個(gè)批量重命名文件的代碼:
import os
path = r'G:\\UAV\\faster-RCNN-test\\'
savedpath = r'G:\\UAV\\faster-RCNN-test1\\'
filelist = os.listdir(path)
for i in range(0 , len(filelist)):
input_img = path + filelist[i]
output_img = savedpath + '%06d' % (i + 1) + '.jpg'
print(input_img)
print(output_img)
os.rename(input_img , output_img)
- 對(duì)圖片進(jìn)行注釋?zhuān)疫@里使用的是Windows10 + Anaconda + LabelImg來(lái)做的,具體做法參見(jiàn)我的另一篇簡(jiǎn)書(shū)(http://www.reibang.com/p/bda8ea406498)
- 將數(shù)據(jù)集分隔成三部分分別用于faster-RCNN的訓(xùn)練梦碗,驗(yàn)證和測(cè)試禽绪,可以通過(guò)以下代碼來(lái)實(shí)現(xiàn):
import cv2
import os
import random
root = '/public/chenhx/Deep_learning_architecture/Faster-RCNN_TF/data/VOCdevkit/VOC2007/faster-RCNN-test1'
fp = open(root + '/'+'name_list.txt' , 'r')
fp_trainval = open(root + '/'+'trainval.txt', 'w')
fp_test = open(root + '/'+'test.txt', 'w')
fp_train = open(root + '/'+'train.txt', 'w')
fp_val = open(root + '/'+'val.txt', 'w')
filenames = fp.readlines()
for i in range(len(filenames)):
pic_name = filenames[i]
pic_name = pic_name.strip()
x = random.uniform(0, 1)
pic_info = pic_name.split('.')[0]
# this 0.5 represents 50% of the data as trainval data
if x >= 0.5:
fp_trainval.writelines(pic_info + '\n')
else:
fp_test.writelines(pic_info + '\n')
fp_trainval.close()
fp_test.close()
fp = open(root + '/' +'trainval.txt')
filenames = fp.readlines()
for i in range(len(filenames)):
pic_name = filenames[i]
pic_name = pic_name.strip()
pic_info = pic_name.split('.')[0]
x = random.uniform(0, 1)
# This 0.5 represents 50% of the trainval data as train data
if x >= 0.5:
fp_train.writelines(pic_info + '\n')
else:
fp_val.writelines(pic_info + '\n')
fp_train.close()
預(yù)訓(xùn)練模型、數(shù)據(jù)集的具體路徑放在:
- Faster-RCNN_TF
- data
- VOCdevkit2007
- VOC2007
- JPEGImages
- Annotations
- ImageSets
- pretrain_model
- VGG_imagenet.npy