当前位置:网站首页>pytorch yolov5 训练自定义数据
pytorch yolov5 训练自定义数据
2022-07-05 17:51:00 【mtl1994】
pytorch yolov5 训练自定义数据
文章目录
前言
环境
python: 3.9.7
torch: 1.10.2
labelimg: 1.8.6
#yolov5 https://github.com/ultralytics/yolov5
#pytorch https://pytorch.org/
#labelimg https://github.com/tzutalin/labelImg
paddleocr 有三种模型 det 检测 cls 方向 rec 识别
一、创建环境
安装miniconda
https://blog.csdn.net/mtl1994/article/details/114968140
创建环境
#linux 需要先 source conda create -n pytorch_yolov5 python=3.9.7 --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
安装完以后进入环境
conda activate pytorch_yolov5
二、安装环境
pytorch
#选择对应的cuda/cpu版本 pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
yolov5
#下载源代码 git clone https://github.com/ultralytics/yolov5 # clone cd yolov5 #安装依赖 python -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
测试一下
import torch # Model model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5n - yolov5x6, custom # Images img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list # Inference results = model(img) # Results results.print() # or .show(), .save(), .crop(), .pandas(), etc.
选择模型
https://github.com/ultralytics/yolov5/releases
模型参数
我使用的模型是
yolov5x
三、标注图片
我使用的labelimg
#安装
pip install labelimg
#打开
labelimg
标注完以后会有两个目录、一个存图片、一个存txt
四、训练
1.切分数据
def make_datasets(txt_path, img_path, out="./out", split_rate=0.05):
txt_dir = Path(txt_path)
img_dir = Path(img_path)
out_dir = Path(out)
dataset = []
train_label = out_dir / "labels/train2007/"
train_image = out_dir / "images/train2007/"
test_label = out_dir / "labels/test2007/"
test_image = out_dir / "images/test2007/"
train_label.mkdir(parents=True, exist_ok=True)
train_image.mkdir(parents=True, exist_ok=True)
test_label.mkdir(parents=True, exist_ok=True)
test_image.mkdir(parents=True, exist_ok=True)
"""
过滤空txt
"""
for item in txt_dir.rglob("*.txt"):
if item.read_text() != "":
dataset.append(item)
"""
切分训练集,验证集
"""
tv = random.sample(dataset, int(len(dataset) * split_rate))
"""
组装数据
"""
print(len(dataset))
for item in dataset:
for jpg in img_dir.rglob(item.stem + ".jpg"):
if item in tv:
print(jpg, test_image / jpg.name)
# jpg.replace(test_image / jpg.name)
shutil.copy(str(jpg), test_image / jpg.name)
shutil.copy(str(item), test_label / item.name)
# item.replace(test_label / item.name)
else:
print(jpg, train_image / jpg.name)
# jpg.replace(train_image / jpg.name)
# item.replace(train_label / item.name)
shutil.copy(str(jpg), train_image / jpg.name)
shutil.copy(str(item), train_label / item.name)
执行完以后,目录结构
2.修改训练的模型 yml
3.开始训练
nohup python train.py --img 640 --batch 32 --epochs 600 --data voc.yaml --weights yolov5s.pt --device 0,1,2,3 &
训练结果都保存在runs/train/
递增的运行目录中,例如runs/train/exp2
,runs/train/exp3
五、已经训练的模型,有了新数据需要继续训练
1.使用迁移学习
训练的时候指定 weights 为 上一次训练的输出
六、常用命令
1.训练
nohup python train.py --img 640 --batch 32 --epochs 600 --data wp_voc.yaml --weights runs/train/exp27/weights/best.pt --device 0,1,2,3 &
2.识别
python detect.py --weights runs/train/exp6/weights/best.pt --source ../datasets/infer/2022-2-24/
3.导出onnx
python export.py --weights yolov5s.pt --img 640 --batch 1 # export at 640x640 with batch size 1
总结
边栏推荐
- tkinter窗口预加载
- Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
- Binder开辟线程数过多导致主线程ANR异常
- [TestLink] testlink1.9.18 solutions to common problems
- 模拟百囚徒问题
- Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
- Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
- ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
- Leetcode daily question: merge two ordered arrays
- 【PaddleClas】常用命令
猜你喜欢
"Xiaodeng in operation and maintenance" is a single sign on solution for cloud applications
Binder开辟线程数过多导致主线程ANR异常
Thesis reading_ Medical NLP model_ EMBERT
Nanjing University: Discussion on the training program of digital talents in the new era
ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测
华夏基金:基金行业数字化转型实践成果分享
LeetCode 练习——206. 反转链表
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
吴恩达团队2022机器学习课程,来啦
Cmake tutorial Step4 (installation and testing)
随机推荐
Find the first k small element select_ k
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
Thesis reading_ Medical NLP model_ EMBERT
Redis基础
OpenShift常用管理命令杂记
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
How awesome is the architecture of "12306"?
Star ring technology data security management platform defender heavy release
Cmake tutorial step1 (basic starting point)
flask接口响应中的中文乱码(unicode)处理
多线程(一) 进程与线程
如何修改mysql字段为自增长字段
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
图像分类,看我就够啦!
Elk log analysis system
Size_t 是无符号的
Read libco save and restore the on-site assembly code
隐私计算助力数据的安全流通与共享
热通孔的有效放置如何改善PCB设计中的热管理?
集群部署如何解决海量视频接入与大并发需求?