Ejemplo n.º 1
0
def get_permute(W_r, H_r, W, H, coeff):
    T_0 = W_r.shape[1]
    T = W.shape[1]
    comp = zeros((T_0, T))
    for t in xrange(T):
        p = sqrt(W_r) - sqrt(tile(W[:, t], (T_0, 1))).T
        comp[:, t] = sum(p ** 2, 0)
    if T < T_0:
        t = array(linear_assignment(comp.T))[:, [1, 0]]
        # http://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column
        return t[t[:, 0].argsort()]
    else:
        return array(linear_assignment(comp))
def best_map(l1, l2):
    """
    Permute labels of l2 to match l1 as much as possible
    """
    if len(l1) != len(l2):
        print "L1.shape must == L2.shape"
        exit(0)

    label1 = np.unique(l1)
    n_class1 = len(label1)

    label2 = np.unique(l2)
    n_class2 = len(label2)

    n_class = max(n_class1, n_class2)
    G = np.zeros((n_class, n_class))

    for i in range(0, n_class1):
        for j in range(0, n_class2):
            ss = l1 == label1[i]
            tt = l2 == label2[j]
            G[i, j] = np.count_nonzero(ss & tt)

    A = la.linear_assignment(-G)

    new_l2 = np.zeros(l2.shape)
    for i in range(0, n_class2):
        new_l2[l2 == label2[A[i][1]]] = label1[A[i][0]]
    return new_l2.astype(int)
Ejemplo n.º 3
0
 def syntax_similarity_conversation(self, documents1, average=False): #syntax similarity of each document with its before and after document
     global numnodes
     documents1parsed = []
     for d1 in range(len(documents1)):
         sys.stderr.write(str(d1)+"\n")
         # print documents1[d1]
         tempsents = (self.sent_detector.tokenize(documents1[d1].strip()))
         for s in tempsents:
             if len(s.split())>100:
                 documents1parsed.append("NA")
                 break
         else:
             temp = list(self.parser.raw_parse_sents((tempsents)))
             for i in range(len(temp)):
                 temp[i] = list(temp[i])[0]
                 temp[i] = ParentedTree.convert(temp[i])
             documents1parsed.append(list(temp))
     results = OrderedDict()
     for d1 in range(len(documents1parsed)):
         d2 = d1+1
         if d2 == len(documents1parsed):
             break
         if documents1parsed[d1] == "NA" or documents1parsed[d2]=="NA":
             continue
         costMatrix = []
         for i in range(len(documents1parsed[d1])):
             numnodes = 0
             tempnode = Node(documents1parsed[d1][i].root().label())
             new_sentencedoc1 = self.convert_mytree(documents1parsed[d1][i],tempnode)
             temp_costMatrix = []
             sen1nodes = numnodes
             for j in range(len(documents1parsed[d2])):
                 numnodes=0.0
                 tempnode = Node(documents1parsed[d2][j].root().label())
                 new_sentencedoc2 = self.convert_mytree(documents1parsed[d2][j],tempnode)
                 ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
                 ED = ED / (numnodes + sen1nodes)
                 temp_costMatrix.append(ED)
             costMatrix.append(temp_costMatrix)
         costMatrix = np.array(costMatrix)
         if average==True:
             return 1-np.mean(costMatrix)
         else:
             indexes = su.linear_assignment(costMatrix)
             total = 0
             rowMarked = [0] * len(documents1parsed[d1])
             colMarked = [0] * len(documents1parsed[d2])
             for row, column in indexes:
                 total += costMatrix[row][column]
                 rowMarked[row] = 1
                 colMarked [column] = 1
             for k in range(len(rowMarked)):
                 if rowMarked[k]==0:
                     total+= np.min(costMatrix[k])
             for c in range(len(colMarked)):
                 if colMarked[c]==0:
                     total+= np.min(costMatrix[:,c])
             maxlengraph = max(len(documents1parsed[d1]),len(documents1parsed[d2]))
             results[(d1,d2)] = 1-total/maxlengraph#, minWeight/minlengraph, randtotal/lengraph
     return results
Ejemplo n.º 4
0
def get_indexList(coord, coord_prev, indexListPrev, totWorms, max_allow_dist = 10.0):
    #get the indexes of the next worms from their nearest neightbors using the hungarian algorithm
    
    if coord_prev.size!=0:
        costMatrix = cdist(coord_prev, coord);
        assigment = linear_assignment(costMatrix)
        
        indexList = np.zeros(coord.shape[0]);
        speed = np.zeros(coord.shape[0])

        for row, column in assigment: #ll = 1:numel(indexList)
            if costMatrix[row,column] < max_allow_dist:
                indexList[column] = indexListPrev[row];
                speed[column] = costMatrix[row][column];
            elif column < coord.shape[0]:
                totWorms = totWorms +1;
                indexList[column] = totWorms;
        
        for rep_ind in list_duplicates(indexList):
            totWorms = totWorms +1; #assign new worm_index to joined trajectories
            indexList[rep_ind] = totWorms;
        
    else:
        indexList = totWorms + np.arange(1,coord.shape[0]+1);
        totWorms = indexList[-1]
        speed = totWorms*[None]
        #print costMatrix[-1,-1]
    return (totWorms, indexList, speed)
Ejemplo n.º 5
0
def assignment(costMatrix, costOfNonAssignment=140):
    Assignment = namedtuple('Assignment', 'trackIndex detectionIndex')
    assignments = []
    unmatchedTracks = []
    unmatchedDetections = []

    # print(costMatrix)

    # If matrix is rectangular, then pad
    rows, cols = costMatrix.shape
    diff = rows - cols
    if diff != 0:
        padValue = costOfNonAssignment + 1
        if diff < 0:
            pad_width = [(0, np.abs(diff)), (0, 0)]
        if diff > 0:
            pad_width = [(0, 0), (0, diff)]
        costMatrix = np.pad(costMatrix, pad_width, mode='constant',
            constant_values=(padValue, padValue))
    # Compute the optimal assignment
    assign = linear_assignment(costMatrix)

    # Throw out any assignments that cost more than the costOfNonAssingment
    for row in assign:
        trackIndex = row[0]
        detectionIndex =  row[1]
        if costMatrix[trackIndex, detectionIndex] > costOfNonAssignment:
            if trackIndex < rows:
                unmatchedTracks.append(trackIndex)
            if detectionIndex < cols:
                unmatchedDetections.append(detectionIndex)
        else:
            assignments.append(Assignment(trackIndex, detectionIndex))

    return assignments, unmatchedTracks, unmatchedDetections
    def track(self, original_img, filtered_img, prev_data):
        n_objects = self.nObjectsSpinBox.value()
        distance_threshold = self.distanceThresholdSpinBox.value()

        if self.k_means is None:
            self.k_means = cluster.KMeans(n_clusters=n_objects)

        non_zero_pos = np.transpose(np.nonzero(filtered_img.T))
        try:
            center_pos = self.k_means.fit(non_zero_pos).cluster_centers_
        except:
            if self.ret_pos_old is None:
                return {'position': np.full((n_objects, 2), np.nan)}
            else:
                return {'position': self.ret_pos_old}

        if self.ret_pos_old is None:
            self.ret_pos_old = center_pos.copy()
            self.ret_pos = center_pos
        else:
            ret_pos_old_repeated = np.repeat(self.ret_pos_old, n_objects, axis=0)
            center_pos_tiled = np.tile(center_pos, (n_objects, 1))
            cost_mtx = np.linalg.norm(ret_pos_old_repeated - center_pos_tiled, axis=1)
            cost_mtx = cost_mtx.reshape((n_objects, n_objects))

            idx = linear_assignment(cost_mtx)
            idx = idx[cost_mtx[idx[:,0], idx[:,1]]<=distance_threshold]

            self.ret_pos[:] = self.ret_pos_old[:]
            self.ret_pos[idx[:, 0], :] = center_pos[idx[:, 1], :]

            self.ret_pos_old[:] = self.ret_pos[:].copy()

        return {'position': self.ret_pos}
