目標檢測 YOLOv2 k means方法

2021-09-12 23:27:22 字數 2471 閱讀 8794

計算公式:

已知:box

1=(w

1,h1

)\mathbf = (w_1, h_1)

box1​=

(w1​

,h1​

),box2

=(w2

,h2)

\mathbf = (w_2, h_2)

box2​=

(w2​

,h2​

) b ox

1\mathbf

box1

​ 和 box

2\mathbf

box2

​ 的交並比:

j (b

1,b2

)=

min(w1

,w2)

⋅min(h

1,h2

)w1h

1+w2

h2

−min(w

1,w2

)⋅

min(h1

,h2)

j(\mathbf, \mathbf) = \frac(w_1, w_2)\cdot\text(h_1, h_2)}(w_1, w_2)\cdot\text(h_1, h_2)}

j(b1​,

b2​)

=w1​

h1​+

w2​h

2​−min(w

1​,w

2​)⋅

min(h1

​,h2

​)min(w1

​,w2

​)⋅min(h

1​,h

2​)​

yolov2中不使用歐式距離,使用如下公式來計算兩個框的距離

d j(

b1,b

2)=1

−j(b

1,b2

)d_j(\mathbf, \mathbf) = 1 - j(\mathbf, \mathbf)

dj​(b1

​,b2

​)=1

−j(b

1​,b

2​)python實現

import numpy as np

# 計算質心和其他boxes的距離

defiou

(box, clusters)

: x = np.minimum(clusters[:,

0], box[0]

) y = np.minimum(clusters[:,

1], box[1]

) intersection = x * y

box_area = box[0]

* box[1]

cluster_area = clusters[:,

0]* clusters[:,

1]iou_ = intersection /

(box_area + cluster_area - intersection)

return iou_

defkmeans

(boxes, k, dist=np.median)

: rows = boxes.shape[0]

# rows行k列,每行對應乙個boxes,每列是boxes與各個質心的距離

distances = np.empty(

(rows, k))#

last_clusters = np.zeros(

(rows,))

np.random.seed(

)# 在boxes中隨機選取k個作為質心clusters

clusters = boxes[np.random.choice(rows, k, replace=

false)]

while

true

:for row in

range

(rows)

: distances[row]=1

- iou(boxes[row]

, clusters)

# 找到與boxes距離最近的clusters的索引

nearest_clusters = np.argmin(distances, axis=1)

# 當兩次聚類結果相同時結束

if(last_clusters == nearest_clusters)

.all()

:break

# 分別取w和h的平均值更新clusters

for cluster in

range

(k):

clusters[cluster]

= dist(boxes[nearest_clusters == cluster]

, axis=0)

last_clusters = nearest_clusters

return clusters

參考:

目標檢測 YOLOv2總結

以下為筆記相關鏈結 推薦使用鏈結閱讀 yolov2個人總結 01.yolo v2 題目 yolo 9000 better,faster,stronger 作者 joseph redmon yolo系列的主要作者 四個問題 要解決什麼問題?在yolov1的基礎上解決小目標檢測精度 定位資訊錯誤及綜合性...

目標檢測之YOLOv1

you only look once unified,real time object detection yolo 一體化的,實時的物體檢測 翻譯 詳解 1 2 1 yolov1框架 步驟 1 resize成448 448,分割得到7 7網格 cell 2 cnn提取特徵和 卷積層負責提取特徵。全...

目標檢測 YOLOv5(八)

簡介 yolov5權重檔案 密碼 00mp 作者給的演算法效能如下圖 改編自知乎大佬的一張圖 yolov5s網路是yolov5系列中深度最小,特徵圖的寬度最小的網路。後面的3種 yolov5m yolov5l yolov5x 都是在此基礎上不斷加深,不斷加寬。1 mosaic資料增強 同yolov4...