手把手教你用PaddleDetection套件训练目标检测模型
手把手教你使用PaddleDetection套件完成目标检测模型的训练
一、数据准备
1.收集图片数据
将搜集到的数据图片简单筛选下,整合在一个文件夹下
免费影视、动漫、音乐、游戏、小说资源长期稳定更新! 👉 点此立即查看 👈
可以使用ReNamer进行图片批量重命名
将图片文件夹压缩得到.zip文件
2.数据处理
(1)导入数据
将图片压缩包上传至EazyData
从网上收集到的数据可能有不符合要求的内容,请手动将部分图片进行删除,后续清洗无法处理这些图片
(2)数据清洗
创建清洗任务
选择合适的清洗方式,去近似以及去模糊可以选择具体的相似度和模糊度
(3)数据增强
这次例子没有使用数据增强原因是数据数量应该够用,倘若数据过少可以尝试使用数据增强进行处理。具体操作请自行摸索了。
(4)标注数据
首先在右侧添加标签
然后就可以在图片内部进行画框,快捷键写标签了,按s保存此张
如果数据集过多可以使用智能标签
需要每样标签都有至少十个才能启动
建议是每个样式的数据都标注一些再启动
智能标注流程如下
(5)导出
选择xml格式导出
在导出记录里点击下载即可
3.上传前预处理
将压缩包的名称以及内部文件夹名称重命名为合适的名称
将annotations和images文件夹首字母改为小写
在左侧data目录下上传重命名后的数据集
!ls ~/data登录后复制
二、环境准备
1.安装PaddleDetection
In [ ]# 解压PaddleDetection压缩包%cd /home/aistudio/data/data267567!unzip -q PaddleDetection-release-2.6.zip -d /home/aistudio登录后复制
2.安装依赖
In [ ]# 安装requirements中的依赖%cd ~/PaddleDetection-release-2.6!pip install -r requirements.txt#安装过慢可以打开requirements.txt文件,注释掉opencv-python <= 4.6.0(在前面加“#”)# 然后将此命令前的“#”删掉重新运行# !pip install opencv-python <= 4.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple# 编译安装paddledet!python setup.py install%cd ~登录后复制
三、数据准备
1.解压数据集压缩包
In [5]# 移动到当前挂载的数据集目录# 解压数据集到指定目录下%cd /home/aistudio/data/data313161/!unzip -q lift.zip -d /home/aistudio登录后复制
/home/aistudio/data/data313161[lift.zip] End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive.note: lift.zip may be a plain executable, not an archiveunzip: cannot find zipfile directory in one of lift.zip or lift.zip.zip, and cannot find lift.zip.ZIP, period.登录后复制

