[Paddle2.0学习之第三步]目标检测(下)AI识虫实战

简介: [Paddle2.0学习之第三步]目标检测(下)AI识虫实战

第一步 解压数据集

读取AI识虫数据集标注信息


AI识虫数据集结构如下:


提供了2183张图片,其中训练集1693张,验证集245,测试集245张。


包含7种昆虫,分别是Boerner、Leconte、Linnaeus、acuminatus、armandi、coleoptera和linnaeus。

!unzip -oq /home/aistudio/data/data96442/insects.zip -d work

#将文件移动至work下,并删除无用文件
!mv  work/data/insects work/
!ls /home/aistudio/work
!rmdir --ignore-fail-on-non-empty work/data
!rm -rf work/data
# 进入工作目录  /home/aistudio/work
%cd  /home/aistudio/work
# 查看工作目录下的文件列表
!ls
/home/aistudio/work
anchor_lables.py  insects      train.py
box_utils.py    insects_reader.py  yolo_epoch0.pdparams
calculate_map.py  map_utils.py       yolo_epoch100.pdparams
darknet.py    multinms.py      yolo_epoch150.pdparams
draw_anchors.py   output_pic.png     yolo_epoch199.pdparams
draw_results.py   predict.py       yolo_epoch50.pdparams
eval.py     __pycache__      yolov3.py
image_utils.py    reader.py


第二步 启动训练

通过运行train.py 文件启动训练,训练好的模型参数会保存在/home/aistudio/work目录下。

# 运行时长: 12小时37分钟50秒582毫秒
!python train.py


第三步 启动评估

通过运行eval.py启动评估,需要制定待评估的图片文件存放路径和需要使用到的模型参数。评估结果会被保存在pred_results.json文件中。


为了演示计算过程,下面使用的是验证集下的图片./insects/val/images,在提交比赛结果的时候,请使用测试集图片./insects/test/images


这里提供的yolo_epoch50.pdparams 是未充分训练好的权重参数,请在比赛时换成自己训练好的权重参数

# 在测试集test上评估训练模型,image_dir指向测试集集路径,weight_file指向要使用的权重路径。
# 参加比赛时需要在测试集上运行这段代码,并把生成的pred_results.json提交上去
# 特别注意,这个weight_file文件提供了199的版本在数据集里面
!python eval.py --image_dir=insects/test/images --weight_file=yolo_epoch199.pdparams
# 在验证集val上评估训练模型,image_dir指向验证集路径,weight_file指向要使用的权重路径。
!python eval.py --image_dir=insects/val/images --weight_file=yolo_epoch199.pdparams 


第四步 算精度指标

通过运行calculate_map.py计算最终精度指标mAP


· 同学们训练完之后,可以在val数据集上计算mAP查看结果,所以下面用到的是val标注数据./insects/val/annotations/xmls


· 提交比赛成绩的话需要在测试集上计算mAP,本地没有测试集的标注,只能提交json文件到比赛服务器上查看成绩


第五步 预测单张图片并可视化预测结果

!python predict.py --image_name=./insects/test/images/3157.jpeg --weight_file=./yolo_epoch50.pdparams
# 预测结果保存在“/home/aistudio/work/output_pic.png"图像中,运行下面的代码进行可视化
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
W0627 09:44:59.653769   875 device_context.cc:404] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0627 09:44:59.658743   875 device_context.cc:422] device: 0, cuDNN Version: 7.6.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return list(data) if isinstance(data, collections.MappingView) else data
# 可视化检测结果
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
img = Image.open("/home/aistudio/work/output_pic.png")
plt.figure("Object Detection", figsize=(15, 15)) # 图像窗口名称
plt.imshow(img)
plt.axis('off') # 关掉坐标轴为 off
plt.title('Bugs Detestion') # 图像题目
plt.show()

de4d4dafcec8994ade0955362841f8ad.png


总结 提升方案

这里给出的是一份基础版本的代码,可以在上面继续改进提升,可以使用的改进方案有:

1、使用其它模型如faster rcnn等 (难度系数5)

