【问题标题】:Implementing an efficient graph data structure for maintaining cluster distances in the Rank-Order Clustering algorithm在 Rank-Order Clustering 算法中实现有效的图数据结构以维护集群距离
【发布时间】:2017-09-13 16:39:46
【问题描述】:

我正在尝试从头开始实现 Rank-Order Clustering here is a link to the paper(这是一种凝聚聚类)算法。我已经通读了这篇论文(很多次),并且我有一个正在运行的实现,尽管它比我预期的要慢得多。

这是我的 Github 的 link,其中包含下载和运行 Jupyter Notebook 的说明。

算法:

算法1基于排序距离的聚类

输入:
N 个面,Rank-Order 距离阈值 t .
输出:
一个集群集 C 和一个“未分组”集群 Cun.
1:初始化集群C = { C1, C2, ... CN }
通过让每个面成为一个单元素集群。
2:重复
3:  for allCjCiC
4:  计算距离DR( Ci, Cj) 由 (4) 和 DN(Ci, Cj ) 通过 (5)。
5:   if DR(Ci, Cj) t 和 DN(Ci, Cj) 那么
6:   表示⟨CiCj⟩ 作为候选合并对。
7:  结束如果
8: 结束
9: 对所有候选合并对进行“传递”合并。
(例如,CiCjCk 已合并
如果 ⟨Ci, Cj⟩ 和 ⟨Cj, Ck⟩ 是候选合并对。)
10: 将 C 和簇之间的绝对距离更新为 (3)。
11:直到没有合并发生
12:将C中的所有单元素聚类移动到“未分组”的人脸聚类Cun
13:返回 CCun

我的实现:

我已经定义了一个Cluster 类,如下所示:

class Cluster:
    def __init__(self):
        self.faces = list()
        self.absolute_distance_neighbours = None

像这样的Face 类:

class Face:
    def __init__(self, embedding):
        self.embedding = embedding # a point in 128 dimensional space
        self.absolute_distance_neighbours = None

我还实现了查找在step 4 中使用的排序距离(D^R(C_i, C_j)) 和归一化距离(D^N(C_i, C_j)),所以这些可以被认为是理所当然的。

这是我找到两个集群之间最近的绝对距离的实现:

def find_nearest_distance_between_clusters(cluster1, cluster2):
    nearest_distance = sys.float_info.max
    for face1 in cluster1.faces:
        for face2 in cluster2.faces:
            distance = np.linalg.norm(face1.embedding - face2.embedding, ord = 1)

            if distance < nearest_distance: 
                nearest_distance = distance

            # If there is a distance of 0 then there is no need to continue
            if distance == 0:
                return(0)
    return(nearest_distance)


def assign_absolute_distance_neighbours_for_clusters(clusters, N = 20):
    for i, cluster1 in enumerate(clusters):
        nearest_neighbours = []
        for j, cluster2 in enumerate(clusters):
            distance = find_nearest_distance_between_clusters(cluster1, cluster2)    
            neighbour = Neighbour(cluster2, distance)
            nearest_neighbours.append(neighbour)
        nearest_neighbours.sort(key = lambda x: x.distance)
        # take only the top N neighbours
        cluster1.absolute_distance_neighbours = nearest_neighbours[0:N]

这是我的排序聚类算法的实现(假设find_normalized_distance_between_clustersfind_rank_order_distance_between_clusters的实现是正确的):

import networkx as nx
def find_clusters(faces):
    clusters = initial_cluster_creation(faces) # makes each face a cluster
    assign_absolute_distance_neighbours_for_clusters(clusters)
    t = 14 # threshold number for rank-order clustering
    prev_cluster_number = len(clusters)
    num_created_clusters = prev_cluster_number
    is_initialized = False

    while (not is_initialized) or (num_created_clusters):
        print("Number of clusters in this iteration: {}".format(len(clusters)))

        G = nx.Graph()
        for cluster in clusters:
            G.add_node(cluster)

        processed_pairs = 0

        # Find the candidate merging pairs
        for i, cluster1 in enumerate(clusters):

            # Only get the top 20 nearest neighbours for each cluster
            for j, cluster2 in enumerate([neighbour.entity for neighbour in \
                                          cluster1.absolute_distance_neighbours]):
                processed_pairs += 1
                print("Processed {}/{} pairs".format(processed_pairs, len(clusters) * 20), end="\r")
                # No need to merge with yourself 
                if cluster1 is cluster2:
                    continue
                else: 
                    normalized_distance = find_normalized_distance_between_clusters(cluster1, cluster2)
                    if (normalized_distance >= 1):
                        continue
                    rank_order_distance = find_rank_order_distance_between_clusters(cluster1, cluster2)
                    if (rank_order_distance >= t):
                        continue
                    G.add_edge(cluster1, cluster2) # add an edge to denote that these two clusters are to be merged

        # Create the new clusters            
        clusters = []
        # Note here that nx.connected_components(G) are 
        # the clusters that are connected
        for _clusters in nx.connected_components(G):
            new_cluster = Cluster()
            for cluster in _clusters:
                for face in cluster.faces:
                    new_cluster.faces.append(face)
            clusters.append(new_cluster)


        current_cluster_number = len(clusters)
        num_created_clusters = prev_cluster_number - current_cluster_number
        prev_cluster_number = current_cluster_number


        # Recalculate the distance between clusters (this is what is taking a long time)
        assign_absolute_distance_neighbours_for_clusters(clusters)


        is_initialized = True

    # Now that the clusters have been created, separate them into clusters that have one face
    # and clusters that have more than one face
    unmatched_clusters = []
    matched_clusters = []

    for cluster in clusters:
        if len(cluster.faces) == 1:
            unmatched_clusters.append(cluster)
        else:
            matched_clusters.append(cluster)

    matched_clusters.sort(key = lambda x: len(x.faces), reverse = True)

    return(matched_clusters, unmatched_clusters)

