摘要:训练模型为上一步建立的配置文件路径如果是训练,可能会爆内存,关掉一些后台,重新运行即可。
环境:opencv_python==4.2.0.34 pytorch==1.9.0
YOLO Fastest系列是轻量级目标检测网络,目前yolo系列最轻最快,适合全平台通用,也可以搭载在嵌入式端做到实时识别 效果
1、准备工作
使用前建议先看下REAMDE.md中的说明
配置环境:
pip install -r requirements.txt
测试是否配置成功
python test.py --data data/coco.data --weights modelzoo/coco2017-0.241078ap-model.pth --img img/000139.jpg
运行成功后 ,会生成图片test_result.png
2、数据集准备:
运行以下代码,会在当前目录新建train和val文件夹,数据集分割成训练集和验证集,分别放入两个文件夹中,并建立train.txt和val.txt文件
import osimport randomimport shutilimage_Path=r"C:/Users/User/Desktop/marked/data" #数据集路径val_num=20 #验证集数据量folder_txt_dir = ["train","val"]def Creating_folder(folder_txt_dir): "创建train和val文件夹" for foler in folder_txt_dir: data_path =os.path.join(str(os.getcwd()),foler) folder = os.path.exists(data_path) if not folder: os.makedirs(data_path) print("创建train和val文件夹成功")def Split_datasets(paths): "索引所有图片" images=[] train_num_list=[] val_num_list=[] for path,folder,imgs in os.walk(paths): for img in imgs: if img.endswith(".jpg"): images.append(os.path.join(path,img)) "分割数据集" alist = random.sample(range(0,len(images)),val_num) alist.sort(reverse=True) for n in alist: val_num_list.append(images[n]) images.pop(n) train_num_list=images "移动图片" train_path=os.path.join(str(os.getcwd()),"train") val_path=os.path.join(str(os.getcwd()),"val") for train in train_num_list: fpath,fname=os.path.split(train) shutil.copy(train, os.path.join(train_path,fname)) for val in val_num_list: fpath,fname=os.path.split(val) shutil.copy(val, os.path.join(val_path,fname)) print("分割数据集成功") def make_txt(folder_txt_dir): "创建train.txt和val.txt" for txt in folder_txt_dir: data_path =os.path.join(str(os.getcwd()),txt) image_list=os.listdir(txt) with open(file=txt+".txt",mode="a+") as f: for name in image_list: if name.endswith(".jpg"): item=os.path.join(data_path,name) f.write(item) f.write("/n") print("创建train.txt和val.txt成功")if __name__ =="__main__": Creating_folder(folder_txt_dir) Split_datasets(image_Path) make_txt(folder_txt_dir)
然后新建class.txt,写入物体类别(一行写一个)
标注数据集:
在Make Sense上进行标志数据集,点击Get Started,然后点击Click here to select them,选择分割好的train或者val文件夹,选中里面所有图片,然后点击Object Detection,点击Load labels from file,点击Click here to select them,选择刚才建立好的class.txt文件
标注图片时注意下对应标签,标注结束点击Actions,点击Export Annotations下载标志文件,将里面的图片.txt文件复制到train或者val文件夹下。
然后将class.txt改为class.namse
按照REAMDE.md上的描述,我可以得到如下目录结构
3、训练配置
生成锚点:
python genanchors.py --traintxt ./train.txt
--traintxt是上一步中生成的train.txt路径,运行后会生成anchors6.txt
配置训练文件:
在data文件夹中可以看到coco.data,我们可以仿照这个文件,新建mytrain.data,复制coco.data的内容到里面
[name]model_name=coco # model name[train-configure]epochs=300 # train epichssteps=150,250 # Declining learning rate stepsbatch_size=64 # batch sizesubdivisions=1 # Same as the subdivisions of the darknet cfg filelearning_rate=0.001 # learning rate[model-configure]pre_weights=None # The path to load the model, if it is none, then restart the trainingclasses=80 # Number of detection categorieswidth=352 # The width of the model input imageheight=352 # The height of the model input imageanchor_num=3 # anchor numanchors=12.64,19.39, 37.88,51.48, 55.71,138.31, 126.91,78.23, 131.57,214.55, 279.92,258.87 #anchor bias[data-configure]train=/media/qiuqiu/D/coco/train2017.txt # train dataset path .txt fileval=/media/qiuqiu/D/coco/val2017.txt # val dataset path .txt file names=./data/coco.names # .names category label file
注意修改里面的anchors的值,为上一步anchors6.txt的内容,另外还有model_name和classes的值以及data-configure里面的路径,其他根据自己的需求修改即可。
4、训练模型
python train.py --data data/mytrain.data
--data为上一步建立的配置文件路径
如果是CPU训练,可能会爆内存,关掉一些后台,重新运行即可。正常运行如下图
运行结束后,可以在weights文件下看到生成的.pth文件,我们使用最大一步的文件即可
5、模型评估
python evaluation.py --data data/mytrain.data --weights weights/mytrain-300-epoch-0.793847ap-model.pth
--weights这个直接指向weights文件夹中最大一步的.pth文件即可
6、使用模型进行检测
也是使用test.py文件
--data指向配置文件mytrain.data --weights指向weights文件夹中最大一步的.pth文件 --img指向想要检测图片路径,运行后也是生成test_result.png,也可以根据自己需求进行修改test.py文件
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/123169.html
摘要:近几年来,目标检测算法取得了很大的突破。本文主要讲述算法的原理,特别是算法的训练与预测中详细细节,最后将给出如何使用实现算法。但是结合卷积运算的特点,我们可以使用实现更高效的滑动窗口方法。这其实是算法的思路。下面将详细介绍算法的设计理念。 1、前言当我们谈起计算机视觉时,首先想到的就是图像分类,没错,图像分类是计算机视觉最基本的任务之一,但是在图像分类的基础上,还有更复杂和有意思的任务,如目...
摘要:检测任务需要精细粒度的视觉信息,因此在检测的时候把网络的输入分辨率从变成。的缺点的物体检测精度低于系列的算法。的损失函数中,大物体误差和小物体误差对网络训练中贡献值接近虽然采用求平方根方式,但没有根本解决问题。 ...
摘要:值得一提的是,基于百度自研的开源深度学习平台的实现,参考了论文,增加了,等处理,精度相比于原作者的实现提高了个绝对百分点,在此基础上加入最终精度相比原作者提高个绝对百分点。 YOLO作为目标检测领域的创新技术,一经推出就受到开发者的广泛关注。值得一提的是,基于百度自研的开源深度学习平台PaddlePaddle的YOLO v3实现,参考了论文【Bag of Tricks for Imag...
摘要:将图像到作为输入,输出,即将图片划分为,每个单元格独立检测。类别损失当有物体的中心点落在单元格中,此单元格就负责预测该物体。 YOLO-v1介绍 YOLO是一个端到端的目标检测算法,不需要预先提取region proposal(RCNN目标检测系列),通过一个网络就可以输出:类别,置信度,坐标位置,检测速度很快,不过,定位精度相对低些,特别是密集型小目标。 showImg(https:...
摘要:将图像到作为输入,输出,即将图片划分为,每个单元格独立检测。类别损失当有物体的中心点落在单元格中,此单元格就负责预测该物体。 YOLO-v1介绍 YOLO是一个端到端的目标检测算法,不需要预先提取region proposal(RCNN目标检测系列),通过一个网络就可以输出:类别,置信度,坐标位置,检测速度很快,不过,定位精度相对低些,特别是密集型小目标。 showImg(https:...
阅读 2146·2021-11-24 10:18
阅读 1402·2021-11-12 10:35
阅读 3127·2021-09-22 15:35
阅读 3145·2021-09-13 10:37
阅读 3524·2021-09-06 15:14
阅读 1856·2021-09-06 15:02
阅读 2027·2021-09-02 15:11
阅读 423·2019-08-30 15:53