項目:基于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
工具官網:CVHub520/X-AnyLabeling: Effortless data labeling with AI support from Segment Anything and other awesome models.
工具下載: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 和 ytop_left = (min(x_coords), min(y_coords))# 右下角為最大的 x 和 ybottom_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_annotationsreturn 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: COCODataSetimage_dir: train2017 # 輸入圖像anno_path: annotations/instances_train2017.json # 標注文件jsondataset_dir: dataset/coco # 輸出保存地址data_fields: ['image', 'gt_bbox', 'gt_class', 'is_crowd']# 驗證
EvalDataset:name: COCODataSetimage_dir: val2017anno_path: annotations/instances_val2017.jsondataset_dir: dataset/cocoallow_empty: true#測試
TestDataset:name: ImageFolderanno_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_finalpretrain_weights: https://bj.bcebos.com/v1/paddledet/models/pretrained/ppyoloe_crn_l_obj365_pretrained.pdparams
depth_mult: 1.0
width_mult: 1.0CustomCSPPAN:num_layers: 4use_trans: TruePPYOLOEHead:reg_range: [-2, 17]static_assigner_epoch: -1assigner:name: TaskAlignedAssigner_CRcenter_radius: 1nms:name: MultiClassNMSnms_top_k: 1000keep_top_k: 300score_threshold: 0.01nms_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: CosineDecaymax_epochs: 96- name: LinearWarmupstart_factor: 0.epochs: 5 # 看自己的顯卡情況OptimizerBuilder:optimizer:momentum: 0.9type: Momentumregularizer:factor: 0.0005type: L2