Spaces:
Sleeping
Sleeping
fix bug and avoid zero division
Browse files- matching_series.py +12 -5
matching_series.py
CHANGED
@@ -142,6 +142,8 @@ class matching_series(evaluate.Metric):
|
|
142 |
return_coverages: bool = False,
|
143 |
return_all: bool = False,
|
144 |
dtype=np.float32,
|
|
|
|
|
145 |
):
|
146 |
"""
|
147 |
Compute the scores of the module given the predictions and references
|
@@ -162,6 +164,11 @@ class matching_series(evaluate.Metric):
|
|
162 |
return_coverages = True
|
163 |
predictions = np.array(predictions).astype(dtype)
|
164 |
references = np.array(references).astype(dtype)
|
|
|
|
|
|
|
|
|
|
|
165 |
if predictions.shape[1:] != references.shape[1:]:
|
166 |
raise ValueError(
|
167 |
"The number of features in the predictions and references should be the same. predictions: {}, references: {}".format(
|
@@ -206,7 +213,7 @@ class matching_series(evaluate.Metric):
|
|
206 |
)
|
207 |
distance[i : i + batch_size, j : j + batch_size] = d
|
208 |
else:
|
209 |
-
distance = self._compute_metric(predictions[:, None], references, metric=metric, axis
|
210 |
|
211 |
index_distance = distance.diagonal(axis1=0, axis2=1).mean().item()
|
212 |
|
@@ -225,13 +232,13 @@ class matching_series(evaluate.Metric):
|
|
225 |
best_match_inv = np.argmin(distance_mean, axis=0)
|
226 |
recall_distance = distance_mean[best_match_inv, np.arange(len(best_match_inv))].mean().item()
|
227 |
|
228 |
-
f1_distance = 2 / (1 / precision_distance + 1 / recall_distance)
|
229 |
mean_distance = (precision_distance + recall_distance) / 2
|
230 |
|
231 |
# matching precision, recall and f1
|
232 |
matching_recall = np.unique(best_match).size / len(best_match_inv)
|
233 |
matching_precision = np.unique(best_match_inv).size / len(best_match)
|
234 |
-
matching_f1 = 2 / (1 / matching_precision + 1 / matching_recall)
|
235 |
|
236 |
# take matching for each feature and compute metrics for them
|
237 |
precision_distance_features = []
|
@@ -251,7 +258,7 @@ class matching_series(evaluate.Metric):
|
|
251 |
precision_distance_f = (distance_f[np.arange(len(best_match_f)), best_match_f].mean()).item()
|
252 |
best_match_inv_f = np.argmin(distance_f, axis=0)
|
253 |
recall_distance_f = (distance_f[best_match_inv_f, np.arange(len(best_match_inv_f))].mean()).item()
|
254 |
-
f1_distance_f = 2 / (1 / precision_distance_f + 1 / recall_distance_f)
|
255 |
mean_distance_f = (precision_distance_f + recall_distance_f) / 2
|
256 |
precision_distance_features.append(precision_distance_f)
|
257 |
recall_distance_features.append(recall_distance_f)
|
@@ -261,7 +268,7 @@ class matching_series(evaluate.Metric):
|
|
261 |
|
262 |
matching_recall_f = np.unique(best_match_f).size / len(best_match_f)
|
263 |
matching_precision_f = np.unique(best_match_inv_f).size / len(best_match_inv_f)
|
264 |
-
matching_f1_f = 2 / (1 / matching_precision_f + 1 / matching_recall_f)
|
265 |
matching_precision_features.append(matching_precision_f)
|
266 |
matching_recall_features.append(matching_recall_f)
|
267 |
matching_f1_features.append(matching_f1_f)
|
|
|
142 |
return_coverages: bool = False,
|
143 |
return_all: bool = False,
|
144 |
dtype=np.float32,
|
145 |
+
instance_normalization: bool = False,
|
146 |
+
eps: float = 1e-10,
|
147 |
):
|
148 |
"""
|
149 |
Compute the scores of the module given the predictions and references
|
|
|
164 |
return_coverages = True
|
165 |
predictions = np.array(predictions).astype(dtype)
|
166 |
references = np.array(references).astype(dtype)
|
167 |
+
if instance_normalization:
|
168 |
+
predictions = (predictions - predictions.mean(axis=1, keepdims=True)) / predictions.std(
|
169 |
+
axis=1, keepdims=True
|
170 |
+
)
|
171 |
+
references = (references - references.mean(axis=1, keepdims=True)) / references.std(axis=1, keepdims=True)
|
172 |
if predictions.shape[1:] != references.shape[1:]:
|
173 |
raise ValueError(
|
174 |
"The number of features in the predictions and references should be the same. predictions: {}, references: {}".format(
|
|
|
213 |
)
|
214 |
distance[i : i + batch_size, j : j + batch_size] = d
|
215 |
else:
|
216 |
+
distance = self._compute_metric(predictions[:, None], references[None, :], metric=metric, axis=-2)
|
217 |
|
218 |
index_distance = distance.diagonal(axis1=0, axis2=1).mean().item()
|
219 |
|
|
|
232 |
best_match_inv = np.argmin(distance_mean, axis=0)
|
233 |
recall_distance = distance_mean[best_match_inv, np.arange(len(best_match_inv))].mean().item()
|
234 |
|
235 |
+
f1_distance = 2 / (1 / (precision_distance + eps) + 1 / (recall_distance + eps))
|
236 |
mean_distance = (precision_distance + recall_distance) / 2
|
237 |
|
238 |
# matching precision, recall and f1
|
239 |
matching_recall = np.unique(best_match).size / len(best_match_inv)
|
240 |
matching_precision = np.unique(best_match_inv).size / len(best_match)
|
241 |
+
matching_f1 = 2 / (1 / (matching_precision + eps) + 1 / (matching_recall + eps))
|
242 |
|
243 |
# take matching for each feature and compute metrics for them
|
244 |
precision_distance_features = []
|
|
|
258 |
precision_distance_f = (distance_f[np.arange(len(best_match_f)), best_match_f].mean()).item()
|
259 |
best_match_inv_f = np.argmin(distance_f, axis=0)
|
260 |
recall_distance_f = (distance_f[best_match_inv_f, np.arange(len(best_match_inv_f))].mean()).item()
|
261 |
+
f1_distance_f = 2 / (1 / (precision_distance_f + eps) + 1 / (recall_distance_f + eps))
|
262 |
mean_distance_f = (precision_distance_f + recall_distance_f) / 2
|
263 |
precision_distance_features.append(precision_distance_f)
|
264 |
recall_distance_features.append(recall_distance_f)
|
|
|
268 |
|
269 |
matching_recall_f = np.unique(best_match_f).size / len(best_match_f)
|
270 |
matching_precision_f = np.unique(best_match_inv_f).size / len(best_match_inv_f)
|
271 |
+
matching_f1_f = 2 / (1 / (matching_precision_f + eps) + 1 / (matching_recall_f + eps))
|
272 |
matching_precision_features.append(matching_precision_f)
|
273 |
matching_recall_features.append(matching_recall_f)
|
274 |
matching_f1_features.append(matching_f1_f)
|