def super_vertex(knns, k, th, th_step, max_sz): pairs, scores = filter_knns(knns, k, th) assert len(pairs) == len(scores) if len(pairs) == 0: return [] components = graph_clustering_dynamic_th(pairs, scores, max_sz, th_step) return components
def test_lgcn(model, cfg, logger): for k, v in cfg.model['kwargs'].items(): setattr(cfg.test_data, k, v) dataset = build_dataset(cfg.test_data) ofn_pred = os.path.join(cfg.work_dir, 'pred_edges_scores.npz') if os.path.isfile(ofn_pred) and not cfg.force: data = np.load(ofn_pred) edges = data['edges'] scores = data['scores'] inst_num = data['inst_num'] if inst_num != len(dataset): logger.warn( 'instance number in {} is different from dataset: {} vs {}'. format(ofn_pred, inst_num, len(dataset))) else: edges, scores, inst_num = test(model, dataset, cfg, logger) # produce predicted labels clusters = graph_clustering_dynamic_th(edges, scores, max_sz=cfg.max_sz, step=cfg.step, pool=cfg.pool) pred_idx2lb = clusters2labels(clusters) pred_labels = intdict2ndarray(pred_idx2lb) if cfg.save_output: print('save predicted edges and scores to {}'.format(ofn_pred)) np.savez_compressed(ofn_pred, edges=edges, scores=scores, inst_num=inst_num) ofn_meta = os.path.join(cfg.work_dir, 'pred_labels.txt') write_meta(ofn_meta, pred_idx2lb, inst_num=inst_num) # evaluation if not dataset.ignore_label: print('==> evaluation') gt_labels = dataset.labels for metric in cfg.metrics: evaluate(gt_labels, pred_labels, metric) single_cluster_idxs = get_cluster_idxs(clusters, size=1) print('==> evaluation (removing {} single clusters)'.format( len(single_cluster_idxs))) remain_idxs = np.setdiff1d(np.arange(len(dataset)), np.array(single_cluster_idxs)) remain_idxs = np.array(remain_idxs) for metric in cfg.metrics: evaluate(gt_labels[remain_idxs], pred_labels[remain_idxs], metric)
def super_vertex(knn, k, th, th_step, max_sz): pairs, scores = filter_knn(knn, k, th) comps = graph_clustering_dynamic_th(pairs, scores, max_sz, th_step) clusters = [sorted([n.name for n in c]) for c in comps] return clusters
def super_vertex(knns, k, th, th_step, max_sz): pairs, scores = filter_knns(knns, k, th) components = graph_clustering_dynamic_th(pairs, scores, max_sz, th_step) return components