本项目以路标数据集roadsign为例,详细说明了如何使用PaddleDetection训练一个目标检测模型,并对模型进行评估和预测。
本项目提供voc格式的roadsign数据集和coco格式的roadsign数据集。
本项目提供 YOLOv3、FasterRCNN、FCOS这几个算法的配置文件。
您可以选择其中一个配置开始训练,快速体验PaddleDeteciton。
效果请戳这里:
PaddleDetection
cuda和cudnn安装包百度网盘链接: https://pan.baidu.com/s/1F5qWXaBwpZfg8Flecc_eng 密码: a367
网速不行的可以赶紧下载了。
在安装过程中,发现cocoapi安装很苦难,主要原因有:
git clone https://hub.fastgit.org/cocodataset/cocoapi --depth=1
pip install pycocotools -e ./PythonAPI
# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原
# View dataset directory. This directory will be recovered automatically after resetting environment.
!ls /home/aistudio/data
data49531 data52968
# 查看工作区文件, 该目录下的变更将会持久保存. 请及时清理不必要的文件, 避免加载过慢.
# View personal work directory. All changes under this directory will be kept even after reset. Please clean unnecessary files in time to speed up environment loading.
!ls /home/aistudio/work
football.gif hw_configs.zip PaddleDetection.zip
# 如果需要进行持久化安装, 需要使用持久化路径, 如下方代码示例:
# If a persistence installation is required, you need to use the persistence path as the following:
!mkdir /home/aistudio/external-libraries
!pip install beautifulsoup4 -t /home/aistudio/external-libraries
Looking in indexes: https://mirror.baidu.com/pypi/simple/
Collecting beautifulsoup4
[?25l Downloading https://mirror.baidu.com/pypi/packages/66/25/ff030e2437265616a1e9b25ccc864e0371a0bc3adb7c5a404fd661c6f4f6/beautifulsoup4-4.9.1-py3-none-any.whl (115kB)
[K |████████████████████████████████| 122kB 28.8MB/s eta 0:00:01
[?25hCollecting soupsieve>1.2 (from beautifulsoup4)
Downloading https://mirror.baidu.com/pypi/packages/6f/8f/457f4a5390eeae1cc3aeab89deb7724c965be841ffca6cfca9197482e470/soupsieve-2.0.1-py3-none-any.whl
Installing collected packages: soupsieve, beautifulsoup4
Successfully installed beautifulsoup4-4.9.1 soupsieve-2.0.1
# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可:
# Also add the following code, so that every time the environment (kernel) starts, just run the following code:
import sys
sys.path.append('/home/aistudio/external-libraries')
AIStudio上已经安装好paddlepaddle 1.8.4。
import paddle
print(paddle.__version__)
1.8.4
通过以下命令克隆最新的PaddleDetection代码库。
! git clone https://github.com/PaddlePaddle/PaddleDetection
如果因为网络问题clone较慢,可以:
git clone https://hub.fastgit.org/PaddlePaddle/PaddleDetection.git
git clone https://gitee.com/paddlepaddle/PaddleDetection
注:码云托管代码可能无法实时同步本github项目更新,存在3~5天延时,请优先从github上克隆。
work/PaddleDetection.zip
这里采用项目提供的代码库
! ls ~/work/PaddleDetection.zip
/home/aistudio/work/PaddleDetection.zip
%cd ~/work/
! unzip -o PaddleDetection.zip
/home/aistudio/work
Archive: PaddleDetection.zip
inflating: PaddleDetection/LICENSE
inflating: PaddleDetection/.style.yapf
inflating: PaddleDetection/slim/quantization/eval.py
inflating: PaddleDetection/slim/quantization/images/TransformForMobilePass.png
…
通过如下方式安装PaddleDetection依赖,并设置环境变量
安装 cocoapi
如果因为网络问题clone较慢,可以:
pip install "git+https://hub.fastgit.org/cocodataset/cocoapi.git#subdirectory=PythonAPI"
# github
#! pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI"
# fast github
# ! pip install "git+https://hub.fastgit.org/cocodataset/cocoapi.git#subdirectory=PythonAPI"
#
! pip install pycocotools
Looking in indexes: https://mirror.baidu.com/pypi/simple/
Collecting pycocotools
Downloading https://mirror.baidu.com/pypi/packages/de/df/056875d697c45182ed6d2ae21f62015896fdb841906fe48e7268e791c467/pycocotools-2.0.2.tar.gz
Requirement already satisfied: setuptools>=18.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pycocotools) (41.4.0)
Requirement already satisfied: cython>=0.27.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pycocotools) (0.29)
Requirement already satisfied: matplotlib>=2.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pycocotools) (2.2.3)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (2.4.2)
Requirement already satisfied: six>=1.10 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (1.15.0)
Requirement already satisfied: numpy>=1.7.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (1.16.4)
Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (2.8.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (1.1.0)
Requirement already satisfied: pytz in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (2019.3)
Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib>=2.1.0->pycocotools) (0.10.0)
Building wheels for collected packages: pycocotools
Building wheel for pycocotools (setup.py) ... [?25ldone
[?25h Created wheel for pycocotools: filename=pycocotools-2.0.2-cp37-cp37m-linux_x86_64.whl size=278366 sha256=f7178b092baa4425a7fd5471f1ecf848629cd597881637f6e2cf166e4f2d33de
Stored in directory: /home/aistudio/.cache/pip/wheels/fb/44/67/8baa69040569b1edbd7776ec6f82c387663e724908aaa60963
Successfully built pycocotools
Installing collected packages: pycocotools
Successfully installed pycocotools-2.0.2
设置环境
%cd ~/work/PaddleDetection/
!pip install -r requirements.txt
%env PYTHONPATH=.:$PYTHONPATH
%env CUDA_VISIBLE_DEVICES=0
/home/aistudio/work/PaddleDetection
Looking in indexes: https://mirror.baidu.com/pypi/simple/
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (4.36.1)
Collecting typeguard (from -r requirements.txt (line 2))
Downloading https://mirror.baidu.com/pypi/packages/52/33/3755584541a18d954389447bfd5f9cb7fa20dfbf5094829aee4a103e580c/typeguard-2.9.1-py3-none-any.whl
Requirement already satisfied: visualdl>=2.0.0b in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r requirements.txt (line 3)) (2.0.0b8)
Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r requirements.txt (line 4)) (4.1.1.26)
Requirement already satisfied: PyYAML in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r requirements.txt (line 5)) (5.1.2)
Collecting shapely (from -r requirements.txt (line 6))
[?25l Downloading https://mirror.baidu.com/pypi/packages/98/f8/db4d3426a1aba9d5dfcc83ed5a3e2935d2b1deb73d350642931791a61c37/Shapely-1.7.1-cp37-cp37m-manylinux1_x86_64.whl (1.0MB)
[K |████████████████████████████████| 1.0MB 17.7MB/s eta 0:00:01
[?25hRequirement already satisfied: Pillow>=7.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (7.1.2)
Requirement already satisfied: six>=1.14.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.15.0)
Requirement already satisfied: flask>=1.1.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.1.1)
Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.21.0)
Requirement already satisfied: numpy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.16.4)
Requirement already satisfied: flake8>=3.7.9 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (3.8.2)
Requirement already satisfied: protobuf>=3.11.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (3.12.2)
Requirement already satisfied: Flask-Babel>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.0.0)
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.22.0)
Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0b->-r requirements.txt (line 3)) (0.16.0)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.1.0)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.10.1)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.1->visualdl>=2.0.0b->-r requirements.txt (line 3)) (7.0)
Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (0.10.0)
Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.4.10)
Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.3.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (0.23)
Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.3.4)
Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.0.1)
Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (16.7.9)
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0b->-r requirements.txt (line 3)) (0.6.1)
Requirement already satisfied: pycodestyle<2.7.0,>=2.6.0a1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.6.0)
Requirement already satisfied: pyflakes<2.3.0,>=2.2.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8>=3.7.9->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.2.0)
Requirement already satisfied: setuptools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from protobuf>=3.11.0->visualdl>=2.0.0b->-r requirements.txt (line 3)) (41.4.0)
Requirement already satisfied: pytz in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2019.3)
Requirement already satisfied: Babel>=2.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.8.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2019.9.11)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->-r requirements.txt (line 3)) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.25.6)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->-r requirements.txt (line 3)) (2.8)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.1->visualdl>=2.0.0b->-r requirements.txt (line 3)) (1.1.1)
Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (0.6.0)
Requirement already satisfied: more-itertools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->pre-commit->visualdl>=2.0.0b->-r requirements.txt (line 3)) (7.2.0)
Installing collected packages: typeguard, shapely
Successfully installed shapely-1.7.1 typeguard-2.9.1
env: PYTHONPATH=.:$PYTHONPATH
env: CUDA_VISIBLE_DEVICES=0
验证安装
! python ppdet/modeling/tests/test_architectures.py
ss/home/aistudio/work/PaddleDetection/ppdet/core/workspace.py:117: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
isinstance(merge_dct[k], collections.Mapping)):
..........
----------------------------------------------------------------------
Ran 12 tests in 3.013s
OK (skipped=2)
本项目使用road-sign-detection 比赛数据,检测4种路标:
划分成训练集和测试集,总共877张图,其中训练集701张图、测试集176张图。
本项目提供voc格式和coco格式的数据:
voc格式:
划分好的数据下载地址为: roadsign_voc.tar。
AiStudio上数据地址:roadsign_voc
coco格式:
划分好的数据下载地址为::roadsign_coco.tar。
AiStudio上数据地址:roadsign_coco
将~/data/
文件夹下的数据解压到PaddleDetection/dataset/
文件夹下。
%cd ~/work/PaddleDetection/dataset/
! pwd
! ls ~/data -l
/home/aistudio/work/PaddleDetection/dataset
/home/aistudio/work/PaddleDetection/dataset
total 8
drwxrwxrwx 2 root root 4096 Sep 18 21:12 data49531
drwxrwxrwx 2 root root 4096 Sep 18 21:12 data52968
VOC数据格式的目标检测数据,是指每个图像文件对应一个同名的xml文件,xml文件中标记物体框的坐标和类别等信息。
Pascal VOC比赛对目标检测任务,对目标物体是否遮挡、是否被截断、是否是难检测物体进行了标注。对于用户自定义数据可根据实际情况对这些字段进行标注。
xml文件中包含以下字段:
road650.png
300
400
3
object字段,表示每个物体。包括
name
: 目标物体类别名称pose
: 关于目标物体姿态描述(非必须字段)truncated
: 目标物体目标因为各种原因被截断(非必须字段)occluded
: 目标物体是否被遮挡(非必须字段)difficult
: 目标物体是否是很难识别(非必须字段)bndbox
: 物体位置坐标,用左上角坐标和右下角坐标表示:xmin
、ymin
、xmax
、ymax
将~/data/data49531/roadsign_voc.tar
解压到PaddleDetection/dataset/roadsign_voc
下
%cd ~/work/PaddleDetection/dataset/roadsign_voc/
! pwd
/home/aistudio/work/PaddleDetection/dataset/roadsign_voc
/home/aistudio/work/PaddleDetection/dataset/roadsign_voc
# copy roadsign_voc.tar and extract
! cp ~/data/data49531/roadsign_voc.tar .
! tar -xvf roadsign_voc.tar
! rm -rf roadsign_voc.tar
./valid.txt
# 查看一条数据
! cat ./annotations/road650.xml
images
road650.png
300
400
3
0
coco数据格式,是指将所有训练图像的标注都存放到一个json文件中。数据以字典嵌套的形式存放。
json文件中存放了 info licenses images annotations categories
的信息:
{
'area': 899,
'iscrowd': 0,
'image_id': 839,
'bbox': [114, 126, 31, 29],
'category_id': 0, 'id': 1,
'ignore': 0,
'segmentation': []
}
将~/data/data49531/roadsign_coco.tar
解压到PaddleDetection/dataset/roadsign_coco
下
%cd ~/work/PaddleDetection/dataset/
! mkdir roadsign_coco
%cd ~/work/PaddleDetection/dataset/roadsign_coco/
! pwd
/home/aistudio/work/PaddleDetection/dataset
/home/aistudio/work/PaddleDetection/dataset/roadsign_coco
/home/aistudio/work/PaddleDetection/dataset/roadsign_coco
# copy roadsign_coco.tar and extract
! cp ~/data/data52968/roadsign_coco.tar .
! tar -xvf roadsign_coco.tar
! rm -rf roadsign_coco.tar
./annotations/
./annotations/train.json
./annotations/valid.json
./images/
./images/road749.png
./images/road355.png
./images/road768.png
…
# 查看一条数据
import json
coco_anno = json.load(open('./annotations/train.json'))
# coco_anno.keys
print('\nkeys:', coco_anno.keys())
# 查看类别信息
print('\n物体类别:', coco_anno['categories'])
# 查看一共多少张图
print('\n图像数量:', len(coco_anno['images']))
# 查看一共多少个目标物体
print('\n标注物体数量:', len(coco_anno['annotations']))
# 查看一条目标物体标注信息
print('\n查看一条目标物体标注信息:', coco_anno['annotations'][0])
keys: dict_keys(['images', 'type', 'annotations', 'categories'])
物体类别: [{'supercategory': 'none', 'id': 0, 'name': 'speedlimit'}, {'supercategory': 'none', 'id': 1, 'name': 'crosswalk'}, {'supercategory': 'none', 'id': 2, 'name': 'trafficlight'}, {'supercategory': 'none', 'id': 3, 'name': 'stop'}]
图像数量: 701
标注物体数量: 991
查看一条目标物体标注信息: {'area': 899, 'iscrowd': 0, 'image_id': 839, 'bbox': [114, 126, 31, 29], 'category_id': 0, 'id': 1, 'ignore': 0, 'segmentation': []}
本项目在work/hw_configs/
目录下提供以下配置文件
将~/work/hw_configs.zip
解压到 configs
文件夹下
%cd ~/work/PaddleDetection/
!unzip -o ~/work/hw_configs.zip -d configs/
! ls configs/hw_configs/
/home/aistudio/work/PaddleDetection
Archive: /home/aistudio/work/hw_configs.zip
creating: configs/hw_configs/
inflating: configs/hw_configs/fcos_r50_roadsign_coco_template.yml
inflating: configs/hw_configs/faster_rcnn_r50_roadsign_coco_template.yml
inflating: configs/hw_configs/faster_rcnn_r50_vd_fpn_roadsign_coco_template.yml
inflating: configs/hw_configs/yolov3_mobilenet_v1_roadsign_voc_template.yml
inflating: configs/hw_configs/yolov3_mobilenet_v1_roadsign_coco_template.yml
creating: configs/hw_configs/.ipynb_checkpoints/
faster_rcnn_r50_roadsign_coco_template.yml
faster_rcnn_r50_vd_fpn_roadsign_coco_template.yml
fcos_r50_roadsign_coco_template.yml
yolov3_mobilenet_v1_roadsign_coco_template.yml
yolov3_mobilenet_v1_roadsign_voc_template.yml
# 选择配置开始训练。可以通过 -o 选项覆盖配置文件中的参数
# faster_rcnn_r50_vd_fpn
! python -u tools/train.py -c configs/hw_configs/faster_rcnn_r50_vd_fpn_roadsign_coco_template.yml -o use_gpu=True --eval
# yolov3
#! python -u tools/train.py -c configs/hw_configs/yolov3_mobilenet_v1_roadsign_voc_template.yml -o use_gpu=True --eval
# fcos
#! python -u tools/train.py -c configs/hw_configs/fcos_r50_roadsign_coco_template.yml -o use_gpu=True --eval
2020-09-18 21:41:41,065-INFO: Best test box ap: 0.6826073406201353, in iter: 9200
您可以通过指定visualDL可视化工具,对loss变化曲线可视化。您仅需要指定 use_vdl
参数和 vdl_log_dir
参加即可。
点击左侧 可视化 按钮,设置 logdir
和模型文件,就可以对训练过程loss变化曲线和模型进行可视化。
# 选择配置开始训练。可以通过 -o 选项覆盖配置文件中的参数 vdl_log_dir 设置vdl日志文件保存路径
# faster_rcnn_r50_vd_fpn
! python -u tools/train.py -c configs/hw_configs/faster_rcnn_r50_vd_fpn_roadsign_coco_template.yml -o use_gpu=True --use_vdl=True --vdl_log_dir=vdl_dir/scalar --eval
# yolov3
#! python -u tools/train.py -c configs/hw_configs/yolov3_mobilenet_v1_roadsign_voc_template.yml -o use_gpu=True --use_vdl=True --vdl_log_dir=vdl_dir/scalar --eval
# fcos
#! python -u tools/train.py -c configs/hw_configs/fcos_r50_roadsign_coco_template.yml -o use_gpu=True --use_vdl=True --vdl_log_dir=vdl_dir/scalar --eval
2020-09-18 22:07:44,790-INFO: Best test box ap: 0.7006540723608486, in iter: 8800
PaddleDetection也提供了tools/eval.py
脚本用于评估模型,评估是可以通过-o weights=
指定待评估权重。
PaddleDetection训练过程中若开始了--eval
,会将所有checkpoint中评估结果最好的checkpoint保存为best_model.pdparams
,可以通过如下命令一键式评估最优checkpoint
这里我们加载预训练好的权重进行预测:
# 评估
# faster_rcnn_r50_vd_fpn
! python -u tools/eval.py -c configs/hw_configs/faster_rcnn_r50_vd_fpn_roadsign_coco_template.yml -o use_gpu=True weights=https://paddlemodels.bj.bcebos.com/object_detection/faster_r50_fpn_best_model_roadsign.pdparams
# yolov3
#! python -u tools/eval.py -c configs/hw_configs/yolov3_mobilenet_v1_roadsign_coco_template.yml -o use_gpu=True weights=https://paddlemodels.bj.bcebos.com/object_detection/yolov3_best_model_roadsign.pdparams
# fcos
#! python -u tools/eval.py -c configs/hw_configs/fcos_r50_roadsign_coco_template.yml -o use_gpu=True weights=https://paddlemodels.bj.bcebos.com/object_detection/fcos_best_model_roadsign.pdparams
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
2020-09-18 22:07:47,889-INFO: places would be ommited when DataLoader is not iterable
W0918 22:07:47.913216 600 device_context.cc:252] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 9.0
W0918 22:07:47.918426 600 device_context.cc:260] device: 0, cuDNN Version: 7.6.
2020-09-18 22:07:50,984-INFO: Downloading faster_r50_fpn_best_model_roadsign.pdparams from https://paddlemodels.bj.bcebos.com/object_detection/faster_r50_fpn_best_model_roadsign.pdparams
100%|██████████████████████████████████| 244263/244263 [05:50<00:00, 696.03KB/s]
2020-09-18 22:13:43,725-INFO: Test iter 0
2020-09-18 22:13:47,434-INFO: Test iter 100
2020-09-18 22:13:50,185-INFO: Test finish iter 176
2020-09-18 22:13:50,185-INFO: Total number of images: 176, inference time: 26.261917657261108 fps.
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
2020-09-18 22:13:50,195-INFO: Start evaluate...
Loading and preparing results...
DONE (t=0.00s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=0.09s).
Accumulating evaluation results...
DONE (t=0.04s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.698
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.918
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.820
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.676
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.755
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.751
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.639
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.742
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.742
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.702
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.787
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.790
PaddleDetection提供了tools/infer.py预测工具,可以使用训练好的模型预测图像并可视化,通过-o weights=指定加载训练过程中保存的权重。
预测脚本如下:
img_path = './dataset/roadsign_voc/images/road554.png'
# faster_rcnn_r50_vd_fpn
! python tools/infer.py -c configs/hw_configs/faster_rcnn_r50_vd_fpn_roadsign_coco_template.yml -o use_gpu=True weights=https://paddlemodels.bj.bcebos.com/object_detection/faster_r50_fpn_best_model_roadsign.pdparams --infer_img=dataset/roadsign_voc/images/road554.png
# yolov3
#! python tools/infer.py -c configs/hw_configs/yolov3_mobilenet_v1_roadsign_voc_template.yml -o use_gpu=True weights=https://paddlemodels.bj.bcebos.com/object_detection/yolov3_best_model_roadsign.pdparams --infer_img=dataset/roadsign_voc/images/road554.png
# fcos
#! python tools/infer.py -c configs/hw_configs/fcos_r50_roadsign_coco_template.yml -o use_gpu=True weights=https://paddlemodels.bj.bcebos.com/object_detection/fcos_best_model_roadsign.pdparams --infer_img=dataset/roadsign_voc/images/road554.png
W0918 22:15:39.446251 660 device_context.cc:252] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 9.0
W0918 22:15:39.451122 660 device_context.cc:260] device: 0, cuDNN Version: 7.6.
2020-09-18 22:15:43,949-INFO: Not found annotation file annotations/valid.json, load coco17 categories.
2020-09-18 22:15:44,182-INFO: Infer iter 0
2020-09-18 22:15:44,197-INFO: Detection bbox results save in output/road554.png
%matplotlib inline
import matplotlib.pyplot as plt
import cv2
infer_img = cv2.imread("output/road554.png")
plt.figure(figsize=(15,10))
plt.imshow(cv2.cvtColor(infer_img, cv2.COLOR_BGR2RGB))
plt.show()