Ejemplo n.º 7
0
 def syntax_similarity_two_documents(self, doc1, doc2, average=False): #syntax similarity of two single documents
     global numnodes
     doc1sents = self.sent_detector.tokenize(doc1.strip())
     doc2sents = self.sent_detector.tokenize(doc2.strip())
     for s in doc1sents: # to handle unusual long sentences.
         if len(s.split())>100:
             return "NA"
     for s in doc2sents:
         if len(s.split())>100:
             return "NA"
     try: #to handle parse errors. Parser errors might happen in cases where there is an unsuall long word in the sentence.
         doc1parsed = self.parser.raw_parse_sents((doc1sents))
         doc2parsed = self.parser.raw_parse_sents((doc2sents))
     except Exception as e:
         sys.stderr.write(str(e))
         return "NA"
     costMatrix = []
     doc1parsed = list(doc1parsed)
     for i in range(len(doc1parsed)):
         doc1parsed[i] = list(doc1parsed[i])[0]
     doc2parsed = list(doc2parsed)
     for i in range(len(doc2parsed)):
         doc2parsed[i] = list(doc2parsed[i])[0]
     for i in range(len(doc1parsed)):
         numnodes = 0
         sentencedoc1 = ParentedTree.convert(doc1parsed[i])
         tempnode = Node(sentencedoc1.root().label())
         new_sentencedoc1 = self.convert_mytree(sentencedoc1,tempnode)
         temp_costMatrix = []
         sen1nodes = numnodes
         for j in range(len(doc2parsed)):
             numnodes=0.0
             sentencedoc2 = ParentedTree.convert(doc2parsed[j])
             tempnode = Node(sentencedoc2.root().label())
             new_sentencedoc2 = self.convert_mytree(sentencedoc2,tempnode)
             ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
             ED = ED / (numnodes + sen1nodes)
             temp_costMatrix.append(ED)
         costMatrix.append(temp_costMatrix)
     costMatrix = np.array(costMatrix)
     if average==True:
         return 1-np.mean(costMatrix)
     else:
         indexes = su.linear_assignment(costMatrix)
         total = 0
         rowMarked = [0] * len(doc1parsed)
         colMarked = [0] * len(doc2parsed)
         for row, column in indexes:
             total += costMatrix[row][column]
             rowMarked[row] = 1
             colMarked [column] = 1
         for k in range(len(rowMarked)):
             if rowMarked[k]==0:
                 total+= np.min(costMatrix[k])
         for c in range(len(colMarked)):
             if colMarked[c]==0:
                 total+= np.min(costMatrix[:,c])
         maxlengraph = max(len(doc1parsed),len(doc2parsed))
         return 1-(total/maxlengraph)
Ejemplo n.º 8
0
 def get_deci_map(self, state_map, w, dnum, tnum):
     score_mat = state_map.dot(w)
     match_idxs = linear_assignment(-score_mat)
     deci_map = np.zeros_like(score_mat)
     for m in match_idxs:
         if m[0]<dnum or m[1]<tnum:
             deci_map[m[0],m[1]] = 1
     return match_idxs, deci_map
Ejemplo n.º 9
0
def accuracy(l,lg):
    profitMatrix = confusion_matrix(lg,l)
    costMatrix = np.iinfo(np.int64).max - profitMatrix
    ind = linear_assignment(costMatrix)
    total = 0.0
    for i in ind:
        total += profitMatrix[tuple(i)]
    return total / lg.shape[0]
Ejemplo n.º 10
0
    def calcAssignMtx(self):
        idx = linear_assignment(self.costMtx)

        if self.assignMtx is None:
            self.assignMtx = np.zeros((self.N, self.M))
        else:
            self.assignMtx[:] = 0
        self.assignMtx[idx[:, 0], idx[:,1]] = 1
Ejemplo n.º 11
0
def accuracy(l,lg):
    profitMatrix = confusion_matrix(lg, l)
    costMatrix = lg.shape[0] - profitMatrix
    ind = linear_assignment(costMatrix)
    total = 0.0
    for i in ind:
        total += profitMatrix[i[0],i[1]]
    return total / lg.shape[0]
Ejemplo n.º 12
0
def ceafe(clusters, gold_clusters):
    clusters = [c for c in clusters if len(c) != 1]
    scores = np.zeros((len(gold_clusters), len(clusters)))
    for i in range(len(gold_clusters)):
        for j in range(len(clusters)):
            scores[i, j] = phi4(gold_clusters[i], clusters[j])
    matching = linear_assignment(-scores)
    similarity = sum(scores[matching[:, 0], matching[:, 1]])
    return similarity, len(clusters), similarity, len(gold_clusters)
Ejemplo n.º 13
0
def cluster_acc(Y_pred, Y):
  from sklearn.utils.linear_assignment_ import linear_assignment
  assert Y_pred.size == Y.size
  D = max(Y_pred.max(), Y.max())+1
  w = np.zeros((D,D), dtype=np.int64)
  for i in range(Y_pred.size):
    w[Y_pred[i], Y[i]] += 1
  ind = linear_assignment(w.max() - w)
  return sum([w[i,j] for i,j in ind])*1.0/Y_pred.size, w
Ejemplo n.º 14
0
 def match_detections(self, old_dets, new_dets, iou_threshold):
     if len(old_dets) == 0 or len(new_dets) == 0:
         return []
     iou_cost = np.array(
         [[iou(old, new) for new in new_dets] for old in old_dets],
         'float32'
     )
     match_pairs = linear_assignment(-iou_cost)
     return match_pairs
Ejemplo n.º 15
0
def linear_assignment(df):
    """Wrapper of sklearn linear assignment algorithm for DataFrame cost matrix. Returns
    DataFrame with columns for matched labels. Minimizes cost.
    """
    from sklearn.utils.linear_assignment_ import linear_assignment

    x = linear_assignment(df.as_matrix())
    y = zip(df.index[x[:, 0]], df.columns[x[:, 1]])
    df_out = pd.DataFrame(y, columns=[df.index.name, df.columns.name])
    return df_out
Ejemplo n.º 16
0
def solve_matching(num_cluster, old_cluster, new_cluster):
  """Solves the hungarian matching based on the non-overlapping words."""
  cost_matrix = np.array([[.0]*num_cluster]*num_cluster)
  # cost_matrix = cost_matrix.astype(np.float32)
  for i in old_cluster:
    for j in new_cluster:
      cost_matrix[i][j] = count_non_overlapping(old_cluster[i], new_cluster[j])
  # import ipdb; ipdb.set_trace()
  assignments = hungarian.linear_assignment(cost_matrix)
  mapping = {}
  for i in xrange(assignments.shape[0]):
    # mapping[i] = assignments[i, 1]
    mapping[assignments[i, 1]] = i
  return mapping
