kmeans.py 2.6 KB
Newer Older
L
Lars Nieradzik 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import numpy as np


def iou(box, clusters):
    """
    Calculates the Intersection over Union (IoU) between a box and k clusters.
    :param box: tuple or array, shifted to the origin (i. e. width and height)
    :param clusters: numpy array of shape (k, 2) where k is the number of clusters
    :return: numpy array of shape (k, 0) where k is the number of clusters
    """
    x = np.minimum(clusters[:, 0], box[0])
    y = np.minimum(clusters[:, 1], box[1])
    if np.count_nonzero(x == 0) > 0 or np.count_nonzero(y == 0) > 0:
        raise ValueError("Box has no area")

    intersection = x * y
    box_area = box[0] * box[1]
    cluster_area = clusters[:, 0] * clusters[:, 1]

    iou_ = intersection / (box_area + cluster_area - intersection)

    return iou_


def avg_iou(boxes, clusters):
    """
    Calculates the average Intersection over Union (IoU) between a numpy array of boxes and k clusters.
    :param boxes: numpy array of shape (r, 2), where r is the number of rows
    :param clusters: numpy array of shape (k, 2) where k is the number of clusters
    :return: average IoU as a single float
    """
    return np.mean([np.max(iou(boxes[i], clusters)) for i in range(boxes.shape[0])])


def translate_boxes(boxes):
    """
    Translates all the boxes to the origin.
    :param boxes: numpy array of shape (r, 4)
    :return: numpy array of shape (r, 2)
    """
    new_boxes = boxes.copy()
    for row in range(new_boxes.shape[0]):
        new_boxes[row][2] = np.abs(new_boxes[row][2] - new_boxes[row][0])
        new_boxes[row][3] = np.abs(new_boxes[row][3] - new_boxes[row][1])
    return np.delete(boxes, [0, 1], axis=1)


L
Lars Nieradzik 已提交
48
def kmeans(boxes, k, dist=np.median):
L
Lars Nieradzik 已提交
49 50 51 52
    """
    Calculates k-means clustering with the Intersection over Union (IoU) metric.
    :param boxes: numpy array of shape (r, 2), where r is the number of rows
    :param k: number of clusters
L
Lars Nieradzik 已提交
53
    :param dist: distance function
L
Lars Nieradzik 已提交
54 55 56 57 58
    :return: numpy array of shape (k, 2)
    """
    rows = boxes.shape[0]

    distances = np.empty((rows, k))
L
Lars Nieradzik 已提交
59
    last_clusters = np.zeros((rows,))
L
Lars Nieradzik 已提交
60

L
Lars Nieradzik 已提交
61 62
    # the Forgy method will fail if the whole array contains the same rows
    clusters = boxes[np.random.choice(rows, k, replace=False)]
L
Lars Nieradzik 已提交
63

L
Lars Nieradzik 已提交
64 65 66
    while True:
        for row in range(rows):
            distances[row] = 1 - iou(boxes[row], clusters)
L
Lars Nieradzik 已提交
67

L
Lars Nieradzik 已提交
68
        nearest_clusters = np.argmin(distances, axis=1)
L
Lars Nieradzik 已提交
69

L
Lars Nieradzik 已提交
70 71
        if (last_clusters == nearest_clusters).all():
            break
L
Lars Nieradzik 已提交
72

L
Lars Nieradzik 已提交
73 74
        for cluster in range(k):
            clusters[cluster] = dist(boxes[nearest_clusters == cluster], axis=0)
L
Lars Nieradzik 已提交
75

L
Lars Nieradzik 已提交
76
        last_clusters = nearest_clusters
L
Lars Nieradzik 已提交
77

L
Lars Nieradzik 已提交
78
    return clusters