本文整理匯總了Python中sklearn.utils.linear_assignment_.linear_assignment方法的典型用法代碼示例。如果您正苦於以下問題:Python linear_assignment_.linear_assignment方法的具體用法?Python linear_assignment_.linear_assignment怎麽用?Python linear_assignment_.linear_assignment使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.utils.linear_assignment_
的用法示例。
在下文中一共展示了linear_assignment_.linear_assignment方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: acc
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
def 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
示例2: ceafe
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
def ceafe(clusters, gold_clusters):
u"""
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)
示例3: __init__
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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)
示例4: cluster_acc
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
示例5: cluster_acc
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed.
(Taken from https://github.com/XifengGuo/IDEC-toy/blob/master/DEC.py)
# 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) # Optimal label mapping based on the Hungarian algorithm
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
示例6: cluster_acc
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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], int(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
示例7: ceafe
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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)
示例8: associate_detections_to_trackers
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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)
'''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.'''
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)
示例9: associate_detections_to_trackers
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
def associate_detections_to_trackers(detections, trackers, affinity_mat,
affinity_threshold):
"""
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, 2), dtype=int)
matched_indices = linear_assignment(-affinity_mat)
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 affinity_mat[m[0], m[1]] < affinity_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)
示例10: associate_detections_to_trackers
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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:
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)
開發者ID:Akhtar303,項目名稱:Vehicle-Detection-and-Tracking-Usig-YOLO-and-Deep-Sort-with-Keras-and-Tensorflow,代碼行數:39,代碼來源:sort.py
示例11: cluster_acc
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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], int(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
示例12: associate_detections_to_trackers
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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:
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)
示例13: linear_assignment
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
def linear_assignment(cost_matrix, thresh):
"""
Simple linear assignment
:type cost_matrix: np.ndarray
:type thresh: float
:return: matches, unmatched_a, unmatched_b
"""
if cost_matrix.size == 0:
return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1]))
cost_matrix[cost_matrix > thresh] = thresh + 1e-4
indices = linear_assignment_.linear_assignment(cost_matrix)
return _indices_to_matches(cost_matrix, indices, thresh)
示例14: best_map
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
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)
示例15: ceafe_matching
# 需要導入模塊: from sklearn.utils import linear_assignment_ [as 別名]
# 或者: from sklearn.utils.linear_assignment_ import linear_assignment [as 別名]
def ceafe_matching(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])
return linear_assignment(-scores), scores