Ejemplo n.º 17
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
    """
    Assigns detections to tracked object (both represented as bounding boxes)
    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    if (len(trackers) == 0):
        return np.empty((0, 2), dtype=int), np.arange(len(detections)), \
               np.empty((0, 5), dtype=int)
    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)
    matched_indices = linear_assignment(-iou_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # filter out matched with low IOU
    matches = []
    for m in matched_indices:
        is_matched = (iou_matrix[m[0], m[1]] < iou_threshold)

        # @mhsung
        if (detections.shape[1] >= 6 and trackers.shape[1] >= 6):
            # det: [x0, y0, x1, y2, score, class_index, ...]
            # If class indices are given, bboxes with the same class index are
            # only matched.
            det_cls = detections[m[0], 5]
            trk_cls = trackers[m[0], 5]
            is_matched = is_matched and (det_cls == trk_cls)

        if is_matched:
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
Ejemplo n.º 18
0
def associate_detections_to_trackers(
        detections, trackers, distance_threshold=0.3):
    """
    Assigns detections to tracked object (both represented as bounding boxes)

    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    if(len(trackers) == 0):
        return np.empty((0, 2), dtype=int), np.arange(
            len(detections)), np.empty((0, 6), dtype=int)
    distance_matrix = np.zeros(
        (len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            distance_matrix[d, t] = distance(det, trk)
            print('distance of new det:{} to tracker {} = {}'.format(
                d, t, distance_matrix[d, t]))

    # warnings.warn(str(distance_matrix))
    # warnings.warn('tracking')

    matched_indices = linear_assignment(-distance_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if(d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if(t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # filter out matched with low distance
    matches = []
    for m in matched_indices:
        if(distance_matrix[m[0], m[1]] < distance_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if(len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(
        unmatched_detections), np.array(unmatched_trackers)
Ejemplo n.º 19
0
    def __init__(self, boxes1, boxes2, labels1=None, labels2=None):
        self._boxes1 = boxes1
        self._boxes2 = boxes2

        if len(boxes1) == 0 or len(boxes2) == 0:
            pass
        else:
            
            if labels1 is None or labels2 is None:
                self._iou_matrix = self._calc(boxes1,
                                              boxes2,
                                              np.ones((len(boxes1),)),
                                              np.ones((len(boxes2),)))
            else:
                self._iou_matrix = self._calc(boxes1, boxes2, labels1, labels2)
            self._match_pairs = linear_assignment(-1*self._iou_matrix)
Ejemplo n.º 20
0
    def ceafe(clusters, gold_clusters):
        """
        Computes the  Constrained EntityAlignment F-Measure (CEAF) for evaluating coreference.
        Gold and predicted mentions are aligned into clusterings which maximise a metric - in
        this case, the F measure between gold and predicted clusters.

        <https://www.semanticscholar.org/paper/On-Coreference-Resolution-Performance-Metrics-Luo/de133c1f22d0dfe12539e25dda70f28672459b99>
        """
        clusters = [cluster for cluster in clusters if len(cluster) != 1]
        scores = np.zeros((len(gold_clusters), len(clusters)))
        for i, gold_cluster in enumerate(gold_clusters):
            for j, cluster in enumerate(clusters):
                scores[i, j] = Scorer.phi4(gold_cluster, cluster)
        matching = linear_assignment(-scores)
        similarity = sum(scores[matching[:, 0], matching[:, 1]])
        return similarity, len(clusters), similarity, len(gold_clusters)
Ejemplo n.º 21
0
def get_optimal_permutation(a, b, k):
    """Finds an optimal permutation `p` of the elements in b to match a,
    using the Hungarian algorithm, such that `a ~ p(b)`. `k` specifies the
    number of possible labels, in case they're not all present in `a` or `b`.
    """
    assert(a.shape == b.shape)
    # if we're testing against ground truth, then we need to store a bigger
    # matrix
    n = max(len(np.unique(a)), len(np.unique(b)), k)
    w = np.zeros((n, n), dtype=int)
    for i, j in zip(a, b):
        w[i, j] += 1
    # make it a cost matrix
    w = np.max(w) - w
    # minimize the cost -- transpose because we want a map from `b` to `a`
    permutation = linear_assignment(w.T)
    return permutation[:, 1]
def cluster_acc(y_true, y_pred):
    '''
    Uses the hungarian algorithm to find the best permutation mapping and then calculates the accuracy wrt
    Implementation inpired from https://github.com/piiswrong/dec, since scikit does not implement this metric
    this mapping and true labels
    :param y_true: True cluster labels
    :param y_pred: Predicted cluster labels
    :return: accuracy score for the clustering
    '''
    D = int(max(y_pred.max(), y_true.max()) + 1)
    w = np.zeros((D, D), dtype=np.int32)
    for i in range(y_pred.size):
        idx1 = int(y_pred[i])
        idx2 = int(y_true[i])
        w[idx1, idx2] += 1
    ind = linear_assignment(w.max() - w)
    return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
Ejemplo n.º 23
0
    def extract_gt_decision(self,gt,dets,img,appl=True,thres=0.05):
        dnum, tnum = len(dets), len(self.targetlst)
        deci_map = np.zeros((dnum+2*tnum,tnum+2*dnum))
        gtnum = len(gt)
        det_gt_mat = np.empty((dnum,gtnum))
        for i in range(dnum):
            for j in range(gtnum):
                det_gt_mat[i,j] = iou(dets[i],gt[j][2:6])
        det_gt_matches = linear_assignment(-det_gt_mat)
        det_gt_matches = [m for m in det_gt_matches if det_gt_mat[m[0],m[1]]>thres]
        gt_taridx_lst = gt[:,1].tolist()
        gt_idxs, det_idxs = range(gtnum), range(dnum)
        ### match or initialize
        curlst = [t.idx for t in self.targetlst]
        for m in det_gt_matches:
            #x1,y1,x2,y2 = gt[m[1],2:6]
            x1,y1,x2,y2 = dets[m[0],:4]
            x1,y1,x2,y2 = int(x1), int(y1),int(x2),int(y2)
            if gt[m[1],1] in curlst:  ### match
                tidx = curlst.index(gt[m[1],1])
                deci_map[m[0],tidx] = 1
                if appl: self.targetlst[tidx].update([x1,y1,x2,y2], img[y1:y2,x1:x2])
            else:   ### initialize
                targetidx = gt[m[1],1]
                target = TrackedTarget(targetidx, [x1,y1,x2,y2], patch=img[y1:y2,x1:x2])
                if appl: self.targetlst.append(target)
                deci_map[m[0],tnum+dnum+m[0]] = 1
        matched_det_lst = [m[0] for m in det_gt_matches]
        for i in range(dnum):
            if i not in matched_det_lst: ### drop
                deci_map[i,tnum+i] = 1

        matched_gt_lst = [m[1] for m in det_gt_matches]
        for j,gtidx in enumerate(gt_taridx_lst):
            if j not in matched_gt_lst: ### miss
                if gtidx in curlst:
                    i = curlst.index(gtidx)
                    deci_map[dnum+i,i] = 1
                #if not in curlst: forget it, initialize it next frame

        for i,tidx in enumerate(curlst):
            if tidx not in gt_taridx_lst:
                self.targetlst[i].state = 'gone'
                deci_map[dnum+tnum+i,i] = 1
        if appl: self.targetlst = [target for target in self.targetlst if target.state!='gone']
        return deci_map
Ejemplo n.º 24
0
def associate_detections_to_trackers(detections,trackers,iou_threshold = 0.3):
    """
    Assigns detections to tracked object (both represented as bounding boxes)
    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    if(len(trackers)==0):
        return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,5),dtype=int)

    iou_matrix = np.zeros((len(detections),len(trackers)),dtype=np.float32)
    id_matrix = np.zeros((len(detections),len(trackers)),dtype=np.float32)
    scale_id = 0.5

    for d,det in enumerate(detections):
        for t,trk in enumerate(trackers):
            trackBox = convert_kfx_to_bbox(trk.kf.x[:4])[0]
            iou_matrix[d,t] = bbox_iou(trackBox, det)
            id_matrix[d,t] = scale_id*det[4]

    matched_indices = linear_assignment(-iou_matrix-id_matrix)

    unmatched_detections = []
    for d,det in enumerate(detections):
        if(d not in matched_indices[:,0]):
            unmatched_detections.append(d)

    unmatched_trackers = []
    for t,trk in enumerate(trackers):
        if(t not in matched_indices[:,1]):
            unmatched_trackers.append(t)

    #filter out matched with low probability
    matches = []
    for m in matched_indices:
        if(iou_matrix[m[0],m[1]]<iou_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1,2))

    if(len(matches)==0):
        matches = np.empty((0,2),dtype=int)
    else:
        matches = np.concatenate(matches,axis=0)

    return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
Ejemplo n.º 25
0
def mean_recall(gt_groups, groups):
    """
    Compute the mean recall.

    :param gt_groups: set of ground truth groups
    :param groups: set of tested groups
    :return: mean recall
    """
    if not groups:
        return 0, 0
    conf = confusion_matrix(gt_groups, groups)
    idx = hungarian.linear_assignment(conf.max() - conf)

    matches = np.array([conf[i[0], i[1]] for i in idx])
    gt_sizes = np.array([size(gt_groups[i[0]]) for i in idx])

    recall = matches / gt_sizes
    return np.sum(recall) / len(gt_groups)
Ejemplo n.º 26
0
def k_means_algo(data_no_label, label_values, k):
    try:
        data_no_label = data_no_label.transpose()
        k_means = KMeans(n_clusters=k)
        k_means.fit(data_no_label)
        labels_from_kmeans = k_means.labels_
        print('K means labels', labels_from_kmeans)
        C = confusion_matrix(y_true=label_values, y_pred=labels_from_kmeans)
        print('Confusion matrix is: ', C)
        C = C.T
        ind = linear_assignment(-C)
        C_opt = C[:, ind[:, 1]]
        print('re ordered matrix', C_opt)
        acc_opt = np.trace(C_opt) / np.sum(C_opt)
        accuracy = cluster_acc(label_values, labels_from_kmeans)
        print('accuracy of k means is:', accuracy * 100)
    except Exception as e:
        print(e)
Ejemplo n.º 27
0
def allignCLus2(cl1, cl2):
    y_true, y_pred = cl1, cl2
    Y_pred = y_pred
    Y = y_true
    from sklearn.utils.linear_assignment_ import linear_assignment
    assert Y_pred.size == Y.size
    D = max(Y_pred.max(), Y.max()) + 1
    w = np.zeros((D, D), dtype=np.int64)
    for i in range(Y_pred.size):
        w[Y_pred[i], Y[i]] += 1
        ind = linear_assignment(w.max() - w)
    DD = {}
    for i, j in ind:
        DD[j] = i
    newCl = []
    for i in cl1:
        newCl.append(DD[i])
    return np.array(newCl)
Ejemplo n.º 28
0
    def evaluate_clusters(self, gold_clusters, auto_clusters):
        def phi4(c1, c2):
            return 2 * len([m for m in c1 if m in c2
                            ]) / float(len(c1) + len(c2))

        # enable ceaf to deal with singletons
        # auto_clusters = [c for c in auto_clusters if len(c) != 1]
        scores = np.zeros((len(gold_clusters), len(auto_clusters)))
        for i in range(len(gold_clusters)):
            for j in range(len(auto_clusters)):
                scores[i, j] = phi4(gold_clusters[i], auto_clusters[j])
        matching = linear_assignment(-scores)
        similarity = float(sum(scores[matching[:, 0], matching[:, 1]]))

        p = similarity / len(auto_clusters) if similarity else 0.0
        r = similarity / len(gold_clusters) if similarity else 0.0

        return p, r, self.f1_score(p, r)
Ejemplo n.º 29
0
def cluster_acc(y_true, y_pred):
    """
    Calculate clustering accuracy. Require scikit-learn installed
    # Arguments
        y: true labels, numpy.array with shape `(n_samples,)`
        y_pred: predicted labels, numpy.array with shape `(n_samples,)`
    # Return
        accuracy, in [0,1]
    """
    y_true = y_true.astype(np.int64)
    assert y_pred.size == y_true.size
    D = max(y_pred.max(), y_true.max()) + 1
    w = np.zeros((D, D), dtype=np.int64)
    for i in range(y_pred.size):
        w[y_pred[i], y_true[i]] += 1
    from sklearn.utils.linear_assignment_ import linear_assignment
    ind = linear_assignment(w.max() - w)
    return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
Ejemplo n.º 30
0
    def match_detections_to_trackers(trackers, detections, min_iou=0.25):

        IOU_mat = np.zeros((len(trackers), len(detections)), dtype=np.float32)
        for t, trk in enumerate(trackers):
            #trk = convert_to_cv2bbox(trk)
            for d, det in enumerate(detections):
                #   det = convert_to_cv2bbox(det)
                IOU_mat[t, d] = src.helpers.box_iou2(trk, det)

        # Produces matches
        # Solve the maximizing the sum of IOU assignment problem using the
        # Hungarian algorithm (also known as Munkres algorithm)

        matched_idx = linear_assignment(-IOU_mat)

        unmatched_trackers, unmatched_detections = [], []
        for t, trk in enumerate(trackers):
            if (t not in matched_idx[:, 0]):
                unmatched_trackers.append(t)

        for d, det in enumerate(detections):
            if (d not in matched_idx[:, 1]):
                unmatched_detections.append(d)

        matches = []

        # For creating trackers we consider any detection with an
        # overlap less than min_iou to signifiy the existence of
        # an untracked object

        for m in matched_idx:
            if (IOU_mat[m[0], m[1]] < min_iou):
                unmatched_trackers.append(m[0])
                unmatched_detections.append(m[1])
            else:
                matches.append(m.reshape(1, 2))

        if (len(matches) == 0):
            matches = np.empty((0, 2), dtype=int)
        else:
            matches = np.concatenate(matches, axis=0)

        return matches, np.array(unmatched_detections), np.array(
            unmatched_trackers)
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.25):
    """
    Assigns detections to tracked object (both represented as bounding boxes)

    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    if (len(trackers) == 0):
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5),
                                                                     dtype=int)
    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)
    '''The linear assignment module tries to minimise the total assignment cost.
    In our case we pass -iou_matrix as we want to maximise the total IOU between track predictions and the frame detection.'''
    print(iou_matrix)
    matched_indices = linear_assignment(-iou_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # filter out matched with low IOU
    matches = []
    for m in matched_indices:
        if (iou_matrix[m[0], m[1]] < iou_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 32
0
    def match_detections_to_trackers(trackers, detections, min_iou=0.25):
        # Initialize 'iou_matrix'
        iou_matrix = np.zeros((len(trackers), len(detections)),
                              dtype=np.float32)

        # Populate 'iou_matrix'
        for t, tracker in enumerate(trackers):
            for d, detection in enumerate(detections):
                iou_matrix[t, d] = box_iou_ratio(tracker, detection)

        # Produce matches by using the Hungarian algorithm to maximize the sum of IOU
        matched_index = linear_assignment(-iou_matrix)

        # Populate 'unmatched_trackers'
        unmatched_trackers = []
        for t in np.arange(len(trackers)):
            if t not in matched_index[:, 0]:
                unmatched_trackers.append(t)

        # Populate 'unmatched_detections'
        unmatched_detections = []
        for d in np.arange(len(detections)):
            if d not in matched_index[:, 1]:
                unmatched_detections.append(d)

        # Populate 'matches'
        matches = []
        for m in matched_index:
            # Create tracker if IOU is greater than 'min_iou'
            if iou_matrix[m[0], m[1]] > min_iou:
                matches.append(m.reshape(1, 2))
            else:
                unmatched_trackers.append(m[0])
                unmatched_detections.append(m[1])

        if matches:
            # Concatenate arrays on the same axis
            matches = np.concatenate(matches, axis=0)
        else:
            matches = np.empty((0, 2), dtype=int)

        # Return matches, unmatched detection and unmatched trackers
        return matches, np.array(unmatched_detections), np.array(
            unmatched_trackers)
Ejemplo n.º 33
0
def associate_detections_to_trackers(detections,trackers,iou_threshold=0.1):
# def associate_detections_to_trackers(detections,trackers,iou_threshold=0.01):     # ablation study
# def associate_detections_to_trackers(detections,trackers,iou_threshold=0.25):
  """
  Assigns detections to tracked object (both represented as bounding boxes)

  detections:  N x 8 x 3
  trackers:    M x 8 x 3

  Returns 3 lists of matches, unmatched_detections and unmatched_trackers
  """
  if(len(trackers)==0):
    return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,8,3),dtype=int)    
  iou_matrix = np.zeros((len(detections),len(trackers)),dtype=np.float32)

  for d,det in enumerate(detections):
    for t,trk in enumerate(trackers):
      iou_matrix[d,t] = iou3d(det,trk)[0]             # det: 8 x 3, trk: 8 x 3
  # print(iou_matrix)
  matched_indices = linear_assignment(-iou_matrix)      # hougarian algorithm

  unmatched_detections = []
  for d,det in enumerate(detections):
    if(d not in matched_indices[:,0]):
      unmatched_detections.append(d)
  unmatched_trackers = []
  for t,trk in enumerate(trackers):
    if(t not in matched_indices[:,1]):
      unmatched_trackers.append(t)

  #filter out matched with low IOU
  matches = []
  for m in matched_indices:
    if(iou_matrix[m[0],m[1]]<iou_threshold):
      unmatched_detections.append(m[0])
      unmatched_trackers.append(m[1])
    else:
      matches.append(m.reshape(1,2))
  if(len(matches)==0):
    matches = np.empty((0,2),dtype=int)
  else:
    matches = np.concatenate(matches,axis=0)

  return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
Ejemplo n.º 34
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
    """Assigns detections to tracked object with 
        
        Apply Hungarian algorithm by linear_assignment from sklearn
        Returns (matches, unmatched_detections, unmatched_tackers)
    """
    if len(trackers) == 0:
        return (np.empty((0, 2), dtype=int), np.arange(len(detections)),
                np.empty((0, 5), dtype=int))

    # row: detection, col: trackers
    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)
    matched_indices = linear_assignment(-iou_matrix)

    # records unmatched detection indices
    unmatched_detections = []
    for d, det in enumerate(detections):
        if d not in matched_indices[:, 0]:
            unmatched_detections.append(d)

    # records unmatched trackers indices
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if t not in matched_indices[:, 1]:
            unmatched_trackers.append(t)

    # filter out matched with low IOU
    matches = []
    for m in matched_indices:
        if iou_matrix[m[0], m[1]] < iou_threshold:
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))

    if len(matches) == 0:
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)
    return (matches, np.array(unmatched_detections),
            np.array(unmatched_trackers))
