-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
83 lines (64 loc) · 2.98 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pandas as pd
import glob
import torch
import numpy as np
# LRAP. Instance-level average
# Assume float preds [BxC], labels [BxC] of 0 or 1
def LRAP(preds, labels):
# Ranks of the predictions
ranked_classes = torch.argsort(preds, dim=-1, descending=True)
# i, j corresponds to rank of prediction in row i
class_ranks = torch.zeros_like(ranked_classes)
for i in range(ranked_classes.size(0)):
for j in range(ranked_classes.size(1)):
class_ranks[i, ranked_classes[i][j]] = j + 1
# Mask out to only use the ranks of relevant GT labels
ground_truth_ranks = class_ranks * labels + (1e6) * (1 - labels)
# All the GT ranks are in front now
sorted_ground_truth_ranks, _ = torch.sort(ground_truth_ranks, dim=-1, descending=False)
pos_matrix = torch.tensor(np.array([i+1 for i in range(labels.size(-1))])).unsqueeze(0)
score_matrix = pos_matrix / sorted_ground_truth_ranks
score_mask_matrix, _ = torch.sort(labels, dim=-1, descending=True)
scores = score_matrix * score_mask_matrix
score = (scores.sum(-1) / labels.sum(-1)).mean()
return score.item()
# label-level average
# Assume float preds [BxC], labels [BxC] of 0 or 1
def LWLRAP(preds, labels):
# Ranks of the predictions
ranked_classes = torch.argsort(preds, dim=-1, descending=True)
# i, j corresponds to rank of prediction in row i
class_ranks = torch.zeros_like(ranked_classes)
for i in range(ranked_classes.size(0)):
for j in range(ranked_classes.size(1)):
class_ranks[i, ranked_classes[i][j]] = j + 1
# Mask out to only use the ranks of relevant GT labels
ground_truth_ranks = class_ranks * labels + (1e6) * (1 - labels)
# All the GT ranks are in front now
sorted_ground_truth_ranks, _ = torch.sort(ground_truth_ranks, dim=-1, descending=False)
# Number of GT labels per instance
num_labels = labels.sum(-1)
pos_matrix = torch.tensor(np.array([i+1 for i in range(labels.size(-1))])).unsqueeze(0)
score_matrix = pos_matrix / sorted_ground_truth_ranks
score_mask_matrix, _ = torch.sort(labels, dim=-1, descending=True)
scores = score_matrix * score_mask_matrix
score = scores.sum() / labels.sum()
return score.item()
def score(submission):
traint = pd.read_csv( 'data/train_tp.csv' )
targets = np.zeros((len(submission), 24))
guesses = np.zeros((len(submission), 24))
for i in range(len(submission)):
ex = submission.iloc[i]
subset = traint[traint['recording_id'] == ex.recording_id]
target = np.zeros(24, dtype=np.float32)
for j in range(len(subset)):
s = subset.iloc[j]
target[int(s.species_id)] = 1.0
targets[i] = target
guess = np.zeros(24)
for j in range(24):
guess[j] = ex['s{}'.format(j)]
guesses[i] = guess
targets, guesses = torch.from_numpy(targets), torch.from_numpy(guesses)
return (LRAP(guesses, targets), LWLRAP(guesses, targets))