def eval_result(labels, result_gen, cache_file): if path.exists(cache_file): print "Loading cached result..." with open(cache_file, "rb") as inf: lbl_clst = pickle.load(inf) print "Done." else: lbl_clst = result_gen() print "Saving result to cache..." with open(cache_file, "wb") as outf: pickle.dump(lbl_clst, outf) print "Done." all_langs = ["da", "de", "el", "en", "es", "fi", "fr", "it", "nl", "pt", "sv"] print "Constructing cost matrix..." nObj = len(labels) nLang = len(all_langs) G = [[0 for j in range(nLang)] for i in range(nLang)] for i in range(nLang): for j in range(nLang): G[i][j] = -sum([(lbl_clst[k] == i and labels[k] == all_langs[j]) for k in range(nObj)]) print "Finding best mapping..." m = Munkres() idx = m.compute(G) print "Calculating accuracy..." total = 0 for row, col in idx: total += G[row][col] accuracy = -total / float(nObj) print "Accuracy = %.4f" % accuracy
def __assign_labels_to_clusters__(assignment, labels, clusters, clusts_labels): cost_matrix = gen_assignment_cost_matrix(assignment, labels, clusters, clusts_labels) munkres = Munkres() assignment_list = munkres.compute(cost_matrix) # assignment_list = [(i,i) for i in xrange(len(clusters))] #for debugging assigned_clusts_labels = dict(assignment_list) # dictionary {cluster_no: assigned_label_no} return assigned_clusts_labels
def match_rows_sign(X, Y): """ Permute the rows of _X_ to minimize error with Y, ignoring signs of the columns @params X numpy.array - input matrix @params Y numpy.array - comparison matrix @return numpy.array - X with permuted rows """ n, d = X.shape n_, d_ = Y.shape assert n == n_ and d == d_ # Create a weight matrix to compare the two W = zeros((n, n)) for i, j in it.product(xrange(n), xrange(n)): # Cost of 'assigning' j to i. W[i, j] = min(norm(X[j] - Y[i]), norm(X[j] + Y[i])) matching = Munkres().compute(W) matching.sort() _, rowp = zip(*matching) rowp = array(rowp) # Permute the rows of B according to Bi X_ = X[rowp] # Change signs to minimize loss for row in xrange(n): if norm(X_[row] + Y[row]) < norm(X_[row] - Y[row]): X_[row] *= -1 return X_
def hun(costMatrix): # Check first, if costmatrix is not empty if costMatrix.shape==(0,0): return [] # Create squared temporary matrix tmpMatrix = numpy.copy(costMatrix) tmpMatrix = makeSquareWithNegValues(tmpMatrix) sqCostMatrix = numpy.copy(tmpMatrix) sqCostMatrix[tmpMatrix==-1]=10e10 # Solve ASP on the temporary matrix m=Munkres() i=m.compute(sqCostMatrix) # Create resultin matrix that contains ones at matching # objects and remove all excluded matches binMatrix = numpy.zeros( tmpMatrix.shape,dtype=bool ) for x,y in i: if tmpMatrix[x,y]==-1: continue binMatrix[x,y]=True return binMatrix
def similar(list_current,list_called): global P,total # comparing the length of two files to compare smaller length to bigger if len(list_current)<len(list_called): # calling comparison function to compare both files line by line for similarity similarity = comparison(list_current,list_called) # storing the lenght of smaller P = len(list_current) point=[[0 for x in range(len(list_called))] for y in range(len(list_current))] else: # calling comparison function to compare both files line by line for similarity similarity = comparison(list_called,list_current) P = len(list_called) point=[[0 for x in range(len(list_current))] for y in range(len(list_called))] # calling functions of munkres to form maximum weighted bipartite matching graph graph_matrix = make_cost_matrix(similarity, lambda cost: 1.0 - cost) m = Munkres() indexes =m.compute(graph_matrix) total = 0 for row, column in indexes: # forming list of points(lines) of similarity between two files value = similarity[row][column] if value>0.0: total += 1 point[row][column]=1 return point
def main(tutors, players): scores = [] #create cross-scores for tutor in tutors: # create the score array for each tutor to every player scores.append([ getModFLF(tutor, player)*getBaseFLF(tutor,player) for player in players ]) # print the matrix #pretty_print(scores) # find max maxscore = max(max(row) for row in scores) # turn the matrix into a min-problem for row in scores: for idx,col in enumerate(row): row[idx] = maxscore-col # using munkres (copy of tutorial) m = Munkres() indexes = m.compute(scores) # pretty_print(scores) total = 0 print "[[Tutor ::: Player]]" for row, col in indexes: total += scores[row][col] print '{0} ::: {1}'.format(tutors[row],players[col]) print 'total FLF: {0}'.format(maxscore*len(tutors)-total)
def fix_parameters(true, guess, weights): """Find a column permutation of guess parameters that matches true parameters most closely (i.e. min |A - B|_F) also apply this to weights""" # The rows of A and B form a weighted bipartite graph. The weights # are computed using the vector_matching algorithm. # We need to find their minimal matching. assert true.shape == guess.shape d, k = true.shape m = Munkres() # Create the weight matrix W = sc.zeros((k, k)) for i in xrange(k): for j in xrange(k): # Best matching between A and B W[i, j] = norm(true.T[i] - guess.T[j]) matching = m.compute(W) matching.sort() _, colp = zip(*matching) colp = array(colp) # Permute the rows of B according to Bi guess = guess[:, colp] weights = weights[colp] return weights, guess
def align(sen1, sen2): """finds the best mapping of words from one sentence to the other""" #find lengths sen1 = list(map(preprocess_word, sen1.split())) sen2 = list(map(preprocess_word, sen2.split())) lengthDif = len(sen1) - len(sen2) if lengthDif > 0: shorter = sen2 longer = sen1 else: shorter = sen1 longer = sen2 lengthDif = abs(lengthDif) shorter += ["emptyWord"] * lengthDif #create matrix matrix = np.zeros((len(longer), len(longer))) for i in range(len(longer)): for j in range(len(longer) - lengthDif): matrix[i,j] = distance.levenshtein(longer[i], shorter[j]) print(matrix) #compare with munkres m = Munkres() indexes = m.compute(matrix) print("mapping is:",[(shorter[i], longer[j]) for (i,j) in indexes])
def accuracy(labels_true, labels_pred): labels_true, labels_pred = check_clusterings(labels_true, labels_pred) n_samples = labels_true.shape[0] classes = np.unique(labels_true) clusters = np.unique(labels_pred) # Special limit cases: no clustering since the data is not split; # or trivial clustering where each document is assigned a unique cluster. # These are perfect matches hence return 1.0. if (classes.shape[0] == clusters.shape[0] == 1 or classes.shape[0] == clusters.shape[0] == 0 or classes.shape[0] == clusters.shape[0] == len(labels_true)): return 1.0 # print "accuracy testing..." contingency = contingency_matrix(labels_true, labels_pred) #Type: <type 'numpy.ndarray'>:rows are clusters, cols are classes contingency = -contingency #print contingency contingency = contingency.tolist() m = Munkres() # Best mapping by using Kuhn-Munkres algorithm map_pairs = m.compute(contingency) #best match to find the minimum cost sum_value = 0 for key,value in map_pairs: sum_value = sum_value + contingency[key][value] return float(-sum_value)/n_samples
def find_ambiguous_mapping(self, res, aa, non_ambiguous_mapping, previous=False): from munkres import Munkres from numpy import fabs, sum, delete total_costs = None mapping = [] ambiguous_keys = [] ambiguous_shifts = [] res_shifts, res_keys = res.get_carbons(previous) aa_shifts, aa_keys = aa.get_carbons() for i, j in non_ambiguous_mapping: if j in aa_keys: k = list(aa_keys).index(j) aa_shifts = delete(aa_shifts, k) aa_keys = delete(aa_keys, k) for i, key in enumerate(res_keys): if self.ambiguous(key, previous): ambiguous_keys.append(key) ambiguous_shifts.append(res_shifts[i]) if len(aa_keys) > 0 and len(ambiguous_shifts) > 0: costs = fabs(subtract.outer(ambiguous_shifts, aa_shifts)) munkres = Munkres() result = munkres.compute(costs * 1.) for i, j in result: mapping.append((ambiguous_keys[i], aa_keys[j])) return mapping
def match(img_location, features, cluster): img = cv2.imread(img_location) detector = cv2.FeatureDetector_create("SIFT") descriptor = cv2.DescriptorExtractor_create("SIFT") keypoints = detector.detect(img) keypoints = sorted(keypoints, key=lambda x: -x.response) keypoints, img_features = descriptor.compute(img, keypoints[0:5]) img_features = img_features.tolist() m = Munkres() distance = {} for filename in cluster: distance[filename] = 0 fea = features[filename] matrix = [[0 for i in range(5)] for j in range(5)] for i in range(5): for j in range(5): if matrix[j][i] != 0: matrix[i][j] = matrix[j][i] continue try: matrix[i][j] = np.linalg.norm( array(img_features[i]) - array(fea[j]) ) except: matrix[i][j] = 10000 indexes = m.compute(matrix) for row, column in indexes: distance[filename] += matrix[row][column] results = sorted(cluster, key = lambda x: distance[x]) # results = features.keys() return results, distance
def maximum_weight_bipartite(matrix): cost_matrix = make_cost_matrix(matrix, lambda cost: 100000 - cost) m = Munkres() indices = m.compute(cost_matrix) return indices
def loss1(usersPerCircle, usersPerCircleP): #psize: either the number of groundtruth, or the number of predicted circles (whichever is larger) psize = max(len(usersPerCircle),len(usersPerCircleP)) # Pad the matrix to be square # mm: matching matrix containing costs of matching groundtruth circles to predicted circles. # mm[i][j] = cost of matching groundtruth circle i to predicted circle j mm = numpy.zeros((psize,psize)) # mm2: copy of mm since the Munkres library destroys the data during computation mm2 = numpy.zeros((psize,psize)) for i in range(psize): for j in range(psize): circleP = set() # Match to an empty circle (delete all users) circle = set() # Match to an empty circle (add all users) if (i < len(usersPerCircleP)): circleP = usersPerCircleP[i] if (j < len(usersPerCircle)): circle = usersPerCircle[j] nedits = len(circle.union(circleP)) - len(circle.intersection(circleP)) # Compute the edit distance between the two circles mm[i][j] = nedits mm2[i][j] = nedits if psize == 0: return 0 # Edge case in case there are no circles else: m = Munkres() #print mm2 # Print the pairwise cost matrix indices = m.compute(mm) # Compute the optimal alignment between predicted and groundtruth circles editCost = 0 for row, column in indices: #print row,column # Print the optimal value selected by matching editCost += mm2[row][column] return int(editCost)
def get_suitability_score(customer_list, product_list): ''' calculate the total maximum suitability score by using munkres algorithm and returns a detailed customer_product_enties & total suitability score ''' suitability_scores = [] customer_suitability_scores = [] for customer in customer_list: for product in product_list: customer_suitability_scores.append(SuitabilityScore.calculate_suitability_score(customer,product)) suitability_scores.append(customer_suitability_scores) customer_suitability_scores = [] customer_product_entries = [] cost_matrix = make_cost_matrix(suitability_scores, lambda cost: 1e10 - cost) munkres = Munkres() indexes = munkres.compute(cost_matrix) total_suitability_score = 0 for customer_index, product_index in indexes: suitability_score = suitability_scores[customer_index][product_index] total_suitability_score += suitability_score suitability_score_entry = SuitabilityScoreEntry(customer_list[customer_index],product_list[product_index],suitability_score) customer_product_entries.append(suitability_score_entry) #print(customer_index,product_index) return customer_product_entries,total_suitability_score
def match(self, model_manager): individuals = model_manager.individuals groups = model_manager.groups pref_matrix = [] individual_count = len(individuals) row_index = -1 individuals_by_row = {} groups_by_col = {} for individual_id, individual in individuals.iteritems(): row_index += 1 individuals_by_row[str(row_index)] = individual pref_matrix_row = [] col_index = -1 for group_id, group in groups.iteritems(): group_pref_value = individual.get_group_pref_value(group.id) # we use group slots to control the max number of individuals per group. one individual per slot # each slot represents a potential membership within a group within the preference matrix. # the len(pref_matrix_group_slots[grounp.id]) == min(group.max_size, len(individuals)) group_slot_size = min(group.max_size, individual_count) for s in range(group_slot_size): # multiply value by negative 1 because munkres finds # the assignment with minimum net value, # and we want to find the assignment with # maximum net value pref_matrix_row.append(group_pref_value * -1) col_index += 1 groups_by_col[str(col_index)] = group pref_matrix.append(pref_matrix_row) munkres = Munkres() optimal_match_indexes = munkres.compute(pref_matrix) assignments = [(individuals_by_row[str(row)], groups_by_col[str(col)]) for row, col in optimal_match_indexes] return assignments
def do_matching(gsrEvents, warnings): matrix = [] M = len(gsrEvents) N = len(warnings) for m in range(M): row = [] gsr = gsrEvents[m] for n in range(N): warn = warnings[n] if match(gsr, warn): row.append(get_quality(gsr, warn)) else: row.append(.0) matrix.append(row) costMatrix = [] #create the cost matrix for m in range(M): r = [] gsr = gsrEvents[m] for n in range(N): warn = warnings[n] r.append(10 - matrix[m][n]) costMatrix.append(r) m = Munkres() if M > 0 and N > 0: indexes = m.compute(costMatrix) else: return None, None return matrix, indexes
def Objmatch(objy,objx,ref,refc,L,W,im,length): global D_limit_s dmtx = np.zeros((L,W)) cmtx = np.zeros((L,W)) Wd = 0.5 #weight of distance Wc = 1-Wd #weight of color for i in range(L): dmtx[i,:] =((objy - ref[i][0])**2+(objx - ref[i][1])**2).T cmtx[i,:] = Color_Dif(im,objx,objy,refc[i],length[i]) dmtx = dmtx/diag # normalize the distance by divid diag dmtx[dmtx>D_limit_s] = 10**6 cmtx = cmtx*Wc + dmtx*Wd tmp = copy.deepcopy(cmtx) m = Munkres() if L<=W: indexes = m.compute(cmtx) else: # len(ori) > # live_blobs indexes = m.compute(cmtx.T) indexes = [(s[1],s[0]) for s in indexes] D_idx = [] if vid_idx>=0: for i in range(len(indexes))[::-1]: if tmp[indexes[i][0],indexes[i][1]]>10**5: D_idx.append(i) indexes.pop(i) return indexes,D_idx
def p345(): A = [[ 7, 53, 183, 439, 863, 497, 383, 563, 79, 973, 287, 63, 343, 169, 583], [627, 343, 773, 959, 943, 767, 473, 103, 699, 303, 957, 703, 583, 639, 913], [447, 283, 463, 29, 23, 487, 463, 993, 119, 883, 327, 493, 423, 159, 743], [217, 623, 3, 399, 853, 407, 103, 983, 89, 463, 290, 516, 212, 462, 350], [960, 376, 682, 962, 300, 780, 486, 502, 912, 800, 250, 346, 172, 812, 350], [870, 456, 192, 162, 593, 473, 915, 45, 989, 873, 823, 965, 425, 329, 803], [973, 965, 905, 919, 133, 673, 665, 235, 509, 613, 673, 815, 165, 992, 326], [322, 148, 972, 962, 286, 255, 941, 541, 265, 323, 925, 281, 601, 95, 973], [445, 721, 11, 525, 473, 65, 511, 164, 138, 672, 18, 428, 154, 448, 848], [414, 456, 310, 312, 798, 104, 566, 520, 302, 248, 694, 976, 430, 392, 198], [184, 829, 373, 181, 631, 101, 969, 613, 840, 740, 778, 458, 284, 760, 390], [821, 461, 843, 513, 17, 901, 711, 993, 293, 157, 274, 94, 192, 156, 574], [ 34, 124, 4, 878, 450, 476, 712, 914, 838, 669, 875, 299, 823, 329, 699], [815, 559, 813, 459, 522, 788, 168, 586, 966, 232, 308, 833, 251, 631, 107], [813, 883, 451, 509, 615, 77, 281, 613, 459, 205, 380, 274, 302, 35, 805]] for r in range(len(A)): for c in range(len(A[0])): A[r][c] *= -1 from munkres import Munkres, print_matrix m = Munkres() indexes = m.compute(A) total = 0 for row, column in indexes: value = A[row][column] total += value return -total
def match_rows(X, Y): """ Permute the rows of _X_ to minimize error with Y @params X numpy.array - input matrix @params Y numpy.array - comparison matrix @return numpy.array - X with permuted rows """ n, d = X.shape n_, d_ = Y.shape assert n == n_ and d == d_ # Create a weight matrix to compare the two W = zeros((n, n)) for i, j in it.product(xrange(n), xrange(n)): # Cost of 'assigning' j to i. W[i, j] = norm(X[j] - Y[i]) matching = Munkres().compute(W) matching.sort() _, rowp = zip(*matching) rowp = array(rowp) # Permute the rows of B according to Bi X_ = X[rowp] return X_
def __init__(self, G1, G2, nattr='weight', eattr='weight', lamb = 0.5): G1, G2 = sorted([G1, G2], key=lambda x: len(x)) csim = gs.tacsim_combined_in_C(G1, G2, node_attribute=nattr, edge_attribute=eattr, lamb=lamb) self.csim = csim / np.sqrt(((csim * csim).sum())) # to ensure valid structural distance self.g1 = G1 self.g2 = G2 m = Munkres() cdist = (1 - self.csim).tolist() self.matching = m.compute(cdist) nmap = {} def _gen_nnid(node): if node not in nmap: nmap[node] = len(nmap) return nmap[node] self.mesos = nx.DiGraph() for (e1_idx, e2_idx) in self.matching: e1 = G1.edges()[e1_idx] e2 = G2.edges()[e2_idx] ns = _gen_nnid(e1[0]) nt = _gen_nnid(e1[1]) self.mesos.add_edge(ns, nt) self.mesos.edge[ns][nt][eattr] = 0.5 * (G1.edge[e1[0]][e1[1]][eattr] + G2.edge[e2[0]][e2[1]][eattr]) self.mesos.node[ns][nattr] = 0.5 * (G1.node[e1[0]][nattr] + G2.node[e2[0]][nattr]) self.mesos.node[nt][nattr] = 0.5 * (G1.node[e1[1]][nattr] + G2.node[e2[1]][nattr])
def getFileSim(fileA,fileB,alpha,beta): #to find the similarity of two files File1=open(dirname+'/'+fileA, "r") File2=open(dirname+'/'+fileB, "r") content1=File1.readlines() flag=0 content2=File2.readlines() if len(content1) > len(content2): flag=1 content1,content2 = content2,content1 sim=[] for sen_A in content1: temp = [] sen_A = sen_A.lower() for sen_B in content2: sen_B = sen_B.lower() temp.append(senSim(sen_A,sen_B,alpha)) sim.append(temp) for i in range(len(sim)) #to make it a maximum cost problem for j in range(len(sim[i])): sim[i][j] = 1.0-sim[i][j] m=Munkres() result_matrix=m.compute(sim) #implementing hungarian maxSimMatrix = [] for row,column in result_matrix: if sim[row][column]!=1.0: if flag==1: row,column=column,row maxSimMatrix.append([row,column]) #storing which lines are matched FileSim=float(len(maxSimMatrix))/(len(content1)) if FileSim<beta: FileSim = 0 return (FileSim, maxSimMatrix)
def closest_permuted_matrix( A, B ): """Find a _row_ permutation of B that matches A most closely (i.e. min |A - B|_F)""" # The rows of A and B form a weighted bipartite graph. The weights # are computed using the vector_matching algorithm. # We need to find their minimal matching. assert( A.shape == B.shape ) n, _ = A.shape m = Munkres() # Create the weight matrix W = sc.zeros( (n, n) ) for i in xrange( n ): for j in xrange( n ): # Best matching between A and B W[i, j] = norm(A[i] - B[j]) matching = m.compute( W ) matching.sort() _, rowp = zip(*matching) rowp = array( rowp ) # Permute the rows of B according to Bi B_ = B[ rowp ] return B_
def assign_projects(): projects = Project.query.join(Priority).order_by(Project.id).all() users = User.query.join(Priority).order_by(User.id).all() user_ids = [user.id for user in users] default_priority = len(projects) + 1 matrix = [] for project in projects: priority_query = db.session.query(User.id, Priority.priority).\ outerjoin(Priority, db.and_(User.id == Priority.user_id, Priority.project_id == project.id)).\ filter(User.id.in_(user_ids)).\ order_by(User.id).all() priorities = [user_priority_tuple.priority if user_priority_tuple.priority is not None else default_priority for user_priority_tuple in priority_query] matrix.append(priorities) if len(matrix) != 0: db.session.query(Project).update({Project.assignee_id: None}) m = Munkres() indexes = m.compute(matrix) for row, column in indexes: projects[row].assignee_id = user_ids[column] db.session.commit() flash('Project assignments updated.') return redirect(url_for('show_index'))
def match_parameters(p, q, method="munkres", discard_misses=False): """ Match two sets of parameters. TODO: finish greedy """ logger.info("Matching with method %s" % method) assert p.shape[1] == q.shape[1], ( "Shapes do not match (%s vs %s)" % (p.shape, q.shape) ) match_size = min(p.shape[0], q.shape[0]) corrs = np.corrcoef(p, q)[match_size:, :match_size] corrs[np.isnan(corrs)] = 0 if method == "munkres": m = Munkres() cl = 1 - np.abs(corrs) if (cl.shape[0] > cl.shape[1]): indices = m.compute(cl.T) else: indices = m.compute(cl) indices = [(i[1], i[0]) for i in indices] elif method == "greedy": q_idx = [] raise NotImplementedError("Greedy not supported yet.") for c in range(q.shape[0]): idx = corrs[c, :].argmax() q_idx.append(idx) corrs[:,idx] = 0 else: raise NotImplementedError("%s matching not supported" % method) return indices
def evaluateFrame(self,X,Y, X_labels=None, Y_labels=None): """ Compute the OSPA metric between two sets of points. """ # check for empty sets if numpy.size(X) == 0 and numpy.size(Y) == 0: return (0,0,0,0) elif numpy.size(X) == 0 or numpy.size(Y) == 0 : return (self.c,0,self.c, 0) # we assume that Y is the larger set m = numpy.size(X,0) n = numpy.size(Y,0) switched = False if m > n: X,Y = Y,X m,n = n,m switched = True dists = self.calculateCostMatrix(X,Y) # Copy cost matrix for munkres module munkres_matrix = numpy.copy(dists) # Only run munkres on non-empty matrix if len(munkres_matrix) > 0: munkres = Munkres() indices = munkres.compute(munkres_matrix) else: indices = [] # compute the OSPA metric total = 0 total_loc = 0 for [i,j] in indices: total_loc += dists[i][j]**self.p # calculate cardinalization error err_cn = (float((self.c)**(self.p)*(n-m))/n)**(1/float(self.p)) # calculate localization error err_loc = (float(total_loc)/n)**(1/float(self.p)) # Not contained in version from github implemented # acc. to paper "A Metric for Performance Evaluation of Multi-Target Tracking Algorithms" # Implementation of labeling error # Penalizes ID switches from frame to frame new_assignments = [] for [i,j] in indices: if (switched): i,j = j,i new_assignments.append((X_labels[i], Y_labels[j])) wrong_labels =len(self.old_assignments) - len(set(new_assignments) & set(self.old_assignments)) rospy.loginfo(wrong_labels) err_label = (float((float(self.a)**float(self.p))/n)*wrong_labels) **(1/float(self.p)) # store current assignments of labels to track self.old_assignments = new_assignments #ospa_err = ( float(total_loc + (n-m)*self.c**self.p) / n)**(1/float(self.p)) ospa_err = ( float(total_loc + self.a*wrong_labels + (n-m)*self.c**self.p) / n)**(1/float(self.p)) ospa_tuple = (ospa_err,err_loc,err_cn, err_label) rospy.loginfo(ospa_tuple) return ospa_tuple
def hungarian_mapping(inames, cnames, target, base): """ Utilizes the hungarian/munkres matching algorithm to compute an initial mapping of inames to cnames. The base cost is the expected correct guesses if each object is matched to itself (i.e., a new object). Then the cost of each object-object match is evaluated by setting each individual object and computing the expected correct guesses. :param inames: the target component names :type inames: collection :param cnames: the base component names :type cnames: collection :param target: An instance or concept.av_counts object to be mapped to the base concept. :type target: :ref:`Instance<instance-rep>` or av_counts obj from concept :param base: A concept to map the target to :type base: TrestleNode :return: a mapping for renaming components in the instance. :rtype: frozenset """ cnames = list(cnames) inames = list(inames) cost_matrix = [] for o in inames: row = [] for c in cnames: nm = {} nm[o] = c cost = mapping_cost({o: c}, target, base) row.append(cost) unmapped_cost = mapping_cost({}, target, base) for other_o in inames: if other_o == o: row.append(unmapped_cost) else: row.append(float('inf')) cost_matrix.append(row) m = Munkres() indices = m.compute(cost_matrix) # comments for using scipy hungarian # indices = linear_sum_assignment(cost_matrix) mapping = {} # for i in range(len(indices[0])): # row = indices[0][i] # col = indices[1][i] for row, col in indices: if col >= len(cnames): mapping[inames[row]] = inames[row] else: mapping[inames[row]] = cnames[col] return frozenset(mapping.items())
def _round(self, matrix): m = Munkres() lists = self.matrix_to_lists(matrix) indexes = m.compute(lists) matrix *= 0 for row, column in indexes: matrix[row, column] = 1 return matrix
def execute_munkres(matrix): m = Munkres() points = m.compute(matrix) result = 0 for point in points: result += matrix[point[0]][point[1]] return result
def match_labels(contingency_table): """Attempt to match the labels between ground truth and prediction using Hungarian algorithm. Extra prediction labels are moved to the right most columns. """ m = Munkres() ind = m.compute(cost_matrix(contingency_table.as_matrix())) return rearrange_columns(ind, contingency_table.copy())
def allocate(self): from debate.models import AdjudicatorAllocation # remove trainees self.adjudicators = filter(lambda a: a.score > self.MIN_SCORE, self.adjudicators) # sort adjudicators and debates in descending score/importance self.adjudicators_sorted = list(self.adjudicators) self.adjudicators_sorted.sort(key=lambda a: a.score, reverse=True) self.debates_sorted = list(self.debates) self.debates_sorted.sort(key=lambda a: a.importance, reverse=True) n_adjudicators = len(self.adjudicators) n_debates = len(self.debates) n_solos = n_debates - (n_adjudicators - n_debates)/2 # get adjudicators that can adjudicate solo chairs = self.adjudicators_sorted[:n_solos] #chairs = [a for a in self.adjudicators_sorted if a.score > # self.CHAIR_CUTOFF] # get debates that will be judged by solo adjudicators chair_debates = self.debates_sorted[:len(chairs)] panel_debates = self.debates_sorted[len(chairs):] panellists = [a for a in self.adjudicators_sorted if a not in chairs] assert len(panel_debates) * 3 <= len(panellists) print "costing chairs" n = len(chairs) cost_matrix = [[0] * n for i in range(n)] for i, debate in enumerate(chair_debates): for j, adj in enumerate(chairs): cost_matrix[i][j] = self.calc_cost(debate, adj) print "optimizing" m = Munkres() indexes = m.compute(cost_matrix) total_cost = 0 for r, c in indexes: total_cost += cost_matrix[r][c] print 'total cost for solos', total_cost print 'number of solo debates', n result = ((chair_debates[i], chairs[j]) for i, j in indexes if i < len(chair_debates)) alloc = [AdjudicatorAllocation(d, c) for d, c in result] print [(a.debate, a.chair) for a in alloc]
def hungarian(self, matrix): munkres = Munkres() cost_matrix = make_cost_matrix(matrix, lambda cost: 1 - cost) indexes = munkres.compute(cost_matrix) total = 0 coord = [] for row, column in indexes: value = matrix[row][column] total += value coord.append([row, column]) return total, coord
def __init__(self, seq_index, tt, length, cuda=True): ''' Evaluating with the MotMetrics :param seq_index: the number of the sequence :param tt: train_test :param length: the number of frames which is used for training :param cuda: True - GPU, False - CPU ''' # top index: 0 - correct matching, 1 - false matching, second index: 0 - min, 1 - max, 2 - total, 3 - counter self.ctau = [[1.0, 0.0, 0.0, 0] for i in xrange(2)] # get the threshold for matching cost self.seq_index = seq_index self.hungarian = Munkres() self.device = torch.device("cuda" if cuda else "cpu") self.tt = tt self.length = length self.missingCounter = 0 print ' Loading the model...' self.loadModel() self.out_dir = t_dir + 'motmetrics/' if not os.path.exists(self.out_dir): os.mkdir(self.out_dir) else: deleteDir(self.out_dir) os.mkdir(self.out_dir) self.initOut()
def record_alignment(self, similarities, mappings): if len(similarities) == 0: return cost_matrix = get_cost_matrix(similarities, mappings) for gold_cluster_index, system_cluster_index in Munkres().compute( cost_matrix): gold_cluster = self.get( 'cluster', 'gold', mappings['gold']['index_to_id'][gold_cluster_index]) system_cluster = self.get( 'cluster', 'system', mappings['system']['index_to_id'][system_cluster_index]) similarity = self.lookup_similarity(similarities, gold_cluster.get('ID'), system_cluster.get('ID')) if similarity > 0: self.get('alignment').get('gold_to_system')[gold_cluster.get( 'ID')] = { 'aligned_to': system_cluster.get('ID'), 'aligned_similarity': similarity } self.get('alignment').get('system_to_gold')[system_cluster.get( 'ID')] = { 'aligned_to': gold_cluster.get('ID'), 'aligned_similarity': similarity }
def order_items(items, trackinfo): """Orders the items based on how they match some canonical track information. Returns a list of Items whose length is equal to the length of ``trackinfo``. This always produces a result if the numbers of items is at most the number of TrackInfo objects (otherwise, returns None). In the case of a partial match, the returned list may contain None in some positions. """ # Make sure lengths match: If there is less items, it might just be that # there is some tracks missing. if len(items) > len(trackinfo): return None # Construct the cost matrix. costs = [] for cur_item in items: row = [] for i, canon_item in enumerate(trackinfo): row.append(track_distance(cur_item, canon_item, i+1)) costs.append(row) # Find a minimum-cost bipartite matching. matching = Munkres().compute(costs) # Order items based on the matching. ordered_items = [None]*len(trackinfo) for cur_idx, canon_idx in matching: ordered_items[canon_idx] = items[cur_idx] return ordered_items
def min_cost_perfect_bipartite_matching(G): n = len(G) try: from scipy.optimize import linear_sum_assignment rows, cols = linear_sum_assignment(G) assert (rows == list(range(n))).all() return list(cols), _matching_cost(G, cols) except ImportError: pass try: from munkres import Munkres cols = [None] * n for row, col in Munkres().compute(G): cols[row] = col return cols, _matching_cost(G, cols) except ImportError: pass if n > 6: raise Exception("Install Python module 'munkres' or 'scipy >= 0.17.0'") # Otherwise just brute-force permutations = itertools.permutations(range(n)) best = list(next(permutations)) best_cost = _matching_cost(G, best) for p in permutations: cost = _matching_cost(G, p) if cost < best_cost: best, best_cost = list(p), cost return best, best_cost
def findMovement(self, data, publisher): m = Munkres() print "processing new scan" convert_data = self.xyConvert(data) if not (len(self.filter_scans) < self.filter_size): print "starting filter" self.update_filter(convert_data) print "adding odom" odom_scan = self.addOdom() matches = [[] for ii in range(len(self.filter_scans[1]))] for ii in range(1, self.filter_size - 1): print "calculating cost mat" cost_mat = self.makeCostMatrix(self.filter_scans[0], self.filter_scans[ii]) print len(cost_mat) print "doing matching" #indexes = m.compute(cost_mat) row_ind, col_ind = linear_sum_assignment(cost_mat) print "done matching for scan: ", ii #for item in indexes: # matches[item[0]].append(self.filter_scans[ii][item[1]]) for jj, item in enumerate(row_ind): matches[item].append(self.filter_scans[ii][jj]) matched_scans = [] print "processing" for item in matches: res = apply(zip, item) matched_scans.append(res) filtered_points = [] print "computing avg and std" for item in matched_scans: x_avg = np.mean(item[0]) x_std = np.std(item[0]) y_avg = np.mean(item[1]) y_std = np.std(item[1]) if x_std > self.max_var and y_std > self.max_var: filtered_points.append([x_avg, y_avg, 0]) print filtered_points pc_cloud = PointCloud2() pc_cloud = pc2.create_cloud_xyz32(pc_cloud.header, filtered_points) publisher.publish(pc_cloud) print "ending filter" else: print "building filter" self.filter_scans.append(convert_data)
def assign_items(items, tracks): """Given a list of Items and a list of TrackInfo objects, find the best mapping between them. Returns a mapping from Items to TrackInfo objects, a set of extra Items, and a set of extra TrackInfo objects. These "extra" objects occur when there is an unequal number of objects of the two types. """ # Construct the cost matrix. costs = [] for item in items: row = [] for i, track in enumerate(tracks): row.append(track_distance(item, track)) costs.append(row) # Find a minimum-cost bipartite matching. matching = Munkres().compute(costs) # Produce the output matching. mapping = dict((items[i], tracks[j]) for (i, j) in matching) extra_items = list(set(items) - set(mapping.keys())) extra_items.sort(key=lambda i: (i.disc, i.track, i.title)) extra_tracks = list(set(tracks) - set(mapping.values())) extra_tracks.sort(key=lambda t: (t.index, t.title)) return mapping, extra_items, extra_tracks
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) t = self.tournament self.min_score = t.pref('adj_min_score') self.max_score = t.pref('adj_max_score') self.min_voting_score = t.pref('adj_min_voting_score') self.conflict_penalty = t.pref('adj_conflict_penalty') self.history_penalty = t.pref('adj_history_penalty') self.no_panellists = t.pref('no_panellist_position') self.no_trainees = t.pref('no_trainee_position') self.duplicate_allocations = t.pref('duplicate_adjs') self.feedback_weight = self.round.feedback_weight self.extra_messages = "" # Surfaced to users for non-error disclosures self.munkres = Munkres()
def order_items(items, trackinfo): """Orders the items based on how they match some canonical track information. This always produces a result if the numbers of tracks match. """ # Make sure lengths match. if len(items) != len(trackinfo): return None # Construct the cost matrix. costs = [] for cur_item in items: row = [] for i, canon_item in enumerate(trackinfo): row.append(track_distance(cur_item, canon_item, i + 1)) costs.append(row) # Find a minimum-cost bipartite matching. matching = Munkres().compute(costs) # Order items based on the matching. ordered_items = [None] * len(items) for cur_idx, canon_idx in matching: ordered_items[canon_idx] = items[cur_idx] return ordered_items
def run(): hungarian_input_dict = convertToSquareMatrix(get_alg_members(settings.GID), get_alg_time_blocks(settings.GID), get_alg_leader_groups(settings.GID), "y_leaders") #return {'final_dict': hungarian_input_dict} count_matrix = hungarian_input_dict["ct_matrix"] m = Munkres() invert(count_matrix) #return hungarian_input_dict indexes = m.compute(count_matrix) ret_dict = { 'indexes': indexes, 'member_matrix': hungarian_input_dict["mem_id_matrix"], 'tb_map': hungarian_input_dict["tb_map"], 'lg_map': hungarian_input_dict["lg_map"], 'ct_matrix': count_matrix } return ret_dict
def pnl(source_train, source_test, target_train, target_test, label1, label2, label3, class_num, classes): # 训练Source KNN分类器 clf = neighbors.KNeighborsClassifier() clf.fit(source_train, label1) source_predict = clf.predict(source_train) # 获得混淆矩阵 cm = confusion_matrix(label1, source_predict, classes) source_predict = clf.predict(source_test) # 获得Semi-label semi_label = get_semi_label(cm, class_num) # Target数据KMeans聚类 len2 = target_train.shape[0] target_cluster = KMeans(n_clusters=class_num).fit(target_train) target_predict = target_cluster.labels_ target_predict = get_predict(target_predict, classes, len2) # 获取WLG wlg = get_wlg(class_num, len2, classes, semi_label, source_predict, target_predict) # 匈牙利算法 munk = Munkres() indexes = munk.compute(wlg) # Labelling final_predict = target_cluster.labels_ result = [] for i in range(0, len2): cInd = final_predict[i] result.append(classes[indexes[cInd][1]]) # 评价参数 accuracy = metrics.accuracy_score(label2, result) recall = metrics.recall_score(label2, result, average='macro') f1 = metrics.f1_score(label2, result, average='weighted') precision = metrics.precision_score(label2, result, average='weighted') print("PNL Labelling:", accuracy, recall, f1, precision) # Recognition len3 = target_test.shape[0] final_predict = target_cluster.predict(target_test) result = [] for i in range(0, len3): cInd = final_predict[i] result.append(classes[indexes[cInd][1]]) # 评价参数 accuracy = metrics.accuracy_score(label3, result) recall = metrics.recall_score(label3, result, average='macro') f1 = metrics.f1_score(label3, result, average='weighted') precision = metrics.precision_score(label3, result, average='weighted') print("PNL-Recognition:", accuracy, recall, f1, precision)
def performmultiparsing(S,L): '''Pass in the S=> part confidencemaps L=>PAF maps''' #Let Dj be the set of candidate parts for multiple people for the jth part #building the D set D = np.zeros((S.shape[0],100, 2)) #(a,100,2) D must contain locations for the ath part, 100=max number of ath parts and 2 is the 2D dimension Dcounters = np.zeros(S.shape[0], np.uint8) #stores the number of points found for each part #speeding things up Snumpy = S.clone().detach().numpy() #cython implementation D, Dcounters = get_D_set_from_S_field(Snumpy, confidencemappartsthreshold) #building E Matrix holding E-values for each #candidate limb associations = {} for i, limb in enumerate(skeleton): partindexfrom, partindexto = limb[0]-1, limb[1]-1 #limb part indexes start from 1 D1, D2 = D[partindexfrom], D[partindexto] Dcounters1, Dcounters2 = Dcounters[partindexfrom], Dcounters[partindexto] if ((Dcounters1 == 0) | (Dcounters2 == 0)):#if any has no-points, makes no sense look at this limb continue if (Dcounters1 != Dcounters2): continue EMtx = np.zeros((int(Dcounters1), int(Dcounters2)), dtype=float) Lpart = L[i] #filling EMtx for a in range(int(Dcounters1)): for b in range(int(Dcounters2)): dj1 = D1[a] dj2 = D2[b] if ((dj1[0] - dj2[0])== 0) & ((dj1[1] - dj2[1])==0): continue EMtx[a,b] = E(dj1, dj2, i, L) #negate values for maximization EMtx *= -1 #once filled we need to find the best combination of a's and b's # by finding the max values of E for a and b combinations with the # constraint that a_i can only be connected to b_j and the same # rules for b values # Munkres or Hungarian Algorithm munkres = Munkres() indices = munkres.compute(EMtx) #storing index association associations[i] = indices #with this info we have enough info to rebuild the skeletons for all the people in the image return associations, D, Dcounters
def main(cmd, dataset, run, conf, make_videos): if make_videos: from visualize_tracking import render_video from config import DatasetConfig from apply_mask import Masker mask = Masker(dataset) dc = DatasetConfig(dataset) config_path = "{rp}{ds}_{rn}/world_tracking_optimization.pklz".format(rp=runs_path, ds=dataset, rn=run) if isfile(config_path): config = load(config_path) else: #raise(ValueError("No world tracking optimized configuration exists at {}".format(config_path))) config = WorldTrackingConfig(default_config) calib = Calibration(dataset) munkres = Munkres() ts = Timestamps(dataset) start_stop = None if cmd == "findvids": from glob import glob vidnames = glob('{dsp}{ds}/videos/*.mkv'.format(dsp=datasets_path, ds=dataset)) vidnames = [right_remove(x.split('/')[-1], '.mkv') for x in vidnames] vidnames.sort() outfolder = '{}{}_{}/tracks_world/'.format(runs_path, dataset, run) mkdir(outfolder) else: vidnames = [cmd] outfolder = './' start_stop = (0,500) for v in vidnames: print_flush(v) out_path = "{of}{v}_tracks.pklz".format(of=outfolder, v=v) print_flush("Loading data...") det_path = "{rp}{ds}_{rn}/detections_world/{v}_world.csv".format(rp=runs_path, ds=dataset, rn=run, v=v) detections3D = pd.read_csv(det_path) klt_path = det_path.replace('.csv', '_klt.pklz') klts = load(klt_path) print_flush("Tracking...") tracks = make_tracks(dataset, v, detections3D, klts, munkres, ts, calib, config, start_stop=start_stop) print_flush("Saving tracks...") save(tracks, out_path) if make_videos: vidpath = "{dsp}{ds}/videos/{v}.mkv".format(dsp=datasets_path, ds=dataset, v=v) print_flush("Rendering video...") render_video(tracks, vidpath, out_path.replace('.pklz','.mp4'), calib=calib, mask=mask, fps=dc.get('video_fps')) print_flush("Done!")
def main(cmd, dataset, run, conf, make_videos): from pathlib import Path if make_videos: from visualize_tracking import render_video from config import DatasetConfig from apply_mask import Masker mask = Masker(dataset) dc = DatasetConfig(dataset) config_path = runs_path / "{}_{}".format(dataset,run) / "world_tracking_optimization.pklz" if config_path.is_file(): config = load(config_path) else: #raise(ValueError("No world tracking optimized configuration exists at {}".format(config_path))) config = WorldTrackingConfig(default_config) calib = Calibration(dataset) munkres = Munkres() ts = Timestamps(dataset) start_stop = None if cmd == "findvids": vidnames = (datasets_path / dataset / "videos").glob('*.mkv') vidnames = [x.stem for x in vidnames] vidnames.sort() outfolder = runs_path / "{}_{}".format(dataset,run) / "tracks_world" mkdir(outfolder) else: vidnames = [cmd] outfolder = Path('./') start_stop = (0,500) for v in vidnames: print_flush(v) out_path = outfolder / (v+'_tracks.pklz') print_flush("Loading data...") det_path = runs_path / "{}_{}".format(dataset,run) / "detections_world" / (v+'_world.csv') detections3D = pd.read_csv(det_path) klt_path = det_path.with_name(det_path.stem + '_klt.pklz') klts = load(klt_path) print_flush("Tracking...") tracks = make_tracks(dataset, v, detections3D, klts, munkres, ts, calib, config, start_stop=start_stop) print_flush("Saving tracks...") save(tracks, out_path) if make_videos: vidpath = datasets_path / dataset / "videos" / (v+'.mkv') print_flush("Rendering video...") render_video(tracks, vidpath, out_path.with_suffix('.mp4'), calib=calib, mask=mask, fps=dc.get('video_fps')) print_flush("Done!")
def cleanup(self): nX = self.X.nodes() nY = self.Y.nodes() C = np.empty([nX, nY]) for i in range(nX): for j in range(nY): C[i, j] = 1.0 - self.M[i, j] m = Munkres() indexmatch = m.compute(C) self.f = [indexmatch[i][1] for i in range(len(indexmatch))] self.f = self.f[0:nX] if (self.swapped): g = [] for i in range(nY): g[f[i]] = i self.f = g self.swap(self)
def mycorrelation(x, y, method='Pearson'): """Evaluate correlation Args: x: data to be sorted y: target data Returns: corr_sort: correlation matrix between x and y (after sorting) sort_idx: sorting index x_sort: x after sorting method: correlation method ('Pearson' or 'Spearman') """ # print("Calculating correlation...") x = x.copy() y = y.copy() dim = x.shape[0] # Calculate correlation ----------------------------------- if method == 'Pearson': corr = np.corrcoef(y, x) corr = corr[0:dim, dim:] elif method == 'Spearman': corr, pvalue = sp.stats.spearmanr(y.T, x.T) corr = corr[0:dim, dim:] # Sort ---------------------------------------------------- munk = Munkres() indexes = munk.compute(-np.absolute(corr)) sort_idx = np.zeros(dim) x_sort = np.zeros(x.shape) for i in range(dim): sort_idx[i] = indexes[i][1] x_sort[i, :] = x[indexes[i][1], :] # Re-calculate correlation -------------------------------- if method == 'Pearson': corr_sort = np.corrcoef(y, x_sort) corr_sort = corr_sort[0:dim, dim:] elif method == 'Spearman': corr_sort, pvalue = sp.stats.spearmanr(y.T, x_sort.T) corr_sort = corr_sort[0:dim, dim:] return corr_sort, sort_idx, x_sort
def pnl(): # 获取数据 features, labels, classes, class_num = get_data() # 划分训练集和测试集 # source选取right knee, target选取necklace source_train, source_test, target_test, target_train, label_train, label_test, test_len, train_len = data_devide( features, labels, 0.5, 1, 0) # 训练Source KNN分类器 clf = neighbors.KNeighborsClassifier() clf.fit(source_train, label_train) # 获取source单独训练正确率 source_predict = clf.predict(source_test) source_cluster_accuracy = get_accuracy(label_test, source_predict, test_len) print("Source KNN预测正确率", source_cluster_accuracy) # 获得混淆矩阵 cm = confusion_matrix(label_test, source_predict, classes) # print("Source KNN混淆矩阵:\n", cm) # 获得Semi-label semi_label = get_semi_label(cm, class_num) # Target数据KMeans聚类 target_cluster = KMeans(n_clusters=17).fit(target_test) target_predict = target_cluster.labels_ target_predict = get_predict(target_predict, classes, test_len) # 获取WLG wlg = get_wlg(class_num, test_len, classes, semi_label, source_predict, target_predict) # 匈牙利算法 munk = Munkres() indexes = munk.compute(wlg) result = [] for i in range(0, test_len): ci = target_predict[i] cInd = classes.index(ci) result.append(classes[indexes[cInd][1]]) final_accuracy = get_accuracy(label_test, result, test_len) print("PNL Target labelling正确率:", final_accuracy) tcm = confusion_matrix(label_test, target_predict, classes) return wlg, tcm
def solveGAP(M, t): w = deepcopy(M) m = w.tolist() # for i in range(len(m)): # for j in range(len(m[i])): # if m[i][j] == 1e9: # m[i][j] = DISALLOWED #print_matrix(m) algo = Munkres() indexes = [] try: indexes = algo.compute(m) except: print('Error in solveGAP') return [] return indexes
def trackObjects(self, objects): # if no object found, skip the rest of processing if len(objects) == 0: return # If any object is registred in the db, assign registerd ID to the most similar object in the current image if len(self.objectDB) > 0: # Create a matix of cosine distance cos_sim_matrix = [[ distance.cosine(objects[j].feature, self.objectDB[i].feature) for j in range(len(objects)) ] for i in range(len(self.objectDB))] # solve feature matching problem by Hungarian assignment algorithm hangarian = Munkres() combination = hangarian.compute(cos_sim_matrix) # assign ID to the object pairs based on assignment matrix for dbIdx, objIdx in combination: if distance.cosine(objects[objIdx].feature, self.objectDB[dbIdx].feature ) < self.similarityThreshold: objects[objIdx].id = self.objectDB[ dbIdx].id # assign an ID self.objectDB[dbIdx].feature = objects[ objIdx].feature # update the feature vector in DB with the latest vector (to make tracking easier) self.objectDB[dbIdx].time = time.monotonic( ) # update last found time xmin, ymin, xmax, ymax = objects[objIdx].pos self.objectDB[dbIdx].trajectory.append([ (xmin + xmax) // 2, (ymin + ymax) // 2 ]) # record position history as trajectory objects[objIdx].trajectory = self.objectDB[ dbIdx].trajectory # Register the new objects which has no ID yet for obj in objects: if obj.id == -1: # no similar objects is registred in feature_db obj.id = self.objectid self.objectDB.append(obj) # register a new feature to the db self.objectDB[-1].time = time.monotonic() xmin, ymin, xmax, ymax = obj.pos self.objectDB[-1].trajectory = [[ (xmin + xmax) // 2, (ymin + ymax) // 2 ]] # position history for trajectory line obj.trajectory = self.objectDB[-1].trajectory self.objectid += 1
def lsa_solve_munkres(costs): """Solves the LSA problem using the Munkres library.""" from munkres import Munkres m = Munkres() # The munkres package may hang if the problem is not feasible. # Therefore, add expensive edges instead of using munkres.DISALLOWED. finite_costs = add_expensive_edges(costs) # Ensure that matrix is square. finite_costs = _zero_pad_to_square(finite_costs) indices = np.array(m.compute(finite_costs), dtype=int) # Exclude extra matches from extension to square matrix. indices = indices[(indices[:, 0] < costs.shape[0]) & (indices[:, 1] < costs.shape[1])] rids, cids = indices[:, 0], indices[:, 1] rids, cids = _exclude_missing_edges(costs, rids, cids) return rids, cids
def hungarian_matching(self, GT_coordinates, det_coordinates, verbose=False): n_dets = det_coordinates.shape[0] n_gts = GT_coordinates.shape[0] print 'n_dets = %d, n_gts = %d' % (n_dets, n_gts) n_max = max(n_dets, n_gts) matrix = np.zeros((n_max, n_max)) + 1 for i_d in range(n_dets): for i_gt in range(n_gts): if ((det_coordinates[i_d, 0] - GT_coordinates[i_gt, 0])**2 + (det_coordinates[i_d, 1] - GT_coordinates[i_gt, 1])** 2) <= self.radius_match**2: matrix[i_d, i_gt] = 0 m = Munkres() indexes = m.compute(copy.copy(matrix)) total = 0 TP = [] #True positive FP = [] #False positive FN = [] #False negative for row, column in indexes: value = matrix[row][column] total += value if verbose: print '(%d, %d) -> %d' % (row, column, value) if value == 0: TP.append((int(det_coordinates[row, 0]), int(det_coordinates[row, 1]))) if value > 0: if row < n_dets: FP.append((int(det_coordinates[row, 0]), int(det_coordinates[row, 1]))) if column < n_gts: FN.append((int(GT_coordinates[column, 0]), int(GT_coordinates[column, 1]))) if verbose: print 'total cost: %d' % total return total, np.asarray(TP), np.asarray(FP), np.asarray(FN)
def mrProfit(matrix): cost_matrix = [] for row in matrix: cost_row = [] for col in row: cost_row += [sys.maxsize - col] cost_matrix += [cost_row] m = Munkres() indexes = m.compute(cost_matrix) print_matrix(matrix, msg='Highest profit through this matrix:') total = 0 for row, column in indexes: value = matrix[row][column] total += value print('(%d, %d) -> %d' % (row, column, value)) print('total profit=%d' % total)
def munkres_maximise_assignment(self, matrix): # call to Munkres algorithm (a variant of the Hungarian algorithm) # maximise "profit" along rows of a matrix cost_matrix = munkres.make_cost_matrix(matrix, lambda cost: sys.maxint - cost) m = Munkres() indexes = m.compute(cost_matrix) #print_matrix(matrix, msg='Highest profit through this matrix:') useful_indices = [] total = 0 for row, column in indexes: value = matrix[row][column] total += value #print '(%d, %d) -> %d' % (row, column, value) useful_indices.append(((row, column))) print 'total score (hungarian) =%d' % total return total, useful_indices
def match_detections_to_tracks_global_nearest_neighbour( self, objects_tracked, objects_detected): """ Match detected objects to existing object tracks using a global nearest neighbour data association """ matched_tracks = {} # Populate match_dist matrix of mahalanobis_dist between every detection and every track match_dist = [ ] # matrix of probability of matching between all people and all detections. eligable_detections = [ ] # Only include detections in match_dist matrix if they're in range of at least one track to speed up munkres for detect in objects_detected: at_least_one_track_in_range = False new_row = [] for track in objects_tracked: # Use mahalanobis dist to do matching cov = track.filtered_state_covariances[0][ 0] + track.var_obs # cov_xx == cov_yy == cov mahalanobis_dist = math.sqrt( ((detect.pos_x - track.pos_x)**2 + (detect.pos_y - track.pos_y)**2) / cov ) # ref: http://en.wikipedia.org/wiki/Mahalanobis_distance#Definition_and_properties if mahalanobis_dist < self.mahalanobis_dist_gate: new_row.append(mahalanobis_dist) at_least_one_track_in_range = True else: new_row.append(self.max_cost) # If the detection is within range of at least one person track, add it as an eligable detection in the munkres matching if at_least_one_track_in_range: match_dist.append(new_row) eligable_detections.append(detect) # Run munkres on match_dist to get the lowest cost assignment if match_dist: munkres = Munkres() # self.pad_matrix(match_dist, pad_value=self.max_cost) # I found no difference when padding it indexes = munkres.compute(match_dist) for elig_detect_index, track_index in indexes: if match_dist[elig_detect_index][ track_index] < self.mahalanobis_dist_gate: detect = eligable_detections[elig_detect_index] track = objects_tracked[track_index] matched_tracks[track] = detect return matched_tracks
def generate_pairings(self): from munkres import Munkres ## create a matrix of mu values matrix = defaultdict(OrderedDict) players = self.all() for player in players: if matrix.get(player.judge, {}).get(player.target, None): print "ERROR, already in matrix: ", player.judge, player.target, player.mu matrix[player.judge][player.target] = player.mu ordered_matrix = OrderedDict( sorted(matrix.items(), key=lambda x: x[0].user_id)) players = matrix.keys() ## create a matrix of combined mu values represnted as costs (lower is better) munkres_matrix = [] key_matrix = [] for player1 in players: munkres_row = [] key_matrix_row = [] for player2 in players: mu_p1_p2 = ordered_matrix.get(player1, {}).get(player2, 0) mu_p2_p1 = ordered_matrix.get(player1, {}).get(player2, 0) munkres_row.append(1000 - mu_p1_p2 - mu_p2_p1) key_matrix_row.append((player1, player2)) munkres_matrix.append(munkres_row) key_matrix.append(key_matrix_row) ## use Munkres to figure out the pairings to achieve highest combined mu values m = Munkres() munkres_indexes = m.compute(munkres_matrix) pairings = [] ## save the results as Parings for row, column in munkres_indexes: print 'output: (%d, %d)' % (row, column) combined_mu = 1000 - munkres_matrix[row][column] player1 = key_matrix[row][column][0] player2 = key_matrix[row][column][1] new_pairing = Pairing(a=player1, b=player2, combined_mu=combined_mu) new_pairing.save() pairings.append(new_pairing) print '(%d, %d) -> %s %s, %s %s -> %d' % ( row, column, player1.first_name, player1.last_name, player2.first_name, player2.last_name, combined_mu) return pairings
def compute_accuracy(y_pred, y_t, num_class): # compute the accuracy using Hungarian algorithm m = Munkres() tot_cl = num_class mat = np.zeros((tot_cl, tot_cl)) for i in range(tot_cl): for j in range(tot_cl): mat[i][j] = np.sum(np.logical_and(y_pred == i, y_t == j)) indexes = m.compute(-mat) corresp = [] for i in range(tot_cl): corresp.append(indexes[i][1]) pred_corresp = [corresp[int(predicted)] for predicted in y_pred] acc = np.sum(pred_corresp == y_t) / float(len(y_t)) return acc
def maximizeTrace(mat): #--- Import modules # import numpy as np # import copy as cp # from munkres import Munkres #-- Main algorithm assert mat.shape[0] == mat.shape[1] # Matrix should be square #--- Use of the Kuhn-Munkres algorithm (or Hungarian Algorithm) m = Munkres() # Convert the cost minimization problem into a profit maximization one costMatrix = np.ones(np.shape(mat))*np.amax(mat) - mat indexes = m.compute(cp.deepcopy(costMatrix)) indexes= np.array(indexes) return (indexes[:,1]).astype(int)
def application(environ, start_response): wsgi_input = environ['wsgi.input'] length = int(environ.get('CONTENT_LENGTH', 0)) data = json.loads(wsgi_input.read(length)) result = Munkres().compute(data) start_response('200 OK', [('Content-Type', 'application/json')]) return [bytes(json.dumps(result), 'utf-8')]
def __init__(self, config): # max DUE/CUE transmit power in dBm self.dB_Pd_max = config["dB_Pd_max"] self.dB_Pc_max = config["dB_Pc_max"] # large scale fading parameters self.stdV2V = config["stdV2V"] self.stdV2I = config["stdV2I"] # cell parameter setup self.freq = config["freq"] self.radius = config["radius"] self.bsHgt = config["bsHgt"] self.disBstoHwy = config["disBstoHwy"] self.bsAntGain = config["bsAntGain"] self.bsNoiseFigure = config["bsNoiseFigure"] # vehicle parameter setup self.vehHgt = config["vehHgt"] self.vehAntGain = config["vehAntGain"] self.vehNoiseFigure = config["vehNoiseFigure"] # Highway parameter setup self.numLane = config["numLane"] self.laneWidth = config["laneWidth"] # QoS parameters for CUE and DUE self.r0 = config["r0"] self.dB_gamma0 = config["dB_gamma0"] self.p0 = config["p0"] self.dB_sigma2 = config["dB_sigma2"] # dB to linear scale conversion self.sig2 = 10**(self.dB_sigma2 / 10) self.gamma0 = 10**(self.dB_gamma0 / 10) self.Pd_max = 10**(self.dB_Pd_max / 10) self.Pc_max = 10**(self.dB_Pc_max / 10) # CUE/DUE number setup self.numDUE = config["numDUE"] self.numCUE = config["numCUE"] # initialize vehicle sampler d0 = np.sqrt(self.radius**2 - self.disBstoHwy**2) self.vehicle_generator = VehicleGenerator(d0, self.laneWidth, self.numLane, self.disBstoHwy) # initialize channel large scale fading generator self.fading_generator = HwyChannelLargeScaleFadingGenerator( self.stdV2I, self.stdV2V, self.vehHgt, self.bsHgt, self.freq, self.vehAntGain, self.bsAntGain, self.bsNoiseFigure, self.vehNoiseFigure) # initialize Hungrian solver self.munkres = Munkres()
def relabel(array1, array2): # this function returns relabeled array2 if len(array1) == len(array2): # set1 is the unique set of array1 set1 = set(array1) # u1 is the unique list of array1 u1 = list(set1) # set2 is the unique set of array2 set2 = set(array2) # u2 is the unique list of array2 u2 = list(set2) # matirx is the Corresponding matrix between u1 and u2 matrix = [[0 for i in range(len(u2))] for j in range(len(u1))] for i in range(len(array1)): # item_1 is the index of array1's element in u1 item_1 = u1.index(array1[i]) # item_2 is the index of array2's element in u2 item_2 = u2.index(array2[i]) # this situation means 1 correspondence between item_1 and item_2 is observed # so corresponding location in corresponding matrix is incremented matrix[item_1][item_2] = matrix[item_1][item_2] + 1 cost_matrix = benefit_to_cost(matrix) # Munkers library solve the cost minimization problem # but I would like to solve benefit maxmization problem # so convert benefit matrix into cost matrix # create mukres object m = Munkres() # get the most corresponded correspondance indexes = m.compute(cost_matrix) # I use array2 as Integer array so, convert it in case array2 = map(int, array2) # call replaced function to replace array2 according to object indexes replaced_matrix = replaced(array2, u1, u2, indexes) return replaced_matrix