Ejemplo n.º 35
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
    """(numpy.array, numpy.array, int) -> numpy.array, numpy.array, numpy.array

    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """

    if (len(trackers) == 0):
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 4),
                                                                     dtype=int)

    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)

    matched_indices = linear_assignment(-iou_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)

    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    matches = []
    for m in matched_indices:
        if (iou_matrix[m[0], m[1]] < iou_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))

    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 36
0
    def associate_detections_to_trackers_embedding(self,
                                                   detections,
                                                   track_list,
                                                   distance_threshold=2):
        """
		Assigns detections to tracked object (both represented as bounding boxes)
		Returns 3 lists of matches, unmatched_detections and unmatched_trackers
		"""
        if (len(track_list) == 0):
            return np.empty((0, 2),
                            dtype=int), np.arange(len(detections)), np.empty(
                                (0, 5), dtype=int)
        distance_matrix = np.zeros((len(detections), len(track_list)),
                                   dtype=np.float32)

        for d, det in enumerate(detections):
            for t, trk in enumerate(track_list):
                distance_matrix[d, t] = self.distance(det, trk)
        matched_indices = linear_assignment(distance_matrix)

        unmatched_detections = []
        for d, det in enumerate(detections):
            if (d not in matched_indices[:, 0]):
                unmatched_detections.append(d)
        unmatched_track_list = []
        for t, trk in enumerate(track_list):
            if (t not in matched_indices[:, 1]):
                unmatched_track_list.append(t)

        #filter out matched with high distance
        matches = []
        for m in matched_indices:
            if (distance_matrix[m[0], m[1]] > distance_threshold):
                unmatched_detections.append(m[0])
                unmatched_track_list.append(m[1])
            else:
                matches.append(m.reshape(1, 2))
        if (len(matches) == 0):
            matches = np.empty((0, 2), dtype=int)
        else:
            matches = np.concatenate(matches, axis=0)

        return matches, np.array(unmatched_detections), np.array(
            unmatched_track_list)
