-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper_functions.py
136 lines (119 loc) · 4.26 KB
/
helper_functions.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import re
import numpy as np
import torch
from tqdm import tqdm
def extract_train_label_prop(run_name):
pattern = r"-(\d+\.\d+)-"
matches = re.search(pattern, run_name)
if matches:
extracted_data = matches.group(1)
return float(extracted_data)
return np.nan
def extract_run_type(run_name):
# Define the regex pattern to split the string at the first dash
pattern = r"-"
# Use re.split to split the string at the first dash
parts = re.split(pattern, run_name, maxsplit=1)
# Extract the substring before the first dash
if len(parts) > 1:
substring_before_first_dash = parts[0]
return substring_before_first_dash
print("No dash found in the input string.")
return ""
def extract_pretraining_type(run_name):
run_name = re.split(r"-", run_name, maxsplit=1)[0]
if "simclrcfaug" in run_name:
return "SimCLR with CF\nin training set"
if "dinocfaug" in run_name:
return "DINO+"
if "dinocf" in run_name:
return "CF-DINO"
if "dino" in run_name:
return "DINO"
if "simclrsexcf" in run_name:
return "SexCF-SimCLR"
if "simclrcf" in run_name:
return "CF-SimCLR"
elif "simclr" in run_name:
return "SimCLR"
elif "supervised" in run_name:
return "ImageNet"
return run_name
def extract_finetuning_type(run_name):
run_name = re.split(r"-", run_name, maxsplit=1)[0]
if "head" in run_name:
return "Linear Probing"
if "supervisedcf" in run_name or "cffine" in run_name:
return "Finetuning with CF"
if "transfer" in run_name or "trf" in run_name:
return "Transfer"
return "Finetuning"
def run_inference(dataloader, classification_model):
confs = []
true_label = []
scanners = []
sexs = []
with torch.no_grad():
for i, batch in tqdm(enumerate(dataloader)):
inputs = batch["x"].float()
logits = classification_model(inputs.cuda()).cpu()
probas = torch.softmax(logits, 1).numpy()
confs.append(probas)
true_label.append(batch["y"].numpy())
if "scanner" in batch.keys():
if isinstance(batch["scanner"], torch.Tensor):
scanners.append(batch["scanner"].numpy())
else:
scanners.append(batch["scanner"])
if "sex" in batch.keys():
if isinstance(batch["sex"], torch.Tensor):
sexs.append(batch["sex"].numpy())
else:
sexs.append(batch["sex"])
if len(sexs) > 0:
sexs = np.concatenate(sexs)
print(sexs)
targets_true = np.concatenate(true_label)
if targets_true.ndim == 2:
if targets_true.shape[1] == 1:
targets_true = targets_true.reshape(-1)
else:
targets_true = np.argmax(targets_true, 1)
confs = np.concatenate(confs)
if len(scanners) > 0:
scanners = np.concatenate(scanners)
return {"confs": confs, "targets": targets_true, "scanners": scanners, "sexs": sexs}
def run_get_embeddings(dataloader, classification_model, max_batch=100):
all_feats = []
true_label = []
scanners = []
paths = []
with torch.no_grad():
for i, batch in tqdm(enumerate(dataloader)):
inputs = batch["x"].float()
feats = classification_model.get_features(inputs.cuda()).cpu()
all_feats.append(feats)
true_label.append(batch["y"].numpy())
paths.append(batch["shortpath"])
if "scanner" in batch.keys():
if isinstance(batch["scanner"], torch.Tensor):
scanners.append(batch["scanner"].numpy())
else:
scanners.append(batch["scanner"])
if i > max_batch:
break
targets_true = np.concatenate(true_label)
if targets_true.ndim == 2:
if targets_true.shape[1] == 1:
targets_true = targets_true.reshape(-1)
else:
targets_true = np.argmax(targets_true, 1)
feats = np.concatenate(all_feats)
if len(scanners) > 0:
scanners = np.concatenate(scanners)
return {
"targets": targets_true,
"scanners": scanners,
"feats": feats,
"paths": np.concatenate(paths),
}