Skip to content

Commit

Permalink
Merge pull request #417 from pshah0086/patch-1
Browse files Browse the repository at this point in the history
Translated to English for better reachout
  • Loading branch information
Tianxiaomo committed Dec 12, 2021
2 parents 47483be + 48c5e88 commit a65d219
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions Use_yolov4_to_train_your_own_data.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
yolov4的发布引起了不少的关注,但由于darknet是大佬c语言写的,对代码的阅读有诸多不变,所以周末的时候写了个pytorch版的(蹭一波热度)。虽然pytorch——yolov4写好已经有一段时间了,但是由于种种原因一直没有进行验证(主要就是懒),大家提出了诸多问题帮助修复很多bug,还有大佬一起增加新的功能,感谢大家的帮助。这些天呼声最高的就是如何如何使用自己的数据进行训练,昨天又是周末,就把这件拖了很久的事做了。并不像使用很多数据,于是自己制作了一个简单的数据集。
The release of yolov4 has attracted a lot of attention, but because darknet is written in big brother c language, there are many unchanged reading of the code, so the weekend wrote a pytorch version (to rub a wave of heat). Although pytorch - yolov4 write good has been a while, but for a variety of reasons have not been validated (mainly lazy), people raised many questions to help fix many bugs, there are big brothers together to add new features, thank you for your help. These days the highest call is how to how to use their own data for training, and yesterday was the weekend, so the thing that has dragged on for a long time to do. It is not like using a lot of data, so I made a simple dataset myself

# 1. 代码准备

github 克隆代码
# 1. Code Preparation

github Cloning Code
```
git clone https://github.com/Tianxiaomo/pytorch-YOLOv4.git
```
# 2. 数据准备
# 2. Data Preparation

准备train.txt,内容是图片名和box。格式如下
Prepare train.txt, which contains the image name and box in the following format

```
image_path1 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
image_path2 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
...
```
- image_path : 图片名
- x1,y1 : 左上角坐标
- x2,y2 : 右下角坐标
- id : 物体类别
- image_path : Image Name
- x1,y1 : Coordinates of the upper left corner
- x2,y2 : Coordinates of the lower right corner
- id : Object Class

我自己用的数据是自己制作的了一个小数据集,检测各种各样的硬币(也就1元,5角,1角三种),为什么不使用其他的东西制作数据集呢,没有啊,手边只有这些硬币感觉比较合适,相对其他的东西也比较简单
I use their own data is their own production of a small data set to detect a variety of coins (also 1 yuan, 50 cents, 10 cents three), why not use other things to produce data sets, no ah, only these coins on hand feel more appropriate, relatively simple compared to other things

![UTOOLS1590383513325.png](https://user-gold-cdn.xitu.io/2020/5/25/1724a3e953909b1b?w=1649&h=791&f=png&s=1290382)

一共准备了没几张
A total of a few prepared

# 3. 参数设置
# 3. Parameter Setting

开始训练的时候我直接用原来的参数,batch size设为64,跑了几个epoch发现不对,我数据一共才二十多个。后修改网络更新策略,不是按照每个epoch的step更新,使用总的steps更新,观察loss貌似可以训练了,于是睡觉,明天再看训练如何(鬼知道我又改了啥)。
When I started training, I directly used the original parameters, batch size set to 64, ran a few epochs found that it is not right, my data is only a total of more than 20. After modifying the network update strategy, not in accordance with the step of each epoch update, using the total steps update, observe the loss seems to be able to train, so sleep, tomorrow to see how the training (the ghost knows what I changed)

今天打开电脑一看,what xx,loss收敛到2.e+4下不去了,此种必又蹊跷,遂kill了。于是把batch size直接设为4,可以正常训练了
Today, I opened my computer and saw that what xx,loss converged to 2.e+4, which must be strange again, so I killed it. So I set the batch size to 4 directly, and can train normally

```
Cfg.batch = 4
Cfg.subdivisions = 1
```

# 4. 开始训练
# 4. Start training

```
python train.py -l 0.001 -g 4 -pretrained ./yolov4.conv.137.pth -classes 3 -dir /home/OCR/coins
-l 学习率
-l learning rate
-g gpu id
-pretrained 预训练的主干网络,从AlexeyAB给的darknet的yolov4.conv.137转换过来的
-classes 类别种类
-dir 图片所在文件夹
-pretrained Pre-trained backbone network, converted from yolov4.conv.137 of darknet given by AlexeyAB
-classes NO. of classes
-dir Training image dir
```


看下loss曲线
Look at the loss curve
```
tensorboard --logdir log --host 192.168.212.75 --port 6008
```
![UTOOLS1590386319240.png](https://user-gold-cdn.xitu.io/2020/5/25/1724a696148d13f3?w=1357&h=795&f=png&s=151465)

# 5. 验证
# 5. Inference

```
python model.py 3 weight/Yolov4_epoch166_coins.pth data/coin2.jpg data/coins.names
Expand All @@ -73,10 +74,10 @@ coins.names

![UTOOLS1590386705468.png](https://user-gold-cdn.xitu.io/2020/5/25/1724a6f46e826bb8?w=774&h=1377&f=png&s=1191048)

效果差强人意(训练数据只有3种类型硬币)。
The results were poor (only 3 types of coins were available for the training data).

#
# Attachment

- coins数据集 (链接:https://pan.baidu.com/s/1y701NRKSdpj6UKDIH-GpqA
提取码:j09s
- yolov4.conv.137.pth (链接:https://pan.baidu.com/s/1ovBie4YyVQQoUrC3AY0joA 提取码:kcel)
- coins dataset (link: https://pan.baidu.com/s/1y701NRKSdpj6UKDIH-GpqA)
(Extraction code: j09s)
- yolov4.conv.137.pth (Link: https://pan.baidu.com/s/1ovBie4YyVQQoUrC3AY0joA Extraction code: kcel)

0 comments on commit a65d219

Please sign in to comment.