Ejemplo n.º 37
0
    def data_association(
        self,
        detections,
        frame,
        iou_threshold=0
    ):  #人愈小 threshold值要愈小,總之threshold值愈小就辨識新物體更難,threshold值愈大辨識到新物體更容易
        iou_matrix = np.zeros((len(detections), len(self.trackers)),
                              dtype=np.float32)
        # self.predict_update(frame)
        for o, obj in enumerate(detections):
            for t, trk in enumerate(self.trackers):
                iou_matrix[o, t] = self.IOU(
                    obj, trk.bbox)  #計算每個trk與obj的IOU並且將值存在iou_matrix裡面(依編號存)
        # print("iou_matrix")
        # print(iou_matrix)
        matched_indices = linear_assignment(-iou_matrix)
        # print("matched_indices")
        # print(matched_indices)
        usedDections = set()
        usedTrackers = set()

        matches = []
        for m in matched_indices:  #m [dect, trk]
            if (iou_matrix[m[0], m[1]] > iou_threshold):
                matches.append(m.reshape(1, 2))
        if (len(matches) == 0):
            matches = np.empty((0, 2), dtype=int)
        else:
            matches = np.concatenate(matches, axis=0)  #令matches作為一個個row排序這樣
        assignment = list()
        for i in range(len(self.trackers)):
            # print("i:{}".format(i))
            assignment.append(-1)
        for match in matches:
            if match[1] in usedTrackers or match[0] in usedDections:
                continue
            assignment[match[1]] = match[0]
            usedDections.add(match[0])
            usedTrackers.add(match[1])
            unusedTrackers = set(range(0, len(
                self.trackers))).difference(usedTrackers)
            for ID in unusedTrackers:
                self.trackers[ID].skipped_frames += 1
        return assignment
