YOLOv5入门

模型检测

关键参数

weights:训练好的模型文件

image-20230803192833654

source: 检测的目标,可以是单张图片、文件夹、屏幕或者摄像头等

image-20230803192903522

conf-thres: 置信度闯值,越低框越多,越高框越少

iou-thres: IOU闻值,越低框越少,越少框越多

torch.hub检测方法

安装Jupyter

命令行输入以下代码

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyterlab
conda install ipykernel

image-20230803193551027

激活环境

python -m ipykernel install --name yolov5

image-20230803200817611

新建Jupyter文件

hub_detect.ipynb

image-20230803193329230

import torch

#Model
model = torch.hub.load("./","yolov5s",source= "local")

#Images
img = "./data/images/zidane.jpg"

# Inference
results = model(img)

# Results
results.show()

注意运行环境,在下yolov5

image-20230803200936030

数据集构建

视频放在data02文件夹下

抽取视频帧

import cv2
import matplotlib.pyplot as plt
#%%
# 打开视频文件
video = cv2.VideoCapture("./BVN.mp4")
# 读取一帧
ret, frame = video.read()

plt.imshow(frame)

plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

video = cv2.VideoCapture("./BVN.mp4")
num = 0         # 计数器
save_step = 30  # 间隔帧
while True:
    ret, frame = video.read()
    if not ret:
        break
    num += 1
    if num % save_step == 0:
        cv2.imwrite("./images/" + str(num) + ".jpg", frame)

安装标准工具labelimg

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple labelimg

使用labelimg

  • 终端输入labelimg

  • 打开需要标注的文件夹images

image-20230803225027177

  • 设置保存路径

image-20230803225658417

  • 点击下图的标志,将保存格式改为YOLO

image-20230803225332505

  • 开启自动保存

image-20230803225752233

  • 开始标注,鼠标右键点击Create RectBox

image-20230803225938948

  • 框出两个人物,并命名

image-20230803230054984

  • 切换下一张图片,继续标准(快捷键A下一张,D下一张,W创建RectBox)

  • 标准完成后,文件夹内容如图所示

image-20230803232028650

模型训练

调整文件命名

命名严格一致,不能改

image-20230803232326057

文件调整

image-20230803232806766

image-20230803232740636

设置yaml文件

  1. 复制coco128.yaml文件,并命名为bvn.yaml

image-20230803233606669

  1. 修改bvn.yaml

image-20230803234334745

  1. 修改train.py

文件名改为bvn

workers设置为1

image-20230803233922856

image-20230804002329930

  1. 运行train.py,训练模型

中间肯能需要下载一写东西,比较慢,慢慢等(可以挂个梯子)

报错,查看下面的报错解决

image-20230804010009222

报错解决

页面文件太小,无法完成操作

训练过程中,发生下图所示的报错,同时pycharm崩溃

image-20230804000941294

1. 更改虚拟内存
  1. 进入高级系统设置,应该都会进,就不说过程了

image-20230804002758867

  1. 设置虚拟内存大小

image-20230804003012617

2. 减小占用内容大小
  1. 新建一个fixNvPe.py程序
# Simple script to disable ASLR and make .nv_fatb sections read-only
# Requires: pefile  ( python -m pip install pefile )
# Usage:  fixNvPe.py --input path/to/*.dll
 
import argparse
import pefile
import glob
import os
import shutil
 
