下载项目:https://github.com/ultralytics/yolov5
1.硬件配置:
CPU:Intel Xeon Gold 6146 3.2GHz
GPU:GTX3090*4
2.1.运行环境:先安装torch+cu110、torchvision==0.8.2+cu110 torchaudio=0.7.2(cuda11.1与torch1.7.1版本不兼容)

pip install torch===1.7.1+cu110 torchvision===0.8.2+cu110 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

然后安装依赖

pip install -r requirements.txt

可能会遇到error:… thop …直接 pip install thop -i https://pypi.douban.com/simple 升级版本
2.2.数据集标注
下载好数据集后,使用labelimg软件对数据集进行标注
Labelimg软件下载:
 打开anaconda prompt
 依次输入以下命令
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/(后面这行是国内的清华镜像源,下载速度才会比较快)()

pip install pyqt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip install labelImg -i https://pypi.tuna.tsinghua.edu.cn/simple/ (Img中的I要大写,注意)
输入labelImg打开软件:


 导入数据集
点击Open Dir导入文件

点击Create RectBox进行标注


标注完成后进行保存,每张图片对应一个xml文件,里面包含一系列图片信息,如图片路径、图片名称、目标坐标、目标类别等

3.增加或者修改项目文件:
• 在YOLO项目文件中新建文件夹用来存放训练图片等(如fish)
D:~~\yolov5\fish
• 在fish文件夹下新建四个文件夹并添加test.py和voc_labels.py
• images、labels下新建train2017文件夹,原来存放图片和标注信息

• test.py

import os
import randomtrainval_percent = 0.9  # 训练和验证集所占比例,剩下的0.1就是测试集的比例
train_percent = 0.8  # 训练集所占比例,可自己进行调整
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)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)ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')for i in list:name = total_xml[i][:-4] + '\n'if i in trainval:ftrainval.write(name)if i in train:ftrain.write(name)else:fval.write(name)else:ftest.write(name)ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

• voc_labels.py

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
from os import getcwd
sets = ['train', 'val', 'test']
classes = ['fish'] #类型按项目要求写可以填多个
abs_path = os.getcwd()
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(image_id): in_file = open('Annotations/%s.xml' % (image_id), encoding='utf-8', errors='ignore') out_file = open('labels/%s.txt' % (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('ymax').text)) b1, b2, b3, b4 = b # 标注越界修正 if b2 > w: b2 = w if b4 > h: b4 = h b = (b1, b2, b3, b4) bb = convert((w, h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') wd = getcwd()
for image_set in sets: if not os.path.exists('labels/'): os.makedirs('labels/') image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split() list_file = open('%s.txt' % (image_set), 'w') for image_id in image_ids: list_file.write(abs_path + '/images/%s.jpg\n' % (image_id)) convert_annotation(image_id) list_file.close()

• Annotations是存放图像标注文件.XML,文件名最好设为数字。(标注工具labelimg)

• ImageSets:在此文件夹新建两个文件夹Main和Layout

  • 运行test.py 生成txt文件,并存放在Main里面。


    – 运行voc_labels.py 提取标注文件信息( xmin xmax ymin ymax),会在labels文件里生成txt文档。同时也会在当前目录生成test、train、val的路径文档。




    – 在YOLOv5项目的data文件里新建.yaml文件。添加如下内容
     # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ./fish/images/train2017/ # 128 images
val: ./fish/images/train2017/
# number of classes
nc: 1
# class names
names: [ 'fish' ]

– 修改模型参数,models目录下的yolov5s.yaml(用其他模型也可以)只需要改nc

• 修改train.py

选择好对应路径,epochs为迭代次数默认300,可根据要求调整。

• 运行train.py 训练完成后,网络权重参数保存在runs/train/exp_/weights文件夹中 ,其中last.pt为最后一次训练的权重,best.pt为在验证集中AP最高的权重

$ python train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 64yolov5m                                40yolov5l                                24yolov5x                                16


5.使用模型进行检测
 使用命令运行detect.py:python detect.py --source 需要检测的图片路径 --weight 权重文件路径 --conf 置信度
 例:python detect.py –-source ./data/images/fish.jpg --weights ./runs/train/exp14/weights/best.pt --conf 0.25
 用摄像头 python . detect.py --source 0 --weights weights\yolov5s.pt
报错图像文件等找不到,将datasets.py的124行注释掉,绝对路径

下图为–source的几个可选参数

 检测结果保存在runs/detect文件夹中

效果如下图所示,同时在终端显示检测到鱼的数量


6.使用自己的训练模型进行检测:
有了训练好的权重后,就可以就行目标检测测试了。直接在根目录的detect.py中进行调试,主要参数解释如下。我们平时用的话,主要用到的只有这几个参数而已:–weights,–source,–conf-thres,–project。
最后运行

