yolov3训练测试自己的数据集

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(&#
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值