问题:

性能缓慢的原因是step 10: Update C and absolute distance between clusters by (3),其中(3)是:

这是C_i (cluster i)C_j (cluster j)中所有面之间的最小L1范数距离

合并集群后
因为每次在steps 3 - 8 中找到候选合并对时,我都必须重新计算新创建的集群之间的绝对距离。我基本上必须为所有创建的集群做一个嵌套的 for 循环,然后让另一个嵌套的 for 循环找到距离最近的两个面。之后,我仍然要按最近距离对邻居进行排序!

我认为这是错误的方法,因为我查看了 OpenBR,它还实现了我希望它在方法名称下的相同等级排序聚类算法:

Clusters br::ClusterGraph(Neighborhood neighborhood, float aggressiveness, const QString &amp;csv)

虽然我对 C++ 不太熟悉,但我很确定他们没有重新计算集群之间的绝对距离,这让我相信这是我错误实施的算法的一部分。

此外,在他们的方法声明的顶部,cmets 说他们已经预先计算了一个 kNN 图,这很有意义,因为当我重新计算集群之间的绝对距离时,我正在做很多我以前做过的计算。我相信关键是为集群预先计算一个 kNN 图,尽管这是我坚持的部分。我想不出如何实现数据结构,以便每次合并时都不必重新计算集群的绝对距离。

【问题讨论】:

  • 你有searchedGitHub吗?
  • 是的,我查看过 OpenBR 库,但我似乎无法理解它在做什么,因为我无法编译它

标签: python algorithm hierarchical-clustering


【解决方案1】:

在高层次上,这就是 OpenBR seems to do as well,需要的是集群 ID 的查找表 -> 集群对象,从中生成新的集群列表,无需重新计算。

可以从this section on OpenBR 的 ID 查找表中查看新集群的生成位置。

对于您的代码,需要为每个 Cluster 对象添加一个 ID,整数可能最适合内存使用。然后更新合并代码以在findClusters 创建一个待合并索引列表,并从现有集群索引创建一个新集群列表。然后根据他们的索引合并和更新邻居。

最后一步,邻居索引合并can be seen here on OpenBR

关键部分是合并时不会创建新集群,并且不会重新计算它们的距离。仅从现有集群对象更新索引并合并它们的相邻距离。

【讨论】:

  • 你认为你可以修改我的功能并实现它吗?如果可行,我很乐意接受您的回答。我仍然对如何合并/更新集群有些困惑。特别是,我很困惑如何创建要合并的索引列表,因为在合并期间,结果集群的数量将大于一个。我将如何跟踪哪些集群应该合并在一起?
  • 要合并的集群 ID 列表应该可以工作 - 这也是 OpenBR 所做的。我没有时间做实施。 OpenBR 中还有足够的代码可以将其用作移植到 Python 代码的基础。
【解决方案2】:

您可以尝试在字典 ex. 中存储人脸之间的距离值。

class Face:
    def __init__(self, embedding, id):
        self.embedding = embedding # a point in 128 dimensional space
        self.absolute_distance_neighbours = None
        self.id = id #Add face unique id

distances = {}

def find_nearest_distance_between_clusters(cluster1, cluster2):
    nearest_distance = sys.float_info.max
    for face1 in cluster1.faces:
        for face2 in cluster2.faces:
            if not distances.has_key( (face1.id, face2.id) ):
                distances[(face1.id, face2.id)] = np.linalg.norm(face1.embedding - face2.embedding, ord = 1) #calc distance only once
            distance = distances[(face1.id, face2.id)] #use precalc distances
            if distance < nearest_distance: 
                nearest_distance = distance

            # If there is a distance of 0 then there is no need to continue
            if distance == 0:
                return(0)
    return(nearest_distance)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 2019-07-23
    • 2021-07-03
    • 1970-01-01
    • 2011-06-19
    相关资源
    最近更新 更多