-
Notifications
You must be signed in to change notification settings - Fork 3
/
rdkit_free_train.py
350 lines (294 loc) · 15.2 KB
/
rdkit_free_train.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
import argparse
import pickle
import numpy as np
import pandas as pd
import torch
from sklearn import metrics
from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split
from torch.optim.lr_scheduler import ReduceLROnPlateau, CosineAnnealingWarmRestarts
from torch.utils.data import DataLoader
from tqdm import tqdm
from features.rdkit_free_datasets import ImageDatasetPreLoaded
from metrics import trackers
from models import imagemodel
if torch.cuda.is_available():
import torch.backends.cudnn
torch.backends.cudnn.benchmark = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class EarlyStopping:
"""Early stops the training if validation loss doesn't improve after a given patience."""
def __init__(self, patience=7, verbose=False, delta=0):
"""
Args:
patience (int): How long to wait after last time validation loss improved.
Default: 7
verbose (bool): If True, prints a message for each validation loss improvement.
Default: False
delta (float): Minimum change in the monitored quantity to qualify as an improvement.
Default: 0
"""
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
def __call__(self, val_loss):
score = -val_loss
if self.best_score is None:
self.best_score = score
elif score < self.best_score + self.delta:
self.counter += 1
print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.counter = 0
def get_optimizer(c):
if c == 'sgd':
return torch.optim.SGD
elif c == 'adam':
return torch.optim.Adam
elif c == 'adamw':
return torch.optim.AdamW
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-i', type=str, required=True, help='smiles input file')
parser.add_argument('--precomputed_values', type=str, required=False, default=None,
help='precomputed decs for trainings')
parser.add_argument('--imputer_pickle', type=str, required=False, default=None,
help='imputer and scaler for transforming data')
parser.add_argument('-p', choices=["none"], help='select property for model')
parser.add_argument('-w', type=int, default=8, help='number of workers for data loaders to use.')
parser.add_argument('-b', type=int, default=64, help='batch size to use')
parser.add_argument('-o', type=str, default='saved_models/model.pt', help='name of file to save model to')
parser.add_argument('-r', type=int, default=32, help='random seed for splitting.')
parser.add_argument('-pb', action='store_true')
parser.add_argument('-g', type=int, default=1, help='use multiple GPUs')
parser.add_argument('-t', type=int, default=1, help='number of tasks')
parser.add_argument('--nheads', type=int, default=1, help='number of attention heads')
parser.add_argument('--metric_plot_prefix', default=None, type=str, help='prefix for graphs for performance')
parser.add_argument('--optimizer', default='adamw', type=str, help='optimizer to use',
choices=['sgd', 'adam', 'adamw'])
parser.add_argument('--rotate', action='store_true')
parser.add_argument('--lr', default=1e-4, type=float, help='learning to use')
parser.add_argument('--epochs', default=50, type=int, help='number of epochs to use')
parser.add_argument('--dropout_rate', default=0.0, type=float, help='dropout rate')
parser.add_argument('--eval', action='store_true')
parser.add_argument('--classifacation', action='store_true')
parser.add_argument('--ensemble_eval', action='store_true')
parser.add_argument('--mae', action='store_true')
args = parser.parse_args()
if args.metric_plot_prefix is None:
args.metric_plot_prefix = "".join(args.o.split(".")[:-1]) + "_"
args.optimizer = get_optimizer(args.optimizer)
print(args)
return args
def run_eval(model, train_loader, ordinal=False, classifacation=False, enseml=True, tasks=1):
with torch.no_grad():
model.eval()
if classifacation:
tracker = trackers.ComplexPytorchHistory() if args.p == 'all' else trackers.PytorchHistory(
metric=metrics.roc_auc_score, metric_name='roc-auc')
else:
tracker = trackers.ComplexPytorchHistory() if args.p == 'all' else trackers.PytorchHistory()
train_loss = 0
test_loss = 0
train_iters = 0
test_iters = 0
preds = []
values = []
predss = []
valuess = []
model.eval()
for i in range(25 if enseml else 1):
for i, (drugfeats, value) in enumerate(train_loader):
drugfeats, value = drugfeats.to(device), value.to(device)
pred, attn = model(drugfeats)
mse_loss = torch.nn.functional.l1_loss(pred, value).mean()
test_loss += mse_loss.item()
test_iters += 1
tracker.track_metric(pred.detach().cpu().numpy(), value.detach().cpu().numpy())
valuess.append(value.cpu().detach().numpy().flatten())
predss.append(pred.detach().cpu().numpy().flatten())
preds.append(np.concatenate(predss, axis=0))
values.append(np.concatenate(valuess, axis=0))
preds = np.stack(preds)
values = np.stack(values)
print(preds.shape, values.shape)
preds = np.mean(preds, axis=0)
values = np.mean(values, axis=0)
if ordinal:
preds, values = np.round(preds), np.round(values)
incorrect = 0
for i in range(preds.shape[0]):
if values[i] != preds[i]:
print("incorrect at", i)
incorrect += 1
print("total incorrect", incorrect, incorrect / preds.shape[0])
tracker.log_loss(test_loss / test_iters, train=False)
tracker.log_metric(internal=True, train=False)
print("val", test_loss / test_iters, 'r2', tracker.get_last_metric(train=False))
print("avg ensmelb r2, mae", metrics.r2_score(values, preds), metrics.mean_absolute_error(values, preds))
return model, tracker
def trainer(model, optimizer, train_loader, test_loader, epochs=5, gpus=1, tasks=1, classifacation=False, mae=False,
pb=True, out=None, cyclic=False, verbose=True):
device = next(model.parameters()).device
if classifacation:
tracker = trackers.ComplexPytorchHistory() if tasks > 1 else trackers.PytorchHistory(
metric=metrics.roc_auc_score, metric_name='roc-auc')
else:
tracker = trackers.ComplexPytorchHistory() if tasks > 1 else trackers.PytorchHistory()
earlystopping = EarlyStopping(patience=25, delta=1e-5)
if cyclic:
lr_red = CosineAnnealingWarmRestarts(optimizer, T_0=20)
else:
lr_red = ReduceLROnPlateau(optimizer, mode='min', factor=0.8, patience=15, cooldown=0, verbose=verbose,
threshold=1e-4,
min_lr=1e-8)
for epochnum in range(epochs):
train_loss = 0
test_loss = 0
train_iters = 0
test_iters = 0
model.train()
if pb:
gen = tqdm(enumerate(train_loader))
else:
gen = enumerate(train_loader)
for i, (drugfeats, value) in gen:
optimizer.zero_grad()
drugfeats, value = drugfeats.to(device), value.to(device)
pred, attn = model(drugfeats)
if classifacation:
mse_loss = torch.nn.functional.binary_cross_entropy_with_logits(pred, value).mean()
elif mae:
mse_loss = torch.nn.functional.l1_loss(pred, value).mean()
else:
mse_loss = torch.nn.functional.mse_loss(pred, value).mean()
mse_loss.backward()
torch.nn.utils.clip_grad_value_(model.parameters(), 10.0)
optimizer.step()
train_loss += mse_loss.item()
train_iters += 1
tracker.track_metric(pred=pred.detach().cpu().numpy(), value=value.detach().cpu().numpy())
tracker.log_loss(train_loss / train_iters, train=True)
tracker.log_metric(internal=True, train=True)
model.eval()
with torch.no_grad():
for i, (drugfeats, value) in enumerate(test_loader):
drugfeats, value = drugfeats.to(device), value.to(device)
pred, attn = model(drugfeats)
if classifacation:
mse_loss = torch.nn.functional.binary_cross_entropy_with_logits(pred, value).mean()
elif mae:
mse_loss = torch.nn.functional.l1_loss(pred, value).mean()
else:
mse_loss = torch.nn.functional.mse_loss(pred, value).mean()
test_loss += mse_loss.item()
test_iters += 1
tracker.track_metric(pred.detach().cpu().numpy(), value.detach().cpu().numpy())
tracker.log_loss(train_loss / train_iters, train=False)
tracker.log_metric(internal=True, train=False)
lr_red.step(test_loss / test_iters)
earlystopping(test_loss / test_iters)
if verbose:
print("Epoch", epochnum, train_loss / train_iters, test_loss / test_iters, tracker.metric_name,
tracker.get_last_metric(train=True), tracker.get_last_metric(train=False))
if out is not None:
if gpus == 1:
state = model.state_dict()
heads = model.nheads
else:
state = model.module.state_dict()
heads = model.module.nheads
torch.save({'model_state': state,
'opt_state': optimizer.state_dict(),
'history': tracker,
'nheads': heads,
'ntasks': tasks}, out)
if earlystopping.early_stop:
break
return model, tracker
def load_data_models(fname, random_seed, workers, batch_size, pname='logp', return_datasets=False, nheads=1,
precompute_frame=None, imputer_pickle=None, eval=False, tasks=1, gpus=1, cvs=None, rotate=False,
classifacation=False, ensembl=False, dropout=0, intermediate_rep=None, precomputed_images=None,
linear_layers=2, model_checkpoint=None):
df = pd.read_csv(fname, header=None)
smiles = list(df.iloc[:, 0])
if precomputed_images is not None:
with open(precomputed_images, 'rb') as f:
precomputed_images = pickle.load(f)
if precompute_frame is not None:
features = np.load(precompute_frame).astype(np.float32)
features = np.nan_to_num(features, nan=0, posinf=0, neginf=0)
assert (features.shape[0] == len(smiles))
if cvs is not None:
kfold = KFold(random_state=random_seed, n_splits=5, shuffle=True)
train_idx, test_idx = list(kfold.split(list(range(len(smiles)))))[cvs]
train_smiles = [smiles[i] for i in train_idx]
test_smiles = [smiles[i] for i in test_idx]
else:
train_idx, test_idx, train_smiles, test_smiles = train_test_split(list(range(len(smiles))), smiles,
test_size=0.2, random_state=random_seed)
train_features = features[train_idx]
test_features = features[test_idx]
train_precomputed_images = [precomputed_images[i] for i in train_idx]
test_precomputed_images = [precomputed_images[i] for i in test_idx]
train_dataset = ImageDatasetPreLoaded(train_smiles, train_features, imputer_pickle,
property_func=None,
values=tasks, rot=rotate, images=train_precomputed_images)
print("Batch size", batch_size)
batch_size = int(batch_size)
train_loader = DataLoader(train_dataset, num_workers=workers, pin_memory=True, batch_size=batch_size,
shuffle=(not eval))
test_dataset = ImageDatasetPreLoaded(test_smiles, test_features, imputer_pickle,
property_func=None,
values=tasks, rot=359, images=test_precomputed_images)
test_loader = DataLoader(test_dataset, num_workers=workers, pin_memory=True, batch_size=batch_size,
shuffle=(not eval))
if intermediate_rep is None:
model = imagemodel.ImageModel(nheads=nheads, outs=tasks, classifacation=classifacation, dr=dropout,
linear_layers=linear_layers, model_path=model_checkpoint)
else:
model = imagemodel.ImageModel(nheads=nheads, outs=tasks, classifacation=classifacation, dr=dropout,
intermediate_rep=intermediate_rep, linear_layers=linear_layers,
model_path=model_checkpoint)
else:
assert (False)
if gpus > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = torch.nn.DataParallel(model)
if return_datasets:
return train_dataset, test_dataset, model
else:
return train_loader, test_loader, model
if __name__ == '__main__':
args = get_args()
np.random.seed(args.r)
torch.manual_seed(args.r)
train_loader, test_loader, model = load_data_models(args.i, args.r, args.w, args.b, args.p, nheads=args.nheads,
precompute_frame=args.precomputed_values,
imputer_pickle=args.imputer_pickle, eval=args.eval,
tasks=args.t, gpus=args.g, rotate=args.rotate,
classifacation=args.classifacation, ensembl=args.ensemble_eval,
dropout=args.dropout_rate)
print("Done.")
print("Starting trainer.")
if args.eval:
model.load_state_dict(torch.load(args.o)['model_state'])
model.to(device)
run_eval(model, test_loader, ordinal=True, enseml=args.ensemble_eval)
exit()
model.to(device)
optimizer = args.optimizer(model.parameters(), lr=args.lr)
print("Number of parameters:",
sum([np.prod(p.size()) for p in filter(lambda p: p.requires_grad, model.parameters())]))
model, history = trainer(model, optimizer, train_loader, test_loader, out=args.o, epochs=args.epochs, pb=args.pb,
gpus=args.g, classifacation=args.classifacation, tasks=args.t, mae=args.mae)
history.plot_loss(save_file=args.metric_plot_prefix + "loss.png", title=args.p + " Loss")
history.plot_metric(save_file=args.metric_plot_prefix + "r2.png", title=args.p + " " + history.metric_name)
print("Finished training, now")