def build_graph(self, fn_node, fn_edge):
        ''' build graph from graph file
            - nodes: NxD,
                     each row represents the feature of a node
            - adj:   NxN,
                     a symmetric similarity matrix with self-connection
        '''
        node = load_data(fn_node)
        edge = load_data(fn_edge)
        assert len(node) > 1, '#node of {}: {}'.format(fn_node, len(node))
        # take majority as label of the graph
        if not self.dataset.ignore_label:
            lb2cnt = {}
            for idx in node:
                if idx not in self.dataset.idx2lb:
                    continue
                lb = self.dataset.idx2lb[idx]
                if lb not in lb2cnt:
                    lb2cnt[lb] = 0
                lb2cnt[lb] += 1
            gt_lb, _ = get_majority(lb2cnt)
            gt_node = self.dataset.lb2idxs[gt_lb]
            if self.dataset.det_label == 'iou':
                label = compute_iou(node, gt_node)
            elif self.dataset.det_label == 'iop':
                label = compute_iop(node, gt_node)
            else:
                raise KeyError('Unknown det_label type: {}'.format(
                    self.dataset.det_label))
        else:
            label = -1.

        adj, _, _ = self.build_adj(node, edge)
        features = self.build_features(node)
        return features, adj, label
 def build_graph(self, fn_node, fn_edge):
     """ build graph from graph file
         - nodes: NxD,
                  each row represents the feature of a node
         - adj:   NxN,
                  a symmetric similarity matrix with self-connection
     """
     node = load_data(fn_node)
     edge = load_data(fn_edge)
     assert len(node) > 1, '#node of {}: {}'.format(fn_node, len(node))
     # take majority as label of the graph
     if not self.dataset.ignore_label:
         lb2cnt = {}
         for idx in node:
             if idx not in self.dataset.idx2lb:
                 continue
             lb = self.dataset.idx2lb[idx]
             if lb not in lb2cnt:
                 lb2cnt[lb] = 0
             lb2cnt[lb] += 1
         gt_lb, _ = get_majority(lb2cnt)
         gt_node = self.dataset.lb2idxs[gt_lb]
         iou = compute_iou(node, gt_node)
     else:
         iou = -1.
     # compute adj
     node = list(node)
     abs2rel = {}
     for i, n in enumerate(node):
         abs2rel[n] = i
     size = len(node)
     adj = np.eye(size)
     for e in edge:
         w = 1.
         if len(e) == 2:
             e1, e2 = e
         elif len(e) == 3:
             e1, e2, dist = e
             if not self.dataset.wo_weight:
                 w = 1. - dist
         else:
             raise ValueError('Unknown length of e: {}'.format(e))
         v1 = abs2rel[e1]
         v2 = abs2rel[e2]
         adj[v1][v2] = w
         adj[v2][v1] = w
     if self.dataset.featureless:
         vertices = adj.sum(axis=1, keepdims=True)
         vertices /= vertices.sum(axis=1, keepdims=True)
     else:
         vertices = self.dataset.features[node, :]
     if self.dataset.is_norm_adj:
         adj /= adj.sum(axis=1, keepdims=True)
     return vertices, adj, iou
Esempio n. 3
0
def nms(clusters, th=1.):
    # nms
    t0 = time.time()
    suppressed = set()
    if th < 1:
        start_idx = 0
        tot_size = len(clusters)
        while start_idx < tot_size:
            if start_idx in suppressed:
                start_idx += 1
                continue
            cluster = clusters[start_idx]
            for j in range(start_idx + 1, tot_size):
                if j in suppressed:
                    continue
                if compute_iou(cluster, clusters[j]) > th:
                    suppressed.add(j)
            start_idx += 1
    else:
        print('th={} >= 1, skip the nms'.format(th))
    print('nms consumes {} s'.format(time.time() - t0))

    # assign label
    lb = 0
    idx2lbs = {}
    for i, cluster in enumerate(clusters):
        if i in suppressed:
            continue
        for v in cluster:
            if v not in idx2lbs:
                idx2lbs[v] = []
            idx2lbs[v].append(lb)
        lb += 1

    # deoverlap (choose the one belongs to the highest predicted iou)
    idx2lb = {}
    for idx, lbs in idx2lbs.items():
        idx2lb[idx] = lbs[0]

    return idx2lb, idx2lbs
        cluster = set(cluster)
        # take majority as label of the graph
        for idx in cluster:
            if idx not in idx2lb:
                print('[warn] {} is not found'.format(idx))
                continue
            lb = idx2lb[idx]
            if lb not in lb2cnt:
                lb2cnt[lb] = 0
            lb2cnt[lb] += 1
        lb, _ = get_majority(lb2cnt)
        if lb is None:
            iou = -1e6
        else:
            idxs = lb2idxs[lb]
            iou = compute_iou(cluster, idxs)
        ious.append(iou)
    ious = np.array(ious)

    # rank by iou
    pos_g_labels = np.where(ious > args.th_pos)[0]
    clusters = [[clusters[i], ious[i]] for i in pos_g_labels]
    clusters = sorted(clusters, key=lambda x: x[1], reverse=True)
    clusters = [n for n, _ in clusters]

    inst_num = len(idx2lb)
    pos_idx_set = set()
    for c in clusters:
        pos_idx_set |= set(c)
    print('inst-coverage before nms: {}'.format(1. * len(pos_idx_set) /
                                                inst_num))