1.测试Demo
1.1.下载源码
git clone https://github.com/pjreddie/darknet
cd darknet
1.2.修改Makefile文件
更改参数:
GPU=1
CUDNN=1
OPENCV=1
NVCC=/usr/local/cuda/bin/nvcc
1.3运行测试命令
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
如果报错:
./darknet: error while loading shared libraries: libcudart.so.9.0: cannot open shared object file: No such file or directory
可以运行以下命令解决:
sudo ldconfig /usr/local/cuda/lib64
2.训练自己的voc格式数据集
2.1准备voc训练数据集
按下列文件夹结构,将训练数据集放到各个文件夹下面,生成4个训练、测试和验证txt文件列表
VOCdevkit
+VOC2007
++Annotations
++ImageSets
+++Main
++JPEGImages
Annotations中是所有的xml文件
JPEGImages中是所有的训练图片
Main中是2个txt文件:train.txt和val.txt(里面的图片名没有后缀),用“generate-train-txt.py”生成:
#coding=utf-8
import os
import random
root_path="/media/wyq/719ffd70-9553-244f-ac13-2d8a0a86e395/数据集/训练数据集+模型/for-rubbish/yolov3/VOCdevkit/VOC2007/JPEGImages"
folderlist = os.listdir(root_path)
folderlist.sort(reverse=False)
file_num=len(folderlist)
file_list=range(file_num)
trainval_percent = 1
train_percent = 0.8
txtsavepath = 'ImageSets/Main'
ftrainval = open(txtsavepath+'/trainval.txt', 'w')
ftest = open(txtsavepath+'/test.txt', 'w')
ftrain = open(txtsavepath+'/train.txt', 'w')
fval = open(txtsavepath+'/val.txt', 'w')
for i in file_list:
xmlfilepath = 'Annotations/' + folderlist[i]
total_xml = os.listdir(xmlfilepath)
total_xml.sort(reverse=False)
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)
for j in list:
name= folderlist[i] + '/' + total_xml[j][:-4]+'\n'
if j in trainval:
ftrainval.write(name)
if j in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest .close()
2.2修改并执行 voc_label.py
# voc_label.py
# 生成2007_train.txt 和 2007_test.txt,分别存放了训练集和测试集图片的路径。
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=[('2007', 'train'),('2007', 'val')] #[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
classes = ["pbag","butt","box","bottle","cup"] #["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
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(year, image_id):
in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, 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(&#