Ejemplo n.º 38
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
    """
  Assigns detections to tracked object (both represented as bounding boxes)

  Returns 3 lists of matches, unmatched_detections and unmatched_trackers
  """
    if (len(trackers) == 0):
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5),
                                                                     dtype=int)

    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)
    # Solve assignment problem via Hungarian algo.
    matched_indices = linear_assignment(-iou_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # Filter out matched with low IOU
    matches = []
    for m in matched_indices:
        # print(iou_matrix[m[0],m[1]])
        if (iou_matrix[m[0], m[1]] < iou_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 39
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.2):
    """
    将检测框BBox和卡尔曼的预测框BBox进行匹配
    Assigns detections to tracked object (both represented as bounding boxes)
    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    if (len(trackers) == 0):
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5),
                                                                     dtype=int)
    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):  # 遍历每个检测框BBox,每个BBox标识为d
        for t, trk in enumerate(trackers):  # 遍历卡尔曼预测框BBox,每个BBox标识为t
            iou_matrix[d, t] = iou(det, trk)
    matched_indices = linear_assignment(-iou_matrix)  # 通过匈牙利匹配算法进行匹配,得对成功匹配的对

    unmatched_detections = []
    for d, det in enumerate(
            detections):  # 没能成功匹配的检测框BBox放入unmatched_detections,用于判断目标新增
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)

    unmatched_trackers = []  # 没能成功匹配的预测框BBox放入unmatched_trackers,用于判断目标删除
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # filter out matched with low IOU,将匹配的IOU过小的检测框和预测框进行过滤
    matches = []  # 最终成功匹配的放入matches
    for m in matched_indices:
        if (iou_matrix[m[0], m[1]] < iou_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 40
0
    def match(self, tracks, detections):
        """ """

        if(len(tracks) == 0):
            return np.empty((0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5), dtype=int)

        # Create the cost matrix
        C = np.zeros((len(detections), len(tracks)), dtype=np.float32)

        # Compute the cost matrix
        for d, det in enumerate(detections):
            for t, trk in enumerate(tracks):
                C[d, t] = self.cost_metric(det, trk)

        # Run the optimization problem
        M = linear_assignment(C)

        unmatched_detections = []
        for d, det in enumerate(detections):
            if(d not in M[:, 0]):
                unmatched_detections.append(d)
        unmatched_tracks = []
        for t, trk in enumerate(tracks):
            if(t not in M[:, 1]):
                unmatched_tracks.append(t)

        matches = []
        for m in M:
            if self.max_distance is None:
                matches.append(m.reshape(1, 2))
            else:
                if(C[m[0], m[1]] > self.max_distance):
                    unmatched_detections.append(m[0])
                    unmatched_tracks.append(m[1])
                else:
                    matches.append(m.reshape(1, 2))

        if(len(matches) == 0):
            matches = np.empty((0, 2), dtype=int)
        else:
            matches = np.concatenate(matches, axis=0)

        return matches, np.array(unmatched_detections), np.array(unmatched_tracks)
Ejemplo n.º 41
0
def associate_detections_to_trackers(detections,
                                     trackers,
                                     conf_threshold=0.95):
    """
    Assigns detections to tracked object (both represented as bounding boxes)

    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    if (len(trackers) == 0):
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5),
                                                                     dtype=int)
    dist_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            dist_matrix[d, t] = mahab_dist(trk, det)
    matched_indices = linear_assignment(dist_matrix)
    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # filter out matched with low IOU
    matches = []
    gate = chi2.ppf(conf_threshold, df=2)
    for m in matched_indices:
        if dist_matrix[m[0], m[1]] > gate:
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 42
0
def start(filename, k):
    data = []

    with open(filename) as f:
        for line in f:
            row = line[:-1].split(',')
            data.append(row)

    Y = np.array(data[0], dtype='int')
    X = np.array(data[1:]).transpose().tolist()

    predicted_labels = form_cluster(X, k)

    C = confusion_matrix(Y, predicted_labels)
    C = C.T
    ind = linear_assignment(-C)
    C_opt = C[:, ind[:, 1]]
    acc_opt = np.trace(C_opt) / np.sum(C_opt)
    print(acc_opt)
Ejemplo n.º 43
0
def cluster_acc(y_true, y_pred):
    """
    Computes clustering accuracy.
    Args:
        y_true ([type]): [description]
        y_pred ([type]): [description]

    Returns:
        [type]: [description]
    """
    y_true = y_true.astype(np.int64)
    assert y_pred.size == y_true.size
    D = max(y_pred.max(), y_true.max()) + 1
    w = np.zeros((D, D), dtype=np.int64)
    for i in range(y_pred.size):
        w[y_pred[i], y_true[i]] += 1
    from sklearn.utils.linear_assignment_ import linear_assignment
    ind = linear_assignment(w.max() - w)
    return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
Ejemplo n.º 44
0
def unsupervised_clustering_accuracy(y, y_pred):
    """
  Unsupervised Clustering Accuracy
  Author: scVI
  https://github.com/YosefLab/scVI/blob/a585f7d096f04ab0d50cadfdf8c2c9f78d907c19/scvi/inference/posterior.py#L637
  """
    # from scipy.optimize import linear_sum_assignment
    from sklearn.utils.linear_assignment_ import linear_assignment
    assert len(y_pred) == len(y)
    u = np.unique(np.concatenate((y, y_pred)))
    n_clusters = len(u)
    mapping = dict(zip(u, range(n_clusters)))
    reward_matrix = np.zeros((n_clusters, n_clusters), dtype=np.int64)
    for y_pred_, y_ in zip(y_pred, y):
        if y_ in mapping:
            reward_matrix[mapping[y_pred_], mapping[y_]] += 1
    cost_matrix = reward_matrix.max() - reward_matrix
    ind = linear_assignment(cost_matrix)
    return sum([reward_matrix[i, j] for i, j in ind]) * 1.0 / y_pred.size, ind
Ejemplo n.º 45
0
def _hungarian_match(flat_preds, flat_targets, num_samples, class_num):
    num_k = class_num
    num_correct = np.zeros((num_k, num_k))

    for c1 in range(0, num_k):
        for c2 in range(0, num_k):
            # elementwise, so each sample contributes once
            votes = int(((flat_preds == c1) * (flat_targets == c2)).sum())
            num_correct[c1, c2] = votes

    # num_correct is small
    match = linear_assignment(num_samples - num_correct)

    # return as list of tuples, out_c to gt_c
    res = []
    for out_c, gt_c in match:
        res.append((out_c, gt_c))

    return res
Ejemplo n.º 46
0
def acc(y_true, y_pred):
    """
    https://github.com/XifengGuo/DEC-keras/blob/master/metrics.py

    Calculate clustering accuracy. Require scikit-learn installed
    # Arguments
        y: true labels, numpy.array with shape `(n_samples,)`
        y_pred: predicted labels, numpy.array with shape `(n_samples,)`
    # Return
        accuracy, in [0,1]
    """
    y_true = y_true.astype(np.int64)
    assert y_pred.size == y_true.size
    D = max(y_pred.max(), y_true.max()) + 1
    w = np.zeros((D, D), dtype=np.int64)
    for i in range(y_pred.size):
        w[y_pred[i], y_true[i]] += 1
    ind = linear_assignment(w.max() - w)
    return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