2、使用数据增多,可以对原图进行翻转、裁剪等操作 (难度系数3)

3、修改anchor参数的设置,教案中的anchor参数设置直接使用原作者在coco数据集上的设置,针对此模型是否要调整 (难度系数3)

4、调整优化器、学习率策略、正则化系数等是否能提升模型精度 (难度系数1)

相关文章
|
28天前
|
机器学习/深度学习 人工智能 算法
AI学习
学习AI涉及编程语言(如Python)、数学基础、AI理论(包括机器学习、深度学习)、实践应用(如使用TensorFlow)、案例研究、伦理法律及持续跟进最新技术。从基础知识到项目实战,全面掌握AI需要扎实的理论与实践经验,关注领域发展,并具备跨学科协作能力。对于个人职业发展和适应未来社会,AI技能至关重要。
18 3
|
2天前
|
人工智能 Oracle 关系型数据库
【AI Agent系列】【LangGraph】1. 进阶实战:给你的 LangGraph 加入条件分支(Conditional edges)
【AI Agent系列】【LangGraph】1. 进阶实战:给你的 LangGraph 加入条件分支(Conditional edges)
10 0
|
2天前
|
数据采集 存储 人工智能
【AI大模型应用开发】【LangChain系列】实战案例4:再战RAG问答,提取在线网页数据,并返回生成答案的来源
【AI大模型应用开发】【LangChain系列】实战案例4:再战RAG问答,提取在线网页数据,并返回生成答案的来源
26 0
|
2天前
|
数据采集 存储 人工智能
【AI大模型应用开发】【LangChain系列】实战案例2:通过URL加载网页内容 - LangChain对爬虫功能的封装
【AI大模型应用开发】【LangChain系列】实战案例2:通过URL加载网页内容 - LangChain对爬虫功能的封装
11 0
|
2天前
|
人工智能 Python
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
6 0
|
2天前
|
存储 人工智能 数据库
【AI Agent系列】【MetaGPT多智能体学习】8. MetaGPT多智能体进阶练习 - 使用MetaGPT重构BabyAGI
【AI Agent系列】【MetaGPT多智能体学习】8. MetaGPT多智能体进阶练习 - 使用MetaGPT重构BabyAGI
7 0
|
2天前
|
人工智能 决策智能
【AI Agent系列】【MetaGPT多智能体学习】3. 开发一个简单的多智能体系统,兼看MetaGPT多智能体运行机制
【AI Agent系列】【MetaGPT多智能体学习】3. 开发一个简单的多智能体系统,兼看MetaGPT多智能体运行机制
26 0
|
2天前
|
数据采集 人工智能 数据可视化
【AI大模型应用开发】【LangChain系列】4. 从Chain到LCEL:探索和实战LangChain的巧妙设计
【AI大模型应用开发】【LangChain系列】4. 从Chain到LCEL:探索和实战LangChain的巧妙设计
15 0
|
2天前
|
存储 人工智能 JSON
【AI大模型应用开发】【LangChain系列】3. 一文了解LangChain的记忆模块(理论实战+细节)
本文介绍了LangChain库中用于处理对话会话记忆的组件。Memory功能用于存储和检索先前的交互信息,以便在对话中提供上下文。目前,LangChain的Memory大多处于测试阶段,其中较为成熟的是`ChatMessageHistory`。Memory类型包括:`ConversationBufferMemory`(保存对话历史数组)、`ConversationBufferWindowMemory`(限制为最近的K条对话)和`ConversationTokenBufferMemory`(根据Token数限制上下文长度)。
12 0
|
2天前
|
JSON 人工智能 数据库
【AI大模型应用开发】【LangChain系列】1. 全面学习LangChain输入输出I/O模块:理论介绍+实战示例+细节注释
【AI大模型应用开发】【LangChain系列】1. 全面学习LangChain输入输出I/O模块:理论介绍+实战示例+细节注释
28 0
【AI大模型应用开发】【LangChain系列】1. 全面学习LangChain输入输出I/O模块:理论介绍+实战示例+细节注释