手把手教你用PaddleDetection套件训练目标检测模型
手把手教你使用PaddleDetection套件完成目标检测模型的训练
一、数据准备
1.收集图片数据
将搜集到的数据图片简单筛选下,整合在一个文件夹下
可以使用ReNamer进行图片批量重命名
将图片文件夹压缩得到.zip文件
2.数据处理
(1)导入数据
将图片压缩包上传至EazyData
从网上收集到的数据可能有不符合要求的内容,请手动将部分图片进行删除,后续清洗无法处理这些图片
(2)数据清洗
创建清洗任务
选择合适的清洗方式,去近似以及去模糊可以选择具体的相似度和模糊度
(3)数据增强
这次例子没有使用数据增强原因是数据数量应该够用,倘若数据过少可以尝试使用数据增强进行处理。具体操作请自行摸索了。
(4)标注数据
首先在右侧添加标签
然后就可以在图片内部进行画框,快捷键写标签了,按s保存此张
如果数据集过多可以使用智能标签
需要每样标签都有至少十个才能启动
建议是每个样式的数据都标注一些再启动
智能标注流程如下
(5)导出
选择xml格式导出
在导出记录里点击下载即可
3.上传前预处理
将压缩包的名称以及内部文件夹名称重命名为合适的名称
将annotations和images文件夹首字母改为小写
在左侧data目录下上传重命名后的数据集
In [ ]
!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 archive
unzip: cannot find zipfile directory in one of lift.zip or
lift.zip.zip, and cannot find lift.zip.ZIP, period.
如果出现以上错误,请运行下面的代码
In [ ]
%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文件
内容为标签名称(每个标签单独一行如图所示)**
In [8]
!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.6 Start 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: 184 bike: 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]
<Figure size 640x480 with 1 Axes>
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)
<Figure size 640x480 with 1 Axes>
五、模型训练
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
以上就是手把手教你用PaddleDetection套件训练目标检测模型的详细内容,更多请关注启程网【www.vszh.cn】AI 人工智能栏目。
如非特殊说明,本站内容均来自于网友自主分享,如有任何问题均请联系我们进行处理!
猜您喜欢
新《VR女友》重磅更新 支持VRM个性模型导入功能
2026-06-18ILLUMINATION社(并非那个I社)宣布,旗下延续I社名作精神“续作”新《VR(的)女友》最新功能上线,重点是加入了对于VRM个性模型的支持,玩家可以与自己喜欢的类型的VR女友进行互动了。 新推出的“VRM女主角功能”只需将VRM格式的3D模型放入指定文件夹并启动,该模型就会作为女主角出现在《VR女友》正作中。 以往的角色只有“夕阳樱花”,但此次功能支持使用VRoid等软件制作和分发的VRM...
Anthropic 秘密提交上市申请,最强安全大模型 Mythos 突然放开内测
2026-06-04全球人工智能安全格局正经历技术突破与资本动向的双重冲击。知名AI研究机构Anthropic于本周二正式对外披露,将全面扩展其前沿安全大模型“神话”(Mythos)的全球内测规模,新增约150家组织获得早期访问权限,目标是在正式发布前协同全球关键基础设施运营方开展系统性风险识别与防护加固。 在当前愈演愈烈的AI技术竞备浪潮中,Mythos凭借卓越的代码生成能力与自动化漏洞探测水平,被广泛视为兼具颠覆...
世界模型,挤满了00后
2026-06-04☞☞☞AI智能聊天,问答助手,AI智能搜索,多模态理解力帮你轻松跨越从0到1的创作门槛☜☜☜ 倘若科技创投也存在代际更替,那么这场AI领域的创业浪潮,或许正是史上节奏最急、落差最大、颠覆性最强的一次年轻化跃迁。 没有渐进式演进,没有温和交接,只有一群00后以“破壁者”之姿,强势切入AI最底层、最艰深的战场——“世界模型”。 与移动互联网时代按部就班的成长路径截然不同,这批千禧年后出生的数字原住民,...
大模型如何真正告别幻觉?元认知或是破局的关键
2026-06-04大模型所表现出的“幻觉”——即以高度确信的语气输出明显违背事实的内容——始终是ai领域难以绕开的关键挑战,尤其在医疗、司法等容错率极低的垂直场景中,此类错误可能带来严重后果。 过去,行业主要依赖两种策略应对幻觉:其一,持续扩充训练语料,寄望于让模型覆盖更广的知识边界;其二,部署保守型响应机制,要求模型在置信度不足时主动拒答。但二者均存在根本性瓶颈:前者受限于数据获取与标注的现实约束,注定无法填满所...
超越GPT-5.5!国产AI大模型MiniMax M3 正式发布
2026-06-02国内人工智能领域迎来里程碑式技术突破。稀宇科技今日正式推出全新一代大模型——minimaxm3。该模型不仅拥有业界领先的代码生成与理解能力,更原生支持长达1m(即100万tokens)的超长上下文窗口。尤为值得关注的是,m3首次在国内开源模型中实现三大核心能力一体化:原生支持图像、视频多模态输入,并可直接理解与操作电脑桌面界面,标志着国产大模型在多模态智能体方向迈出关键一步。 ☞☞☞AI智能聊天,...
LobsterAI上线图片视频大模型矩阵 一次性接入四大主流图像视频生成模型
2026-06-02☞☞☞AI智能聊天,问答助手,AI智能搜索,多模态理解力帮你轻松跨越从0到1的创作门槛☜☜☜ 国内AIGC多模态创作领域迎来新一轮突破。近日,国内头部互联网企业推出的首个开源“龙虾系”人工智能产品LobsterAI(网易有道龙虾)完成重大迭代,正式开放图片与视频生成两大核心能力。 本次升级引入全新矩阵化协同架构,同步集成业界公认的四大先进多模态生成模型——Seedream、Seedance、Hap...
Anthropic发布Claude Opus 4.8模型,强化协作与安全性
2026-06-025月29日讯,anthropic正式推出claudeopus4.8模型,在智能体编程、多学科推理及知识型代理任务等关键能力上相较opus4.7取得实质性跃升,同时延续原有定价策略,实现“加量不加价”。 本次更新重点强化模型的“可信协作”属性,尤其在“诚实度”维度实现突破性优化:其对自身生成代码中缺陷的主动识别与标注能力显著增强,漏过错误而不提示的概率较前代下降约四倍;在对齐性评估中,“隐性欺骗”“...
阶跃星辰发布新一代Step 3.7 Flash模型,面向生产级智能体优化
2026-06-025月29日,阶跃星辰正式推出并开源其全新高效模型——step3.7flash。该模型面向智能体(agent)的实际落地与规模化应用需求而打造,在推理速度、运行成本、系统稳定性及复杂任务处理性能之间达成更优协同,标志着flash系列模型正由“更快、更省”迈向真正意义上的“生产就绪型基础模型”。 Step3.7Flash原生支持多模态理解与执行,能够精准解析UI界面、数据图表、PDF文档等多样化视觉内...