def main(args):
    failures = []
    for file in glob.glob( args.input, recursive=args.recursive ):
        print(f"\n---\nChecking {file}...")
        pe = pefile.PE(file, fast_load=True)
        nvbSect = [ section for section in pe.sections if section.Name.decode().startswith(".nv_fatb")]
        if len(nvbSect) == 1:
            sect = nvbSect[0]
            size = sect.Misc_VirtualSize
            aslr = pe.OPTIONAL_HEADER.IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
            writable = 0 != ( sect.Characteristics & pefile.SECTION_CHARACTERISTICS['IMAGE_SCN_MEM_WRITE'] )
            print(f"Found NV FatBin! Size: {size/1024/1024:0.2f}MB  ASLR: {aslr}  Writable: {writable}")
            if (writable or aslr) and size > 0:
                print("- Modifying DLL")
                if args.backup:
                    bakFile = f"{file}_bak"
                    print(f"- Backing up [{file}] -> [{bakFile}]")
                    if os.path.exists( bakFile ):
                        print( f"- Warning: Backup file already exists ({bakFile}), not modifying file! Delete the 'bak' to allow modification")
                        failures.append( file )
                        continue
                    try:
                        shutil.copy2( file, bakFile)
                    except Exception as e:
                        print( f"- Failed to create backup! [{str(e)}], not modifying file!")
                        failures.append( file )
                        continue
                # Disable ASLR for DLL, and disable writing for section
                pe.OPTIONAL_HEADER.DllCharacteristics &= ~pefile.DLL_CHARACTERISTICS['IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE']
                sect.Characteristics = sect.Characteristics & ~pefile.SECTION_CHARACTERISTICS['IMAGE_SCN_MEM_WRITE']
                try:
                    newFile = f"{file}_mod"
                    print( f"- Writing modified DLL to [{newFile}]")
                    pe.write( newFile )
                    pe.close()
                    print( f"- Moving modified DLL to [{file}]")
                    os.remove( file )
                    shutil.move( newFile, file )
                except Exception as e:
                    print( f"- Failed to write modified DLL! [{str(e)}]")
                    failures.append( file )
                    continue
 
    print("\n\nDone!")
    if len(failures) > 0:
        print("***WARNING**** These files needed modification but failed: ")
        for failure in failures:
            print( f" - {failure}")
 
 
 
 
 
 
 
def parseArgs():
    parser = argparse.ArgumentParser( description="Disable ASLR and make .nv_fatb sections read-only", formatter_class=argparse.ArgumentDefaultsHelpFormatter )
    parser.add_argument('--input', help="Glob to parse", default="*.dll")
    parser.add_argument('--backup', help="Backup modified files", default=True, required=False)
    parser.add_argument('--recursive', '-r', default=False, action='store_true', help="Recurse into subdirectories")
 
    return parser.parse_args()
 
 
###############################
# program entry point
#
if __name__ == "__main__":
    args = parseArgs()
    main( args )
  1. 安装pefile
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pefile

image-20230804001137635

  1. 终端运行fixNvPe.py文件
python fixNvPe.py --input E:\kaifa\Anaconda3\envs\yolov5\lib\site-packages\torch\lib\cudnn_adv_infer64_8.dll

intput后面的路径,就是报错那里,后面给的路径

出现下图所示表示执行完毕

image-20230804001547119

RuntimeError: CUDA out of memory.

解决方式:

换小模型

image-20230804004234793

AttributeError: ‘FreeTypeFont’ object has no attribute ‘getsize’

image-20230804004739957

这是因为安装了新版本的 Pillow (10),pip install tf-models-official删除了该getsize 功能,降级到 Pillow 9.5 解决了该问题

解决方式:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Pillow==9.5

查看日志

终端输入tensorboard --logdir runs

image-20230804010221749

训练效果检测

命令行输入以下命令

python detect.py  --weights runs/train/exp/weights/best.pt --source data02/BVN.mp4 --view-img

image-20230804011029138

image-20230804011220424

image-20230804011303814

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
YOLOv5是目标检测算法中的一种,通过训练自己的数据集可以实现对自定义目标的检测。下面是YOLOv5入门实践的步骤。 第一步,准备数据集。首先收集一些与你想要检测的目标相关的图像,确保图像中的目标已标注好边界框坐标。将图像和对应的标注文件放在数据集文件夹中。 第二步,配置运行环境。需要在计算机上安装Python环境,并根据YOLOv5的要求安装相应的库和依赖。可以通过pip或conda进行安装。 第三步,调整配置文件。YOLOv5提供了一个默认的配置文件,可以根据自己的需求进行修改。主要需要调整的是类别数量和路径配置。 第四步,划分训练集和验证集。将数据集中的图像和标注文件划分为训练集和验证集,一般可以按照70%的比例划分。 第五步,训练模型。在终端中运行训练命令,指定相关参数,如模型类型、数据集路径、训练集和验证集路径等。训练时可以选择使用预训练权重或从头开始。 第六步,评估模型。训练完成后,可以通过评估命令对模型进行评估,得到关于模型性能的指标,如精确度、召回率等。 第七步,使用模型进行目标检测。训练完成的模型可以用于检测自定义数据集中的目标。可以在终端中运行检测命令,指定相关参数,如模型路径、检测图像路径等。 通过以上步骤,我们可以进行YOLOv5入门实践,训练自己的数据集,并使用训练好的模型进行目标检测。随着更多的实践和调优,可以提高模型性能和检测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值