Ejemplo n.º 47
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
    """
  tracking object(둘 다 bounding box)에 detection을 지정합니다.
  unmatched_detections 및 unmatched_trackers와 match 3 개의 목록을 반환합니다.
  """
    # 추적 하는게 없다면 반환
    if (len(trackers) == 0) or (len(detections) == 0):
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5),
                                                                     dtype=int)
    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)
    # Hungarian Algorithm
    matched_indices = linear_assignment(-iou_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    #filter out matched with low IOU
    matches = []
    for m in matched_indices:
        if (iou_matrix[m[0], m[1]] < iou_threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 48
0
def LAP_iou_sklearn(detections, trackers, threshold):
    """
    Assigns detections to tracked object (both represented as bounding boxes)
    Hungarian algorithm

    Returns 3 lists of matches, unmatched_detections and unmatched_trackers
    """
    # todo delete line
    val = False
    if (len(trackers) == 0):
        return np.empty((0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5), dtype=int)

    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)
    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou(det, trk)

    matched_indices = linear_assignment(-iou_matrix)  # [row,col]

    unmatched_detections = []
    for d, det in enumerate(detections):
        if (d not in matched_indices[:, 0]):
            unmatched_detections.append(d)
    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if (t not in matched_indices[:, 1]):
            unmatched_trackers.append(t)

    # filter out matched with low IOU
    matches = []
    for m in matched_indices:
        if (iou_matrix[m[0], m[1]] < threshold):
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))
    if (len(matches) == 0):
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
Ejemplo n.º 49
0
def associate_detections_to_trackers(detections, trackers, iou_threshold=0.3):
    if len(trackers) == 0:
        return np.empty(
            (0, 2), dtype=int), np.arange(len(detections)), np.empty((0, 5),
                                                                     dtype=int)

    iou_matrix = np.zeros((len(detections), len(trackers)), dtype=np.float32)

    for d, det in enumerate(detections):
        for t, trk in enumerate(trackers):
            iou_matrix[d, t] = iou_tracker(trk, det)

    # Solve the linear assignment problem using the Hungarian algorithm
    # The problem is also known as maximum weight matching in bipartite graphs. The method is also known as the
    # Munkres or Kuhn-Munkres algorithm.
    matched_indices = linear_assignment(-iou_matrix)

    unmatched_detections = []
    for d, det in enumerate(detections):
        if d not in matched_indices[:, 0]:
            unmatched_detections.append(d)  # store index

    unmatched_trackers = []
    for t, trk in enumerate(trackers):
        if t not in matched_indices[:, 1]:
            unmatched_trackers.append(t)  # store index

    matches = []
    for m in matched_indices:
        if iou_matrix[m[0], m[1]] < iou_threshold:
            unmatched_detections.append(m[0])
            unmatched_trackers.append(m[1])
        else:
            matches.append(m.reshape(1, 2))

    if len(matches) == 0:
        matches = np.empty((0, 2), dtype=int)
    else:
        matches = np.concatenate(matches, axis=0)

    return matches, np.array(unmatched_detections), np.array(
        unmatched_trackers)
Ejemplo n.º 50
0
def rarUdet(udet, q1=1, q2=0):
    iomatrix = np.zeros((len(udet), len(udet)))
    for i in range(len(udet)):
        for j in range(len(udet)):
            if udet[i, q1, 0] != 0 and udet[j, q2, 0] != 0 and udet[
                    i, q2, 0] == 0 and udet[j, q1, 0] == 0:
                iomatrix[i, j] = bbox_iou2(udet[i, q1, :4], udet[j, q2, :4])
    iomatrix[iomatrix[:, :] < 0.6] = 0

    matched_indices = linear_assignment(-iomatrix)

    dop_1a = np.count_nonzero(iomatrix, 1)
    dop_1b = np.count_nonzero(iomatrix, 0)
    dop_2 = np.nonzero(iomatrix)
    list_to_del = []
    if len(dop_2) > 0:
        num = len(dop_2[0])
        for i in range(num):
            ind_i = dop_2[0][i]
            ind_j = dop_2[1][i]
            if dop_1a[ind_i] == 1 and dop_1b[ind_j] == 1:
                udet[ind_j, q1, :] = udet[ind_i, q1, :]
                #udet[ind_i, q2, :] = udet[ind_j, q2, :]
                list_to_del.append(ind_i)
            if dop_1a[ind_i] > 1:
                j = matched_indices[np.where(matched_indices[:,
                                                             0] == ind_i)[0],
                                    1]
                udet[j, q1, :] = udet[ind_i, q1, :]
                list_to_del.append(ind_i)

            # if dop_1b[ind_j] > 1:
            #     i = matched_indices[np.where(matched_indices[:, 1] == ind_j)[0], 0]
            #     udet[j, q1, :] = udet[ind_i, q1, :]
            #     list_to_del.append(ind_i)

        ls = list(set(list_to_del))
        ls.sort()
        for i in ls[::-1]:
            udet = np.delete(udet, i, 0)

    return udet
Ejemplo n.º 51
0
def optimal_permutation(X, Y):
    """Compute the optmal permutation matrix of X toward Y

    Parameters
    ----------
    X: (n_samples, n_features) nd array
        source data
    Y: (n_samples, n_features) nd array
        target data

    Returns
    ----------
    permutation : (n_features, n_features) nd array
        transformation matrix
    """
    dist = pairwise_distances(X.T, Y.T)
    u = linear_assignment(dist)
    permutation = scipy.sparse.csr_matrix(
        (np.ones(X.shape[1]), (u[:, 0], u[:, 1]))).T
    return permutation
Ejemplo n.º 52
0
def unsupervised_clustering_accuracy(labels_true, labels_pred):
    """
    Calculate the unsupervised clustering accuracy.
    # Arguments
        labels_true: true labels, numpy.array with shape `(n_samples,)`
        labels_pred: predicted labels, numpy.array with shape `(n_samples,)`
    # Return
        accuracy, in [0,1]
    Adapted from https://github.com/XifengGuo/DEC-keras/blob/master/metrics.py
    """
    y_true = relabel(labels_true)
    y_pred = relabel(labels_pred)
    assert y_true.size == y_pred.size
    D = max(y_pred.max(), y_true.max()) + 1
    w = numpy.zeros((D, D), dtype=numpy.int64)
    for i in range(y_pred.size):
        w[y_pred[i], y_true[i]] += 1
    from sklearn.utils.linear_assignment_ import linear_assignment
    ind = linear_assignment(w.max() - w)
    return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
Ejemplo n.º 53
0
def mention2GoldClusterIds_ceafe(clusters, gold_clusters):
    clusters = [c for c in clusters]
    scores = np.zeros((len(gold_clusters), len(clusters)))
    for i in range(len(gold_clusters)):
        for j in range(len(clusters)):
            scores[i, j] = phi4(gold_clusters[i], clusters[j])
    matching = linear_assignment(-scores)
    p2g = {m[1]: m[0] for m in matching if scores[m[0]][m[1]] > 0}
    extra_ids = len(gold_clusters) + 1
    mention2gcid = {}
    for pid, cl in enumerate(clusters):
        if pid in p2g:
            gid = p2g[pid] + 1
        else:
            gid = extra_ids
            extra_ids += 1
        for m in cl:
            mention2gcid[m] = gid

    return mention2gcid
