【发布时间】: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 all 对 Cj 和 Ci 在 C 做
4: 计算距离DR( Ci, Cj) 由 (4) 和 DN(Ci, Cj ) 通过 (5)。
5: if DR(Ci, Cj) t 和 DN(Ci, Cj) 那么
6: 表示⟨Ci,Cj⟩ 作为候选合并对。
7: 结束如果
8: 结束
9: 对所有候选合并对进行“传递”合并。
(例如,Ci、Cj、Ck 已合并
如果 ⟨Ci, Cj⟩ 和 ⟨Cj, Ck⟩ 是候选合并对。)
10: 将 C 和簇之间的绝对距离更新为 (3)。
11:直到没有合并发生
12:将C中的所有单元素聚类移动到“未分组”的人脸聚类Cun。
13:返回 C 和 Cun。
我的实现:
我已经定义了一个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_clusters和find_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 &csv)
虽然我对 C++ 不太熟悉,但我很确定他们没有重新计算集群之间的绝对距离,这让我相信这是我错误实施的算法的一部分。
此外,在他们的方法声明的顶部,cmets 说他们已经预先计算了一个 kNN 图,这很有意义,因为当我重新计算集群之间的绝对距离时,我正在做很多我以前做过的计算。我相信关键是为集群预先计算一个 kNN 图,尽管这是我坚持的部分。我想不出如何实现数据结构,以便每次合并时都不必重新计算集群的绝对距离。
【问题讨论】:
-
你有searchedGitHub吗?
-
是的,我查看过 OpenBR 库,但我似乎无法理解它在做什么,因为我无法编译它
标签: python algorithm hierarchical-clustering