蒙文网站建设情况汇报材料,着陆页制作网站,辽宁建设工程信息网抚顺,互联网创业做什么好前言
因工作要求使用的都是yolov5系列的模型#xff0c;今天学习一下最先进的yolov11#xff0c;记录一下环境配置及训练过程。
1.项目下载及环境安装
源码位置#xff1a;yolov11 可以看到#xff0c;这里要求python版本大于等于3.8#xff0c;我这里安装python3.10.…前言
因工作要求使用的都是yolov5系列的模型今天学习一下最先进的yolov11记录一下环境配置及训练过程。
1.项目下载及环境安装
源码位置yolov11 可以看到这里要求python版本大于等于3.8我这里安装python3.10.
conda create -n yolov11 python3.10
conda activate yolov11
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple2.标注自己的数据集
标注实例分割数据集的工具有很多这里建议labelme和AnyLabeling任意选一个。 如图所示标注后的数据集是json格式的 我们需要将其转成yolo系列需要的txt格式。 json转txt格式转化代码
# json2txt.py
# json2txt.py
import cv2
import os
import json
import glob
import numpy as npclass_names [cls1_name, cls2_name, cls3_name, cls4_name, cls5_name]def convert_json_label_to_yolov_seg_label():json_path F:/Desktop/hand/labels # 本地json路径json_files glob.glob(json_path /*.json)# print(json_files)# 指定输出文件夹output_folder F:/Desktop/hand/labels_txt # txt存放路径if not os.path.exists(output_folder):os.makedirs(output_folder)for json_file in json_files:# print(json_file)with open(json_file, r) as f:json_info json.load(f)img cv2.imread(os.path.join(json_path, json_info[imagePath]))height, width, _ img.shapenp_w_h np.array([[width, height]], np.int32)txt_file os.path.join(output_folder, os.path.basename(json_file).replace(.json, .txt))with open(txt_file, w) as f:for point_json in json_info[shapes]:txt_content np_points np.array(point_json[points], np.int32)label point_json[label]index class_names.index(label)# print(type(label))norm_points np_points / np_w_hnorm_points_list norm_points.tolist()txt_content str(index) .join([ .join([str(cell[0]), str(cell[1])]) for cell in norm_points_list]) \nf.write(txt_content)convert_json_label_to_yolov_seg_label()
转换后是这样的 分割数据集我们需要将转化成txt的数据集分割成训练集、验证集和测试集这是分割代码
# txt_split.py
# 将图片和标注数据按比例切分为 训练集和测试集
import shutil
import random
import os# 原始路径
image_original_path hhh/images/
label_original_path hhh/labels_txt/cur_path os.getcwd()
#cur_path D:/image_denoising_test/denoise/
# 训练集路径
train_image_path os.path.join(cur_path, datasets/images/train/)
train_label_path os.path.join(cur_path, datasets/labels/train/)# 验证集路径
val_image_path os.path.join(cur_path, datasets/images/val/)
val_label_path os.path.join(cur_path, datasets/labels/val/)# 测试集路径
test_image_path os.path.join(cur_path, datasets/images/test/)
test_label_path os.path.join(cur_path, datasets/labels/test/)# 训练集目录
list_train os.path.join(cur_path, datasets/train.txt)
list_val os.path.join(cur_path, datasets/val.txt)
list_test os.path.join(cur_path, datasets/test.txt)train_percent 0.8
val_percent 0.1
test_percent 0.1def del_file(path):for i in os.listdir(path):file_data path \\ ios.remove(file_data)def mkdir():if not os.path.exists(train_image_path):os.makedirs(train_image_path)else:del_file(train_image_path)if not os.path.exists(train_label_path):os.makedirs(train_label_path)else:del_file(train_label_path)if not os.path.exists(val_image_path):os.makedirs(val_image_path)else:del_file(val_image_path)if not os.path.exists(val_label_path):os.makedirs(val_label_path)else:del_file(val_label_path)if not os.path.exists(test_image_path):os.makedirs(test_image_path)else:del_file(test_image_path)if not os.path.exists(test_label_path):os.makedirs(test_label_path)else:del_file(test_label_path)def clearfile():if os.path.exists(list_train):os.remove(list_train)if os.path.exists(list_val):os.remove(list_val)if os.path.exists(list_test):os.remove(list_test)def main():mkdir()clearfile()file_train open(list_train, w)file_val open(list_val, w)file_test open(list_test, w)total_txt os.listdir(label_original_path)num_txt len(total_txt)list_all_txt range(num_txt)num_train int(num_txt * train_percent)num_val int(num_txt * val_percent)num_test num_txt - num_train - num_valtrain random.sample(list_all_txt, num_train)# train从list_all_txt取出num_train个元素# 所以list_all_txt列表只剩下了这些元素val_test [i for i in list_all_txt if not i in train]# 再从val_test取出num_val个元素val_test剩下的元素就是testval random.sample(val_test, num_val)print(训练集数目{}, 验证集数目{}, 测试集数目{}.format(len(train), len(val), len(val_test) - len(val)))for i in list_all_txt:name total_txt[i][:-4]srcImage image_original_path name .jpgsrcLabel label_original_path name .txtif i in train:dst_train_Image train_image_path name .jpgdst_train_Label train_label_path name .txtshutil.copyfile(srcImage, dst_train_Image)shutil.copyfile(srcLabel, dst_train_Label)file_train.write(dst_train_Image \n)elif i in val:dst_val_Image val_image_path name .jpgdst_val_Label val_label_path name .txtshutil.copyfile(srcImage, dst_val_Image)shutil.copyfile(srcLabel, dst_val_Label)file_val.write(dst_val_Image \n)else:dst_test_Image test_image_path name .jpgdst_test_Label test_label_path name .txtshutil.copyfile(srcImage, dst_test_Image)shutil.copyfile(srcLabel, dst_test_Label)file_test.write(dst_test_Image \n)file_train.close()file_val.close()file_test.close()if __name__ __main__:main()
3.编写训练代码并训练
我这里习惯使用代码训练还有命令训练如果感兴趣的朋友可以去官网了解。
# train.py
from ultralytics import YOLOif __name__ __main__:model YOLO(rultralytics/cfg/models/11/yolo11-seg.yaml) model.train(datarconfig.yaml,imgsz640,epochs800,single_clsTrue, batch16,workers10,device0,)
配置文件
# config.yaml
path: ../datasets/images # 数据集所在路径
train: train # 数据集路径下的train.txt
val: val # 数据集路径下的val.txt
test: test # 数据集路径下的test.txt# Classes
names:0: class1_name1: class2_name2: class3_name3: class4_name4: class5_name
这里的path改成你的数据集位置如果txt_split.py在项目根目录下运行则不需要修改路径只需要修改类别即可。 修改之后只需要python train.py运行即可。
测试代码
# test.py
from ultralytics import YOLO
# 加载训练好的模型改为自己的路径
model YOLO(runs/train/exp22/weights/best.pt) #修改为训练好的路径
source 11.jpg #修改为自己的图片路径及文件名
# 运行推理并附加参数
model.predict(source, saveTrue, imgsz640)
转成onnx模型并运行:
yolo export modelruns/segment/train11/weights/best.pt imgsz640 formatonnx opset12 simplify
python examples/YOLOv8-Segmentation-ONNXRuntime-Python/main.py --model runs/segment/train5n/weights/bestv8.onnx4.常见报错
RuntimeError: Trying to create tensor with negative dimension -37: [0, -37] 运行YOLOv8-Segmentation-ONNXRuntime-Python时报错修改配置文件
参考
语义分割YOLOv11的分割模型训练自己的数据集从代码下载到实例测试 配置文件位置在ultralytics/cfg/datasets/如果这里一直报错cant find file就直接写绝对路径。
总结
因为项目还没完成主要精力在此项目中过程写的有点仓促后面会慢慢优化文章质量补全没完成的部分。 文章转载自: http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.sbwr.cn.gov.cn.sbwr.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.sjwiki.com.gov.cn.sjwiki.com http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.lcbnb.cn.gov.cn.lcbnb.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn