系列文章目錄
task01 導學課程
task02 YOLO系列發展線
文章目錄
- 系列文章目錄
- 前言
- 1 功能分塊
- 1.1 骨干網絡 Backbone
- 1.2 頸部網絡 Neck
- 1.3 頭部網絡 Head
- 1.3.1 邊界框回歸頭
- 1.3.2 分類頭
- 2 關鍵概念
- 3 典型算法
- 3.1 NMS
- 3.2 IoU
- 總結
前言
- Datawhale是一個專注于AI與數據科學的開源組織,匯集了眾多領域院校和知名企業的優秀學習者,聚合了一群有開源精神和探索精神的團隊成員
- YOLO-Master
- 本章學習資料
1 功能分塊
1.1 骨干網絡 Backbone
- 它的任務是把一張圖片的關鍵信息濃縮出來,比如形狀、紋理、顏色等特征,就像把水果榨成汁一樣,保留精華,去掉多余水分(無關背景)。
1.2 頸部網絡 Neck
1.3 頭部網絡 Head
1.3.1 邊界框回歸頭
1.3.2 分類頭
2 關鍵概念
3 典型算法
3.1 NMS
3.2 IoU
IoU(Intersection over Union):衡量兩個邊界框(Bounding Box)之間重疊程度和匹配度,常用于目標檢測任務中評估預測框 Predicted Box與真實框 Ground Truth Box的匹配度。相比于 Jaccard Index
-
其值范圍在 [ 0 , 1 ] [0,1] [0,1],
1
表示完全重疊,0
表示無重疊。 -
數學定義:
I o U = 交集面積 并集面積 \mathrm{IoU} = \frac{交集面積}{并集面積} IoU=并集面積交集面積?
- 即:
IoU = A inter A union = A inter A box1 + A box2 ? A inter \text{IoU} = \frac{A_{\text{inter}}}{A_{\text{union}}} = \frac{A_{\text{inter}}}{A_{\text{box1}} + A_{\text{box2}} - A_{\text{inter}}} IoU=Aunion?Ainter??=Abox1?+Abox2??Ainter?Ainter??
- 計算步驟:
- 交集坐標
- 左上角:取兩框左上坐標的較大值
- 右下角:取兩框右下坐標的較小值
- 若右下坐標 < 左上坐標,則交集面積為
0
x LeftTop = max ? ( x 1 a , x 1 b ) , y LeftTop = min ? ( y 1 a , y 1 b ) x_{\text{LeftTop}} = \max(x_1^a, x_1^b), \quad y_{\text{LeftTop}} = \min(y_1^a, y_1^b) xLeftTop?=max(x1a?,x1b?),yLeftTop?=min(y1a?,y1b?)
x RightBottom = min ? ( x 2 a , x 2 b ) , y RightBottom = max ? ( y 2 a , y 2 b ) x_{\text{RightBottom}} = \min(x_2^a, x_2^b), \quad y_{\text{RightBottom}} = \max(y_2^a, y_2^b) xRightBottom?=min(x2a?,x2b?),yRightBottom?=max(y2a?,y2b?)
-
面積計算
- 交集面積 = max ? ( 0 , x RightBottom ? x LeftTop ) × max ? ( 0 , y RightBottom ? y LeftTop ) \max(0, x_{\text{RightBottom}} - x_{\text{LeftTop}}) \times \max(0, y_{\text{RightBottom}} - y_{\text{LeftTop}}) max(0,xRightBottom??xLeftTop?)×max(0,yRightBottom??yLeftTop?)
- 并集面積 = 框1面積 + 框2面積 - 交集面積
- 代碼實踐
import matplotlib.pyplot as plt
import matplotlib.patches as pts# 現在定義一個函數,接收兩個參數,都是兩個點坐標,以數組的形式
def calculate_iou(box1, box2):"""計算兩個邊界框之間的 IoU (Intersection over Union)參數:box1: 第一個邊界框,格式為 [x1, y1, x2, y2]box2: 第二個邊界框,格式為 [x1, y1, x2, y2]返回:iou: 兩個邊界框之間的 IoU 值"""# 解析邊界框坐標x1_box1, y1_box1, x2_box1, y2_box1 = box1x1_box2, y1_box2, x2_box2, y2_box2 = box2# 確保坐標的有效性assert x1_box1 < x2_box1, f"無效的 box1 x坐標: {x1_box1} >= {x2_box1}"assert y1_box1 < y2_box1, f"無效的 box1 y坐標: {y1_box1} >= {y2_box1}"assert x1_box2 < x2_box2, f"無效的 box2 x坐標: {x1_box2} >= {x2_box2}"assert y1_box2 < y2_box2, f"無效的 box2 y坐標: {y1_box2} >= {y2_box2}"# 計算交集區域的坐標x_left = max(x1_box1, x1_box2)y_top = max(y1_box1, y1_box2)x_right = min(x2_box1, x2_box2)y_bottom = min(y2_box1, y2_box2)# 檢查是否有交集if x_right < x_left or y_bottom < y_top:return 0.0# 計算交集區域面積intersection_area = (x_right - x_left) * (y_bottom - y_top)# 計算兩個邊界框各自的面積area_box1 = (x2_box1 - x1_box1) * (y2_box1 - y1_box1)area_box2 = (x2_box2 - x1_box2) * (y2_box2 - y1_box2)# 計算并集區域面積union_area = area_box1 + area_box2 - intersection_area# 計算IOU,并避免除零錯誤if union_area == 0:iou = 0else:iou = intersection_area / union_areareturn iou# 定義一個函數,根據坐標畫出兩個框
def plot_boxes(box1, box2):fig, ax = plt.subplots()# 繪制第一個框rect1 = pts.Rectangle((box1[0], box1[1]), box1[2]-box1[0], box1[3]-box1[1], linewidth=1, edgecolor='r', facecolor='none')ax.add_patch(rect1)# 繪制第二個框rect2 = pts.Rectangle((box2[0], box2[1]), box2[2]-box2[0], box2[3]-box2[1], linewidth=1, edgecolor='b', facecolor='none')ax.add_patch(rect2)# 設置坐標軸范圍ax.set_xlim(0, 100)ax.set_ylim(0, 100)# 顯示圖像plt.show()# 測試用例
if __name__ == "__main__":# # 測試1: 完全重疊box_a = [10, 10, 50, 50]# box_b = [10, 10, 50, 50]# iou = calculate_iou(box_a, box_b)# print(f"測試1 (完全重疊): IoU = {iou:.2f} (預期: 1.00)")# 測試2: 部分重疊box_c = [20, 20, 60, 60]iou = calculate_iou(box_a, box_c)print(f"測試2 (部分重疊): IoU = {iou:.2f} (預期: 0.47)")# 繪制框plot_boxes(box_a, box_c)
- 效果圖
總結
- 分段介紹YOLO的框架和基本原理。