[英]Intersection over Union over two segments
Let's say I have this two Dataframes: Groundtruth and Prediction .假设我有这两个数据框: Groundtruth和Prediction 。
Each Dataframe has 3 columns;每个 Dataframe 有 3 列; Action, Start and End .行动,开始和结束。
**Prediction :**
Action | Start | End
-------------------------
3 | 0 | 10
2 | 10 | 70
3 | 80 | 120
0 | 120 | 350
7 | 400 | 610
...
**Groundtruth :**
Action | Start | End
-------------------------
2 | 20 | 140
0 | 150 | 340
6 | 420 | 600
...
I want to compute the Intersection-over-Union (IoU) over these two dataframes using all columns, meaning Action first to see if it's a correct prediction or not plus the start and the end segments for each action to see if starts and ends correctly.我想使用所有列在这两个数据帧上计算交集比联合(IoU),这意味着首先操作以查看它是否是正确的预测,再加上每个操作的开始和结束段以查看是否正确开始和结束.
Here's my code:这是我的代码:
def compute_iou(y_pred, y_true):
y_pred = y_pred.flatten()
y_true = y_true.flatten()
cm = confusion_matrix(y_true, y_pred)
intersection = np.diag(cm)
ground_truth_set = cm.sum(axis=1)
predicted_set = cm.sum(axis=0)
union = ground_truth_set + predicted_set - intersection
IoU = intersection / union
for i in range(len(IoU)):
if (IoU[i]>0.5):
IoU[i] = 1
return round(np.mean(IoU)*100, 3)
This works when I want to calculate the IoU over the actions column.当我想在操作列上计算 IoU 时,这很有效。
Now how can I adapt this so I can get IoU to get the overlapping segments over the start and end columns?现在我该如何调整它,以便我可以让 IoU 在开始列和结束列上获得重叠段?
PS: Groundtruth and Prediction dataframes don't have the same number of rows. PS:Groundtruth 和 Prediction 数据帧的行数不同。
(post edit) (后期编辑)
The calculation is broken into three cases:计算分为三种情况:
Here's the code:这是代码:
df = pd.merge(pred, groundtruth, on = "Action", how = "outer", suffixes = ["_pred", "_gt"])
overlap = df[(df.Start_pred < df.End_gt) & (df.Start_gt < df.End_pred)]
intersection = (overlap[["End_pred", "End_gt"]].min(axis=1) - overlap[["Start_pred", "Start_gt"]].max(axis=1)).sum()
union_where_overlap = (overlap[["End_pred", "End_gt"]].max(axis=1) - overlap[["Start_pred", "Start_gt"]].\
min(axis=1)).sum()
no_hit = df[df.isna().sum(axis=1) > 0]
union_no_hit = (no_hit[["End_pred", "End_gt"]].max(axis=1) - no_hit[["Start_pred", "Start_gt"]].min(axis=1)).sum()
no_overlap = df[~((df.Start_pred < df.End_gt) & (df.Start_gt < df.End_pred))].dropna()
union_no_overlap = ((no_overlap.End_pred - no_overlap.Start_pred) + (no_overlap.End_gt - no_overlap.Start_gt)).sum()
IoU = intersection / (union_no_hit + union_where_overlap + union_no_overlap)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.