如果出现以上错误,请运行下面的代码
%cd /home/aistudio/!unzip -q lift.zip -d /home/aistudio登录后复制
2.生成训练所需文件
这里训练集和验证集是按4:1划分的,可以将全部的数据集都用作训练
In [7]import os#生成train.txt、val.txtxml_dir = '/home/aistudio/lift/annotations'img_dir = '/home/aistudio/lift/images'path_list = list()for img in os.listdir(img_dir): img_path = os.path.join(img_dir,img) xml_path = os.path.join(xml_dir,img.replace('jpg', 'xml')) path_list.append((img_path, xml_path))train_f = open('/home/aistudio/lift/train.txt','w') val_f = open('/home/aistudio/lift/val.txt','w') for i ,content in enumerate(path_list): img, xml = content text = img + ' ' + xml + '\n' if i % 5 == 0: val_f.write(text) else: train_f.write(text)train_f.close()val_f.close()登录后复制 在运行下面代码块前,请修改label_list.txt文件
内容为标签名称(每个标签单独一行如图所示)**
!cp ~/label_list.txt ~/lift%cd ~/PaddleDetection-release-2.6# 生成训练集!python tools/x2coco.py \ --dataset_type voc \ --voc_anno_dir /home/aistudio/lift/annotations/ \ --voc_anno_list /home/aistudio/lift/train.txt \ --voc_label_list /home/aistudio/lift/label_list.txt \ --voc_out_name /home/aistudio/lift/train.json# 生成验证集!python tools/x2coco.py \ --dataset_type voc \ --voc_anno_dir /home/aistudio/lift/annotations/ \ --voc_anno_list /home/aistudio/lift/val.txt \ --voc_label_list /home/aistudio/lift/label_list.txt \ --voc_out_name /home/aistudio/lift/val.json登录后复制
/home/aistudio/PaddleDetection-release-2.6Start converting !100%|██████████████████████████████████████| 161/161 [00:00<00:00, 26787.38it/s]Start converting !100%|████████████████████████████████████████| 41/41 [00:00<00:00, 23877.60it/s]登录后复制
四、数据分析
1.标签数量统计
In [9]import osfrom unicodedata import nameimport xml.etree.ElementTree as ETimport globdef count_num(indir): # 提取xml文件列表 os.chdir(indir) annotations = os.listdir('.') annotations = glob.glob(str(annotations) + '*.xml') dict = {} # 新建字典,用于存放各类标签名及其对应的数目 for i, file in enumerate(annotations): # 遍历xml文件 # actual parsing in_file = open(file, encoding = 'utf-8') tree = ET.parse(in_file) root = tree.getroot() # 遍历文件的所有标签 for obj in root.iter('object'): name = obj.find('name').text if(name in dict.keys()): dict[name] += 1 # 如果标签不是第一次出现,则+1 else: dict[name] = 1 # 如果标签是第一次出现,则将该标签名对应的value初始化为1 # 打印结果 print("各类标签的数量分别为:") for key in dict.keys(): print(key + ': ' + str(dict[key])) indir='/home/aistudio/lift/annotations' # xml文件所在的目录count_num(indir) # 调用函数统计各类标签数目登录后复制 各类标签的数量分别为:motorcycle: 184bike: 24登录后复制
2.标注框高宽比分析
第一次运行卡了,可以尝试重新运行一次,下面应该是要有图的
In [11]import osimport matplotlib.pyplot as pltfrom unicodedata import nameimport xml.etree.ElementTree as ETimport globdef ratio(indir): # 提取xml文件列表 os.chdir(indir) annotations = os.listdir('.') annotations = glob.glob(str(annotations) + '*.xml') # count_0, count_1, count_2, count_3 = 0, 0, 0, 0 # 举反例,不要这么写 count = [0 for i in range(20)] for i, file in enumerate(annotations): # 遍历xml文件 # actual parsing in_file = open(file, encoding = 'utf-8') tree = ET.parse(in_file) root = tree.getroot() # 遍历文件的所有检测框 for obj in root.iter('object'): xmin = obj.find('bndbox').find('xmin').text ymin = obj.find('bndbox').find('ymin').text xmax = obj.find('bndbox').find('xmax').text ymax = obj.find('bndbox').find('ymax').text Aspect_ratio = (int(ymax)-int(ymin)) / (int(xmax)-int(xmin)) if int(Aspect_ratio/0.25) < 19: count[int(Aspect_ratio/0.25)] += 1 else: count[-1] += 1 sign = [0.25*i for i in range(20)] plt.bar(x=sign, height=count) print(count)indir='/home/aistudio/lift/annotations/' # xml文件所在的目录ratio(indir)登录后复制 [0, 8, 22, 36, 27, 33, 30, 23, 15, 8, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0]登录后复制
登录后复制登录后复制
3.图像尺寸分析
In [ ]import osfrom unicodedata import nameimport xml.etree.ElementTree as ETimport globdef Image_size(indir): # 提取xml文件列表 os.chdir(indir) annotations = os.listdir('.') annotations = glob.glob(str(annotations) + '*.xml') width_heights = [] for i, file in enumerate(annotations): # 遍历xml文件 # actual parsing in_file = open(file, encoding = 'utf-8') tree = ET.parse(in_file) root = tree.getroot() width = int(root.find('size').find('width').text) height = int(root.find('size').find('height').text) if [width, height] not in width_heights: width_heights.append([width, height]) print("数据集中,有{}种不同的尺寸,分别是:".format(len(width_heights))) for item in width_heights: print(item)indir='/home/aistudio/lift/annotations/' # xml文件所在的目录Image_size(indir)登录后复制 4.检测框中心分布分析
In [14]import osfrom unicodedata import nameimport xml.etree.ElementTree as ETimport globdef distribution(indir): # 提取xml文件列表 os.chdir(indir) annotations = os.listdir('.') annotations = glob.glob(str(annotations) + '*.xml') data_x, data_y = [], [] for i, file in enumerate(annotations): # 遍历xml文件 # actual parsing in_file = open(file, encoding = 'utf-8') tree = ET.parse(in_file) root = tree.getroot() width = int(root.find('size').find('width').text) height = int(root.find('size').find('height').text) # 遍历文件的所有检测框 for obj in root.iter('object'): xmin = int(obj.find('bndbox').find('xmin').text) ymin = int(obj.find('bndbox').find('ymin').text) xmax = int(obj.find('bndbox').find('xmax').text) ymax = int(obj.find('bndbox').find('ymax').text) x = (xmin + (xmax-xmin)) / width y = (ymin + (ymax-ymin)) / height data_x.append(x) data_y.append(y) plt.scatter(data_x, data_y, s=1, alpha=0.1)indir='/home/aistudio/lift/annotations/' # xml文件所在的目录distribution(indir)登录后复制 登录后复制登录后复制
五、模型训练
1.修改~/coco_lift.yml
2.修改数据集配置文件
修改模型配置文件中的第一行,将PaddleDetection/configs/ppyoloe/ppyoloe_plus_crn_m_80e_coco.yml第一行的coco_detection.yml修改为coco_lift.yml
In [16]%cd /home/aistudio!cp coco_lift.yml /home/aistudio/PaddleDetection-release-2.6/configs/datasets/登录后复制
/home/aistudio登录后复制登录后复制
3.修改模型配置文件参数
具体参数需自行调整,默认使用最新的,建议调整下base_lr,在/home/aistudio/PaddleDetection-release-2.6/configs/ppyoloe/base/optimizer_80e.yml路径下,由于我们的单卡,可以将默认的数值改为原先的1/8
4.模型训练
这里采用的模型是ppyoloe+_m
In [ ]%cd /home/aistudio/PaddleDetection-release-2.6# 模型训练 # 如果要恢复训练,则加上 -r output/ppyoloe_plus_crn_m_80e_coco/best_model# 如果要边训练边评估,则加上--eval# 训练时的日志输出将保存在--vdl_log_dir所指的路径下# 模型压缩则加上--slim_config configs/slim/xxx/{SLIM_CONFIG.yml}({SLIM_CONFIG.yml}为指定压缩策略配置文件)!python tools/train.py \ -c configs/ppyoloe/ppyoloe_plus_crn_m_80e_coco.yml --eval \ --use_vdl=true \ --vdl_log_dir=VisualDL登录后复制 In [ ]# 当训练完成,如有需要,可以删除所有的checkpoint,只保留best_model%cd ~/PaddleDetection-release-2.6/output/ppyoloe_plus_crn_m_80e_coco!find . -type f -name '[0-9]*'!find . -type f -name '[0-9]*' -exec rm -f {} \;!echo "delete checkpoints complete!"登录后复制 六、模型检验和导出
1.模型预测
可以运行了看一下对每张图片的识别,比对后可以针对性进行数据增强
In [ ]%cd /home/aistudio/PaddleDetection-release-2.6# infer_img表示预测改路径的单张图片# infer_dir表示对该路径下的所有图片进行预测# draw_threshold表示置信度大于该值的框才画出来# 生成的图片保存在/home/aistudio/work/PaddleDetection/infer_output!python tools/infer.py \ -c configs/ppyoloe/ppyoloe_plus_crn_m_80e_coco.yml \ --infer_dir=/home/aistudio/lift/images \ --output_dir=infer_output/ \ --draw_threshold=0.5 \ -o weights=/home/aistudio/PaddleDetection-release-2.6/output/ppyoloe_plus_crn_m_80e_coco/best_model.pdparams登录后复制
2.模型导出
出现full_graph问题就打开trainer.py把full_graph=True删掉即可
In [ ]%cd /home/aistudio/PaddleDetection-release-2.6# 将"-o weights"里的模型路径换成你自己训好的模型!python tools/export_model.py \ -c configs/ppyoloe/ppyoloe_plus_crn_m_80e_coco.yml \ -o weights=/home/aistudio/PaddleDetection-release-2.6/output/ppyoloe_plus_crn_m_80e_coco/best_model.pdparams \ TestReader.fuse_normalize=true登录后复制
3.打包下载
In [22]# 创建model文件夹if not os.path.exists('/home/aistudio/model/'): os.makedirs('/home/aistudio/model/') #创建路径# 将检测模型拷贝到model文件夹中!cp -r /home/aistudio/PaddleDetection-release-2.6/output_inference/ppyoloe_plus_crn_m_80e_coco/* /home/aistudio/model/登录后复制 In [23]# 打包代码%cd /home/aistudio/!zip -r -q -o model.zip model/登录后复制
/home/aistudio登录后复制登录后复制
游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。
同类文章
说一下WorkBuddy 的 Plan 模式
如何切换到 Plan 模式 想体验这种更可控的方式?操作很简单。在 WorkBuddy 主界面的右下角,你会看到一个“安全模式切换”的下拉菜单,从中选择“Plan”选项即可完成切换。 核心使用流程 光说概念可能有点抽象,咱们直接看个例子。假设你手头有个任务:“把桌面上‘项目报告’文件夹里所有Exce
滴滴出行开放打车 Skill,“龙虾”叫车全程不需要切换 App
滴滴出行全网首发语音打车Skill,一句话智能叫车全攻略 近日,滴滴出行正式上线了一项创新的语音交互功能:全面开放打车Skill。这意味着,用户只需通过语音指令,即可完成从叫车到行程追踪的全流程,真正实现“动口不动手”的便捷出行体验。 整个操作过程,包括目的地搜索、车型比价、下单确认、查看订单状态等
阿里千问 AI 眼镜接入蚂蚁 GPASS:语音解锁共享单车、停车缴费
当AI眼镜学会“跑腿”:语音解锁单车,无感支付停车费 近来,智能穿戴领域的一个新动向值得关注:阿里旗下的千问AI眼镜,正式接入了蚂蚁集团的GPASS平台。这可不是一次简单的功能叠加,它意味着,诸如共享单车骑行、停车缴费这一系列高频的“AI办事”功能,开始从手机屏幕转移到了你的眼前。 简单说,借助GP
Workbuddy注册额外积分
角色定位与核心任务目标 明确了基本定位后,我们直接切入核心:作为一名专业的文章优化师,我的核心职责在于,将那些带有明显AI生成特征的文本,深度重塑为拥有个人特色与行业洞见的优质内容。 换句话说,这项任务的关键在于实施一次“精准的换血手术”。你必须严格保证原文所有的事实依据、核心观点、逻辑框架,以及每
我把 Anthropic 的 Harness 工程思想做成了一个 Skill
用AI写代码,难在哪儿? 用AI生成代码本身并不难,真正的挑战在于让它稳定地交付一个真正可用的东西。这篇文章,我们就来聊聊Anthropic工程团队是如何破解这个难题的,以及我如何将这套方法论落地成了一个可以复用的实战工具。 用 AI 写代码有多难?不是写不出来难,是让它稳定交付可用的东西很难。这篇
- 日榜
- 周榜
- 月榜
1
2
3
4
5
6
7
8
9
10
相关攻略
2015-03-10 11:25
2015-03-10 11:05
2021-08-04 13:30
2015-03-10 11:22
2015-03-10 12:39
2022-05-16 18:57
2025-05-23 13:43
2025-05-23 14:01
热门教程
- 游戏攻略
- 安卓教程
- 苹果教程
- 电脑教程

