赞
踩
PaddleDetection是由百度推出的目标检测开源模型库;
.pdparams:保存参数权重的文件格式。
Python版本:python <= 3.10;
PaddlePaddle版本:[PaddlePaddle/PaddleDetection],需要到安装说明中查看,一般是在满足最低版本要求后安装最新的稳定版本;
PaddlePaddle安装:开始使用_飞桨
数据集目录结构如下:
PaddleDetection
└── dataset
└── coco
├── train2017
├── val2017
└── annotations
python -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 tools/train.py -c configs/ppyoloe/ppyoloe_plus_crn_l_80e_coco.yml --eval --amp
后面加上存档的epoch索引(从0开始);
# 预测
python tools/infer.py -c configs/faster_rcnn_r50_1x.yml --infer_img=demo/000000570688.jpg
# 在CPU上进行推理
python tools/infer.py -c configs/ppyolo/ppyolo_r50vd_dcn_1x_coco.yml -o use_gpu=false weights=https://paddledet.bj.bcebos.com/models/ppyolo_r50vd_dcn_1x_coco.pdparams --infer_img=demo/000000014439.jpg
# CPU推理需要显式指定:use_gpu=false
# demo/000000014439.jpg已经内置在PaddleDetection的repo文件夹中
Note:在output目录下生成的同名的测试文件会被替换。
在PaddleDetection使用model.yml来配置模型的结构,配置文件的路径一般如下所示:
PaddleDetection/configs/model/model_***_coco.yml
模型参数:
Model:模型整体设置Backbone:主干网络设置PostProcess:后处理操作,(仅用于“CornerNet”模型)输入参数:
backbone:主干网络类名neck:检测颈类名head:检测头类名输入参数:
depth:主干网络深度variant:变体型号norm_type:归一化层类名输入参数:
in_channels:list,输入通道数out_channel:int,输出通道数(所有输出stage的通道数一样)extra_stage:额外输出的层数Note
FPN模块在初始化时,会使用backbone输出的out_shape更新FPN的输入参数,而导致config中FPN.in_channels的设置失效。
| 参数 | 描述 |
|---|---|
| TrainReader.batch_size | 每张卡上的batch-size |
utils/checkpoint.py存档权重的格式要求是.pdparams;
ppdet/optimizer.py包含了PaddleDetection优化器的设置代码;
| Class | Description |
|---|---|
OptimizerBuilder | 根据config的设置构建优化器 |
用于进行图像的预测
import os, sys
# add python path of PadleDetection to sys.path
parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 2)))
if parent_path not in sys.path:
sys.path.append(parent_path)
# 使用sys.path.append添加环境变量,在脚本执行完成之后则会失效
用于目标检测的后处理;
用于CornerNet的后处理函数;
参数:
results:bbox: 检测框信息,包含类别和坐标信息im_id: 图像iddef corner_post_process(results, config, num_classes):
detections = results['bbox'][0]
keep_inds = (detections[:, 1] > -1)
detections = detections[keep_inds]
labels = detections[:, 0]
scores = detections[:, 1]
boxes = detections[:, 2:6]
cls_boxes = get_nms_result(
boxes, scores, config, num_classes, background_label=-1, labels=labels)
results.update({'bbox': (cls_boxes, [[len(cls_boxes)]])})
process_single_sample():处理单个样本def process_single_sample(info, image_id, class_names): image_file = info['image_file'] single_image = dict() single_image['file_name'] = os.path.split(image_file)[-1] single_image['id'] = image_id image = cv2.imread(image_file) height, width, _ = image.shape single_image['width'] = width single_image['height'] = height # process annotation field single_objs = [] objects = info['annotation'] for obj in objects: poly, name, difficult = obj['poly'], obj['name'], obj['difficult'] if difficult == '2': continue single_obj = dict() single_obj['category_id'] = class_names.index(name) + 1 single_obj['segmentation'] = [poly] single_obj['iscrowd'] = 0 xmin, ymin, xmax, ymax = min(poly[0::2]), min(poly[1::2]), max(poly[ 0::2]), max(poly[1::2]) # 使用间隔索引的方法获得四个点的X或Y坐标,并取最小值和最大值,作为bbox的坐标 width, height = xmax - xmin, ymax - ymin single_obj['bbox'] = [xmin, ymin, width, height] single_obj['area'] = height * width single_obj['image_id'] = image_id single_objs.append(single_obj) return (single_image, single_objs)
sample_transforms官方文档:数据预处理算子 - PaddleDetection/READER.md at release/2.5
今天在使用Paddle时,使用括号索引改变颜色分量顺序时,代码报错了,
期望改变图像张量的颜色分量顺序:
img_tensor = img_tensor[:, (2, 1, 0), :, :]
Paddle报错:
...
File "D:\Professional\Paddle\Paddle_oriented_reppoints\ppdet\engine\train_aligner.py", line 461, in train
img_tensor = img_tensor[:, (2, 1, 0), :, :]
File "C:\Users\songyuc\.conda\envs\conda-paddle\lib\site-packages\paddle\fluid\dygraph\varbase_patch_methods.py", line 753, in __getitem__
return self._getitem_index_not_tensor(item)
ValueError: (InvalidArgument) Currently, Tensor.__indices__() only allows indexing by Integers,
Slices, Ellipsis, None, tuples of these types and list of Bool and Integers, but received tuple in
2th slice item (at ..\paddle/fluid/pybind/slice_utils.h:295)
感觉好像就是Paddle不支持这种索引操作;
关于PP-YOLOE的论文学习笔记,请参考《PP-YOLOE的译读笔记》;
学习资料:
Paddle目标检测算法课程——PPYOLOE
Paddle目标检测算法课程——PPYOLOE | PPT
技术一览表:
| 名称 | 超参数 |
|---|---|
| Input size | 640 |
| Down sample | AdaptiveAvgPool2d (目前是在Head部分的代码中看到此算子作为下采样的方式) |
| Act | Swish (SiLU) |
| Head | PPYOLOEHead |
| Optimizer | SGD with momentum and L2-regularization |
| Weight decay | 0.0005 |
关于这个repo的说明文档,请参阅《PPYOLO、PPYOLOv2、PPYOLOE的pytorch实现三合一!尽在miemiedetection!》;
关于PP-YOLOE对bias项weight_decay的处理,请参考[PaddleDetection | backbones/cspresnet.py];
总的来说,就是对BN层的所有参数不使用weight_decay,即:
self.bn = nn.BatchNorm2D(
ch_out,
weight_attr=ParamAttr(regularizer=L2Decay(0.0)),
bias_attr=ParamAttr(regularizer=L2Decay(0.0)))
这里我们参考的是[miemie2013/miemiedetection]的实现方法:[miemiedetection]是从[mmdetection]中移植过来的,这里继承了mmdetection的书写方式[code – add_param_group]:

这里的model.add_param_group调用的是模型的.add_param_group()方法[ppyoloe.py – add_param_group()];
Paddle文档:《自定义C++算子-PaddlePaddle文档 | 支持多种数据类型》
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。