-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_fdr.py
225 lines (179 loc) · 7.17 KB
/
train_fdr.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
import argparse
from tqdm import tqdm
import yaml
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from sklearn.metrics import roc_auc_score
from dataset import FDRDataset
from model_tcn import FDRNet
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
def train_step(model, loader, optimizer, device):
model.train()
criterion = nn.BCELoss()
total_loss = 0
total_acc = 0
y_true = []
y_scores = []
with tqdm(total=len(loader)) as bar:
for step, batch in enumerate(loader):
_, x, env, f, y = batch
x = x.to(device, dtype=torch.float32)
env = env.to(device, dtype=torch.float32)
f = f.to(device, dtype=torch.float32)
y = y.to(device, dtype=torch.float32)
optimizer.zero_grad()
y_hat = model(x, env, f)
y_hat = torch.sigmoid(y_hat)
# loss = criterion(y, y_hat) # wrong
loss = criterion(y_hat, y) # correct
if torch.isnan(loss):
print(f"Warning: NaN loss detected during training.")
continue
loss.backward()
optimizer.step()
total_loss += loss.item() * x.size(0)
total_acc += torch.eq(y.round(), y_hat.round()).float().sum().item()
y_true.extend(y.cpu().numpy())
y_scores.extend(y_hat.detach().cpu().numpy())
bar.set_description('Train')
bar.set_postfix(lr=get_lr(optimizer), loss=loss.item())
bar.update(1)
auc_score = roc_auc_score(y_true, y_scores)
return total_loss/len(loader.dataset), total_acc/len(loader.dataset), auc_score
def eval_step(model, loader, device):
model.eval()
criterion = nn.BCELoss()
total_loss = 0
total_acc = 0
y_true = []
y_scores = []
with torch.no_grad():
for batch in loader:
_, x, env, f, y = batch
x = x.to(device, dtype=torch.float32)
env = env.to(device, dtype=torch.float32)
f = f.to(device, dtype=torch.float32)
y = y.to(device, dtype=torch.float32)
y_hat = model(x, env, f)
y_hat = torch.sigmoid(y_hat)
loss = criterion(y_hat, y)
total_loss += loss.item() * x.size(0)
total_acc += torch.eq(y.round(), y_hat.round()).float().sum().item()
y_true.extend(y.cpu().numpy())
y_scores.extend(y_hat.detach().cpu().numpy())
auc_score = roc_auc_score(y_true, y_scores)
return total_loss / len(loader.dataset), total_acc / len(loader.dataset), auc_score
def init_random_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
return
if __name__ == "__main__":
# Training settings
parser = argparse.ArgumentParser(description='Mass Spectra and formula to FDR (train)')
parser.add_argument('--train_data', type=str, required=True,
help='Path to training data (.pkl)')
parser.add_argument('--test_data', type=str, required=True,
help='Path to test data (.pkl)')
parser.add_argument('--config_path', type=str, required=True,
help='Path to configuration (.yaml)')
parser.add_argument('--checkpoint_path', type=str, required=True,
help='Path to save checkpoint')
parser.add_argument('--transfer', action='store_true',
help='Whether to load the pretrained encoder')
parser.add_argument('--resume_path', type=str, default='',
help='Path to pretrained model')
parser.add_argument('--seed', type=int, default=42,
help='Seed for random functions')
parser.add_argument('--device', type=int, nargs='+', default=[0],
help='Which GPUs to use if any (default: [0]). Accepts multiple values separated by space.')
parser.add_argument('--no_cuda', action='store_true', default=False,
help='Disables CUDA training')
args = parser.parse_args()
init_random_seed(args.seed)
with open(args.config_path, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
print('Load the model & training configuration from {}'.format(args.config_path))
device_1st = torch.device("cuda:" + str(args.device[0])) if torch.cuda.is_available() and not args.no_cuda else torch.device("cpu")
print(f'Device(s): {args.device}')
# 1. Data
train_set = FDRDataset(args.train_data)
train_loader = DataLoader(train_set,
batch_size=config['train_fdr']['batch_size'],
shuffle=True,
num_workers=config['train_fdr']['num_workers'],
drop_last=True)
valid_set = FDRDataset(args.test_data)
valid_loader = DataLoader(valid_set,
batch_size=config['train_fdr']['batch_size'],
shuffle=False,
num_workers=config['train_fdr']['num_workers'],
drop_last=True)
# 2. Model
model = FDRNet(config['model']).to(device_1st)
num_params = sum(p.numel() for p in model.parameters())
# print(f'{str(model)} #Params: {num_params}')
print(f'# FDRNet Params: {num_params}')
if len(args.device) > 1: # Wrap the model with nn.DataParallel
model = nn.DataParallel(model, device_ids=args.device)
# need to do something when using one GPU
# 3. Train FDRNet
# Define the hyperparameters
optimizer = optim.AdamW(model.parameters(), lr=config['train_fdr']['lr'])
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.5, patience=5)
# Load the checkpoints
if args.transfer and args.resume_path != '':
print("Load the pretrained encoder (freeze the encoder)")
state_dict = torch.load(args.resume_path, map_location=device_1st)['model_state_dict']
encoder_dict = {}
for name, param in state_dict.items():
if name.startswith("encoder"):
param.requires_grad = False # freeze the encoder
encoder_dict[name] = param
model.load_state_dict(encoder_dict, strict=False)
epoch_start = 0
best_valid_auc = 0
elif args.resume_path:
print("Load the checkpoints of the whole model")
checkpoint = torch.load(args.resume_path, map_location=device_1st)
epoch_start = checkpoint['epoch']
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
best_valid_auc = checkpoint['best_val_auc']
else:
epoch_start = 0
best_valid_auc = 0
# Train
early_stop_patience = 0
for epoch in range(epoch_start + 1, config['train_fdr']['epochs'] + 1):
print(f'\n=====Epoch {epoch}')
train_loss, train_acc, train_auc = train_step(model, train_loader, optimizer, device_1st)
print(f"Train loss: {train_loss:.4f} acc: {train_acc:.4f} auc: {train_auc:.4f}")
valid_loss, valid_acc, valid_auc = eval_step(model, valid_loader, device_1st)
print(f"Validation loss: {valid_loss:.4f} acc: {valid_acc:.4f} auc: {valid_auc:.4f}")
if valid_auc > best_valid_auc:
best_valid_auc = valid_auc
if args.checkpoint_path:
print('Saving checkpoint...')
checkpoint = {'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict(),
'num_params': num_params,
'best_val_auc': best_valid_auc}
torch.save(checkpoint, args.checkpoint_path)
early_stop_patience = 0
print('Early stop patience reset')
else:
early_stop_patience += 1
print(f'Early stop count: {early_stop_patience}/{config["train_fdr"]["early_stop_step"]}')
scheduler.step(valid_auc) # ReduceLROnPlateau
print(f'Best auc so far: {best_valid_auc}')
if early_stop_patience == config['train_fdr']['early_stop_step']:
print('Early stop!')
break
print('Done!')