# 在RarePlanes數據集上怎么使用YOLOv5
## 引言
目標檢測是計算機視覺領域的重要任務之一,而YOLOv5作為YOLO系列的最新版本之一,因其高效性和易用性受到廣泛關注。RarePlanes數據集是一個專注于航空影像中飛機檢測的公開數據集,包含合成和真實圖像,適用于軍事和民用領域的應用。本文將詳細介紹如何在RarePlanes數據集上使用YOLOv5進行目標檢測,包括數據準備、模型訓練、評估和推理等步驟。
## 1. 環境準備
在開始之前,需要確保你的系統滿足以下要求:
- Python 3.8或更高版本
- PyTorch 1.7或更高版本
- CUDA(如果使用GPU加速)
- 其他依賴庫(如OpenCV、matplotlib等)
### 1.1 安裝YOLOv5
首先,克隆YOLOv5的官方倉庫并安裝依賴:
```bash
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -r requirements.txt
RarePlanes數據集由CosmiQ Works和.Reverie聯合發布,包含以下特點: - 合成數據:通過模擬生成的航空影像,標注精確。 - 真實數據:來自衛星或航空攝影的真實圖像。 - 標注信息:包括飛機的邊界框、類別(如戰斗機、客機等)和屬性(如機翼形狀、發動機數量等)。
數據集通常以COCO或Pascal VOC格式提供,需要轉換為YOLOv5支持的格式。
從官方渠道下載RarePlanes數據集:
- 訪問RarePlanes官網或.Reverie平臺。
- 下載數據集并解壓到本地目錄,例如data/rareplanes
。
解壓后的數據集結構如下:
rareplanes/
├── train/
│ ├── images/
│ └── labels/
├── val/
│ ├── images/
│ └── labels/
└── test/
├── images/
└── labels/
如果數據集是COCO格式,需要轉換為YOLOv5的TXT格式(每行一個標注,格式為class_id x_center y_center width height
)??梢允褂靡韵履_本:
import json
import os
def coco_to_yolo(coco_json, output_dir):
with open(coco_json) as f:
data = json.load(f)
os.makedirs(output_dir, exist_ok=True)
for image in data['images']:
image_id = image['id']
file_name = image['file_name']
width = image['width']
height = image['height']
annotations = [ann for ann in data['annotations'] if ann['image_id'] == image_id]
with open(os.path.join(output_dir, file_name.replace('.png', '.txt')), 'w') as f:
for ann in annotations:
class_id = ann['category_id']
x, y, w, h = ann['bbox']
x_center = x + w / 2
y_center = y + h / 2
f.write(f"{class_id} {x_center/width} {y_center/height} {w/width} {h/height}\n")
# 示例調用
coco_to_yolo('rareplanes/annotations/train.json', 'rareplanes/train/labels')
在yolov5/data/
目錄下創建rareplanes.yaml
文件,內容如下:
# rareplanes.yaml
train: ../data/rareplanes/train/images
val: ../data/rareplanes/val/images
test: ../data/rareplanes/test/images
# 類別數量和名稱
nc: 5 # 根據實際類別數調整
names: ['fighter', 'passenger', 'cargo', 'helicopter', 'other'] # 示例類別
YOLOv5提供多種預訓練模型(如YOLOv5s、YOLOv5m、YOLOv5l等)。根據需求選擇: - YOLOv5s:輕量級,適合快速實驗。 - YOLOv5l:更大模型,精度更高。
使用以下命令開始訓練:
python train.py --img 640 --batch 16 --epochs 100 --data data/rareplanes.yaml --cfg models/yolov5s.yaml --weights yolov5s.pt --name rareplanes_exp
參數說明:
- --img 640
:輸入圖像尺寸。
- --batch 16
:批次大?。ǜ鶕礼PU顯存調整)。
- --epochs 100
:訓練輪數。
- --data
:數據集配置文件路徑。
- --cfg
:模型配置文件路徑。
- --weights
:預訓練權重路徑。
- --name
:實驗名稱。
YOLOv5會自動記錄訓練日志,并通過TensorBoard可視化:
tensorboard --logdir runs/train
訓練完成后,使用以下命令評估模型性能:
python val.py --data data/rareplanes.yaml --weights runs/train/rareplanes_exp/weights/best.pt --img 640
輸出包括mAP、精確率、召回率等指標。
使用訓練好的模型進行預測:
python detect.py --source ../data/rareplanes/test/images --weights runs/train/rareplanes_exp/weights/best.pt --conf 0.25 --img 640
結果會保存在runs/detect/
目錄下。
在rareplanes.yaml
中配置數據增強參數:
# 數據增強
hsv_h: 0.015 # 色調增強
hsv_s: 0.7 # 飽和度增強
hsv_v: 0.4 # 明度增強
flipud: 0.5 # 上下翻轉概率
使用hyp.scratch.yaml
調整學習率、權重衰減等超參數。
將模型導出為ONNX或TorchScript格式以便部署:
python export.py --weights runs/train/rareplanes_exp/weights/best.pt --include onnx
--batch
大小。--img 320
降低輸入尺寸。本文詳細介紹了在RarePlanes數據集上使用YOLOv5進行目標檢測的全流程。通過合理配置和調優,YOLOv5可以在航空影像中實現高效的飛機檢測。未來可以嘗試: - 結合更多數據增強提升泛化性。 - 使用更大的模型(如YOLOv5x)提高精度。 - 部署到邊緣設備(如無人機)實現實時檢測。
”`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。