forked from april-tools/e4selflearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep_dl.py
221 lines (203 loc) · 7.2 KB
/
sweep_dl.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import argparse
import multiprocessing as mp
import os
from datetime import datetime
from functools import partial
import wandb
import pre_train as pre_text_trainer
import train_ann as target_trainer
def forked(fn):
"""
Does not work on Windows (except WSL2), since the fork syscall is not supported here.
fork creates a new process which inherits all the memory without it being copied.
Memory is copied on write instead, meaning it is very cheap to create a new process
Reference: https://gist.github.com/schlamar/2311116?permalink_comment_id=3932763#gistcomment-3932763
"""
def call(*args, **kwargs):
ctx = mp.get_context("fork")
q = ctx.Queue(1)
is_error = ctx.Value("b", False)
def target():
try:
q.put(fn(*args, **kwargs))
except BaseException as e:
is_error.value = True
q.put(e)
ctx.Process(target=target).start()
result = q.get()
if is_error.value:
raise result
return result
return call
class Args:
def __init__(
self,
id: str,
config: wandb.Config,
output_dir: str,
num_workers: int = 2,
verbose: int = 1,
split_mode: int = 0,
task_mode: int = None,
pretext_task: str = None,
path2pretraining_res: str = None,
):
self.output_dir = os.path.join(
output_dir, f"{datetime.now():%Y%m%d-%Hh%Mm}-{id}"
)
self.task_mode = task_mode
self.epochs = 250
self.device = None
self.batch_size = 256
if self.task_mode not in (1, 2):
self.dataset = "data/preprocessed/sl512_ss128"
self.split_mode = split_mode
self.scaling_mode = 2
if self.task_mode in (1, 2):
self.path2pretraining_res = path2pretraining_res
if pretext_task:
self.pretext_task = pretext_task
self.filter_collections = None
self.downsize_pre_training = 1
self.exclude_anomalies = False
match self.pretext_task:
case "masked_prediction":
self.masking_ratio = 0.15
self.lm = 3
self.overwrite_masks = False
case "transformation_prediction":
self.snr = 1.5
self.num_sub_segments = 4
self.stretch_factor = 4
case "contrastive":
self.temperature = 0.5
if self.task_mode in (1, 2, 3) and split_mode == 0:
self.critic_score_lambda = 0
self.seed = 1234
self.num_workers = num_workers
self.min_epochs = 50
self.lr_patience = 10
self.save_test_model_outputs = False
self.test_time = False
self.reuse_stats = True
self.save_plots = False
self.format = "svg"
self.dpi = 120
self.plot_mode = 0
self.verbose = verbose
self.clear_output_dir = False
self.use_wandb = True
for key, value in config.items():
if not hasattr(self, key):
setattr(self, key, value)
def main(
output_dir: str,
wandb_group: str,
num_workers: int = 2,
verbose: int = 1,
split_mode: int = None,
task_mode: int = None,
pretext_task: str = None,
path2pretraining_res: str = None,
):
run = wandb.init(group=wandb_group)
config = run.config
run.name = run.id
args = Args(
id=run.id,
config=config,
output_dir=output_dir,
num_workers=num_workers,
split_mode=split_mode,
task_mode=task_mode,
pretext_task=pretext_task,
path2pretraining_res=path2pretraining_res,
verbose=verbose,
)
if task_mode in (1, 2, 3):
target_trainer.main(args, wandb_sweep=True)
else:
pre_text_trainer.main(args, wandb_sweep=True)
@forked
def agent(params):
wandb.agent(
sweep_id=params.sweep_id,
function=partial(
main,
output_dir=params.output_dir,
wandb_group=params.wandb_group,
num_workers=params.num_workers,
verbose=params.verbose,
split_mode=params.split_mode,
task_mode=params.task_mode,
pretext_task=params.pretext_task,
path2pretraining_res=params.path2pretraining_res,
),
count=1,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output_dir", type=str, required=True)
parser.add_argument("--sweep_id", type=str, required=True)
parser.add_argument("--wandb_group", type=str, required=True)
parser.add_argument("--num_workers", type=int, default=4)
parser.add_argument(
"--num_trials",
type=int,
default=1,
help="number of trials to run with this agent",
)
parser.add_argument("--verbose", type=int, default=1, choices=[0, 1, 2])
parser.add_argument(
"--split_mode",
type=int,
default=0,
choices=[0, 1],
required=False,
help="criterion for train/val/test split:"
"0) time-split: each session is split into 70:15:15 along the temporal "
"dimension such that segments from different splits map to "
"different parts of the recording"
"1) subject-split: cases and controls are split into 70:15:15 "
"train/val/test such that subjects are not shared across splits",
)
parser.add_argument(
"--task_mode",
type=int,
choices=[1, 2, 3],
required=False,
help="criterion for train/val/test split:"
"1) Pre-trained encoder is fine-tuned"
"2) Pre-trained encoder is frozen (features are simply "
"read out) only the classification is trained on the target task"
"3) Encoder and classification head are trained together end-to-end on "
"target task directly",
)
parser.add_argument("--path2pretraining_res", type=str, required=False)
if (not parser.parse_known_args()[0].path2pretraining_res) and (
parser.parse_known_args()[0].task_mode in (1, 2)
):
raise Exception(
"--path2pretraining_res to be specified when --task_mode is in (1, 2)"
)
parser.add_argument(
"--pretext_task",
type=str,
choices=["masked_prediction", "transformation_prediction", "contrastive"],
required=False,
help="criterion for train/val/test split:"
"masked_prediction: parts of the input are selected with a mask and "
"corrupted; the representation module is trained to impute the missing "
"(corrupted) values"
"transformation_prediction: some transformations are sampled from a set of "
"transformations and applied across channels; the representation "
"module is trained to guess what transformation (if any) was applied"
"contrastive:",
)
if (not parser.parse_known_args()[0].task_mode) and (
not parser.parse_known_args()[0].pretext_task
):
raise Exception("--pretext_task to be specified in self-supervised training")
params = parser.parse_args()
for _ in range(params.num_trials):
agent(params)