![cover](https://img-blog.csdnimg.cn/img_convert/1b49233b68f40bf2a6f806c5bd7dd995.png)
【YOLOv5】 02-标注图片,训练并使用自己的模型
安装labelimg使用labelimg训练模型使用模型继续训练
![](https://csdnimg.cn/release/devpress/public/img/ic-book.4f347164.png)
在上一篇文章中,我们完成了YOLOv5的安装和测试。如果想检测自定义目标,就需要用到LabelImg来对图片打标签,本篇文章介绍了LabelImg安装与使用,以及如何训练并使用自己的模型。
一、安装LabelImg
输入如下命令进行安装:
pip install labelimg
![](https://img-blog.csdnimg.cn/img_convert/244582bb369491af2764fd3669cb6ca4.png)
二、使用LabelImg
输入“labelimg”来打开该软件
![](https://img-blog.csdnimg.cn/img_convert/61415774881e53b97d862f6e9f282b34.png)
软件界面如下:
![](https://img-blog.csdnimg.cn/img_convert/810e859d123b50d0b9cc6d0862fa765f.png)
常用的快捷键有:
![](https://img-blog.csdnimg.cn/img_convert/09ea4a0b8312d3df31572df455a76eea.png)
点击如下按钮,打开样本所在的文件夹
![](https://img-blog.csdnimg.cn/img_convert/1b49233b68f40bf2a6f806c5bd7dd995.png)
点击如下按钮来框选目标的范围
![](https://img-blog.csdnimg.cn/img_convert/2d6c0a4cd431fe429b4fad8b48893163.png)
框选好后,选择存为YOLO格式(即将标记的信息保存为.txt格式)
![](https://img-blog.csdnimg.cn/img_convert/dc9023a55269242d37e66b7e9a733133.png)
点击“Save”来保存标记好后的第一张图(这里是灰色是因为已经保存过了),保存到一个指定的文件夹,之后所有标记好的txt都存储到该文件夹中。
注意:保存的文件名要与原始图片的文件名保持一致。每标记一张图都要记得保存!
![](https://img-blog.csdnimg.cn/img_convert/64dd21b44b254c4d9183767060010eb0.png)
点击File-》Change Save Dir ,选择刚才存储txt的那个文件夹
点击“Next Image”来准备标注下一个图片
![](https://img-blog.csdnimg.cn/img_convert/49b8d8f3fda1fb7f17b2fffccb1e4475.png)
如果遇到只能框选方形,无法框选矩形的情况,只需按下“ctrl+shift+R”就可以恢复创建矩形box
三、训练模型
首先新建训练集和验证集文件夹,分别命名为“train”和“val”
![](https://img-blog.csdnimg.cn/img_convert/59814b3e0064af78b2de2833c7d993f9.png)
“train”和“val”文件夹都需包含“images”和“labels”文件夹。其中,“images”文件夹用于存放图片,“labels”文件夹用来存放txt
![](https://img-blog.csdnimg.cn/img_convert/c938c848a8b49b10aa6cde49195c21d9.png)
将刚才标注好的约75%的图片和标签分别放入“train”中的“images”和“labels”,用作训练集
注意:图片和标签必需一一对应
![](https://img-blog.csdnimg.cn/img_convert/848d17d53d2460c1af41120450e28b7e.png)
将剩下的放入“val”中的“images”和“labels”,用作验证集
![](https://img-blog.csdnimg.cn/img_convert/ae14e08d2fbeb65066753745d179ea7e.png)
创建一个yaml文件
![](https://img-blog.csdnimg.cn/img_convert/3dd711e0d0280969041c57c5c13c4d1d.png)
文件内容如下:
![](https://img-blog.csdnimg.cn/img_convert/ff20c16c74b28805d771c36bfdc9f0aa.png)
修改“train.py”中的一些参数
![](https://img-blog.csdnimg.cn/img_convert/1f7aa80ed1d1770461c36563a3ec08d4.png)
运行train.py来训练,可以看到训练的结果存储在“runs\train\exp4\weights”中
![](https://img-blog.csdnimg.cn/img_convert/b35455fee8e2faf81158d505a7868b51.png)
四、使用模型
如下图,将detect.py中的“--weights”参数替换为刚训练好的pt文件路径,“--source”改为待检测的图片路径
![](https://img-blog.csdnimg.cn/img_convert/85c149532b908e229f69593f96684e98.png)
五、继续训练
如果想在上一次训练的基础上继续训练模型,可以更改“train.py”中的“--weights”参数,改为上一次训练的pt文件
![](https://img-blog.csdnimg.cn/img_convert/3a03a816e95fe1a25239de47db1dd3f7.png)
更多推荐
所有评论(0)