YOLOv3:Darknet代码解析(四)结构更改与训练

article/2025/10/7 6:37:13

背景:我们需要降低YOLOv2-tiny的参数量和存储量,以便硬件实现。

目的:更改YOLO结构,去掉后面的两层卷积层,降低参数量和运算量。

相关文章:

YOLOv3:Darknet代码解析(一)安装Darknet

YOLOv3:Darknet代码解析(二)代码初步

YOLOv3:Darknet代码解析(三)卷积操作

YOLOv3:Darknet代码解析(四)结构更改与训练

YOLOv3:Darknet代码解析(五)权重与特征存储

YOLOv3:Darknet代码解析(六)简化的程序与卷积拆分

目录

一、更改后的结构

二、训练VOC数据集与COCO数据集

2.1 训练VOC数据集

2.1.1 获得Pascal VOC Data

2.1.2 Generate Labels for VOC

2.1.3 Modify Cfg for Pascal Data

2.1.4 Download Pretrained Convolutional Weights

2.1.5 Train The Model

2.2 Training YOLO on COCO

2.2.1 Get The COCO Data

2.2.2 Modify cfg for COCO

2.2.3 Train The Model

三、在我们的数据集上训练

3.1 用到的一些linux指令

3.1.1 pwd 显示路径

3.1.2 cp复制指令

3.1.3 vi指令,显示文档

3.1.4 重复上条指令

3.2 数据集与cfg

3.3 训练

3.3.1数据集准备

3.3.2 训练

3.3.3 测试


一、更改后的结构

鉴于Yolov2结构相对简单,只有conv和pool。所以直接去掉后面两层卷积,并把最后一层的filter的个数减少为30个。相应的head-hw-v2.cfg文件如下。

