在Linux下使用Pytorch運行yolov3訓練自己的資料
1.說明
最近我本人一直在學習深度學習方面的內容,嘗試了tensorflow下使用yolov3也嘗試了Pytorch下使用yolov3,今天在這里就分享一下我完成Pytorch下使用yolov3訓練自己資料集的程序,希望可以幫助到其他正在學習的人,
首先要在這里由衷的感謝那些讓我吸取了經驗的帖子,csdn文章的作者,那么接下來就詳細的寫一下我自己訓練的程序,
2.環境
1.centos作業系統
2.Pytorch1.2
3.anaconda
4.英偉達Tesla p4顯卡 8g
剛開始配環境是一個特別困難的事,在這里分享一篇我覺得受益匪淺的文章:感謝大佬W_Tortoise安裝Pytorch的經驗
再分享一些需要提前安裝好的包
pip install opencv-python
pip install tqdm
pip install matplotlib
pip install pycocotools
3.制作資料集
1. 制作資料集第一步是大家要獲取到自己所需的照片,這個就不用我多說了,如果自己沒有照片,可以去voc的官網下載為大家準備好的資料集,voc資料集下載
2. 有了照片之后,咱們就需要用labelImge標注工具對圖片進行標注,可以直接去github上搜索labelImage,然后下載的檔案包括data檔案夾和可執行的exe檔案,可以直接運行,
3. 使用labelImge標記后,jpg檔案會變成xml檔案,這里我們就要按照voc資料集的格式進行檔案設定,
4. 先要從github上clone下來yolov3的檔案夾,方便后續操作,clone下來后,我們先將data檔案夾移至yolov3檔案夾下,
5. 將資料集Annotations、JPEGImages復制到YOLOV3工程目錄下的data檔案下;同時新建兩個檔案夾,分別命名為ImageSets和labels,最后我們將JPEGImages檔案夾復制粘貼一下,并將檔案夾重命名為images
6. 之后將convert_to_txt.py腳本檔案放在Annotations同級目錄下,運行這個腳本檔案生成ImageSets下的四個txt檔案,分別是train.txt,val.txt,test.txt,trainval.txt檔案,
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftest.write(name)
else:
fval.write(name)
else:
ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
7. 之后是將voc_label.py放在data的同級目錄下,運行生成labels檔案夾下的檔案,
8. voc資料集制作還有一些細節,需要大家去專門查看關于voc資料集制作的博客
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train', 'test','val']
classes = ["RBC"]
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open('data/Annotations/%s.xml' % (image_id))
out_file = open('data/labels/%s.txt' % (image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
print(wd)
for image_set in sets:
if not os.path.exists('data/labels/'):
os.makedirs('data/labels/')
image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split()
list_file = open('data/%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write('data/images/%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
4.接下來需要調整yolo中的幾個組態檔
在data檔案下新建car.data,配置內容如下:
classes=1#classes是訓練的類別的個數,可以多個
train=data/train.txt
valid=data/test.txt
names=data/car.names
backup=backup/
eval=coco
再在data檔案下新建car.names,配置內容如下:
car
5.修改yolo-tiny.cfg檔案
[net]
batch=1
subdivisions=1
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1
learning_rate=0.001
burn_in=1000
max_batches = 500200
policy=steps
steps=400000,450000
scales=.1,.1
[convolutional]
batch_normalize=1
filters=16
size=3
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=2
[convolutional]
batch_normalize=1
filters=32
size=3
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=2
[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=2
[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=2
[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=2
[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=1
[convolutional]
batch_normalize=1
filters=1024
size=3
stride=1
pad=1
activation=leaky
[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky
[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky
[convolutional]
size=1
stride=1
pad=1
filters=18
activation=linear
[yolo]
mask = 3,4,5
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=1
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
[route]
layers = -4
[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky
[upsample]
stride=2
[route]
layers = -1, 8
[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky
[convolutional]
size=1
stride=1
pad=1
filters=18
activation=linear
[yolo]
mask = 0,1,2
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=1
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
博主就在這里踩了坑,這里千萬要注意別的一些博客是有空行的,但是我訓練時cfg照著別人的改,有空行和注釋是不行的,必須像我這樣寫,
還有一個要注意的是最后一個[convolutional]里面的filters是根據classes的數量改變的,關系是filters=18 #3(class + 4 + 1),
6.之后要下載yolov3-tiny.weights權重檔案,并且要將train.py檔案中的這一行改為yolo-tiny.cfg和yolov3-tiny.weights對應的情況,要不也會出現錯誤,這個也是一個值得注意的點,
parser.add_argument('--weights', type=str, default='weights/yolov3-tiny.weights', help='initial weights path')
7.下載weights檔案后放入yolov3的weights檔案夾下,后需要運行一行代碼生成yolov3-tiny.conv.15(這里需要在darknet檔案夾中生成,比較麻煩所以我為大家提供了下載地址)
./darknet partial cfg/yolov3-tiny.cfg yolov3-tiny.weights yolov3-tiny.conv.15 15
鏈接:https://pan.baidu.com/s/1DRYENKz3u7z1W9_v7CwgJA
提取碼:woa6
復制這段內容后打開百度網盤手機App,操作更方便哦
8.訓練
因為我用的Liunx系統沒有圖形界面,我是在Linux下cmd訓練的,在簡單說一下步驟,運行你anaconda下的pytorch環境,然后cd到yolov3檔案夾,之后運行
python train.py --data data/car.data --cfg cfg/yolov3-tiny.cfg --epochs 10
epochs可以隨意改變,別的博客寫的是python train.py --data-cfg data/rbc.data --cfg cfg/yolov3-tiny.cfg --epochs 10,但是我在–data后加-cfg會報錯,所以去掉,

訓練完成后會得到模型

9.預測
這樣就到了最后的預測階段,可以拿到最后的被標注的圖片,把我們需要進行預測的圖片也就是資料集里的圖片放到data中的samples檔案夾下,運行
python detect.py --names data/car.name --source data/samples/ --cfg cfg/yolov3-tiny.cfg --weights weights/best.pt
很多博客是這樣寫的,大家可以嘗試,python detect.py --data-cfg data/rbc.data --cfg cfg/yolov3-tiny.cfg --weights weights/best.pt,我在預測的時候這樣寫會報unrecognized arguments的錯誤

我這個精度還是比較低,可能是因為epochs次數比較少,大家可以多多嘗試,
10.致謝
今天的文章就寫到這里,在最后再次感謝多我提供了幫助的人,
這里非常感謝為本次寫作提供靈感的文章感謝作者陶陶name
感謝教研室優秀的師姐@ToLiveXX感謝教研室的師兄@floodgone感謝我的同學@weixin_50935759
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/229080.html
標籤:AI
