一、準(zhǔn)備自己的數(shù)據(jù)
拿nwpu數(shù)據(jù)集來舉例囱桨,nwpu數(shù)據(jù)集文件夾中的內(nèi)容是:
images文件夾:存放數(shù)據(jù)圖片
labelTxt文件夾:存放標(biāo)注信息伴找,images文件夾中每張圖片都對應(yīng)一個txt文件存放在此文件夾中,圖片與標(biāo)注文件的名稱相同
test.txt和train.txt:記錄訓(xùn)練集和測試集中的圖片名稱
二但汞、轉(zhuǎn)換成voc格式數(shù)據(jù)集
轉(zhuǎn)換過程提供了兩個腳本以簡化操作宿刮,將create_dir.py
與creat_annotations.py
拷貝至希望存放voc數(shù)據(jù)集的目錄(VOCdevkit)下,之后:
1私蕾、創(chuàng)建目錄
在根目錄(.../VOCdevkit)下輸入命令行命令python create_dir.py
會創(chuàng)建出以下樣式的目錄結(jié)構(gòu):
└── VOCdevkit #根目錄
└── VOC2007
├── Annotations #存放xml文件僵缺,與JPEGImages中的圖片一一對應(yīng),解釋圖片的內(nèi)容等等
├── ImageSets
│ ├── Action
│ ├── Layout
│ ├── Main
│ └── Segmentation
├── JPEGImages #存放源圖片
├── SegmentationClass #存放的是圖片踩叭,語義分割相關(guān)
└── SegmentationObject #存放的是圖片磕潮,實(shí)例分割相關(guān)
2、拷貝圖片
將圖片全部拷貝至JPEGImages中
或者容贝,創(chuàng)建軟連接到JPEGImages
總之自脯,使得點(diǎn)擊JPEGImages文件夾后的內(nèi)容就是數(shù)據(jù)集的圖片
3、生成Annotations文件夾中xml文件
在根目錄(.../VOCdevkit)下輸入命令行命令$ python create_annotations.py
PS:需要先修改下文件內(nèi)容的四處(已在代碼行后標(biāo)注)
import os, sys
import glob
from PIL import Image
label_lists = []
img_lists = []
src_label_dir = '/mnt/B/nwpu/labelTxt/' ###指向自己數(shù)據(jù)集的labelTxt文件夾
src_img_dir = '/mnt/B/nwpu/images/' ###指向自己數(shù)據(jù)集的images文件夾
out_xml_dir = '/mnt/D/yuangan/datasets/nwpu/VOC2007/Annotations/' ###指向voc數(shù)據(jù)集的Annotations文件夾
# write in xml file
# os.mknod(src_xml_dir + '/' + img + '.xml')
xml_file = open((out_xml_dir + '/' + name + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(name) + '.png' + '</filename>\n') ###若準(zhǔn)備的圖片為jpg格式則將png替換為jpg
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
xml_file.close()
for label_item in label_lists:
4斤富、確定voc數(shù)據(jù)集的訓(xùn)練集與測試集
將自己數(shù)據(jù)集中的test.txt和train.txt拷貝到/ImageSets/Main下膏潮,并將test.txt改名成testval.txt
至此,數(shù)據(jù)集格式轉(zhuǎn)化完成
二满力、用mmdetection訓(xùn)練faster_rcnn和ssd網(wǎng)絡(luò)
以下操作均在進(jìn)入mmdetection項(xiàng)目目錄(.../mmdetection)里完成
1焕参、修改配置文件
1.1、配置檢測類別
打開mmdet/datasets/voc.py
將紅框內(nèi)容修改成數(shù)據(jù)集中的類別
1.2油额、配置圖片格式
打開mmdet/datasets/xml_style.py
紅框內(nèi)容按照圖片是jpg格式還是png格式修改
2叠纷、訓(xùn)練方法
訓(xùn)練很簡單,利用官方給出的命令:
python tools/train.py ${CONFIG_FILE}
{CONFIG_FILE}文件存放在configs/pascal_voc中
比如用faster_rcnn網(wǎng)絡(luò)訓(xùn)練:
python tools/train.py configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py
用ssd訓(xùn)練:
python tools/train.py configs/pascal_voc/ssd512_voc.py
3潦嘶、更改訓(xùn)練好的模型的存放目錄
訓(xùn)練好的網(wǎng)絡(luò)存放地址默認(rèn)在mmdetection/work_dir涩嚣,若要指定存放目錄則在命令后加入--work_dir ${YOUR_WORK_DIR}
如:
python tools/train.py configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py --work_dir /home/jokery/project/mmdetection/work_dirs/dota_faster_rcnn/