[net]
# Testing
#batch=1
#subdivisions=1
# Training
batch=64
subdivisions=8
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1learning_rate=0.001
max_batches = 40200
policy=steps
steps=-1,100,20000,30000
scales=.1,10,.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
size=3
stride=1
pad=1
filters=1024
activation=leaky[convolutional]
size=1
stride=1
pad=1
filters=30
activation=linear[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=1
coords=4
num=5
softmax=1
jitter=.2
rescore=1object_scale=5
noobject_scale=1
class_scale=1
coord_scale=1absolute=1
thresh = .6
random=1

更改结构时注意尽量用mousepad,其他编辑工具容易编辑不当,运行时容易报错。

 

二、训练VOC数据集与COCO数据集

2.1 训练VOC数据集

2.1.1 获得Pascal VOC Data

训练YOLO需要2007到2012年的VOC data。 You can find links to the data here. To get all the data, make a directory to store it all and from that directory run:

wget https://pjreddie.com/media/files/VOCtrainval_11-May-2012.tar
wget https://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tar
wget https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar
tar xf VOCtrainval_11-May-2012.tar
tar xf VOCtrainval_06-Nov-2007.tar
tar xf VOCtest_06-Nov-2007.tar

There will now be a VOCdevkit/ subdirectory with all the VOC training data in it.

2.1.2 Generate Labels for VOC

Now we need to generate the label files that Darknet uses. Darknet wants a .txt file for each image with a line for each ground truth object in the image that looks like:

<object-class> <x> <y> <width> <height>

Where x, y, width, and height are relative to the image's width and height. To generate these file we will run the voc_label.py script in Darknet's scripts/ directory. Let's just download it again because we are lazy.

wget https://pjreddie.com/media/files/voc_label.py
python voc_label.py

After a few minutes, this script will generate all of the requisite files. Mostly it generates a lot of label files in VOCdevkit/VOC2007/labels/ and VOCdevkit/VOC2012/labels/. In your directory you should see:

ls
2007_test.txt   VOCdevkit
2007_train.txt  voc_label.py
2007_val.txt    VOCtest_06-Nov-2007.tar
2012_train.txt  VOCtrainval_06-Nov-2007.tar
2012_val.txt    VOCtrainval_11-May-2012.tar

The text files like 2007_train.txt list the image files for that year and image set. Darknet needs one text file with all of the images you want to train on. In this example, let's train with everything except the 2007 test set so that we can test our model. Run:

cat 2007_train.txt 2007_val.txt 2012_*.txt > train.txt

Now we have all the 2007 trainval and the 2012 trainval set in one big list. That's all we have to do for data setup!

2.1.3 Modify Cfg for Pascal Data

Now go to your Darknet directory. We have to change the cfg/voc.data config file to point to your data:

  1 classes= 202 train  = <path-to-voc>/train.txt3 valid  = <path-to-voc>2007_test.txt4 names = data/voc.names5 backup = backup

You should replace <path-to-voc> with the directory where you put the VOC data.

2.1.4 Download Pretrained Convolutional Weights

For training we use convolutional weights that are pre-trained on Imagenet. We use weights from the darknet53 model. You can just download the weights for the convolutional layers here (76 MB).

wget https://pjreddie.com/media/files/darknet53.conv.74

2.1.5 Train The Model

Now we can train! Run the command:

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74

2.2 Training YOLO on COCO

You can train YOLO from scratch if you want to play with different training regimes, hyper-parameters, or datasets. Here's how to get it working on the COCO dataset.

2.2.1 Get The COCO Data

To train YOLO you will need all of the COCO data and labels. The script scripts/get_coco_dataset.sh will do this for you. Figure out where you want to put the COCO data and download it, for example:

cp scripts/get_coco_dataset.sh data
cd data
bash get_coco_dataset.sh

Now you should have all the data and the labels generated for Darknet.

2.2.2 Modify cfg for COCO

Now go to your Darknet directory. We have to change the cfg/coco.data config file to point to your data:

  1 classes= 802 train  = <path-to-coco>/trainvalno5k.txt3 valid  = <path-to-coco>/5k.txt4 names = data/coco.names5 backup = backup

You should replace <path-to-coco> with the directory where you put the COCO data.

You should also modify your model cfg for training instead of testing. cfg/yolo.cfg should look like this:

[net]
# Testing
# batch=1
# subdivisions=1
# Training
batch=64
subdivisions=8
....

2.2.3 Train The Model

Now we can train! Run the command:

./darknet detector train cfg/coco.data cfg/yolov3.cfg darknet53.conv.74

If you want to use multiple gpus run:

./darknet detector train cfg/coco.data cfg/yolov3.cfg darknet53.conv.74 -gpus 0,1,2,3

If you want to stop and restart training from a checkpoint:

./darknet detector train cfg/coco.data cfg/yolov3.cfg backup/yolov3.backup -gpus 0,1,2,3

三、在我们的数据集上训练

3.1 用到的一些linux指令

3.1.1 pwd 显示路径

3.1.2 cp复制指令

http://man.linuxde.net/cp

cp -r /home/gpu/datasets/lzw/Data/head_data/ /home/gpu/datasets/xxr/darknet/data/

cp -r 整个文件夹复制

以上命令把相应文件整个文件夹拷到我的文件夹中

3.1.3 vi指令,显示文档

http://man.linuxde.net/vi

例如,vi cfg/head.data

esc退出编辑模式进入命令模式,ZZ为存盘并推出,:q为退出

3.1.4 重复上条指令

ctrl+p

3.2 数据集与cfg

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74

参照voc数据集我们创建cfg/head.data

分类数为1,其中文件夹中train为相应的路径,valid暂时用不到,head.names文件中改为head,训练好的权重存于backup文件夹中。运用3.1.2整个文件夹复制到相应路径。

3.3 训练

3.3.1数据集准备

运用Python的批量写入程序,将准备好的数据集写入train.txt,

"""
Created on Wed Dec 13, 15:14:53 2017
批量写入 train.txt
@auther xingxiangrui
"""        
import os
#root = os.getcwd()
#picpath= '\\0\\1_f\\'
#filename=os.listdir(root+picpath)
#for file in filename:
#    if 'jpg' in file:
#        print(file)
openfile=open('train.txt','w')for root,dirs,files in os.walk(os.getcwd())for item in files:if 'jpg' in item:openfile.write(os.path.join(root,item)+'\n')if '.mat' in item or '.m' in itemos.remove(os.path.join(root,item))
openfile.close()

3.3.2 训练

./darknet detector train cfg/head.data cfg/yolov2-tinyhw.cfg

输入相应的参量,即可训练。

3.3.3 测试

./darknet detect cfg/head-hw-v2.cfg headweights/obj_9000.weights data/dog.jpg

 

相关文章:

YOLOv3:Darknet代码解析(一)安装Darknet

YOLOv3:Darknet代码解析(二)代码初步

YOLOv3:Darknet代码解析(三)卷积操作

YOLOv3:Darknet代码解析(四)结构更改与训练

YOLOv3:Darknet代码解析(五)权重与特征存储

YOLOv3:Darknet代码解析(六)简化的程序与卷积拆分


http://chatgpt.dhexx.cn/article/emCiHexi.shtml

相关文章

StrongSORT(deepsort强化版)浅实战+代码解析

1.实战部分 1.1 具体操作 其实和之前的deepsort没差 到github上下载Yolov5_StrongSORT_OSNet下载对应的yolov5去替代原文件中yolov5下载yolov5权重&#xff08;可以自动下载&#xff09;和ReID权重&#xff08;可能要科学上网&#xff09;放到weight里面 ReID权重有点神秘&a…

对比学习 ——simsiam 代码解析。:

目录 1 &#xff1a; 事先准备 。 2 &#xff1a; 代码阅读。 2.1: 数据读取 2.2&#xff1a; 模型载入 3 训练过程&#xff1a; 4 测试过程&#xff1a; 5 &#xff1a;线性验证 6 &#xff1a; 用自己数据集进行对比学习。 第一&#xff1a; 改数据集 &#x…

AutoWare 代码解析

Auto Ware 代码解析系列&#xff08;1&#xff09; Auto Ware 是日本名古屋大学的开源无人车项目&#xff0c;下图为ros仿真环境下的各个节点的关系图&#xff1a; 代码库地址为&#xff1a;https://github.com/CPFL/Autoware 上面有较为详细的仿真环境配置信息&#xff0c;建…

FAST-LIO2代码解析(六)

0. 简介 上一节我们将while内部的IKD-Tree部分全部讲完&#xff0c;下面将是最后一部分&#xff0c;关于后端优化更新的部分。 1. 迭代更新 最后一块主要做的就是&#xff0c;拿当前帧与IKD-Tree建立的地图算出的残差&#xff0c;然后去计算更新自己的位置&#xff0c;并将更…

DeblurGAN-V2源代码解析

DeblurGAN-V2源代码解析&#xff08;pytorch&#xff09; DeblurGAN-V2是DeblurGAN的改进版&#xff0c;主要解决的是去图像运动模糊的问题&#xff0c;相比于DeblurGAN而言有速度更快&#xff0c;效果更好的优点。 论文&#xff1a;https://arxiv.org/pdf/1908.03826.pdf 代码…

mmsegmentation模型生成代码解析

前言 疫情在家办公&#xff0c;新Team这边习惯用MMLab开发网络&#xff0c;正好趁这段时间理解一下商汤大佬们的框架。我之前其实网络开发的比较少&#xff0c;主要是学习用的&#xff0c;而且开发网络基本是靠手写或者copy&#xff0c;用这种架构开发我是十分赞成的&#xff…

PX4代码解析(1)

前言 做pixhawk飞控有一段时间了&#xff0c;但在学习过程中遇到许多困难&#xff0c;目前网上找不到比较完整的PX4学习笔记&#xff0c;我打算结合自己理解&#xff0c;写写自己对PX4源码的理解&#xff0c;不一定对&#xff0c;只是希望与各位大佬交流交流&#xff0c;同时梳…

PX4代码解析(2)

前言 在大致了解PX4代码架构后&#xff0c;我们需要了解PX4的通信机制。在PX4代码架构中&#xff0c;每通信总线主要分为两个部分&#xff0c;一是内部通信总线uORB,即PX4内部进程通信采用的协议&#xff0c;例如PX4内部姿态控制需要获取飞行器姿态&#xff0c;而飞行器姿态是…

Teams Bot App 代码解析

上一篇文章我们讲了如何使用 teams toolkit 来快速弄一个 teams bot&#xff0c;可以看到 toolkit 给我们提供了极大的方便性&#xff0c;让开发人员可以更好的把重心放在 coding 上&#xff0c;而不是各种配置上。 那我们这篇文章主要接着上篇&#xff0c;来解析一下 teams b…

代码分析(一)

2021SCSDUSC 分析前言 对于APIJSON的代码分析首先就是&#xff0c;看一下该项目的作用以及如何进行&#xff0c;看一下原来不部署这个项目的正常流程&#xff1a; 再来看一下部署上APIJSON后项目的流程走向&#xff1a; 接下来开始按照这个流程对相应的代码进行分析。 Abst…

Linux命令之lsusb

一、lsusb命令用于显示本机的USB设备列表&#xff0c;以及USB设备的详细信息。 二、lsusb命令显示的USB设备信息来自“/proc/bus/usb”目录下的对应文件。 三、Linux从/var/lib/usbutils/usb.ids识别USB设备的详细信息。 语法格式 lsusb [参数] 常用参数&#xff1a; -v显…

Linux命令-磁盘管理-lsusb

1 需求 2 语法 C:\>adb shell lsusb --help Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)usage: lsusbList USB hosts/devices. 3 示例 adb shell lsusb 4 参考资料