python detect.py --source 0 --weights weights/best.pt
parser = argparse.ArgumentParser()
# 选用训练的权重,可用根目录下的yolov5s.pt,也可用runs/train/exp/weights/best.pt
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
# 检测数据,可以是图片/视频路径,也可以是'0'(电脑自带摄像头),也可以是rtsp等视频流
parser.add_argument('--source', type=str, default='inference/videos/猫猫识别.mp4', help='source')  # file/folder, 0 for webcam
# 网络输入图片大小
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
# 置信度阈值,检测到的对象属于特定类(狗,猫,香蕉,汽车等)的概率
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
# 做nms的iou阈值
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
# 检测的设备,cpu;0(表示一个gpu设备cuda:0);0,1,2,3(多个gpu设备)。值为空时,训练时默认使用计算机自带的显卡或CPU
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
# 是否展示检测之后的图片/视频,默认False
parser.add_argument('--view-img', action='store_true', help='display results')
# 是否将检测的框坐标以txt文件形式保存,默认False
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
# 是否将检测的labels以txt文件形式保存,默认False
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
# 设置只保留某一部分类别,如0或者0 2 3
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
# 进行nms是否也去除不同类别之间的框,默认False
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
# 推理的时候进行多尺度,翻转等操作(TTA)推理
parser.add_argument('--augment', action='store_true', help='augmented inference')
# 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
parser.add_argument('--update', action='store_true', help='update all models')
# 检测结果所存放的路径,默认为runs/detect
parser.add_argument('--project', default='runs/detect', help='save results to project/name')
# 检测结果所在文件夹的名称,默认为exp
parser.add_argument('--name', default='exp', help='save results to project/name')
# 若现有的project/name存在,则不进行递增
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
opt = parser.parse_args()

YOLOV5训练鱼类数据集过程相关推荐

  1. YOLOv5训练KAIST数据集

    YOLOv5训练KAIST数据集 YOLOv5目前比较火热,因此模型下载和使用不再赘述,网上资源很多,该文章主要是介绍如何将KAIST数据集处理成YOLOv5可以使用的格式. 一.数据获取 1.KAI ...

  2. 【目标检测】yolo系列:从yolov1到yolov5之YOLOv5训练自己数据集(v6.0)

    一.源码下载及requirments 源码下载地址:https://github.com/ultralytics/yolov5 (持续更新中) 本人所用环境如下: pytorch:1.8(因为cuda ...

  3. 【深度学习】【Python】【Widerface数据集】 转VOC格式,VOC 转YOLOv5格式,YOLOv5训练WiderFace数据集,检查yolo labels对不对

    文章目录 Widerface数据集转VOC格式 VOC 转YOLO格式 数据集的imageslisttxt YOLOv5训练 检查yolo labels对不对 并行训练 Widerface数据集转VO ...

  4. Yolov5训练自制数据集

    一.准备 1.项目链接 https://github.com/ultralytics/yolov5 2.制作数据集 将标注好的图片放到data/images/train 和data/images/va ...

  5. yolov5训练voc数据集

    1.数据集 下载好voc数据集,以2007为例,把数据集(VOCtrainval_06-Nov-2007和VOCtest_06-Nov-2007)都解压同一个文件夹里,记住解压后的图片是从000001 ...

  6. YOLOv5训练coco128数据集流程

    一.安装pytorch 1.创建虚拟环境 使用Anaconda安装,建议先创建一个虚拟环境.启动Anaconda Prompt,在命令行输入: conda create -name torch pyt ...

  7. 使用YOLOv5训练NEU-DET数据集

    一.下载YOLOv5源码和NEU-DET(钢材表面缺陷)数据集 YOLOv5源码 NEU-DET(钢材表面缺陷)数据集 这里的数据集已经经过处理了,下载即可 若通过其他途径下载的原始数据集标签为xml ...

  8. 图像语义分割模型DeepLab训练Cityscapes数据集过程记录

    参考:https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/cityscapes.md https://git ...

  9. YOLOV5训练数据集过程中特殊问题记录

    项目场景: yolov5训练GX数据集 问题描述: 运行train.py Traceback (most recent call last):File "/home/milk/yolov52 ...

最新文章

  1. Leangoo看板协作工具与Trello还真的不一样
  2. 便携式不锈钢管道焊接机器人_不锈钢管道焊接工艺
  3. python输出csv文件-更高效的Python CSV文件导出
  4. java bufferedinputstream 编码_java中关于编码的问题(字符转换流及字符缓冲流 )
  5. 【渝粤题库】国家开放大学2021春2732土地利用规划题目
  6. ntnub原理怎么看_老电工由浅入深带你入门学PLC的工作原理和梯形图的编程规则...
  7. “科学学”视角下的科研工作者行为研究
  8. phpcmsV9:后台无法选择模板
  9. js 中meta 移除head_JS函数和winform函数之间的相互调用
  10. 浅谈ASP.NET的内部机制(二)
  11. dstat系统监控工具配置与使用
  12. C++实现11平台魔兽全图外挂
  13. C语言怎么等价汇编语言,第四章汇编语言程序结构.ppt
  14. 派生类中重新定义虚函数及虚函数的注意事项
  15. 原来把300页Word转成PPT,只需要一个键!别再复制粘贴了
  16. 从师傅到伙伴:华为背后总是有IBM的影子
  17. 《趣味知识博文》小W与小L带你聊天式备考CDA Level Ⅰ(三)
  18. “首月0保费”导流模式告终 知名互联网保险平台开始裁员
  19. 【Linux】——用户账号和组账号
  20. 【愚公系列】2022年11月 uniapp专题-优购电商-个人中心页面

热门文章

  1. (转载)不完全解读 Emacs 编辑器—自由软件的旗舰
  2. java 中 u表示啥意思_“U”到底是什么意思呢?
  3. iphone 5 5s 6 6s 6plus开发
  4. 字符编码详解及利用C++ STL string遍历中文字符串
  5. 2017年软上半年软考网络工程师级别考前冲刺之第三天-朱小平-专题视频课程
  6. 绿色版Eclipse启动弹出查看错误日志
  7. ASEMI的MOS管10N65如何测量好坏
  8. 实现QQ的第三方登录 iOS开发
  9. w7 升级 W10 正规流程
  10. x264压制4K HDR电影的vs转换脚本,由HDR到SDR