-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathfinetune.py
437 lines (349 loc) · 16.7 KB
/
finetune.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
import os
import shutil
import sys
import yaml
import numpy as np
import pandas as pd
from datetime import datetime
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from torch.optim.lr_scheduler import CosineAnnealingLR
from sklearn.metrics import roc_auc_score, mean_squared_error, mean_absolute_error
from dataset.dataset_test import MolTestDatasetWrapper
apex_support = False
try:
sys.path.append('./apex')
from apex import amp
apex_support = True
except:
print("Please install apex for mixed precision training from: /~https://github.com/NVIDIA/apex")
apex_support = False
def _save_config_file(model_checkpoints_folder):
if not os.path.exists(model_checkpoints_folder):
os.makedirs(model_checkpoints_folder)
shutil.copy('./config_finetune.yaml', os.path.join(model_checkpoints_folder, 'config_finetune.yaml'))
class Normalizer(object):
"""Normalize a Tensor and restore it later. """
def __init__(self, tensor):
"""tensor is taken as a sample to calculate the mean and std"""
self.mean = torch.mean(tensor)
self.std = torch.std(tensor)
def norm(self, tensor):
return (tensor - self.mean) / self.std
def denorm(self, normed_tensor):
return normed_tensor * self.std + self.mean
def state_dict(self):
return {'mean': self.mean,
'std': self.std}
def load_state_dict(self, state_dict):
self.mean = state_dict['mean']
self.std = state_dict['std']
class FineTune(object):
def __init__(self, dataset, config):
self.config = config
self.device = self._get_device()
current_time = datetime.now().strftime('%b%d_%H-%M-%S')
dir_name = current_time + '_' + config['task_name'] + '_' + config['dataset']['target']
log_dir = os.path.join('finetune', dir_name)
self.writer = SummaryWriter(log_dir=log_dir)
self.dataset = dataset
if config['dataset']['task'] == 'classification':
self.criterion = nn.CrossEntropyLoss()
elif config['dataset']['task'] == 'regression':
if self.config["task_name"] in ['qm7', 'qm8', 'qm9']:
self.criterion = nn.L1Loss()
else:
self.criterion = nn.MSELoss()
def _get_device(self):
if torch.cuda.is_available() and self.config['gpu'] != 'cpu':
device = self.config['gpu']
torch.cuda.set_device(device)
else:
device = 'cpu'
print("Running on:", device)
return device
def _step(self, model, data, n_iter):
# get the prediction
__, pred = model(data) # [N,C]
if self.config['dataset']['task'] == 'classification':
loss = self.criterion(pred, data.y.flatten())
elif self.config['dataset']['task'] == 'regression':
if self.normalizer:
loss = self.criterion(pred, self.normalizer.norm(data.y))
else:
loss = self.criterion(pred, data.y)
return loss
def train(self):
train_loader, valid_loader, test_loader = self.dataset.get_data_loaders()
self.normalizer = None
if self.config["task_name"] in ['qm7', 'qm9']:
labels = []
for d, __ in train_loader:
labels.append(d.y)
labels = torch.cat(labels)
self.normalizer = Normalizer(labels)
print(self.normalizer.mean, self.normalizer.std, labels.shape)
if self.config['model_type'] == 'gin':
from models.ginet_finetune import GINet
model = GINet(self.config['dataset']['task'], **self.config["model"]).to(self.device)
model = self._load_pre_trained_weights(model)
elif self.config['model_type'] == 'gcn':
from models.gcn_finetune import GCN
model = GCN(self.config['dataset']['task'], **self.config["model"]).to(self.device)
model = self._load_pre_trained_weights(model)
layer_list = []
for name, param in model.named_parameters():
if 'pred_head' in name:
print(name, param.requires_grad)
layer_list.append(name)
params = list(map(lambda x: x[1],list(filter(lambda kv: kv[0] in layer_list, model.named_parameters()))))
base_params = list(map(lambda x: x[1],list(filter(lambda kv: kv[0] not in layer_list, model.named_parameters()))))
optimizer = torch.optim.Adam(
[{'params': base_params, 'lr': self.config['init_base_lr']}, {'params': params}],
self.config['init_lr'], weight_decay=eval(self.config['weight_decay'])
)
if apex_support and self.config['fp16_precision']:
model, optimizer = amp.initialize(
model, optimizer, opt_level='O2', keep_batchnorm_fp32=True
)
model_checkpoints_folder = os.path.join(self.writer.log_dir, 'checkpoints')
# save config file
_save_config_file(model_checkpoints_folder)
n_iter = 0
valid_n_iter = 0
best_valid_loss = np.inf
best_valid_rgr = np.inf
best_valid_cls = 0
for epoch_counter in range(self.config['epochs']):
for bn, data in enumerate(train_loader):
optimizer.zero_grad()
data = data.to(self.device)
loss = self._step(model, data, n_iter)
if n_iter % self.config['log_every_n_steps'] == 0:
self.writer.add_scalar('train_loss', loss, global_step=n_iter)
print(epoch_counter, bn, loss.item())
if apex_support and self.config['fp16_precision']:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
n_iter += 1
# validate the model if requested
if epoch_counter % self.config['eval_every_n_epochs'] == 0:
if self.config['dataset']['task'] == 'classification':
valid_loss, valid_cls = self._validate(model, valid_loader)
if valid_cls > best_valid_cls:
# save the model weights
best_valid_cls = valid_cls
torch.save(model.state_dict(), os.path.join(model_checkpoints_folder, 'model.pth'))
elif self.config['dataset']['task'] == 'regression':
valid_loss, valid_rgr = self._validate(model, valid_loader)
if valid_rgr < best_valid_rgr:
# save the model weights
best_valid_rgr = valid_rgr
torch.save(model.state_dict(), os.path.join(model_checkpoints_folder, 'model.pth'))
self.writer.add_scalar('validation_loss', valid_loss, global_step=valid_n_iter)
valid_n_iter += 1
self._test(model, test_loader)
def _load_pre_trained_weights(self, model):
try:
checkpoints_folder = os.path.join('./ckpt', self.config['fine_tune_from'], 'checkpoints')
state_dict = torch.load(os.path.join(checkpoints_folder, 'model.pth'), map_location=self.device)
# model.load_state_dict(state_dict)
model.load_my_state_dict(state_dict)
print("Loaded pre-trained model with success.")
except FileNotFoundError:
print("Pre-trained weights not found. Training from scratch.")
return model
def _validate(self, model, valid_loader):
predictions = []
labels = []
with torch.no_grad():
model.eval()
valid_loss = 0.0
num_data = 0
for bn, data in enumerate(valid_loader):
data = data.to(self.device)
__, pred = model(data)
loss = self._step(model, data, bn)
valid_loss += loss.item() * data.y.size(0)
num_data += data.y.size(0)
if self.normalizer:
pred = self.normalizer.denorm(pred)
if self.config['dataset']['task'] == 'classification':
pred = F.softmax(pred, dim=-1)
if self.device == 'cpu':
predictions.extend(pred.detach().numpy())
labels.extend(data.y.flatten().numpy())
else:
predictions.extend(pred.cpu().detach().numpy())
labels.extend(data.y.cpu().flatten().numpy())
valid_loss /= num_data
model.train()
if self.config['dataset']['task'] == 'regression':
predictions = np.array(predictions)
labels = np.array(labels)
if self.config['task_name'] in ['qm7', 'qm8', 'qm9']:
mae = mean_absolute_error(labels, predictions)
print('Validation loss:', valid_loss, 'MAE:', mae)
return valid_loss, mae
else:
rmse = mean_squared_error(labels, predictions, squared=False)
print('Validation loss:', valid_loss, 'RMSE:', rmse)
return valid_loss, rmse
elif self.config['dataset']['task'] == 'classification':
predictions = np.array(predictions)
labels = np.array(labels)
roc_auc = roc_auc_score(labels, predictions[:,1])
print('Validation loss:', valid_loss, 'ROC AUC:', roc_auc)
return valid_loss, roc_auc
def _test(self, model, test_loader):
model_path = os.path.join(self.writer.log_dir, 'checkpoints', 'model.pth')
state_dict = torch.load(model_path, map_location=self.device)
model.load_state_dict(state_dict)
print("Loaded trained model with success.")
# test steps
predictions = []
labels = []
with torch.no_grad():
model.eval()
test_loss = 0.0
num_data = 0
for bn, data in enumerate(test_loader):
data = data.to(self.device)
__, pred = model(data)
loss = self._step(model, data, bn)
test_loss += loss.item() * data.y.size(0)
num_data += data.y.size(0)
if self.normalizer:
pred = self.normalizer.denorm(pred)
if self.config['dataset']['task'] == 'classification':
pred = F.softmax(pred, dim=-1)
if self.device == 'cpu':
predictions.extend(pred.detach().numpy())
labels.extend(data.y.flatten().numpy())
else:
predictions.extend(pred.cpu().detach().numpy())
labels.extend(data.y.cpu().flatten().numpy())
test_loss /= num_data
model.train()
if self.config['dataset']['task'] == 'regression':
predictions = np.array(predictions)
labels = np.array(labels)
if self.config['task_name'] in ['qm7', 'qm8', 'qm9']:
self.mae = mean_absolute_error(labels, predictions)
print('Test loss:', test_loss, 'Test MAE:', self.mae)
else:
self.rmse = mean_squared_error(labels, predictions, squared=False)
print('Test loss:', test_loss, 'Test RMSE:', self.rmse)
elif self.config['dataset']['task'] == 'classification':
predictions = np.array(predictions)
labels = np.array(labels)
self.roc_auc = roc_auc_score(labels, predictions[:,1])
print('Test loss:', test_loss, 'Test ROC AUC:', self.roc_auc)
def main(config):
dataset = MolTestDatasetWrapper(config['batch_size'], **config['dataset'])
fine_tune = FineTune(dataset, config)
fine_tune.train()
if config['dataset']['task'] == 'classification':
return fine_tune.roc_auc
if config['dataset']['task'] == 'regression':
if config['task_name'] in ['qm7', 'qm8', 'qm9']:
return fine_tune.mae
else:
return fine_tune.rmse
if __name__ == "__main__":
config = yaml.load(open("config_finetune.yaml", "r"), Loader=yaml.FullLoader)
if config['task_name'] == 'BBBP':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/bbbp/BBBP.csv'
target_list = ["p_np"]
elif config['task_name'] == 'Tox21':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/tox21/tox21.csv'
target_list = [
"NR-AR", "NR-AR-LBD", "NR-AhR", "NR-Aromatase", "NR-ER", "NR-ER-LBD",
"NR-PPAR-gamma", "SR-ARE", "SR-ATAD5", "SR-HSE", "SR-MMP", "SR-p53"
]
elif config['task_name'] == 'ClinTox':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/clintox/clintox.csv'
target_list = ['CT_TOX', 'FDA_APPROVED']
elif config['task_name'] == 'HIV':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/hiv/HIV.csv'
target_list = ["HIV_active"]
elif config['task_name'] == 'BACE':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/bace/bace.csv'
target_list = ["Class"]
elif config['task_name'] == 'SIDER':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/sider/sider.csv'
target_list = [
"Hepatobiliary disorders", "Metabolism and nutrition disorders", "Product issues",
"Eye disorders", "Investigations", "Musculoskeletal and connective tissue disorders",
"Gastrointestinal disorders", "Social circumstances", "Immune system disorders",
"Reproductive system and breast disorders",
"Neoplasms benign, malignant and unspecified (incl cysts and polyps)",
"General disorders and administration site conditions", "Endocrine disorders",
"Surgical and medical procedures", "Vascular disorders",
"Blood and lymphatic system disorders", "Skin and subcutaneous tissue disorders",
"Congenital, familial and genetic disorders", "Infections and infestations",
"Respiratory, thoracic and mediastinal disorders", "Psychiatric disorders",
"Renal and urinary disorders", "Pregnancy, puerperium and perinatal conditions",
"Ear and labyrinth disorders", "Cardiac disorders",
"Nervous system disorders", "Injury, poisoning and procedural complications"
]
elif config['task_name'] == 'MUV':
config['dataset']['task'] = 'classification'
config['dataset']['data_path'] = 'data/muv/muv.csv'
target_list = [
'MUV-692', 'MUV-689', 'MUV-846', 'MUV-859', 'MUV-644', 'MUV-548', 'MUV-852',
'MUV-600', 'MUV-810', 'MUV-712', 'MUV-737', 'MUV-858', 'MUV-713', 'MUV-733',
'MUV-652', 'MUV-466', 'MUV-832'
]
elif config['task_name'] == 'FreeSolv':
config['dataset']['task'] = 'regression'
config['dataset']['data_path'] = 'data/freesolv/freesolv.csv'
target_list = ["expt"]
elif config["task_name"] == 'ESOL':
config['dataset']['task'] = 'regression'
config['dataset']['data_path'] = 'data/esol/esol.csv'
target_list = ["measured log solubility in mols per litre"]
elif config["task_name"] == 'Lipo':
config['dataset']['task'] = 'regression'
config['dataset']['data_path'] = 'data/lipophilicity/Lipophilicity.csv'
target_list = ["exp"]
elif config["task_name"] == 'qm7':
config['dataset']['task'] = 'regression'
config['dataset']['data_path'] = 'data/qm7/qm7.csv'
target_list = ["u0_atom"]
elif config["task_name"] == 'qm8':
config['dataset']['task'] = 'regression'
config['dataset']['data_path'] = 'data/qm8/qm8.csv'
target_list = [
"E1-CC2", "E2-CC2", "f1-CC2", "f2-CC2", "E1-PBE0", "E2-PBE0",
"f1-PBE0", "f2-PBE0", "E1-CAM", "E2-CAM", "f1-CAM","f2-CAM"
]
elif config["task_name"] == 'qm9':
config['dataset']['task'] = 'regression'
config['dataset']['data_path'] = 'data/qm9/qm9.csv'
target_list = ['mu', 'alpha', 'homo', 'lumo', 'gap', 'r2', 'zpve', 'cv']
else:
raise ValueError('Undefined downstream task!')
print(config)
results_list = []
for target in target_list:
config['dataset']['target'] = target
result = main(config)
results_list.append([target, result])
os.makedirs('experiments', exist_ok=True)
df = pd.DataFrame(results_list)
df.to_csv(
'experiments/{}_{}_finetune.csv'.format(config['fine_tune_from'], config['task_name']),
mode='a', index=False, header=False
)