-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteam_code.py
More file actions
1716 lines (1446 loc) · 80 KB
/
team_code.py
File metadata and controls
1716 lines (1446 loc) · 80 KB
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Edit this script to add your team's code. Some functions are *required*, but you can edit most parts of the required functions,
# change or remove non-required functions, and add your own functions.
################################################################################
#
# Optional libraries, functions, and variables. You can change or remove them.
#
################################################################################
import copy
import math
import os
import sys
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed, wait, FIRST_COMPLETED
from functools import partial
import gc
import psutil
import requests
import json
import shutil # Import shutil for file operations
import joblib
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.profiler
import torch.utils.model_zoo as model_zoo
from scipy.interpolate import interp1d
from scipy.signal import butter, filtfilt, resample
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.metrics import accuracy_score, average_precision_score, f1_score, roc_auc_score
from sklearn.model_selection import KFold, StratifiedKFold
from torch.utils.data import DataLoader, Dataset, Subset, WeightedRandomSampler
from torch.optim.lr_scheduler import OneCycleLR
import h5py
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Import from our modules
from dataset import *
from helper_code import *
from loss import *
from model import *
from utils import *
################################################################################
#
# Global configuration.
#
################################################################################
class Config:
def __init__(self):
# --- General & Path Settings ---
self.model_name = os.getenv('MODEL_NAME', 'ECGFeatureExtractor') # [ECGFeatureExtractor, ecgfounder, ResNet18, ResNet34, ResNet50]
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Using a relative path is more robust as it relies on the WORKDIR set in the Dockerfile.
self.download_folder = os.getenv('CACHE_FOLDER', './downloaded_data')
# This is the folder where the script will create/process HDF5 files at runtime.
self.cache_folder = os.getenv('CACHE_FOLDER', '/tmp/wmqn2362/runtime_cache')
self.pretrain_model_folder = os.getenv('PRETRAIN_MODEL_FOLDER', './Trained_models') # This will be inside the container's WORKDIR
self.pretrain_model_path = os.path.join(self.pretrain_model_folder, 'pretrain_model.pth')
self.visualisation_folder = os.getenv('VISUALISATION_FOLDER', './tmp')
self.num_preprocess_workers = os.cpu_count() // 4
# --- Model Architecture ---
self.model = {
"use_pretrained": True,
"dropout_rate": 0.3,
"net1d_dropout_rate": 0.3,
"meta_features": {
"use_age": True,
"use_sex": True,
"use_signal_stats": False,
}
}
# --- Pre-training Settings ---
self.pretrain = {
"num_epochs": 50,
"learning_rate": 3e-5,
"batch_size": 128,
"early_stop_patience": 5,
"loss": {
"focal_gamma": 2,
"margin": 0.1,
"s": 30,
"lmf_alpha": 0.98,
"lmf_beta": 0.02,
"label_smoothing": 0.0,
"elr_lambda": 3.0,
"elr_beta": 0.7,
"elr_loss_weight": float(os.getenv('ELR_WEIGHT', 0.05))
}
}
# --- Fine-tuning Settings ---
self.finetune = {
"num_epochs": 100,
"learning_rate": 1e-6,
"batch_size": 64,
"early_stop_patience": 5,
"is_train_encoder": bool(int(os.getenv('IS_TRAIN_ENCODER', 1))), # New parameter
"loss": {
"focal_gamma": 2,
"margin": 0.8,
"s": 1,
"lmf_alpha": 0.98,
"lmf_beta": 0.02,
"label_smoothing": 0.2,
"distill_lambda": float(os.getenv('DISTILL_LAMBDA', 0.05))
}
}
# --- Domain-Adversarial Neural Network (DANN) Settings ---
self.dann = {
"num_domains": 8,
"external_datasets": ['CODE15', 'CSPC', 'CSPC_extra', 'Chapman_Shaoxing', 'Georgia', 'Ningbo', 'PTB', 'ST_Petersburg'],
"lambda": float(os.getenv('DANN_LAMBDA', 1.0)), # Max weight for domain confusion loss
"alpha": 10.0 # Steepness of the lambda scheduler
}
# --- Data Augmentation Settings ---
self.augmentation = {
"pos_sample_weight_multiplier": 1.0,
"noise": {"use": True, "std": 0.03, "prob": 0.8},
"scaling": {"use": True, "min": 0.5, "max": 2.0, "prob": 0.8},
"flip": {"use": False, "prob": 0.2},
"shift": {"use": True, "max_ratio": 0.8, "prob": 0.8},
"drop": {"use": True, "max_prob": 0.02, "prob": 0.8},
"power_noise": {"use": False, "amplitude": 0.03, "prob": 0.5},
"cutout": {"use": True, "max_ratio": 0.2, "prob": 0.5},
"lead_mixing": {"use": True, "lambda": 0.2, "prob": 0.8},
"time_warp": {"use": True, "min_hz": 450, "max_hz": 550, "prob": 0.5},
"baseline_wander": {"use": True, "min_freq": 0.05, "max_freq": 0.2, "amp_ratio": 0.2, "prob": 0.3}
}
def get_meta_feature_dim(self):
dim = 0
if self.model['meta_features']['use_age']: dim += 1
if self.model['meta_features']['use_sex']: dim += 3
if self.model['meta_features']['use_signal_stats']: dim += 2
return dim
def print_config(self):
# This function remains the same.
print(">>>>>>>>>>>>>>>>>>>>>>>>>Configuration:<<<<<<<<<<<<<<<<<<<<<<<<<<")
print(f"Model Name: {self.model_name}")
print(">>>>>>>>>Pretraining Parameters:<<<<<<<<<<")
print(f"Number of Epochs: {self.pretrain['num_epochs']}")
print(f"Learning Rate: {self.pretrain['learning_rate']}")
print(f"Batch Size: {self.pretrain['batch_size']}")
print(f"Early Stop Patience: {self.pretrain['early_stop_patience']}")
print(f"Pretrain ELR Lambda: {self.pretrain['loss']['elr_lambda']}")
print(f"Pretrain ELR Beta: {self.pretrain['loss']['elr_beta']}")
print(f"Pretrain ELR Loss Weight: {self.pretrain['loss']['elr_loss_weight']}")
print(">>>>>>>>>Training Parameters:<<<<<<<<<<")
print(f"Number of Epochs: {self.finetune['num_epochs']}")
print(f"Learning Rate: {self.finetune['learning_rate']}")
print(f"Dropout Rate: {self.model['dropout_rate']}")
print(f"Net1D Dropout Rate: {self.model['net1d_dropout_rate']}")
print(f"Batch Size: {self.finetune['batch_size']}")
print(f"Early Stop Patience: {self.finetune['early_stop_patience']}")
print(f"Is Train Encoder: {self.finetune['is_train_encoder']}") # New print
print(f"Finetune Distill Lambda: {self.finetune['loss']['distill_lambda']}")
print(">>>>>>>>>Meta Features:<<<<<<<<<<")
print(f"Use Age: {self.model['meta_features']['use_age']}")
print(f"Use Sex: {self.model['meta_features']['use_sex']}")
print(f"Use Signal Stats: {self.model['meta_features']['use_signal_stats']}")
print(f"Meta Feature Dimension: {self.get_meta_feature_dim()}")
print(">>>>>>>>>DANN Parameters:<<<<<<<<<<")
print(f"DANN Lambda: {self.dann['lambda']}")
print(f"DANN Alpha: {self.dann['alpha']}")
print(">>>>>>>>>Data Augmentation:<<<<<<<<<<")
print(f"Use Noise Augmentation: {self.augmentation['noise']['use']}, Probability: {self.augmentation['noise']['prob']}")
print(f"Use Scaling Augmentation: {self.augmentation['scaling']['use']}, Probability: {self.augmentation['scaling']['prob']}")
print(f"Use Flip Augmentation: {self.augmentation['flip']['use']}, Probability: {self.augmentation['flip']['prob']}")
print(f"Use Shift Augmentation: {self.augmentation['shift']['use']}, Max Ratio: {self.augmentation['shift']['max_ratio']}, Probability: {self.augmentation['shift']['prob']}")
print(f"Use Drop Augmentation: {self.augmentation['drop']['use']}, Max Probability: {self.augmentation['drop']['max_prob']}, Probability: {self.augmentation['drop']['prob']}")
print(f"Use 50Hz Power Noise: {self.augmentation['power_noise']['use']}, Probability: {self.augmentation['power_noise']['prob']}")
print(f"Use Cutout Augmentation: {self.augmentation['cutout']['use']}, Max Ratio: {self.augmentation['cutout']['max_ratio']}, Probability: {self.augmentation['cutout']['prob']}")
print(f"Use Lead Mixing Augmentation: {self.augmentation['lead_mixing']['use']}, Lambda: {self.augmentation['lead_mixing']['lambda']}, Probability: {self.augmentation['lead_mixing']['prob']}")
print(f"Use Time Warping Augmentation: {self.augmentation['time_warp']['use']}, Freq Range: [{self.augmentation['time_warp']['min_hz']}, {self.augmentation['time_warp']['max_hz']}], Probability: {self.augmentation['time_warp']['prob']}")
print(f"Use Baseline Wander Augmentation: {self.augmentation['baseline_wander']['use']}, Min Freq: {self.augmentation['baseline_wander']['min_freq']}, Max Freq: {self.augmentation['baseline_wander']['max_freq']}, Amp Ratio: {self.augmentation['baseline_wander']['amp_ratio']}, Probability: {self.augmentation['baseline_wander']['prob']}")
print(">>>>>>>>>Loss Parameters:<<<<<<<<<<")
print(f"Pretrain Focal Gamma: {self.pretrain['loss']['focal_gamma']}")
print(f"Pretrain Margin: {self.pretrain['loss']['margin']}")
print(f"Pretrain S: {self.pretrain['loss']['s']}")
print(f"Pretrain LMF Alpha: {self.pretrain['loss']['lmf_alpha']}")
print(f"Pretrain LMF Beta: {self.pretrain['loss']['lmf_beta']}")
print(f"Pretrain Label Smoothing: {self.pretrain['loss']['label_smoothing']}")
print(f"Finetune Focal Gamma: {self.finetune['loss']['focal_gamma']}")
print(f"Finetune Margin: {self.finetune['loss']['margin']}")
print(f"Finetune S: {self.finetune['loss']['s']}")
print(f"Finetune LMF Alpha: {self.finetune['loss']['lmf_alpha']}")
print(f"Finetune LMF Beta: {self.finetune['loss']['lmf_beta']}")
print(f"Finetune Label Smoothing: {self.finetune['loss']['label_smoothing']}")
print(f"Positive Sample Weight Multiplier: {self.augmentation['pos_sample_weight_multiplier']}")
print(">>>>>>>>>Device:<<<<<<<<<<")
print(f"Device: {self.device}")
config = Config()
config.print_config()
# Configure CUDA/cuDNN for stability
if torch.cuda.is_available():
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
# make cache folder if it does not exist
if not os.path.exists(config.cache_folder):
os.makedirs(config.cache_folder)
################################################################################
#
# Required functions. Edit these functions to add your code, but do not change the arguments for the functions.
#
################################################################################
def train_model(data_folder, model_folder, verbose):
"""Train the model using the three-stage process"""
# Create the runtime cache directory. This is the MOST IMPORTANT change.
os.makedirs(config.cache_folder, exist_ok=True)
if verbose:
print(f"Runtime cache folder created at: {config.cache_folder}")
start_total_time = time.time()
############################################################################
# Stage 0: Data Loading and Preprocessing
############################################################################
if verbose:
print("Stage 0: Data Loading and Preprocessing...")
stage0_start_time = time.time()
records_relative = find_records(data_folder)
records_full_path = [os.path.join(data_folder, r) for r in records_relative]
# Define HDF5 file paths using the runtime cache folder for WRITING
code15_hdf5_path = os.path.join(config.cache_folder, 'CODE15_data.hdf5')
samitrop_hdf5_path = os.path.join(config.cache_folder, 'SaMiTrop_data.hdf5')
ptbxl_hdf5_path = os.path.join(config.cache_folder, 'PTBXL_data.hdf5')
# Initialize filters once
highpass_filter_params, lowpass_filter_params, notch50_filter_params, notch60_filter_params = initialize_filters()
cpu_num = os.cpu_count()
with ProcessPoolExecutor(max_workers=cpu_num) as executor:
results = list(executor.map(classify_record, records_full_path))
code15_records_full_path = [r for r, src in results if src == 'CODE15']
samitrop_records_full_path = [r for r, src in results if src == 'SaMiTrop']
ptbxl_records_full_path = [r for r, src in results if src == 'PTBXL']
del records_relative, results
torch.cuda.empty_cache()
gc.collect()
def preprocess_and_write_to_hdf5(records_list, hdf5_path, dataset_name):
if not os.path.exists(hdf5_path):
start_time = time.time()
print(f"Starting data preprocessing for {dataset_name} (creating HDF5 in {config.cache_folder})")
if not records_list:
print(f"No records for {dataset_name}. Skipping HDF5 creation.")
return
n_records = len(records_list)
n_channels = 12
n_samples = 5000
meta_dim = config.get_meta_feature_dim()
label_dim = 1
chunk_n = os.cpu_count()
dataset_to_domain_label = {
'CODE15_data': 0,
'CSPC_data': 1,
'CSPC_extra_data': 2,
'Chapman_Shaoxing_data': 3,
'Georgia_data': 4,
'Ningbo_data': 5,
'PTB_data': 6,
'ST_Petersburg_data': 7,
'PTBXL_data': 8,
'SaMiTrop_data': 9
}
domain_label_value = dataset_to_domain_label.get(dataset_name, -1)
if domain_label_value == -1:
print(f"Warning: Unknown dataset_name '{dataset_name}'. Domain label will be -1.")
with h5py.File(hdf5_path, 'w') as hdf5_file:
with ProcessPoolExecutor(max_workers=cpu_num) as executor:
signal_chunks = (min(chunk_n, n_records), n_samples, n_channels)
hdf5_file.create_dataset(
'signals',
shape=(n_records, n_samples, n_channels),
dtype='float32',
chunks=signal_chunks,
compression=None
)
hdf5_file.create_dataset(
'meta_features',
shape=(n_records, meta_dim),
dtype='float32',
compression=None
)
hdf5_file.create_dataset(
'labels',
shape=(n_records, label_dim),
dtype='float32',
compression=None
)
hdf5_file.create_dataset(
'domain_labels',
shape=(n_records, label_dim),
dtype='float32',
compression=None
)
pending = set()
for idx, record_path in enumerate(records_list):
fut = executor.submit(
data_preprocess,
record_path,
config,
highpass_filter_params,
lowpass_filter_params,
notch50_filter_params,
notch60_filter_params,
idx
)
pending.add(fut)
if len(pending) >= cpu_num:
done, pending = wait(pending, return_when=FIRST_COMPLETED)
for fut in done:
idx, signal, meta_features, label = fut.result()
hdf5_file['signals'][idx] = signal
hdf5_file['meta_features'][idx] = meta_features
hdf5_file['labels'][idx] = label
hdf5_file['domain_labels'][idx] = domain_label_value
del signal, meta_features, label
for fut in as_completed(pending):
idx, signal, meta_features, label = fut.result()
hdf5_file['signals'][idx] = signal
hdf5_file['meta_features'][idx] = meta_features
hdf5_file['labels'][idx] = label
hdf5_file['domain_labels'][idx] = domain_label_value
del signal, meta_features, label
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Created new {dataset_name} data file at {hdf5_path}")
print(f"Data preprocessing and HDF5 creation for {dataset_name} took {elapsed_time:.2f} seconds.")
else:
print(f"Using existing {dataset_name} data file at {hdf5_path}")
# The logic here is that if the HDF5 files for the training data don't exist in the writable cache, we create them there.
# The external datasets are assumed to exist in the read-only download folder.
if not os.path.exists(code15_hdf5_path):
preprocess_and_write_to_hdf5(code15_records_full_path, code15_hdf5_path, 'CODE15_data')
else:
print(f"Skipping CODE15_data preprocessing as {code15_hdf5_path} already exists in cache.")
if not os.path.exists(samitrop_hdf5_path):
preprocess_and_write_to_hdf5(samitrop_records_full_path, samitrop_hdf5_path, 'SaMiTrop_data')
else:
print(f"Skipping SaMiTrop_data preprocessing as {samitrop_hdf5_path} already exists in cache.")
if not os.path.exists(ptbxl_hdf5_path):
preprocess_and_write_to_hdf5(ptbxl_records_full_path, ptbxl_hdf5_path, 'PTBXL_data')
else:
print(f"Skipping PTBXL_data preprocessing as {ptbxl_hdf5_path} already exists in cache.")
del code15_records_full_path, samitrop_records_full_path, ptbxl_records_full_path
stage0_end_time = time.time()
if verbose:
print(f"Stage 0 completed in {stage0_end_time - stage0_start_time:.2f} seconds.")
############################################################################
# Stage 1: Pretrain Model
############################################################################
if verbose:
print("Stage 1: Pretrain Model...")
# Load datasets. Pre-generated data is now read from the DOWNLOAD folder.
# Data generated on-the-fly (from training folder) is read from the CACHE folder.
pretrain_dataset = ECGDataset(dataset_name='CODE15', data_folder=config.cache_folder, is_training=True, config=config)
external_datasets = []
for dataset_name in config.dann['external_datasets']:
if dataset_name == 'CODE15':
data_folder_path = config.cache_folder
else:
data_folder_path = config.download_folder
external_datasets.append(ECGDataset(dataset_name=dataset_name, data_folder=data_folder_path, is_training=True, config=config))
stage1_start_time = time.time()
# Initialize model and training components
model = HybridModel(
device=config.device,
config=config
)
# First stage: update all parameters
for param in model.parameters():
param.requires_grad = True
# Print model parameters
print_model_parameters(model, verbose)
# Use create_binary_lmf_loss for pretraining
pretrain_labels = [pretrain_dataset[i][1] for i in range(len(pretrain_dataset))]
pretrain_neg_samples = pretrain_labels.count(0)
pretrain_pos_samples = pretrain_labels.count(1)
pretrain_pos_samples_weighted = pretrain_pos_samples * config.augmentation['pos_sample_weight_multiplier']
criterion = create_binary_lmf_loss(
pos_samples=pretrain_pos_samples_weighted,
neg_samples=pretrain_neg_samples,
device=config.device,
alpha=config.pretrain['loss']['lmf_alpha'],
beta=config.pretrain['loss']['lmf_beta'],
focal_gamma=config.pretrain['loss']['focal_gamma'],
ldam_margin=config.pretrain['loss']['margin'],
ldam_s=config.pretrain['loss']['s'],
label_smoothing=config.pretrain['loss']['label_smoothing']
)
elr_criterion = ELRLoss(
num_examples=len(pretrain_dataset),
elr_lambda=config.pretrain['loss']['elr_lambda'],
elr_beta=config.pretrain['loss']['elr_beta'],
num_classes=1,
device=config.device
)
model = pretrain_model(
pretrain_dataset=pretrain_dataset,
external_datasets=external_datasets,
model=model,
criterion=criterion,
elr_criterion=elr_criterion,
num_epochs=config.pretrain['num_epochs'],
batch_size=config.pretrain['batch_size'],
early_stop_patience=config.pretrain['early_stop_patience'],
device=config.device,
pretrain_model_pth=os.path.join(model_folder, 'pretrain_model.pth'),
verbose=verbose
)
# Close pretrain datasets
pretrain_dataset.close()
for ds in external_datasets:
ds.close()
del pretrain_dataset, external_datasets
torch.cuda.empty_cache()
gc.collect()
stage1_end_time = time.time()
if verbose:
print(f"Stage 1 completed in {stage1_end_time - stage1_start_time:.2f} seconds.")
############################################################################
# Stage 2: Evaluate pretrained model
############################################################################
if verbose:
print("Stage 2: Evaluate pretrained model...")
stage2_start_time = time.time()
# For evaluation, datasets are also loaded from their respective locations
pretrain_eval_dataset = ECGDataset(dataset_name='CODE15', data_folder=config.cache_folder, is_training=False, config=config)
samitrop_eval_dataset = ECGDataset(dataset_name='SaMiTrop', data_folder=config.cache_folder, is_training=False, config=config)
ptbxl_eval_dataset = ECGDataset(dataset_name='PTBXL', data_folder=config.cache_folder, is_training=False, config=config)
external_eval_datasets = []
for dataset_name in config.dann['external_datasets']:
if dataset_name == 'CODE15':
data_folder_path = config.cache_folder
else:
data_folder_path = config.download_folder
external_eval_datasets.append(ECGDataset(dataset_name=dataset_name, data_folder=data_folder_path, is_training=False, config=config))
evaluate_model(model, pretrain_eval_dataset, samitrop_eval_dataset, ptbxl_eval_dataset, external_eval_datasets, verbose, 'pretrain')
# Close evaluation datasets
pretrain_eval_dataset.close()
samitrop_eval_dataset.close()
ptbxl_eval_dataset.close()
for ds in external_eval_datasets:
ds.close()
del pretrain_eval_dataset, samitrop_eval_dataset, ptbxl_eval_dataset, external_eval_datasets
torch.cuda.empty_cache()
gc.collect()
stage2_end_time = time.time()
print(f"Stage 2 completed in {stage2_end_time - stage2_start_time:.2f} seconds.")
############################################################################
# Stage 3: Finetune on target datasets
############################################################################
samitrop_dataset = ECGDataset(dataset_name='SaMiTrop', data_folder=config.cache_folder, is_training=True, config=config)
ptbxl_dataset = ECGDataset(dataset_name='PTBXL', data_folder=config.cache_folder, is_training=True, config=config)
# --- Start of new logic for negative sampling ---
num_positive_samitrop = len(samitrop_dataset)
num_negative_needed = int(num_positive_samitrop * 49)
negative_dataset_names = [name for name in config.dann['external_datasets'] if name != 'CODE15']
negative_dataset_names.append('PTBXL')
num_negative_datasets = len(negative_dataset_names)
num_negative_per_dataset = num_negative_needed // num_negative_datasets
print(f"SaMiTrop positive samples: {num_positive_samitrop}")
print(f"Total negative samples needed: {num_negative_needed}")
print(f"Negative datasets: {negative_dataset_names}")
print(f"Negative samples per dataset: {num_negative_per_dataset}")
sampled_negative_datasets = []
actual_negative_datasets = []
for ds_name in negative_dataset_names:
if ds_name == 'PTBXL':
current_dataset = ptbxl_dataset
else:
current_dataset = ECGDataset(dataset_name=ds_name, data_folder=config.download_folder, is_training=True, config=config)
actual_negative_datasets.append(current_dataset)
negative_indices = [i for i, (_, label, _, _) in enumerate(current_dataset) if label == 0]
if len(negative_indices) > 0:
num_samples_to_take = min(num_negative_per_dataset, len(negative_indices))
sampled_indices = np.random.choice(negative_indices, num_samples_to_take, replace=False)
sampled_negative_datasets.append(Subset(current_dataset, sampled_indices))
else:
print(f"Warning: No negative samples found in {ds_name} or dataset is empty.")
finetune_dataset = [samitrop_dataset]
finetune_dataset.extend(sampled_negative_datasets)
finetune_dataset = torch.utils.data.ConcatDataset(finetune_dataset)
# --- End of new logic for negative sampling ---
if verbose:
print("Stage 3: Finetune on target datasets...")
stage3_start_time = time.time()
print_model_parameters(model, verbose)
finetune_labels = [finetune_dataset[i][1] for i in range(len(finetune_dataset))]
finetune_neg_samples = finetune_labels.count(0)
finetune_pos_samples = finetune_labels.count(1)
finetune_pos_samples_weighted = finetune_pos_samples * config.augmentation['pos_sample_weight_multiplier']
criterion = create_binary_lmf_loss(
pos_samples=finetune_pos_samples_weighted,
neg_samples=finetune_neg_samples,
device=config.device,
alpha=config.finetune['loss']['lmf_alpha'],
beta=config.finetune['loss']['lmf_beta'],
focal_gamma=config.finetune['loss']['focal_gamma'],
ldam_margin=config.finetune['loss']['margin'],
ldam_s=config.finetune['loss']['s'],
label_smoothing=config.finetune['loss']['label_smoothing']
)
optimizers = []
schedulers = []
for _ in range(5):
param_groups = []
if config.finetune['is_train_encoder']:
param_groups.append({'params': list(model.encoder.parameters()), 'lr': config.finetune['learning_rate'], 'name': 'encoder'})
param_groups.append({'params': model.classifier.parameters(), 'lr': config.finetune['learning_rate'] * 5, 'name': 'classifier'})
optimizer = torch.optim.AdamW(
param_groups,
weight_decay=1e-4
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=config.finetune['num_epochs']
)
optimizers.append(optimizer)
schedulers.append(scheduler)
kf = StratifiedKFold(n_splits=5)
finetuned_models = finetune_model(
model=model,
finetune_dataset=finetune_dataset,
model_folder=model_folder,
verbose=verbose,
criterion=criterion,
optimizers=optimizers,
schedulers=schedulers,
kf=kf
)
samitrop_dataset.close()
ptbxl_dataset.close()
for ds in actual_negative_datasets:
ds.close()
del samitrop_dataset, ptbxl_dataset, actual_negative_datasets, finetune_dataset
torch.cuda.empty_cache()
gc.collect()
stage3_end_time = time.time()
if verbose:
print(f"Stage 3 completed in {stage3_end_time - stage3_start_time:.2f} seconds.")
############################################################################
# Stage 4: Evaluate finetuned model
############################################################################
if verbose:
print("Stage 4: Evaluate finetuned model...")
stage4_start_time = time.time()
if verbose:
pretrain_eval_dataset = ECGDataset(dataset_name='CODE15', data_folder=config.cache_folder, is_training=False, config=config)
samitrop_eval_dataset = ECGDataset(dataset_name='SaMiTrop', data_folder=config.cache_folder, is_training=False, config=config)
ptbxl_eval_dataset = ECGDataset(dataset_name='PTBXL', data_folder=config.cache_folder, is_training=False, config=config)
external_eval_datasets = []
for dataset_name in config.dann['external_datasets']:
if dataset_name == 'CODE15':
data_folder_path = config.cache_folder
else:
data_folder_path = config.download_folder
external_eval_datasets.append(ECGDataset(dataset_name=dataset_name, data_folder=data_folder_path, is_training=False, config=config))
evaluate_model(finetuned_models, pretrain_eval_dataset, samitrop_eval_dataset, ptbxl_eval_dataset, external_eval_datasets, verbose, 'finetune')
stage4_end_time = time.time()
if verbose:
print(f"Stage 4 completed in {stage4_end_time - stage4_start_time:.2f} seconds.")
end_total_time = time.time()
if verbose:
print(f"Total training process completed in {end_total_time - start_total_time:.2f} seconds.")
print('Done.')
print()
def pretrain_model(pretrain_dataset, external_datasets, model, criterion, elr_criterion, # Add elr_criterion
num_epochs, batch_size, early_stop_patience, device,
pretrain_model_pth, verbose):
"""Core pretraining logic with training loop and model saving, incorporating DANN"""
# torch.autograd.set_detect_anomaly(True)
os.makedirs(os.path.dirname(pretrain_model_pth), exist_ok=True)
if os.path.exists(config.pretrain_model_path): # Now config.pretrain_model_path is the full file path
if verbose:
print(f"Found existing pretrained model at {config.pretrain_model_path}. Loading model and skipping pretraining.")
model.load_state_dict(torch.load(config.pretrain_model_path, map_location=device))
# The model is already loaded from config.pretrain_model_path,
# so saving it to pretrain_model_pth (which is the same path if pretrain_model_path is correctly set)
# is redundant but harmless. Keep it for consistency with original logic.
torch.save(model.state_dict(), pretrain_model_pth)
if verbose:
print(f"Loaded pretrained model saved to {pretrain_model_pth}.")
return model
if verbose:
print(f'Starting pretraining on CODE-15% ({len(pretrain_dataset)} records) and External ({sum(len(ds) for ds in external_datasets)} records) for domain adaptation.')
# Create data loaders for all datasets
# CODE-15% dataset (source domain for DANN and task classification)
code15_train_size = int(0.8 * len(pretrain_dataset))
code15_val_size = len(pretrain_dataset) - code15_train_size
code15_train_dataset, code15_val_dataset = torch.utils.data.random_split(pretrain_dataset, [code15_train_size, code15_val_size])
code15_train_weights = make_weights_for_balanced_classes(code15_train_dataset)
code15_train_sampler = WeightedRandomSampler(code15_train_weights, len(code15_train_weights))
# DataLoader for Chagas classification (full batch_size)
code15_chagas_loader = DataLoader(code15_train_dataset,
batch_size=batch_size,
sampler=code15_train_sampler,
num_workers=config.num_preprocess_workers)
code15_val_loader = DataLoader(code15_val_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=config.num_preprocess_workers)
# External datasets (source domains for DANN)
external_train_loaders = []
external_val_loaders = []
for ext_ds in external_datasets:
print(ext_ds.dataset_name, len(ext_ds), "records")
ext_train_size = int(0.8 * len(ext_ds))
ext_val_size = len(ext_ds) - ext_train_size
ext_train_dataset, ext_val_dataset = torch.utils.data.random_split(ext_ds, [ext_train_size, ext_val_size])
ext_train_loader = DataLoader(ext_train_dataset,
batch_size=batch_size // config.dann['num_domains'],
shuffle=True,
num_workers=config.num_preprocess_workers)
ext_val_loader = DataLoader(ext_val_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=config.num_preprocess_workers)
external_train_loaders.append(ext_train_loader)
external_val_loaders.append(ext_val_loader)
# Define optimizers for different parts of the model
# Optimizer for feature extractor (model.encoder) and task classifier (model.classifier)
optimizer_task = torch.optim.AdamW(
list(model.encoder.parameters()) + list(model.classifier.parameters()),
lr=config.pretrain['learning_rate'],
weight_decay=2e-4
)
# Optimizer for domain classifier (model.domain_classifier)
optimizer_domain_classifier = torch.optim.AdamW(
model.domain_classifier.parameters(),
lr=config.pretrain['learning_rate'] * 0.1, # Can be different from task optimizer LR
weight_decay=2e-4
)
# Optimizer for encoder (model.encoder) for confusion
optimizer_encoder_confusion = torch.optim.AdamW(
model.encoder.parameters(), # Only encoder parameters
lr=config.pretrain['learning_rate'],
weight_decay=2e-4
)
# Schedulers for all optimizers
warmup_epochs = int(num_epochs * 0.1)
def lr_lambda(epoch):
if epoch < warmup_epochs:
return 0.1 + (epoch / warmup_epochs) * 0.9
return 0.5 * (1 + math.cos(math.pi * (epoch - warmup_epochs) / (num_epochs - warmup_epochs)))
scheduler_task = torch.optim.lr_scheduler.LambdaLR(optimizer_task, lr_lambda)
scheduler_domain_classifier = torch.optim.lr_scheduler.LambdaLR(optimizer_domain_classifier, lr_lambda)
scheduler_encoder_confusion = torch.optim.lr_scheduler.LambdaLR(optimizer_encoder_confusion, lr_lambda)
# Loss for domain classification
domain_criterion = nn.CrossEntropyLoss()
# Loss for confusion (same as domain criterion, but we will maximize it)
confusion_criterion = ConfusionLoss()
best_auprc = 0.0 # This will track AUPRC for Chagas classification on CODE-15% validation set
best_epoch = 0
epochs_no_improve = 0
best_model_state = None
# Define the epoch from which early stopping should start
start_early_stopping_epoch = 15 # User wants to start early stopping from epoch 15
def calculate_lambda(epoch, num_epochs, max_lambda=config.dann['lambda'], alpha=config.dann['alpha']):
progress = epoch / num_epochs
p = 2.0 / (1.0 + math.exp(-alpha * progress)) - 1.0
return max_lambda * p
for epoch in range(num_epochs):
epoch_start_time = time.time()
model.train()
total_chagas_loss = 0.0
total_elr_loss = 0.0
total_combined_chagas_loss = 0.0
total_domain_loss = 0.0
total_confusion_loss = 0.0
train_targets = []
train_outputs = []
train_domain_targets = []
train_domain_outputs = []
# For Chagas logit printing (training)
train_pos_logits_sum = 0.0
train_neg_logits_sum = 0.0
train_pos_count = 0
train_neg_count = 0
# Iterators for all data loaders
code15_chagas_iter = iter(code15_chagas_loader)
external_iters = [iter(loader) for loader in external_train_loaders] # Use external_train_loaders directly
# Determine the maximum number of batches to iterate over for domain adaptation
max_batches = len(code15_chagas_loader)
for i in range(max_batches):
# ------------------------------------------------------------------
# --- Phase 1: Task Training (Update Encoder and Label Predictor) ---
# ------------------------------------------------------------------
model.train()
# Set requires_grad for task-related parameters
for param in model.encoder.parameters():
param.requires_grad = True
for param in model.classifier.parameters():
param.requires_grad = True
for param in model.domain_classifier.parameters():
param.requires_grad = False # Fix domain predictor
optimizer_task.zero_grad()
try:
code15_features_chagas, code15_label_chagas, code15_domain_label, index = next(code15_chagas_iter)
except StopIteration:
code15_chagas_iter = iter(code15_chagas_loader)
code15_features_chagas, code15_label_chagas, code15_domain_label, index = next(code15_chagas_iter)
signal_chagas, meta_chagas = code15_features_chagas
signal_chagas = signal_chagas.to(device)
meta_chagas = meta_chagas.to(device)
code15_label_chagas = code15_label_chagas.to(device).view(-1, 1)
task_output_chagas, _, _ = model(signal_chagas, meta_chagas)
chagas_loss = criterion(task_output_chagas, code15_label_chagas)
# Add ELR loss with warmup
elr_loss = elr_criterion(index, task_output_chagas, code15_label_chagas)
if epoch >= 5: # Apply ELR loss after 5 epochs
chagas_loss_with_elr = chagas_loss + config.pretrain['loss']['elr_loss_weight'] * elr_loss
else:
chagas_loss_with_elr = chagas_loss
chagas_loss_with_elr.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer_task.step()
total_chagas_loss += chagas_loss.item()
if epoch >= 5: # Accumulate ELR loss only after warmup
total_elr_loss += elr_loss.item()
total_combined_chagas_loss += chagas_loss_with_elr.item()
train_targets.extend(code15_label_chagas.cpu().numpy())
train_outputs.extend(torch.sigmoid(task_output_chagas).detach().cpu().numpy())
# Accumulate positive/negative logit stats for training
if verbose:
pos_mask_train = code15_label_chagas == 1
neg_mask_train = ~pos_mask_train
if pos_mask_train.any():
train_pos_logits_sum += task_output_chagas[pos_mask_train].sum().item()
train_pos_count += pos_mask_train.sum().item()
if neg_mask_train.any():
train_neg_logits_sum += task_output_chagas[neg_mask_train].sum().item()
train_neg_count += neg_mask_train.sum().item()
# ------------------------------------------------------------------
# --- Phase 2: Domain Classifier Training (Update Domain Classifier) ---
# ------------------------------------------------------------------
model.train()
# Set requires_grad for domain classifier parameters
for param in model.encoder.parameters():
param.requires_grad = False # Fix encoder
for param in model.classifier.parameters():
param.requires_grad = False # Fix label predictor
for param in model.domain_classifier.parameters():
param.requires_grad = True
optimizer_domain_classifier.zero_grad()
# Prepare combined data for Domain Adversarial and Confusion Training
all_signals_domain = []
all_metas_domain = []
all_domain_labels_combined = []
# Add all DANN datasets for domain tasks
for j, ext_iter in enumerate(external_iters): # Iterate over external_iters
try:
ext_features, ext_label, ext_domain_label, ext_idx = next(ext_iter)
except StopIteration:
external_iters[j] = iter(external_train_loaders[j]) # Use external_train_loaders here
ext_features, ext_label, ext_domain_label, ext_idx = next(external_iters[j])
signal_ext, meta_ext = ext_features
all_signals_domain.append(signal_ext)
all_metas_domain.append(meta_ext)
all_domain_labels_combined.append(ext_domain_label.to(device))
combined_domain_signal = torch.cat(all_signals_domain, 0).to(device)
combined_domain_meta = torch.cat(all_metas_domain, 0).to(device)
combined_domain_target = torch.cat(all_domain_labels_combined, 0).to(device)
# Add .squeeze() to convert (N, 1) to (N)
combined_domain_target = combined_domain_target.squeeze()
# Convert target to long type for CrossEntropyLoss
combined_domain_target = combined_domain_target.long()
# Use detached features to prevent gradient flow back to encoder
_, domain_output_combined, _ = model(combined_domain_signal, combined_domain_meta)
domain_loss = domain_criterion(domain_output_combined, combined_domain_target)
domain_loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer_domain_classifier.step()
total_domain_loss += domain_loss.item()
train_domain_targets.extend(combined_domain_target.cpu().numpy())
train_domain_outputs.extend(torch.argmax(domain_output_combined, dim=1).detach().cpu().numpy())
if epoch >= 10: # Warmup for 5 epochs without DANN
# ------------------------------------------------------------------
# --- Phase 3: Confusion Training (Update Encoder) ---
# ------------------------------------------------------------------
model.train()
# Set requires_grad for encoder parameters
for param in model.domain_classifier.parameters():
param.requires_grad = False # Fix domain predictor
for param in model.encoder.parameters():
param.requires_grad = True
for param in model.classifier.parameters():
param.requires_grad = False # Fix label predictor
optimizer_encoder_confusion.zero_grad()
# Re-run forward pass to get features with gradients enabled for encoder
_, domain_output_confusion, _ = model(combined_domain_signal, combined_domain_meta)
# Maximize domain classifier error: use negative of domain loss
# Dynamically calculate dann_lambda
dann_lambda = calculate_lambda(epoch - 10, num_epochs, max_lambda=config.dann['lambda'], alpha=config.dann['alpha'])
confusion_loss = dann_lambda * confusion_criterion(domain_output_confusion, combined_domain_target)
confusion_loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer_encoder_confusion.step()
total_confusion_loss += confusion_loss.item()
total_chagas_loss /= len(code15_chagas_loader) # Average over code15 batches
total_elr_loss /= len(code15_chagas_loader) # Average over code15 batches
total_combined_chagas_loss /= len(code15_chagas_loader) # Average over code15 batches
total_domain_loss /= max_batches # Average over DANN batches
total_confusion_loss /= max_batches # Average over DANN batches
# Calculate epoch averages for training Chagas logits
if verbose:
epoch_train_pos_logit = train_pos_logits_sum / train_pos_count if train_pos_count > 0 else 0.0
epoch_train_neg_logit = train_neg_logits_sum / train_neg_count if train_neg_count > 0 else 0.0
# ------------------------------------------------------------------
# --- Validation on CODE-15% for Chagas classification ---
# ------------------------------------------------------------------
model.eval()
val_loss = 0.0
val_targets = []
val_outputs = []
# For Chagas logit printing (validation)
val_pos_logits_sum = 0.0
val_neg_logits_sum = 0.0
val_pos_count = 0
val_neg_count = 0
with torch.no_grad():
for features, label, domain_label, idx in code15_val_loader:
signal, meta_features = features
signal = signal.to(device)
meta_features = meta_features.to(device)
label = label.to(device)
task_output, _, _ = model(signal, meta_features)
label_reshaped = label.view(-1, 1)
loss = criterion(task_output, label_reshaped)
val_loss += loss.item()
val_targets.extend(label.cpu().numpy())
val_outputs.extend(torch.sigmoid(task_output).detach().cpu().numpy())
# Accumulate positive/negative logit stats for validation
if verbose:
pos_mask_val = label == 1
neg_mask_val = ~pos_mask_val
if pos_mask_val.any():
val_pos_logits_sum += task_output[pos_mask_val].sum().item()
val_pos_count += pos_mask_val.sum().item()
if neg_mask_val.any():
val_neg_logits_sum += task_output[neg_mask_val].sum().item()
val_neg_count += neg_mask_val.sum().item()
val_loss /= len(code15_val_loader)
val_outputs_arr = np.array(val_outputs)
val_targets_arr = np.array(val_targets)
if verbose:
# Calculate validation epoch averages
val_epoch_pos_logit = val_pos_logits_sum / val_pos_count if val_pos_count > 0 else 0.0
val_epoch_neg_logit = val_neg_logits_sum / val_neg_count if val_neg_count > 0 else 0.0
if len(np.unique(val_targets_arr)) < 2:
print("WARNING: Only one class present in validation targets")
val_auroc = 0.5
else:
val_auroc = roc_auc_score(val_targets_arr, val_outputs_arr)
val_auprc = average_precision_score(val_targets_arr, val_outputs_arr)
val_accuracy = accuracy_score(val_targets_arr, np.round(val_outputs_arr))
val_f1 = f1_score(val_targets_arr, np.round(val_outputs_arr))
epoch_duration = time.time() - epoch_start_time