Ejemplo n.º 54
0
def assign(dist_map, pair_dist, pair_velocity, pre_num, post_num):

    cost_mat = np.full((pre_num, post_num), 100, dtype='float32')
    for i in range(pre_num):
        for j, x in enumerate(dist_map[i]):
            if x is None:
                continue
            if pair_velocity[i][j] is None:
                cost_mat[i, x] = pair_dist[i][j] + 10
            if pair_velocity[i][j] is not None:
                cost_mat[i, x] = pair_dist[i][j] + pair_velocity[i][j]
                
    assign_result_holder = sk_assignment.linear_assignment(cost_mat)
    assign_result = dict([i, None] for i in range(pre_num))
    for i in range(assign_result_holder.shape[0]):
        pre, post = assign_result_holder[i]
        if cost_mat[pre, post] < 20:       # meaning both distance and velocity pair requirment are not met
            assign_result[pre] = post
    
    return assign_result
Ejemplo n.º 55
0
def data_associate(score_mat, match_thres=0.3):
    detnum,targetnum = score_mat.shape
    matched_indices = linear_assignment(-score_mat)
    #print score_mat
    unm_dets = []
    for d in range(detnum):
        if(d not in matched_indices[:,0]):
            unm_dets.append(d)
    unm_targets = []
    for t in range(targetnum):
        if(t not in matched_indices[:,1]):
            unm_targets.append(t)

    matches = []
    for m in matched_indices:
        if(score_mat[m[0],m[1]])<match_thres:
            unm_dets.append(m[0])
            unm_targets.append(m[1])
        else: matches.append(m)

    return matches, unm_dets, unm_targets
Ejemplo n.º 56
0
def consensus_score(a, b, similarity="jaccard"):
    """The similarity of two sets of biclusters.

    Similarity between individual biclusters is computed. Then the
    best matching between sets is found using the Hungarian algorithm.
    The final score is the sum of similarities divided by the size of
    the larger set.

    Read more in the :ref:`User Guide <biclustering>`.

    Parameters
    ----------
    a : (rows, columns)
        Tuple of row and column indicators for a set of biclusters.

    b : (rows, columns)
        Another set of biclusters like ``a``.

    similarity : string or function, optional, default: "jaccard"
        May be the string "jaccard" to use the Jaccard coefficient, or
        any function that takes four arguments, each of which is a 1d
        indicator vector: (a_rows, a_columns, b_rows, b_columns).

    References
    ----------

    * Hochreiter, Bodenhofer, et. al., 2010. `FABIA: factor analysis
      for bicluster acquisition
      <https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2881408/>`__.

    """
    if similarity == "jaccard":
        similarity = _jaccard
    matrix = _pairwise_similarity(a, b, similarity)
    indices = linear_assignment(1. - matrix)
    n_a = len(a[0])
    n_b = len(b[0])
    return matrix[indices[:, 0], indices[:, 1]].sum() / max(n_a, n_b)
Ejemplo n.º 57
0
Archivo: sort.py Proyecto: Millczc/sort
def associate_detections_to_trackers(detections,trackers,iou_threshold = 0.3):
  """
  Assigns detections to tracked object (both represented as bounding boxes)

  Returns 3 lists of matches, unmatched_detections and unmatched_trackers
  """
  if(len(trackers)==0):
    return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,5),dtype=int)
  iou_matrix = np.zeros((len(detections),len(trackers)),dtype=np.float32)

  for d,det in enumerate(detections):
    for t,trk in enumerate(trackers):
      iou_matrix[d,t] = iou(det,trk)
  matched_indices = linear_assignment(-iou_matrix)

  unmatched_detections = []
  for d,det in enumerate(detections):
    if(d not in matched_indices[:,0]):
      unmatched_detections.append(d)
  unmatched_trackers = []
  for d,det in enumerate(trackers):
    if(d not in matched_indices[:,1]):
      unmatched_trackers.append(d)

  #filter out matched with low IOU
  matches = []
  for m in matched_indices:
    if(iou_matrix[m[0],m[1]]<iou_threshold):
      unmatched_detections.append(m[0])
      unmatched_trackers.append(m[1])
    else:
      matches.append(m.reshape(1,2))
  if(len(matches)==0):
    matches = np.empty((0,2),dtype=int)
  else:
    matches = np.concatenate(matches,axis=0)

  return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
Ejemplo n.º 58
0
def acc(ypred, y):
    """
    Calculating the clustering accuracy. The predicted result must have the same number of clusters as the ground truth.
    
    ypred: 1-D numpy vector, predicted labels
    y: 1-D numpy vector, ground truth

    The problem of finding the best permutation to calculate the clustering accuracy is a linear assignment problem.
    This function construct a N-by-N cost matrix, then pass it to scipy.optimize.linear_sum_assignment to solve the assignment problem.
    
    """
    assert len(y) > 0
    assert len(np.unique(ypred)) == len(np.unique(y))
    
    s = np.unique(ypred)
    t = np.unique(y)
    
    N = len(np.unique(ypred))
    C = np.zeros((N, N), dtype = np.int32)
    for i in range(N):
        for j in range(N):
            idx = np.logical_and(ypred == s[i], y == t[j])
            C[i][j] = np.count_nonzero(idx)
    
    # convert the C matrix to the 'true' cost
    Cmax = np.amax(C)
    C = Cmax - C
    # 
    indices = linear_assignment(C)
    row = indices[:][:, 0]
    col = indices[:][:, 1]
    # calculating the accuracy according to the optimal assignment
    count = 0
    for i in range(N):
        idx = np.logical_and(ypred == s[row[i]], y == t[col[i]] )
        count += np.count_nonzero(idx)
    
    return 1.0*count/len(y)
Ejemplo n.º 59
0
def accuracy(true_row_labels, predicted_row_labels):
    """Get the best accuracy.

    Parameters
    ----------
    true_row_labels: array-like
        The true row labels, given as external information
    predicted_row_labels: array-like
        The row labels predicted by the model

    Returns
    -------
    float
        Best value of accuracy
    """

    cm = confusion_matrix(true_row_labels, predicted_row_labels)
    indexes = linear_assignment(_make_cost_m(cm))
    total = 0
    for row, column in indexes:
        value = cm[row][column]
        total += value

    return (total * 1. / np.sum(cm))
Ejemplo n.º 60
0
def assign(observed, predicted, max_dist):
    '''
    Assigns observed locations of bees based on predicted locations using the
    Hungarian algorithm.
    Args:
        observed - numpy array with shape (3, n) containing observed coordinates
                   and a third row containing weights for assignment in the case
                   where more than 2 bees are coassigned and then split.
        predicted - numpy array with first two lines containing predicted coords
        max_dist - maximum distance between predicted and observed
    Returns:
        an nx3 array of indices (predicted_index, observed_index, non_linear)
    '''
    costs = cdist(predicted[0:2, :].transpose(), observed[0:2, :].transpose(),
                  'euclidean')
    l = linear_assignment(costs)
    rm_list = []

    # Remove assignments which make the distance between observed and predicted
    # greater than max_dist
    for row in range(l.shape[0]):
        if costs[l[row, 0], l[row, 1]] > max_dist \
                and (predicted[0:2, ] != 0).all():
            rm_list.append(row)
    l = np.delete(l, rm_list, axis=0)

    l = reassign(l, predicted.shape[1], costs, max_dist,
                 observed[2, :])

    # Calculate co-assignments
    l = np.hstack([l, np.zeros((l.shape[0], 1), dtype=l.dtype)])
    u, u_counts = np.unique(l[:, 1], return_counts=True)
    for i in range(l.shape[0]):
        u_idx = np.where(u == l[i, 1])[0]
        l[i, 2] = u_counts[u_idx]
    return l