k-means algorithm anchors

Text later added:

 

 

AS NP numpy Import 

# Box class definitions, bounding box coordinates described
class Box ():
DEF the __init __ (Self, X, Y, W, H):
self.x = X
self.y = Y
self.w = W
Self. H = H

DEF box_iou (a, b):
'' '
# Box a and b are examples of the type
# is the box a return value area and the area of the intersection Box b
' ''
a_x1 = AX-AW / 2
a_y1 = AY - AH / 2
a_x2 = a.x + AW / 2
a_y2 = AY + AH / 2
b_x1 = BX-BW / 2
b_y1 by = - BH / 2
b_x2 b.x = + BW / 2
b_y2 = BH + by / 2
box_x1 max = (a_x1, b_x1)
box_y1 = max (a_y1, b_y1)
box_x2 = min (a_x2, b_x2)
box_y2 = min (a_y2, b_y2)
box_w = box_x2-box_x1
box_h = box_y2 - box_y1
IF box_w <0 or box_h <0:
Area = 0
the else:
Area = box_w * box_h
box_intersection = Area
box_union = AW * AH + BW * BH-box_intersection
IOU = box_intersection / box_union
return IOU

# initialization using the k-means ++ centroids, reduce random initialization centroids influence on the final result
DEF init_centroids (Boxes, n_anchors):
'' '
randomly select a box as
: param boxes: Box is a list of all bounding boxes of objects
: param n_anchors: n_anchors is the k-value k-means
: return: return value centroids are initialized n_anchors a Centroid
'' '
centroids = []
boxes_num = len (Boxes)
centroid_index = np.random.choice(boxes_num, 1) # 在boxes_num=55 中产生一个数23
centroids.append(boxes[centroid_index])
print(centroids[0].w, centroids[0].h)

for centroid_index in range(0, n_anchors-1):
sum_distance = 0
distance_list = []
cur_sum = 0
for box in boxes:
min_distance = 1
for centroid_i, centroid in enumerate(centroids):
distance = (1 - box_iou(box, centroid))
if distance < min_distance:
min_distance = distance
sum_distance += min_distance
distance_list.append(min_distance)
* = sum_distance np.random.random distance_thresh ()

for I in Range (0, boxes_num):
cur_sum distance_list = + [I]
IF cur_sum> distance_thresh:
centroids.append (Boxes [I])
Print (Boxes [I] .W , Boxes [I] .h)
BREAK
return centroids of

# k-means for calculating new centroids of
DEF do_kmeans (n_anchors, Boxes, centroids of):
'' '
: param n_anchors: the value of k is the k-means
: param boxes: all Box object list bounding boxes of
: param centroids: is the center of all clusters
: return: # return value new_centroids is calculated new cluster center
# return value groups are boxes list n_anchors clusters included
# return value loss is all the box away distance nearest centroid belongs and
'' '
= 0 Loss
Groups = []
new_centroids = []
for I in Range (n_anchors):
groups.append ([]) # [[], [], [], []]
new_centroids.append (Box (0, 0, 0, 0))
# initialization code above established
for box in Boxes:
MIN_DISTANCE = 1
GROUP_INDEX = 0
for centroid_index, Centroid in the enumerate (centroids):
# this cycle is actually looking box with which centroidsiou minimum, the closest
distance = (1 - box_iou (Box, Centroid))
IF Distance <MIN_DISTANCE:
MIN_DISTANCE = Distance
GROUP_INDEX = centroid_index
Groups [GROUP_INDEX] .append (Box) group # corresponding to reservation
loss + = min_distance
new_centroids [group_index] .w + = w box.w # a group corresponding to the accumulated
new_centroids [GROUP_INDEX] + = box.h .h

for I in Range (n_anchors): # obtain a new family of w and H
new_centroids [ I] .W / len = (Groups [I])
new_centroids [I] .h / len = (Groups [I])

return new_centroids, Groups, Loss

DEF init_all_value (use_init_centroids =. 1, n_anchors =. 9):
# initialization Construction group Center
IF use_init_centroids:
centroids of init_centroids = (Boxes, n_anchors)
the else:
centroid_indices np.random.choice = (len (Boxes), n_anchors)
centroids of = []
for centroid_index in centroid_indices:
centroids.append (Boxes [centroid_index])
# initialization Construction groups corresponding to the stored group box class
centroids of, Groups, old_loss = do_kmeans (n_anchors, Boxes, centroids of)

return centroids of, Groups, old_loss

IF __name__ __ == '__ main__':
# construct Boxes

Boxes = []
boxes.append (Box (4,5,6,7)) # the actual fill their
num_anchor = 9 # is the number of center point group generated

# Construction stop condition
NUM_ITERATIONS = 2000
loss_stop. 6 = 1E-

centroids of, Groups, old_loss init_all_value = (. 1, num_anchor)

# of cycles to find the best group and w H
Iterations =. 1
the while (True):
centroids of, Groups, Loss = do_kmeans (num_anchor, Boxes, centroids of)
Iterations = Iterations +. 1
Print ( "Loss =% F"% Loss)
IF ABS (old_loss - Loss) <loss_stop or Iterations > num_iterations:
break
old_loss = loss

# 打印最终结果

for centroid in centroids:
print("k-means result:\n")
print(centroid.w, centroid.h)

Guess you like

Origin www.cnblogs.com/tangjunjun/p/12393018.html