1.1.下载源码
git clone
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.1准备voc训练数据集
按下列文件夹结构,将训练数据集放到各个文件夹下面,生成4个训练、测试和验证txt文件列表
VOCdevkit
+VOC2007
++Annotations
++ImageSets
+++Main
++JPEGImages
Annotations中是所有的xml文件
JPEGImages中是所有的训练图片
Main中是2个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+'/', 'w')
ftest = open(txtsavepath+'/', 'w')
ftrain = open(txtsavepath+'/', 'w')
fval = open(txtsavepath+'/', '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
# 生成 和 ,分别存放了训练集和测试集图片的路径。ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import joinsets=[('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 - 1y = (box[2] + box[3])/2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x*dww = w*dwy = y*dhh = h*dhreturn (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 = t()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').textcls = obj.find('name').textif cls not in classes or int(difficult)==1:continuecls_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(&#
本文发布于:2024-01-28 18:00:09,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064360159235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |