forked from DeepPSP/torch_ecg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg.py
679 lines (602 loc) · 22.3 KB
/
cfg.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
"""
"""
import pathlib
from copy import deepcopy
from typing import Sequence, Union
import numpy as np
import torch
from cfg_models import ModelArchCfg
from inputs import InputConfig
from sklearn.model_selection import ParameterGrid
from torch_ecg.cfg import CFG
from torch_ecg.utils.utils_nn import adjust_cnn_filter_lengths
__all__ = [
"BaseCfg",
"TrainCfg",
"ModelCfg",
"OutcomeCfg",
"remove_extra_heads",
]
_BASE_DIR = pathlib.Path(__file__).absolute().parent
###############################################################################
# Base Configs,
# including path, data type, classes, etc.
###############################################################################
BaseCfg = CFG()
BaseCfg.db_dir = None
BaseCfg.project_dir = _BASE_DIR
BaseCfg.log_dir = _BASE_DIR / "log"
BaseCfg.model_dir = _BASE_DIR / "saved_models"
BaseCfg.log_dir.mkdir(exist_ok=True)
BaseCfg.model_dir.mkdir(exist_ok=True)
BaseCfg.fs = 1000
BaseCfg.torch_dtype = torch.float32 # "double"
BaseCfg.np_dtype = np.float32
BaseCfg.ignore_index = -100
BaseCfg.ignore_unannotated = True
BaseCfg.outcomes = [
"Abnormal",
"Normal",
]
BaseCfg.classes = [
"Present",
"Unknown",
"Absent",
]
BaseCfg.states = [
"unannotated",
"S1",
"systolic",
"S2",
"diastolic",
]
# for example, can use scipy.signal.buttord(wp=[15, 250], ws=[5, 400], gpass=1, gstop=40, fs=1000)
BaseCfg.passband = [25, 400] # Hz, candidates: [20, 500], [15, 250]
BaseCfg.filter_order = 3
# challenge specific configs, for merging results from multiple recordings into one
BaseCfg.merge_rule = "avg" # "avg", "max"
###############################################################################
# training configurations for machine learning and deep learning
###############################################################################
TrainCfg = deepcopy(BaseCfg)
###########################################
# common configurations for all tasks
###########################################
TrainCfg.checkpoints = _BASE_DIR / "checkpoints"
TrainCfg.checkpoints.mkdir(exist_ok=True)
TrainCfg.train_ratio = 0.8
# configs of training epochs, batch, etc.
TrainCfg.n_epochs = 60
# TODO: automatic adjust batch size according to GPU capacity
# https://stackoverflow.com/questions/45132809/how-to-select-batch-size-automatically-to-fit-gpu
TrainCfg.batch_size = 24
# configs of optimizers and lr_schedulers
TrainCfg.optimizer = "adamw_amsgrad" # "sgd", "adam", "adamw"
TrainCfg.momentum = 0.949 # default values for corresponding PyTorch optimizers
TrainCfg.betas = (0.9, 0.999) # default values for corresponding PyTorch optimizers
TrainCfg.decay = 1e-2 # default values for corresponding PyTorch optimizers
TrainCfg.learning_rate = 5e-4 # 1e-3
TrainCfg.lr = TrainCfg.learning_rate
TrainCfg.lr_scheduler = "one_cycle" # "one_cycle", "plateau", "burn_in", "step", None
TrainCfg.lr_step_size = 50
TrainCfg.lr_gamma = 0.1
TrainCfg.max_lr = 2e-3 # for "one_cycle" scheduler, to adjust via expriments
# configs of callbacks, including early stopping, checkpoint, etc.
TrainCfg.early_stopping = CFG() # early stopping according to challenge metric
TrainCfg.early_stopping.min_delta = 0.001 # should be non-negative
TrainCfg.early_stopping.patience = TrainCfg.n_epochs // 2
TrainCfg.keep_checkpoint_max = 10
# configs of loss function
# TrainCfg.loss = "AsymmetricLoss" # "FocalLoss", "BCEWithLogitsLoss"
# TrainCfg.loss_kw = CFG(gamma_pos=0, gamma_neg=0.2, implementation="deep-psp")
TrainCfg.flooding_level = 0.0 # flooding performed if positive,
# configs of logging
TrainCfg.log_step = 20
# TrainCfg.eval_every = 20
###########################################
# task specific configurations
###########################################
# tasks of training
TrainCfg.tasks = [
"classification",
"segmentation",
"multi_task", # classification and segmentation with weight sharing
]
for t in TrainCfg.tasks:
TrainCfg[t] = CFG()
###########################################
# classification configurations
###########################################
TrainCfg.classification.fs = BaseCfg.fs
TrainCfg.classification.final_model_name = None
# input format configurations
TrainCfg.classification.data_format = "channel_first"
TrainCfg.classification.input_config = InputConfig(
input_type="waveform", # "waveform", "spectrogram", "mel", "mfcc", "spectral"
n_channels=1,
fs=TrainCfg.classification.fs,
)
TrainCfg.classification.num_channels = TrainCfg.classification.input_config.n_channels
TrainCfg.classification.input_len = int(30 * TrainCfg.classification.fs) # 30 seconds, to adjust
TrainCfg.classification.siglen = TrainCfg.classification.input_len # alias
TrainCfg.classification.sig_slice_tol = 0.2 # None, do no slicing
TrainCfg.classification.classes = deepcopy(BaseCfg.classes)
TrainCfg.classification.outcomes = deepcopy(BaseCfg.outcomes)
# TrainCfg.classification.outcomes = None
if TrainCfg.classification.outcomes is not None:
TrainCfg.classification.outcome_map = {c: i for i, c in enumerate(TrainCfg.classification.outcomes)}
else:
TrainCfg.classification.outcome_map = None
TrainCfg.classification.class_map = {c: i for i, c in enumerate(TrainCfg.classification.classes)}
# preprocess configurations
TrainCfg.classification.resample = CFG(fs=TrainCfg.classification.fs)
TrainCfg.classification.bandpass = CFG(
lowcut=BaseCfg.passband[0],
highcut=BaseCfg.passband[1],
filter_type="butter",
filter_order=BaseCfg.filter_order,
)
TrainCfg.classification.normalize = CFG( # None or False for no normalization
method="z-score",
mean=0.0,
std=1.0,
)
# augmentations configurations via `from_dict` of `torch-audiomentations`
TrainCfg.classification.augmentations = [
dict(
transform="AddColoredNoise",
params=dict(
min_snr_in_db=1.0,
max_snr_in_db=5.0,
min_f_decay=-2.0,
max_f_decay=2.0,
mode="per_example",
p=0.5,
sample_rate=TrainCfg.classification.fs,
),
),
# dict(
# transform="PitchShift",
# params=dict(
# sample_rate=TrainCfg.classification.fs,
# min_transpose_semitones=-4.0,
# max_transpose_semitones=4.0,
# mode="per_example",
# p=0.4,
# ),
# ),
dict(
transform="PolarityInversion",
params=dict(
mode="per_example",
p=0.6,
sample_rate=TrainCfg.classification.fs,
),
),
]
TrainCfg.classification.augmentations_kw = CFG(
p=0.7,
p_mode="per_batch",
)
# model choices
TrainCfg.classification.model_name = "crnn" # "wav2vec", "crnn"
TrainCfg.classification.cnn_name = "resnet_nature_comm_bottle_neck_se"
TrainCfg.classification.rnn_name = "lstm" # "none", "lstm"
TrainCfg.classification.attn_name = "se" # "none", "se", "gc", "nl"
# loss function choices
TrainCfg.classification.loss = CFG(
# murmur="AsymmetricLoss", # "FocalLoss"
# outcome="CrossEntropyLoss", # valid only if outcomes is not None
murmur="BCEWithLogitsWithClassWeightLoss",
outcome="BCEWithLogitsWithClassWeightLoss",
)
TrainCfg.classification.loss_kw = CFG(
# murmur=CFG(gamma_pos=0, gamma_neg=0.2, implementation="deep-psp"),
# outcome={},
murmur=CFG(class_weight=torch.tensor([[5.0, 3.0, 1.0]])), # "Present", "Unknown", "Absent"
outcome=CFG(class_weight=torch.tensor([[5.0, 1.0]])), # "Abnormal", "Normal"
)
# monitor choices
# challenge metric is the **cost** of misclassification
# hence it is the lower the better
TrainCfg.classification.monitor = "neg_weighted_cost" # weighted_accuracy (not recommended) # the higher the better
TrainCfg.classification.head_weights = CFG(
# used to compute a numeric value to use the monitor
murmur=0.5,
outcome=0.5,
)
# freeze backbone configs, -1 for no freezing
TrainCfg.classification.freeze_backbone_at = int(0.6 * TrainCfg.n_epochs)
###########################################
# segmentation configurations
###########################################
TrainCfg.segmentation.fs = 1000
TrainCfg.segmentation.final_model_name = None
# input format configurations
TrainCfg.segmentation.data_format = "channel_first"
TrainCfg.segmentation.input_config = InputConfig(
input_type="waveform", # "waveform", "spectrogram", "mel", "mfcc", "spectral"
n_channels=1,
fs=TrainCfg.segmentation.fs,
)
TrainCfg.segmentation.num_channels = TrainCfg.segmentation.input_config.n_channels
TrainCfg.segmentation.input_len = int(30 * TrainCfg.segmentation.fs) # 30seconds, to adjust
TrainCfg.segmentation.siglen = TrainCfg.segmentation.input_len # alias
TrainCfg.segmentation.sig_slice_tol = 0.4 # None, do no slicing
TrainCfg.segmentation.classes = deepcopy(BaseCfg.states)
if TrainCfg.ignore_unannotated:
TrainCfg.segmentation.classes = [s for s in TrainCfg.segmentation.classes if s != "unannotated"]
TrainCfg.segmentation.class_map = {c: i for i, c in enumerate(TrainCfg.segmentation.classes)}
# preprocess configurations
TrainCfg.segmentation.resample = CFG(fs=TrainCfg.segmentation.fs)
TrainCfg.segmentation.bandpass = CFG(
lowcut=BaseCfg.passband[0],
highcut=BaseCfg.passband[1],
filter_type="butter",
filter_order=BaseCfg.filter_order,
)
TrainCfg.segmentation.normalize = CFG( # None or False for no normalization
method="z-score",
mean=0.0,
std=1.0,
)
# augmentations configurations via `from_dict` of `torch-audiomentations`
TrainCfg.segmentation.augmentations = [
dict(
transform="AddColoredNoise",
params=dict(
min_snr_in_db=1.0,
max_snr_in_db=5.0,
min_f_decay=-2.0,
max_f_decay=2.0,
mode="per_example",
p=0.5,
sample_rate=TrainCfg.segmentation.fs,
),
),
# dict(
# transform="PitchShift",
# params=dict(
# sample_rate=TrainCfg.segmentation.fs,
# min_transpose_semitones=-4.0,
# max_transpose_semitones=4.0,
# mode="per_example",
# p=0.4,
# ),
# ),
dict(
transform="PolarityInversion",
params=dict(
mode="per_example",
p=0.6,
sample_rate=TrainCfg.segmentation.fs,
),
),
]
TrainCfg.segmentation.augmentations_kw = CFG(
p=0.7,
p_mode="per_batch",
)
# model choices
TrainCfg.segmentation.model_name = "seq_lab" # unet
TrainCfg.segmentation.cnn_name = "resnet_nature_comm_bottle_neck_se"
TrainCfg.segmentation.rnn_name = "lstm" # "none", "lstm"
TrainCfg.segmentation.attn_name = "se" # "none", "se", "gc", "nl"
# loss function choices
TrainCfg.segmentation.loss = CFG(
segmentation="AsymmetricLoss", # "FocalLoss"
)
TrainCfg.segmentation.loss_kw = CFG(
segmentation=CFG(gamma_pos=0, gamma_neg=0.2, implementation="deep-psp"),
)
# monitor choices
TrainCfg.segmentation.monitor = "jaccard"
# freeze backbone configs, -1 for no freezing
TrainCfg.segmentation.freeze_backbone_at = -1
###########################################
# multi-task configurations
###########################################
TrainCfg.multi_task.fs = 1000
TrainCfg.multi_task.final_model_name = None
# input format configurations
TrainCfg.multi_task.data_format = "channel_first"
TrainCfg.multi_task.input_config = InputConfig(
input_type="waveform", # "waveform", "spectrogram", "mel", "mfcc", "spectral"
n_channels=1,
fs=TrainCfg.multi_task.fs,
)
TrainCfg.multi_task.num_channels = TrainCfg.multi_task.input_config.n_channels
TrainCfg.multi_task.input_len = int(30 * TrainCfg.multi_task.fs) # 30seconds, to adjust
TrainCfg.multi_task.siglen = TrainCfg.multi_task.input_len # alias
TrainCfg.multi_task.sig_slice_tol = 0.4 # None, do no slicing
TrainCfg.multi_task.classes = deepcopy(BaseCfg.classes)
TrainCfg.multi_task.class_map = {c: i for i, c in enumerate(TrainCfg.multi_task.classes)}
TrainCfg.multi_task.outcomes = deepcopy(BaseCfg.outcomes)
TrainCfg.multi_task.outcome_map = {c: i for i, c in enumerate(TrainCfg.multi_task.outcomes)}
TrainCfg.multi_task.states = deepcopy(BaseCfg.states)
if TrainCfg.ignore_unannotated:
TrainCfg.multi_task.states = [s for s in TrainCfg.multi_task.states if s != "unannotated"]
TrainCfg.multi_task.state_map = {s: i for i, s in enumerate(TrainCfg.multi_task.states)}
# preprocess configurations
TrainCfg.multi_task.resample = CFG(fs=TrainCfg.multi_task.fs)
TrainCfg.multi_task.bandpass = CFG(
lowcut=BaseCfg.passband[0],
highcut=BaseCfg.passband[1],
filter_type="butter",
filter_order=BaseCfg.filter_order,
)
TrainCfg.multi_task.normalize = CFG( # None or False for no normalization
method="z-score",
mean=0.0,
std=1.0,
)
# augmentations configurations via `from_dict` of `torch-audiomentations`
TrainCfg.multi_task.augmentations = [
dict(
transform="AddColoredNoise",
params=dict(
min_snr_in_db=1.0,
max_snr_in_db=5.0,
min_f_decay=-2.0,
max_f_decay=2.0,
mode="per_example",
p=0.5,
sample_rate=TrainCfg.multi_task.fs,
),
),
# dict(
# transform="PitchShift",
# params=dict(
# sample_rate=TrainCfg.multi_task.fs,
# min_transpose_semitones=-4.0,
# max_transpose_semitones=4.0,
# mode="per_example",
# p=0.4,
# ),
# ),
dict(
transform="PolarityInversion",
params=dict(
mode="per_example",
p=0.6,
sample_rate=TrainCfg.multi_task.fs,
),
),
]
TrainCfg.multi_task.augmentations_kw = CFG(
p=0.7,
p_mode="per_batch",
)
# model choices
TrainCfg.multi_task.model_name = "crnn" # unet
TrainCfg.multi_task.cnn_name = "resnet_nature_comm_bottle_neck_se"
TrainCfg.multi_task.rnn_name = "lstm" # "none", "lstm"
TrainCfg.multi_task.attn_name = "se" # "none", "se", "gc", "nl"
# loss function choices
TrainCfg.multi_task.loss = CFG(
# murmur="AsymmetricLoss", # "FocalLoss"
# outcome="CrossEntropyLoss", # "FocalLoss", "AsymmetricLoss"
murmur="BCEWithLogitsWithClassWeightLoss",
outcome="BCEWithLogitsWithClassWeightLoss",
segmentation="AsymmetricLoss", # "FocalLoss", "CrossEntropyLoss"
)
TrainCfg.multi_task.loss_kw = CFG(
# murmur=CFG(gamma_pos=0, gamma_neg=0.2, implementation="deep-psp"),
# outcome={},
murmur=CFG(class_weight=torch.tensor([[5.0 / 9.0, 3.0 / 9.0, 1.0 / 9.0]])), # "Present", "Unknown", "Absent"
outcome=CFG(class_weight=torch.tensor([[5.0 / 6.0, 1.0 / 6.0]])), # "Abnormal", "Normal"
segmentation=CFG(gamma_pos=0, gamma_neg=0.2, implementation="deep-psp"),
)
# monitor choices
TrainCfg.multi_task.monitor = "neg_weighted_cost" # the higher the better
TrainCfg.multi_task.head_weights = CFG(
# used to compute a numeric value to use the monitor
murmur=0.5,
outcome=0.5,
)
# freeze backbone configs, -1 for no freezing
TrainCfg.multi_task.freeze_backbone_at = int(0.6 * TrainCfg.n_epochs)
def set_entry_test_flag(test_flag: bool):
TrainCfg.entry_test_flag = test_flag
###############################################################################
# configurations for building deep learning models
# terminologies of stanford ecg repo. will be adopted
###############################################################################
_BASE_MODEL_CONFIG = CFG()
_BASE_MODEL_CONFIG.torch_dtype = BaseCfg.torch_dtype
ModelCfg = deepcopy(_BASE_MODEL_CONFIG)
for t in TrainCfg.tasks:
ModelCfg[t] = deepcopy(_BASE_MODEL_CONFIG)
ModelCfg[t].task = t
ModelCfg[t].fs = TrainCfg[t].fs
ModelCfg[t].update(deepcopy(ModelArchCfg[t]))
ModelCfg[t].classes = TrainCfg[t].classes
ModelCfg[t].num_channels = TrainCfg[t].num_channels
ModelCfg[t].input_len = TrainCfg[t].input_len
ModelCfg[t].model_name = TrainCfg[t].model_name
ModelCfg[t].cnn_name = TrainCfg[t].cnn_name
ModelCfg[t].rnn_name = TrainCfg[t].rnn_name
ModelCfg[t].attn_name = TrainCfg[t].attn_name
# adjust filter length; cnn, rnn, attn choices in model configs
for mn in [
"crnn",
"seq_lab",
# "unet",
]:
if mn not in ModelCfg[t]:
continue
ModelCfg[t][mn] = adjust_cnn_filter_lengths(ModelCfg[t][mn], ModelCfg[t].fs)
ModelCfg[t][mn].cnn.name = ModelCfg[t].cnn_name
ModelCfg[t][mn].rnn.name = ModelCfg[t].rnn_name
ModelCfg[t][mn].attn.name = ModelCfg[t].attn_name
# classification model outcome head
ModelCfg.classification.outcomes = deepcopy(TrainCfg.classification.outcomes)
if ModelCfg.classification.outcomes is None:
ModelCfg.classification.outcome_head = None
else:
ModelCfg.classification.outcome_head.loss = TrainCfg.classification.loss.outcome
ModelCfg.classification.outcome_head.loss_kw = deepcopy(TrainCfg.classification.loss_kw.outcome)
ModelCfg.classification.states = None
# multi-task model outcome and segmentation head
ModelCfg.multi_task.outcomes = deepcopy(TrainCfg.multi_task.outcomes)
ModelCfg.multi_task.outcome_head.loss = TrainCfg.multi_task.loss.outcome
ModelCfg.multi_task.outcome_head.loss_kw = deepcopy(TrainCfg.multi_task.loss_kw.outcome)
ModelCfg.multi_task.states = deepcopy(TrainCfg.multi_task.states)
ModelCfg.multi_task.segmentation_head.loss = TrainCfg.multi_task.loss.segmentation
ModelCfg.multi_task.segmentation_head.loss_kw = deepcopy(TrainCfg.multi_task.loss_kw.segmentation)
# model for the outcome (final diagnosis)
OutcomeCfg = CFG()
OutcomeCfg.db_dir = None
OutcomeCfg.log_dir = BaseCfg.log_dir
OutcomeCfg.model_dir = BaseCfg.model_dir
OutcomeCfg.split_col = "Patient ID" # for train-test split
OutcomeCfg.y_col = "Outcome"
OutcomeCfg.classes = deepcopy(BaseCfg.outcomes)
OutcomeCfg.class_map = {c: i for i, c in enumerate(OutcomeCfg.classes)}
OutcomeCfg.x_cols_cate = [
"Age",
"Sex",
"Pregnancy status",
"Locations",
"Murmur locations",
]
OutcomeCfg.x_cols_cont = [
"Height",
"Weight",
]
OutcomeCfg.cont_scaler = "standard" # "minmax", "standard"
OutcomeCfg.x_cols = OutcomeCfg.x_cols_cate + OutcomeCfg.x_cols_cont
OutcomeCfg.ordinal_mappings = {
"Age": {
"Neonate": 0,
"Infant": 1,
"Child": 2,
"Adolescent": 3,
"NA": 4,
# the public database has no "Young adult"
"Young adult": 4,
"Young Adult": 4,
"default": 4,
},
"Sex": {
"Female": 0,
"Male": 1,
"default": 0,
},
}
# OutcomeCfg.location_list = ["PV", "AV", "MV", "TV", "Phc"]
# only 2 subjects have "Phc" location audio recordings
# hence this location is ignored
OutcomeCfg.location_list = ["PV", "AV", "MV", "TV"]
OutcomeCfg.feature_list = ["Age", "Sex", "Height", "Weight", "Pregnancy status"] + [
f"Location-{loc}" for loc in OutcomeCfg.location_list
]
OutcomeCfg.grids = CFG()
OutcomeCfg.grids.rf = ParameterGrid(
{
"n_estimators": [10, 15, 20, 50, 100],
"criterion": ["gini", "entropy"],
"min_samples_split": [2, 3, 4],
"max_features": ["auto", "sqrt", "log2"],
"bootstrap": [True, False],
"oob_score": [True, False],
"warm_start": [True, False],
"class_weight": ["balanced", "balanced_subsample", {0: 5, 1: 1}, None],
}
)
OutcomeCfg.grids.xgb = ParameterGrid(
{
"n_estimators": [10, 15, 20, 50],
"learning_rate": [0.01, 0.05, 0.1],
"reg_alpha": [0.0, 0.1, 0.5, 1.0],
"reg_lambda": [0.0, 0.1, 0.5, 1.0],
"max_depth": [3, 5, 8],
"verbosity": [0],
}
)
OutcomeCfg.grids.gdbt = ParameterGrid(
{
"n_estimators": [10, 15, 20, 50, 100],
"loss": ["deviance", "exponential"],
"learning_rate": [0.01, 0.05, 0.1],
"criterion": ["friedman_mse", "mse"],
"min_samples_split": [2, 3, 4],
"max_features": ["auto", "sqrt", "log2"],
"warm_start": [True, False],
"ccp_alpha": [0.0, 0.1, 0.5, 1.0],
}
)
OutcomeCfg.grids.svc = ParameterGrid(
{
"C": [0.1, 0.5, 1, 10],
"kernel": ["linear", "poly", "rbf", "sigmoid"],
"degree": [2, 3, 5], # for "poly" kernel
"gamma": [
"scale",
"auto",
], # Kernel coefficient for 'rbf', 'poly' and 'sigmoid'
"coef0": [0.0, 0.2, 0.5, 1.0], # for 'poly' and 'sigmoid'
"class_weight": ["balanced", None],
"probability": [True],
"shrinking": [True, False],
}
)
OutcomeCfg.grids.bagging = ParameterGrid(
{
"n_estimators": [10, 15, 20, 50, 100],
"max_features": [0.1, 0.2, 0.5, 0.9, 1.0],
"bootstrap": [True, False],
"bootstrap_features": [True, False],
"oob_score": [True, False],
"warm_start": [True, False],
}
)
# OutcomeCfg.grids.sk_mlp =
# ParameterGrid({
# "hidden_layer_sizes": [(50,), (100,), (50, 100), (50, 100, 50)],
# "activation": ["logistic", "tanh", "relu"],
# "solver": ["lbfgs", "sgd", "adam"],
# "alpha": [0.0001, 0.001, 0.01],
# "learning_rate": ["constant", "invscaling", "adaptive"],
# "learning_rate_init": [
# 0.001,
# 0.01,
# ],
# "warm_start": [True, False],
# })
OutcomeCfg.monitor = "outcome_cost" # the lower the better
def remove_extra_heads(train_config: CFG, model_config: CFG, heads: Union[str, Sequence[str]]) -> None:
"""
remove extra heads from **task-specific** train config and model config,
e.g. `TrainCfg.classification` and `ModelCfg.classification`
Parameters
----------
train_config : CFG
train config
model_config : CFG
model config
heads : str or sequence of str,
names of heads to remove
"""
if heads in ["", None, []]:
return
if isinstance(heads, str):
heads = [heads]
assert set(heads) <= set(["outcome", "outcomes", "segmentation"])
for head in heads:
if head.lower() in ["outcome", "outcomes"]:
train_config.outcomes = None
train_config.outcome_map = None
train_config.loss.pop("outcome", None)
train_config.loss_kw.pop("outcome", None)
train_config.head_weights = {"murmur": 1.0}
train_config.monitor = "murmur_weighted_accuracy"
model_config.outcomes = None
model_config.outcome_head = None
if head.lower() in ["segmentation"]:
train_config.states = None
train_config.state_map = None
train_config.loss.pop("segmentation", None)
train_config.loss_kw.pop("segmentation", None)
model_config.states = None
model_config.segmentation_head = None