YOLOv6 安装及使用详细教程
前言YOLOv6 是美团研发的轻量级目标检测算法兼顾检测精度与推理速度适配工业落地与学术入门场景。该算法针对工程化部署深度优化环境配置简单、运行流程清晰是零基础用户入门目标检测的优选方案。本文将手把手带你完成 YOLOv6 的安装与基础使用快速掌握目标检测核心实操流程。一、源代码的下载手动下载压缩包适合未安装 Git 的新手打开 YOLOv6 官方 GitHub 仓库https://github.com/meituan/YOLOv6点击页面右上角的「Code」按钮选择「Download ZIP」下载代码压缩包将压缩包解压到电脑任意目录:二、环境变量的配置步骤 1打开终端Windows打开 Anaconda Prompt 或 cmdLinux/Mac打开终端Terminal。步骤2创建虚拟环境到这一步说明虚拟环境创建成功激活yolov6环境:激活成功标志终端提示符前出现(yolov6)。步骤3环境配置1.进入yolov6的工程目录2.执行pip install -r requirements.txt。如果网速较慢可以切换国内清华PyPI镜像源秒速完成安装 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple将我们的torch版本降为2.6.0下载网址在https://pytorch.org/get-started/previous-versions/安装好后输入一下语句进行验证如果输出true就可以了三、代码的调试四、数据集的准备YOLOv6 支持 YOLO 格式数据集目录结构如下plaintextdata/ ├── images/ # 所有图片.jpg/.png │ ├── img1.jpg │ ├── img2.jpg │ └── ... └── labels/ # 对应标签文件.txt与图片同名 ├── img1.txt ├── img2.txt └── ...去网上找自己需要训练的数据集然后将他划分成训练集和测试集将以下代码放在data里面txt_split.py就可以自己划分 **** 整个文件适用于将已转换的txt标签文件进行数据集划分 import os import random import argparse import shutil from tqdm import tqdm # 参数解析 parser argparse.ArgumentParser() parser.add_argument(--txt_path, default../data/labels, typestr, helpinput txt label path) parser.add_argument(--images_path, default../data/images, typestr, helporiginal images path) parser.add_argument(--output_path, default../data, typestr, helpoutput base path) opt parser.parse_args() # 定义划分比例 trainval_percent 1.0 train_percent 0.8 val_percent 0.2 # 设置路径 txt_path opt.txt_path images_path opt.images_path output_path opt.output_path # 创建输出目录结构 os.makedirs(os.path.join(output_path, images/train), exist_okTrue) os.makedirs(os.path.join(output_path, images/val), exist_okTrue) os.makedirs(os.path.join(output_path, images/test), exist_okTrue) os.makedirs(os.path.join(output_path, labels/train), exist_okTrue) os.makedirs(os.path.join(output_path, labels/val), exist_okTrue) os.makedirs(os.path.join(output_path, labels/test), exist_okTrue) # 获取所有TXT文件 txt_files [f for f in os.listdir(txt_path) if f.endswith(.txt)] random.shuffle(txt_files) num len(txt_files) # 划分数据集 tv int(num * trainval_percent) tr int(tv * train_percent) ta tv - tr # 确保trainvaltrainval trainval txt_files[:tv] train trainval[:tr] val trainval[tr:tr ta] test txt_files[tv:] # 处理训练集 print(Processing training set...) train_list [] for txt_file in tqdm(train): image_id os.path.splitext(txt_file)[0] # 移动标签文件 src_txt os.path.join(txt_path, txt_file) dst_txt os.path.join(output_path, labels/train, txt_file) if os.path.exists(src_txt): shutil.move(src_txt, dst_txt) # 移动对应的图片文件 src_img os.path.join(images_path, f{image_id}.jpg) dst_img os.path.join(output_path, images/train, f{image_id}.jpg) if os.path.exists(src_img): shutil.move(src_img, dst_img) train_list.append(dst_img) # 处理验证集 print(Processing validation set...) val_list [] for txt_file in tqdm(val): image_id os.path.splitext(txt_file)[0] # 移动标签文件 src_txt os.path.join(txt_path, txt_file) dst_txt os.path.join(output_path, labels/val, txt_file) if os.path.exists(src_txt): shutil.move(src_txt, dst_txt) # 移动对应的图片文件 src_img os.path.join(images_path, f{image_id}.jpg) dst_img os.path.join(output_path, images/val, f{image_id}.jpg) if os.path.exists(src_img): shutil.move(src_img, dst_img) val_list.append(dst_img) # 处理测试集 print(Processing test set...) test_list [] for txt_file in tqdm(test): image_id os.path.splitext(txt_file)[0] # 移动标签文件 src_txt os.path.join(txt_path, txt_file) dst_txt os.path.join(output_path, labels/test, txt_file) if os.path.exists(src_txt): shutil.move(src_txt, dst_txt) # 移动对应的图片文件 src_img os.path.join(images_path, f{image_id}.jpg) dst_img os.path.join(output_path, images/test, f{image_id}.jpg) if os.path.exists(src_img): shutil.move(src_img, dst_img) test_list.append(dst_img) # 保存路径文件 def save_path_file(file_path, path_list): with open(file_path, w) as f: for path in path_list: f.write(f{path}\n) save_path_file(os.path.join(output_path, train.txt), train_list) save_path_file(os.path.join(output_path, val.txt), val_list) save_path_file(os.path.join(output_path, test.txt), test_list) print(Dataset preparation completed!) print(fTrain: {len(train_list)} images, Val: {len(val_list)} images, Test: {len(test_list)} images)五、源代码的修改与训练修改在data下面新建文件myvoc.yaml在里面写入以下代码开始训练bash运行# 回到 YOLOv6 根目录 cd .. # 训练命令以 yolov6s 为例 python tools/train.py \ --batch 16 \ # 批次大小根据显卡显存调整显存小则改8/4 --epochs 100 \ # 训练轮数 --data data/myvoc.yaml \ # 自定义数据集配置 --cfg configs/yolov6s.py \ # 模型配置文件 --weights weights/yolov6s.pt \ # 预训练权重 --device 0 \ # 显卡编号CPU 写 cpu --name my_train \ # 训练任务名称结果保存在 runs/train/my_train --save-dir runs/train \ # 结果保存目录 --eval-interval 5 # 每5轮验证一次六、模型验证验证训练好的模型bash运行# 评估模型精度mAP python tools/eval.py \ --weights runs/train/my_train/weights/best_ckpt.pt \ # 训练好的最优权重 --data data/myvoc.yaml \ --device 0 \ --batch 16批量图片 / 视频推理bash运行# 批量图片指定目录 python tools/infer.py --weights 权重文件 --source data/images/test/ --device 0 # 视频推理 python tools/infer.py --weights 权重文件 --source test_video.mp4 --device 0 # 摄像头实时推理 python tools/infer.py --weights 权重文件 --source 0 --device 0
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2412020.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!