嵌入式debian没有lsusb命令解决

问题 -bash: lsusb: command not found 解决

linux之lsusb命令和cd -命令使用总结

1、lsusb命令介绍 使用 lsusb 来列出 USB 设备和它的属性,lsusb 会显示驱动和内部连接到你系统的设备。直接在控制台输入 lsusb 即可 2、lsusb简单使用 在控制台输入 lsusb 效果如下 系统中同时使用了 USB 2.0 root hub 驱动和 USB 3.0 root hub 驱动。 bus 002 指明设备…

LSB

知识点 LSB即为最低有效位&#xff08;Least Significant Bit&#xff0c;lsb&#xff09;&#xff0c;这里百度了一下&#xff1a;图片中的图像像素一般是由RGB三原色&#xff08;红绿蓝&#xff09;组成&#xff0c;每一种颜色占用8位&#xff0c;取值范围为0x00~0xFF&#…

lsusb命令-在系统中显示有关USB设备信息

在 中我们使用lsusb 列出USB设备及其属性&#xff0c;lsusb用于显示系统中的USB总线及其连接的设备信息。下面介绍如何安装并使用。 系统环境 7 安装usbutils 默认Centos7系统中没有lsusb &#xff0c;我们需要安装usbutils安装包&#xff0c;才能使用lsusb&#xff1a; […

