-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
282 lines (222 loc) · 8.3 KB
/
train.py
File metadata and controls
282 lines (222 loc) · 8.3 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
import time
import warnings
import numpy as np
from tqdm import tqdm
from sksurv.metrics import concordance_index_censored
import torch
import torch.optim as optim
from torch.cuda import amp
from dataset import build_loader
from models.snn_model import build_model, build_loss
from configs.survival_config import get_config
from utils import build_scheduler, fix_random_seeds , MetricLogger
warnings.filterwarnings('ignore')
# Training settings
config = get_config()
fix_random_seeds(config.SEED)
train_loader,val_loader,test_loader = build_loader(config)
model = build_model(config)
if config.DEVICES == 'cuda':
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.cuda()
print('Device: {}\n'.format(device))
else:
device = "cpu"
optimizer = optim.AdamW(model.parameters(), lr=config.TRAIN.BASE_LR,
betas=config.TRAIN.OPTIMIZER.BETAS, weight_decay=config.TRAIN.WEIGHT_DECAY)
lr_scheduler = build_scheduler(optimizer, config, train_loader)
losses_dict = build_loss()
def prepare_omics_data(omics_data, device):
"""Prepare omics data by converting to tensor and moving to device."""
gene_data = []
for i, omic in enumerate(omics_data):
gene_data.append(omic.to(device, dtype=torch.float))
return gene_data
def calculate_c_index(risk_scores, censor_status, survival_time):
"""Calculate C-index metric for survival analysis."""
risk_scores = np.concatenate(risk_scores)
censor_status = np.concatenate(censor_status)
survival_time = np.concatenate(survival_time)
# Check for NaN values
if np.any(np.isnan(risk_scores)):
raise ValueError("Risk scores contain NaN values")
# Convert to survival analysis format (censor=1 indicates event occurred)
event_occurred = (1 - censor_status).astype(bool)
c_index = concordance_index_censored(
event_occurred,
survival_time,
risk_scores,
tied_tol=1e-08
)[0]
return c_index, risk_scores, censor_status, survival_time
def train_one_epoch(model, train_loader, optimizer, device, losses_dict):
"""
Train the model for one epoch.
Args:
model: Model to train
train_loader: Training data loader
optimizer: Optimizer
device: Computation device (CPU/GPU)
losses_dict: Dictionary of loss functions
Returns:
tuple: (average_loss, c_index)
"""
model.train()
# Initialize statistics
total_loss = 0.0
all_risk_scores = []
all_censor_status = []
all_survival_time = []
# Hyperparameters (alpha adjusted from 0.7 to 0.4 to match loss function)
alpha = 0.4
eps = 1e-7
# Use progress bar
pbar = tqdm(
enumerate(train_loader),
total=len(train_loader),
desc='Training'
)
for batch_idx, batch_data in pbar:
# Unpack batch data
(
survival_time_batch,
censor_status_batch,
labels_batch,
*omics_batches,
features_batch
) = batch_data
# Prepare data and move to device
omics_data = prepare_omics_data(omics_batches, device)
survival_time = survival_time_batch.to(device, dtype=torch.float32)
censor_status = censor_status_batch.to(device, dtype=torch.float32)
features = features_batch.to(device, dtype=torch.float32)
labels = labels_batch.to(device, dtype=torch.long)
# Clear gradients
optimizer.zero_grad()
# Forward pass
predictions, survival_scores = model.forward(omics_data)
# Calculate loss
loss = losses_dict["SURCELoss"](
predictions,
survival_scores,
labels,
censor_status,
alpha=alpha,
eps=eps
)
# Backward pass
loss.backward()
# Optimizer update
optimizer.step()
# Accumulate statistics
total_loss += loss.item()
# Calculate risk scores (risk = -sum of survival functions)
risk_scores = -torch.sum(survival_scores, dim=1)
# Save statistics for evaluation
all_risk_scores.append(risk_scores.detach().cpu().numpy().reshape(-1))
all_censor_status.append(censor_status.detach().cpu().numpy().reshape(-1))
all_survival_time.append(survival_time.detach().cpu().numpy().reshape(-1))
# Update progress bar
pbar.set_postfix({
'loss': f'{loss.item():.4f}',
'batch': batch_idx + 1
})
# Calculate average loss for the epoch
avg_loss = total_loss / len(train_loader)
# Calculate C-index
try:
c_index, _, _, _ = calculate_c_index(
all_risk_scores,
all_censor_status,
all_survival_time
)
# Get current learning rate
current_lr = optimizer.param_groups[0]['lr']
# Print training summary
print(f"\nEpoch Summary:")
print(f" Learning Rate: {current_lr:.6f}")
print(f" Average Loss: {avg_loss:.6f}")
print(f" C-index: {c_index:.4f}")
print("-" * 40)
return avg_loss, c_index
except ValueError as e:
print(f"\nWarning: {e}")
return avg_loss, None
@torch.no_grad()
def validate_one_epoch(model, valid_loader, device):
"""
Validate the model for one epoch.
Args:
model: Model to validate
valid_loader: Validation data loader
device: Computation device (CPU/GPU)
Returns:
float: Validation C-index
"""
model.eval()
# Initialize statistics
all_risk_scores = []
all_censor_status = []
all_survival_time = []
# Use progress bar
pbar = tqdm(
enumerate(valid_loader),
total=len(valid_loader),
desc='Validating'
)
for batch_idx, batch_data in pbar:
# Unpack batch data
(
survival_time_batch,
censor_status_batch,
labels_batch,
*omics_batches,
features_batch
) = batch_data
# Prepare data and move to device
omics_data = prepare_omics_data(omics_batches, device)
survival_time = survival_time_batch.to(device, dtype=torch.float32)
censor_status = censor_status_batch.to(device, dtype=torch.float32)
features = features_batch.to(device, dtype=torch.float32)
labels = labels_batch.to(device, dtype=torch.long)
# Forward pass
predictions, survival_scores = model.forward(omics_data)
# Calculate risk scores
risk_scores = -torch.sum(survival_scores, dim=1)
# Save statistics
all_risk_scores.append(risk_scores.cpu().numpy().reshape(-1))
all_censor_status.append(censor_status.cpu().numpy().reshape(-1))
all_survival_time.append(survival_time.cpu().numpy().reshape(-1))
# Update progress bar
pbar.set_postfix({'batch': batch_idx + 1})
# Calculate C-index
try:
c_index, _, _, _ = calculate_c_index(
all_risk_scores,
all_censor_status,
all_survival_time
)
print(f"\nValidation Summary:")
print(f" C-index: {c_index:.4f}")
print("-" * 40)
return c_index
except ValueError as e:
print(f"\nWarning in validation: {e}")
return None
if __name__ == "__main__":
print('Start Training')
best_c_index = 0
logger = MetricLogger(config)
for epoch in range(1, config.TRAIN.EPOCHS + 1):
start_time = time.time()
train_one_epoch(model, train_loader, optimizer)
if config.TRAIN.LR_SCHEDULER.NAME == 'StepLR':
lr_scheduler.step()
val_c_index = validate_one_epoch(model, val_loader)
if val_c_index > best_c_index:
best_c_index = val_c_index
torch.save(model.state_dict(), f"{config.CHECKPOINTS_PATH}/{config.CHECKPOINTS_NAME}")
epoch_time = time.time() - start_time
print("epoch:{}, time:{:.2f}s, best_c_index:{:.4f}\n".format(epoch, epoch_time, best_c_index), flush=True)
print('\nStart Testing')
test_acc = validate_one_epoch(model, test_loader)