项目:基于PP-YOLOE-SOD的无人机航拍图像检测案例全流程实操 - 飞桨AI Studio星河社区
一、安装环境
先准备新环境py>=3.9
1.先cd到源代码的根目录下
2.pip install -r requirements.txt
3.python setup.py install
这一步需要看自己的GPU情况,去飞浆官网对着下载即可,第四步下载不了看第六步
4.pip install paddlepaddle-gpu==2.4.2 -i xxx(镜像源)
5.pip install paddlepaddle==2.4.2-i xxx(这是CPU版本,也需要配套下载,不然GPU无法运行)
6.pip install --upgrade pip
二、标注工具
定义自己的数据集:PaddleDetection/docs/tutorials/data/DetAnnoTools.md at develop · PaddlePaddle/PaddleDetection
labelme 和 labelImg 就不介绍了很常规的标注工具。
数据格式转换:PaddleDetection/tools/x2coco.py at develop · PaddlePaddle/PaddleDetection
X-Anylabeling
特别介绍,AnyLabeling = LabelImg + Labelme + Improved UI + Auto-labeling
工具下载:Releases · CVHub520/X-AnyLabeling
功能:
支持GPU推理加速;
支持图像和视频处理;
支持单帧和批量预测所有任务;
支持自定义模型和二次开发设计;
支持一键导入和导出主流的标签格式,如COCO\VOC\YOLO\DOTA\MOT\MASK;
支持多种图像标注样式,包括 多边形、矩形、旋转框、圆形、线条、点,以及 文本检测、识别 和 KIE 标注;
支持各类视觉任务,如图像分类、目标检测、实例分割、姿态估计、旋转检测、多目标跟踪、光学字符识别、图像文本描述、车道线检测、分割一切系列等。
自动标注(无论是路径还是图片都禁止使用中文)后如果是自动保存的格式json需要先进行标注内容处理,因为上述格式转换过程需要标注框信息只有对角两个点,因此需要脚本处理后再进行格式转换。
import os
import json
# 函数:读取和转换坐标
def convert_bbox_to_coordinates(data):
# 存储转换后的标注数据
converted_annotations = []
# 遍历每个标注框
for shape in data.get('shapes', []):
# 提取四个点的坐标
points = shape['points']
# 获取所有 x 和 y 坐标
x_coords = [point[0] for point in points]
y_coords = [point[1] for point in points]
# 左上角为最小的 x 和 y
top_left = (min(x_coords), min(y_coords))
# 右下角为最大的 x 和 y
bottom_right = (max(x_coords), max(y_coords))
# 转换后的坐标
coordinates = {
'label': shape['label'],
'points': [top_left, bottom_right], # 只保存左上角和右下角
'group_id': shape.get('group_id'),
'description': shape.get('description', ''),
'difficult': shape.get('difficult', False),
'shape_type': shape.get('shape_type', 'rectangle'),
'flags': shape.get('flags', {}),
'attributes': shape.get('attributes', {})
}
# 将转换后的数据添加到列表
converted_annotations.append(coordinates)
# 返回转换后的数据
data['shapes'] = converted_annotations
return data
# 函数:处理文件夹中的所有 JSON 文件
def process_json_folder(input_folder, output_folder):
# 确保输出文件夹存在
os.makedirs(output_folder, exist_ok=True)
# 遍历输入文件夹中的所有 JSON 文件
for filename in os.listdir(input_folder):
if filename.endswith('.json'):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
# 打开并读取 JSON 文件
with open(input_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# 执行坐标转换操作
converted_data = convert_bbox_to_coordinates(data)
# 保存转换后的数据到新文件夹
with open(output_path, 'w', encoding='utf-8') as outfile:
json.dump(converted_data, outfile, ensure_ascii=False, indent=4)
print(f'Converted and saved: {filename}')
# 设置输入文件夹和输出文件夹的路径
input_folder = 'input_json_folder' # 输入文件夹路径
output_folder = 'output_json_folder' # 输出文件夹路径
# 调用函数,处理文件夹中的所有 JSON 文件
process_json_folder(input_folder, output_folder)
三、标注后准备
源代码:PaddleDetection: PaddleDetection的目的是为工业界和学术界提供丰富、易用的目标检测模型
数据路径配置文件(重点,简单参数):
PaddleDetection/configs/datasets/coco_detection.yml
metric: COCO
num_classes: 1 #类别
# 训练
TrainDataset:
name: COCODataSet
image_dir: train2017 # 输入图像
anno_path: annotations/instances_train2017.json # 标注文件json
dataset_dir: dataset/coco # 输出保存地址
data_fields: ['image', 'gt_bbox', 'gt_class', 'is_crowd']
# 验证
EvalDataset:
name: COCODataSet
image_dir: val2017
anno_path: annotations/instances_val2017.json
dataset_dir: dataset/coco
allow_empty: true
#测试
TestDataset:
name: ImageFolder
anno_path: annotations/instances_val2017.json # also support txt (like VOC's label_list.txt)
dataset_dir: dataset/coco # if set, anno_path will be 'dataset_dir/anno_path'
模型参数配置文件(重点):
① PaddleDetection/configs/smalldet/ppyoloe_plus_sod_crn_l_80e_coco.yml
_BASE_: [
'../datasets/coco_detection.yml',
'../runtime.yml',
'../ppyoloe/_base_/optimizer_80e.yml',
'../ppyoloe/_base_/ppyoloe_plus_crn.yml',
'../ppyoloe/_base_/ppyoloe_plus_reader.yml',
]
log_iter: 10 # 打印日志log的间隔
snapshot_epoch: 5 # 每过多少轮评估一次
weights: output/ppyoloe_plus_sod_crn_l_80e_coco/model_final
pretrain_weights: https://bj.bcebos.com/v1/paddledet/models/pretrained/ppyoloe_crn_l_obj365_pretrained.pdparams
depth_mult: 1.0
width_mult: 1.0
CustomCSPPAN:
num_layers: 4
use_trans: True
PPYOLOEHead:
reg_range: [-2, 17]
static_assigner_epoch: -1
assigner:
name: TaskAlignedAssigner_CR
center_radius: 1
nms:
name: MultiClassNMS
nms_top_k: 1000
keep_top_k: 300
score_threshold: 0.01
nms_threshold: 0.7
② PaddleDetection-release-2.8.1\configs\ppyoloe\_base_\optimizer_80e.yml
epoch: 80 # 训练轮数
LearningRate:
base_lr: 0.001 # 学习率{一般是10**(-3)}
schedulers:
- name: CosineDecay
max_epochs: 96
- name: LinearWarmup
start_factor: 0.
epochs: 5 # 看自己的显卡情况
OptimizerBuilder:
optimizer:
momentum: 0.9
type: Momentum
regularizer:
factor: 0.0005
type: L2