LSF-bsub命令

文章目录 一、LSF(load sharing facility)二、bsub命令三、 常用命令3.1 bhosts3.2 bqueues3.3 bjobs3.4 bkill3.5 bhist3.6 busers 一、LSF(load sharing facility) 分布资源管理的工具&#xff0c;用来调度、监视、分析联网计算机的负载。 目的&#xff1a;通过集中监控和调…

Linux下的lsusb命令详解

lsusb命令详解 参考&#xff1a; 1、https://zhuanlan.zhihu.com/p/142403866 2、https://blog.csdn.net/phmatthaus/article/details/124198879 简介 ​USB&#xff0c;是英文Universal Serial Bus&#xff08;通用串行总线&#xff09;的缩写&#xff0c;是一个外部总线标…

详解 lsusb命令

USB设备检测的一般过程 USB设备检测也是通过/proc目录下的USB文件系统进行的。为了使一个USB设备能够正常工作&#xff0c;必须要现在系统中插入USB桥接器模块。在检测开始时&#xff0c;一般要先检测是否存在/proc/bus/usb目录&#xff0c;若不存在则尝试插入USB桥接模块。 现…

lsusb

1.lsusb查看系统的USB设备 $ lsusb Bus 001 Device 006: ID 0951:1666 Kingston Technology Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 002 Device 004: ID 0e0f:0008 VMware, Inc. Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB H…