-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer_print.py
executable file
·88 lines (66 loc) · 2.85 KB
/
infer_print.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3.10
# -*- coding: utf-8 -*-
from __future__ import annotations
import csv
import pickle
import sys
from dataclasses import dataclass
from typing import Any, Dict, Sequence, Type, TypeVar
import numpy as np
from best_majvote import FloatArray
from util import zip_strict
AnyPreds = TypeVar('AnyPreds', bound='Preds')
def pred_cmp(cpred: float, thresh: float) -> bool:
# Negative threshold -> label only negative predictions
return cpred <= -thresh if thresh < 0 else cpred > thresh
def _load_predfile(path: str) -> Dict[str, Any]:
with open(path, 'rb') as f:
header = pickle.load(f)
if 'preds' in header:
return header # Old style
# Incremental style
preds = []
uncertainties = []
while True:
try:
chunk = pickle.load(f)
except EOFError:
break
preds.append(chunk['preds'])
uncertainties.append(chunk['uncertainties'])
return dict(header, preds=np.concatenate(preds), uncertainties=np.concatenate(uncertainties))
@dataclass(frozen=True)
class Preds:
class_names: Sequence[str] # Not necessarily all classes
sample_paths: Sequence[str]
preds: FloatArray
uncertainties: FloatArray
@classmethod
def load(cls: Type[AnyPreds], path: str) -> AnyPreds:
data = _load_predfile(path)
us = data['uncertainties']
if us.ndim == 3 and us.shape[-1] == 1:
# @TEMPORARY Remove extra dimension from broken pred_uncertainty
data['uncertainties'] = us[..., 0]
inst = cls(**data)
ls, lp, lu = map(len, (inst.sample_paths, inst.preds, inst.uncertainties)) # type: ignore[arg-type]
if not ls == lp == lu:
raise ValueError("Prediction file lengths don't match: pred={}, u={}, sample={}".format(ls, lp, lu))
return inst
def lbl_preds(self, label: str) -> FloatArray:
return self.preds[:, self.class_names.index(label)]
def lbl_uncertainties(self, label: str) -> FloatArray:
return self.uncertainties[:, self.class_names.index(label)]
if __name__ == '__main__':
if len(sys.argv) != 5:
raise ValueError('Expected 4 arguments, got {}'.format(len(sys.argv) - 1))
# Predictions file generated by infer.py
data_cnames_str, lblidx_str, threshold_str, predictions_file = sys.argv[1:]
data_cnames, lblidx, threshold = data_cnames_str.split(','), int(lblidx_str), float(threshold_str)
label = data_cnames[lblidx]
del data_cnames_str, lblidx_str
preds = Preds.load(predictions_file)
writer = csv.writer(sys.stdout)
writer.writerow(['sample_path', 'uncertainty', 'label'])
for fname, pred, u in zip_strict(preds.sample_paths, preds.lbl_preds(label), preds.lbl_uncertainties(label)):
writer.writerow([fname, u, label if pred_cmp(pred, threshold) else ''])