r"""Perfom clustering for input embeddings and output the labels. Args: model_dir: A model dir. model_config: The model config.
| 192 | |
| 193 | |
| 194 | class ClusterBackend(torch.nn.Module): |
| 195 | r"""Perfom clustering for input embeddings and output the labels. |
| 196 | Args: |
| 197 | model_dir: A model dir. |
| 198 | model_config: The model config. |
| 199 | """ |
| 200 | |
| 201 | def __init__(self, merge_thr=0.78): |
| 202 | """Initialize ClusterBackend. |
| 203 | |
| 204 | Args: |
| 205 | merge_thr: TODO. |
| 206 | """ |
| 207 | super().__init__() |
| 208 | self.model_config = {"merge_thr": merge_thr} |
| 209 | # self.other_config = kwargs |
| 210 | |
| 211 | self.spectral_cluster = SpectralCluster() |
| 212 | self.umap_hdbscan_cluster = UmapHdbscan() |
| 213 | |
| 214 | def forward(self, X, **params): |
| 215 | # clustering and return the labels |
| 216 | """Forward pass for training. |
| 217 | |
| 218 | Args: |
| 219 | X: TODO. |
| 220 | **params: Additional keyword arguments. |
| 221 | """ |
| 222 | k = params["oracle_num"] if "oracle_num" in params else None |
| 223 | assert len(X.shape) == 2, "modelscope error: the shape of input should be [N, C]" |
| 224 | if X.shape[0] < 20: |
| 225 | return np.zeros(X.shape[0], dtype="int") |
| 226 | if X.shape[0] < 2048 or k is not None: |
| 227 | # unexpected corner case |
| 228 | labels = self.spectral_cluster(X, k) |
| 229 | else: |
| 230 | labels = self.umap_hdbscan_cluster(X) |
| 231 | |
| 232 | if k is None and "merge_thr" in self.model_config: |
| 233 | labels = self.merge_by_cos(labels, X, self.model_config["merge_thr"]) |
| 234 | |
| 235 | return labels |
| 236 | |
| 237 | def merge_by_cos(self, labels, embs, cos_thr): |
| 238 | # merge the similar speakers by cosine similarity |
| 239 | """Merge by cos. |
| 240 | |
| 241 | Args: |
| 242 | labels: TODO. |
| 243 | embs: TODO. |
| 244 | cos_thr: TODO. |
| 245 | """ |
| 246 | assert cos_thr > 0 and cos_thr <= 1 |
| 247 | while True: |
| 248 | spk_num = labels.max() + 1 |
| 249 | if spk_num == 1: |
| 250 | break |
| 251 | spk_center = [] |
no outgoing calls
no test coverage detected
searching dependent graphs…