-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_cuscpi.py
327 lines (288 loc) · 18.9 KB
/
train_cuscpi.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
import argparse
import concurrent.futures
import copy
import os
import re
import sys
from icecream import install
from ogb.lsc import DglPCQM4MDataset, PCQM4MEvaluator
from ogb.utils import smiles2graph
from commons.utils import seed_all, get_random_indices, TENSORBOARD_FUNCTIONS, move_to_device
from datasets.ZINC_dataset import ZINCDataset
from datasets.bace_geomol_feat import BACEGeomol
from datasets.bace_geomol_featurization_of_qm9 import BACEGeomolQM9Featurization
from datasets.bace_geomol_random_split import BACEGeomolRandom
from datasets.bbbp_geomol_feat import BBBPGeomol
from datasets.bbbp_geomol_featurization_of_qm9 import BBBPGeomolQM9Featurization
from datasets.bbbp_geomol_random_split import BBBPGeomolRandom
from datasets.esol_geomol_feat import ESOLGeomol
from datasets.esol_geomol_featurization_of_qm9 import ESOLGeomolQM9Featurization
from datasets.file_loader_drugs import FileLoaderDrugs
from datasets.file_loader_qm9 import FileLoaderQM9
from datasets.geom_drugs_dataset import GEOMDrugs
from datasets.geom_qm9_dataset import GEOMqm9
from datasets.geomol_geom_qm9_dataset import QM9GeomolFeatDataset
from datasets.inference_dataset import InferenceDataset
from datasets.cus_dataset import CusDataset
from datasets.lipo_geomol_feat import LIPOGeomol
from datasets.lipo_geomol_featurization_of_qm9 import LIPOGeomolQM9Featurization
from datasets.ogbg_dataset_extension import OGBGDatasetExtension
from datasets.qm9_dataset_geomol_conformers import QM9DatasetGeomolConformers
from datasets.qm9_dataset_rdkit_conformers import QM9DatasetRDKITConformers
from datasets.qm9_geomol_featurization import QM9GeomolFeaturization
from datasets.qmugs_dataset import QMugsDataset
from models.geomol_mpnn import GeomolGNNWrapper
from train import load_model
from trainer.byol_trainer import BYOLTrainer
from trainer.byol_wrapper import BYOLwrapper
import seaborn
from trainer.graphcl_trainer import GraphCLTrainer
from trainer.optimal_transport_trainer import OptimalTransportTrainer
from trainer.philosophy_trainer import PhilosophyTrainer
from trainer.self_supervised_ae_trainer import SelfSupervisedAETrainer
from trainer.self_supervised_alternating_trainer import SelfSupervisedAlternatingTrainer
from trainer.self_supervised_trainer import SelfSupervisedTrainer
import yaml
from datasets.custom_collate import * # do not remove
from models import * # do not remove
from torch.nn import * # do not remove
from torch.optim import * # do not remove
from commons.losses import * # do not remove
from torch.optim.lr_scheduler import * # do not remove
from datasets.samplers import * # do not remove
from datasets.qm9_dataset import QM9Dataset
from torch.utils.data import DataLoader, Subset
from trainer.metrics import QM9DenormalizedL1, QM9DenormalizedL2, \
QM9SingleTargetDenormalizedL1, Rsquared, NegativeSimilarity, MeanPredictorLoss, \
PositiveSimilarity, ContrastiveAccuracy, TrueNegativeRate, TruePositiveRate, Alignment, Uniformity, \
BatchVariance, DimensionCovariance, MAE, PositiveSimilarityMultiplePositivesSeparate2d, \
NegativeSimilarityMultiplePositivesSeparate2d, OGBEvaluator, PearsonR, PositiveProb, NegativeProb, \
Conformer2DVariance, Conformer3DVariance, PCQM4MEvaluatorWrapper, MSE, C_index
from trainer.trainer_cpi import Trainer
###################################
from datasets.cus_dataset_cpi_processed1 import CusDatasetCPI
import biotite.structure as struc
import biotite.structure.io as strucio
# turn on for debugging C code like Segmentation Faults
import faulthandler
import time
import wandb
time_ex = time.strftime("%Y%m%d_%H%M%S",time.localtime())
print(time_ex)
faulthandler.enable()
install()
seaborn.set_theme()
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
torch.set_num_threads(1)
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"
os.environ['OPENBLAS_NUM_THREADS'] = "1"
def parse_arguments():
p = argparse.ArgumentParser()
p.add_argument('--config', type=argparse.FileType(mode='r'), default='configs_clean/fingerprint_inference.yml')
p.add_argument('--experiment_name', type=str, help='name that will be added to the runs folder output')
p.add_argument('--logdir', type=str, default='runs', help='tensorboard logdirectory')
p.add_argument('--num_epochs', type=int, default=2500, help='number of times to iterate through all samples')
p.add_argument('--batch_size', type=int, default=1024, help='samples that will be processed in parallel')
p.add_argument('--patience', type=int, default=20, help='stop training after no improvement in this many epochs')
p.add_argument('--minimum_epochs', type=int, default=0, help='minimum numer of epochs to run')
p.add_argument('--dataset', type=str, default='qm9', help='[qm9, zinc, drugs, geom_qm9, molhiv]')
p.add_argument('--num_train', type=int, default=-1, help='n samples of the model samples to use for train')
p.add_argument('--seed', type=int, default=123, help='seed for reproducibility')
p.add_argument('--num_val', type=int, default=None, help='n samples of the model samples to use for validation')
p.add_argument('--multithreaded_seeds', type=list, default=[],
help='if this is non empty, multiple threads will be started, training the same model but with the different seeds')
p.add_argument('--seed_data', type=int, default=123, help='if you want to use a different seed for the datasplit')
p.add_argument('--loss_func', type=str, default='MSELoss', help='Class name of torch.nn like [MSELoss, L1Loss]')
p.add_argument('--loss_params', type=dict, default={}, help='parameters with keywords of the chosen loss function')
p.add_argument('--critic_loss', type=str, default='MSELoss', help='Class name of torch.nn like [MSELoss, L1Loss]')
p.add_argument('--critic_loss_params', type=dict, default={},
help='parameters with keywords of the chosen loss function')
p.add_argument('--optimizer', type=str, default='Adam', help='Class name of torch.optim like [Adam, SGD, AdamW]')
p.add_argument('--optimizer_params', type=dict, help='parameters with keywords of the chosen optimizer like lr')
p.add_argument('--lr_scheduler', type=str,
help='Class name of torch.optim.lr_scheduler like [CosineAnnealingLR, ExponentialLR, LambdaLR]')
p.add_argument('--lr_scheduler_params', type=dict, help='parameters with keywords of the chosen lr_scheduler')
p.add_argument('--scheduler_step_per_batch', default=True, type=bool,
help='step every batch if true step every epoch otherwise')
p.add_argument('--log_iterations', type=int, default=-1,
help='log every log_iterations iterations (-1 for only logging after each epoch)')
p.add_argument('--expensive_log_iterations', type=int, default=100,
help='frequency with which to do expensive logging operations')
p.add_argument('--eval_per_epochs', type=int, default=0,
help='frequency with which to do run the function run_eval_per_epoch that can do some expensive calculations on the val set or sth like that. If this is zero, then the function will never be called')
p.add_argument('--linear_probing_samples', type=int, default=500,
help='number of samples to use for linear probing in the run_eval_per_epoch function of the self supervised trainer')
p.add_argument('--num_conformers', type=int, default=3,
help='number of conformers to use if we are using multiple conformers on the 3d side')
p.add_argument('--metrics', default=['mse'], help='tensorboard metrics [mae, mae_denormalized, qm9_properties ...]')
p.add_argument('--main_metric', default='mse', help='for early stopping etc.')
p.add_argument('--main_metric_goal', type=str, default='max', help='controls early stopping. [max, min]')
p.add_argument('--val_per_batch', type=bool, default=True,
help='run evaluation every batch and then average over the eval results. When running the molhiv benchmark for example, this needs to be Fale because we need to evaluate on all val data at once since the metric is rocauc')
p.add_argument('--tensorboard_functions', default=[], help='choices of the TENSORBOARD_FUNCTIONS in utils')
p.add_argument('--checkpoint', type=str, help='path to directory that contains a checkpoint to continue training')
p.add_argument('--pretrain_checkpoint', type=str, help='Specify path to finetune from a pretrained checkpoint')
p.add_argument('--transfer_layers', default=[],
help='strings contained in the keys of the weights that are transferred')
p.add_argument('--frozen_layers', default=[],
help='strings contained in the keys of the weights that are transferred')
p.add_argument('--exclude_from_transfer', default=[],
help='parameters that usually should not be transferred like batchnorm params')
p.add_argument('--transferred_lr', type=float, default=None, help='set to use a different LR for transfer layers')
p.add_argument('--num_epochs_local_only', type=int, default=1,
help='when training with OptimalTransportTrainer, this specifies for how many epochs only the local predictions will get a loss')
p.add_argument('--required_data', default=[],
help='what will be included in a batch like [dgl_graph, targets, dgl_graph3d]')
p.add_argument('--collate_function', default='graph_collate', help='the collate function to use for DataLoader')
p.add_argument('--collate_params', type=dict, default={},
help='parameters with keywords of the chosen collate function')
p.add_argument('--use_e_features', default=True, type=bool, help='ignore edge features if set to False')
p.add_argument('--targets', default=[], help='properties that should be predicted')
p.add_argument('--device', type=str, default='cuda', help='What device to train on: cuda or cpu')
p.add_argument('--dist_embedding', type=bool, default=False, help='add dist embedding to complete graphs edges')
p.add_argument('--num_radial', type=int, default=6, help='number of frequencies for distance embedding')
p.add_argument('--models_to_save', type=list, default=[],
help='specify after which epochs to remember the best model')
p.add_argument('--model_type', type=str, default='MPNN', help='Classname of one of the models in the models dir')
p.add_argument('--model_parameters', type=dict, help='dictionary of model parameters')
p.add_argument('--model3d_type', type=str, default=None, help='Classname of one of the models in the models dir')
p.add_argument('--model3d_parameters', type=dict, help='dictionary of model parameters')
p.add_argument('--critic_type', type=str, default=None, help='Classname of one of the models in the models dir')
p.add_argument('--critic_parameters', type=dict, help='dictionary of model parameters')
p.add_argument('--trainer', type=str, default='contrastive', help='[contrastive, byol, alternating, philosophy]')
p.add_argument('--train_sampler', type=str, default=None, help='any of pytorchs samplers or a custom sampler')
p.add_argument('--eval_on_test', type=bool, default=True, help='runs evaluation on test set if true')
p.add_argument('--force_random_split', type=bool, default=False, help='use random split for ogb')
p.add_argument('--reuse_pre_train_data', type=bool, default=False, help='use all data instead of ignoring that used during pre-training')
p.add_argument('--transfer_3d', type=bool, default=False, help='set true to load the 3d network instead of the 2d network')
p.add_argument('--smiles_txt_path', type=str, default='dataset/inference_smiles.txt', help='')
p.add_argument('--data_path', type=str)
p.add_argument('--result_file', type=str)
return p.parse_args()
def train_cpi(args,folder,result_file,freeze):
seed_all(args.seed)
device = torch.device("cuda:0" if torch.cuda.is_available() and args.device == 'cuda' else "cpu")
metrics_dict = {'mse':MSE(),
'ci': C_index(),
'rsquared': Rsquared(),
'mae': MAE(),
'pearsonr': PearsonR(),
'ogbg-molhiv': OGBEvaluator(d_name='ogbg-molhiv', metric='rocauc'),
'ogbg-molpcba': OGBEvaluator(d_name='ogbg-molpcba', metric='ap'),
'ogbg-molbace': OGBEvaluator(d_name='ogbg-molbace', metric='rocauc'),
'ogbg-molbbbp': OGBEvaluator(d_name='ogbg-molbbbp', metric='rocauc'),
'ogbg-molclintox': OGBEvaluator(d_name='ogbg-molclintox', metric='rocauc'),
'ogbg-moltoxcast': OGBEvaluator(d_name='ogbg-moltoxcast', metric='rocauc'),
'ogbg-moltox21': OGBEvaluator(d_name='ogbg-moltox21', metric='rocauc'),
'ogbg-mollipo': OGBEvaluator(d_name='ogbg-mollipo', metric='rmse'),
'ogbg-molmuv': OGBEvaluator(d_name='ogbg-molmuv', metric='ap'),
'ogbg-molsider': OGBEvaluator(d_name='ogbg-molsider', metric='rocauc'),
'ogbg-molfreesolv': OGBEvaluator(d_name='ogbg-molfreesolv', metric='rmse'),
'ogbg-molesol': OGBEvaluator(d_name='ogbg-molesol', metric='rmse'),
'pcqm4m': PCQM4MEvaluatorWrapper(),
'conformer_3d_variance': Conformer3DVariance(),
'conformer_2d_variance': Conformer2DVariance(),
'positive_similarity': PositiveSimilarity(),
'positive_similarity_multiple_positives_separate2d': PositiveSimilarityMultiplePositivesSeparate2d(),
'positive_prob': PositiveProb(),
'negative_prob': NegativeProb(),
'negative_similarity': NegativeSimilarity(),
'negative_similarity_multiple_positives_separate2d': NegativeSimilarityMultiplePositivesSeparate2d(),
'contrastive_accuracy': ContrastiveAccuracy(threshold=0.5009),
'true_negative_rate': TrueNegativeRate(threshold=0.5009),
'true_positive_rate': TruePositiveRate(threshold=0.5009),
'mean_predictor_loss': MeanPredictorLoss(globals()[args.loss_func](**args.loss_params)),
'uniformity': Uniformity(t=2),
'alignment': Alignment(alpha=2),
'batch_variance': BatchVariance(),
'dimension_covariance': DimensionCovariance()
}
train_data_path_pro = os.path.join(folder,'trainprocessed.pt')
val_data_path_pro = os.path.join(folder,'valprocessed.pt')
test_data_path_pro = os.path.join(folder,'testprocessed.pt')
train_data = CusDatasetCPI(device=device , processed_fold = train_data_path_pro)
val_data = CusDatasetCPI(device=device , processed_fold = val_data_path_pro)
test_data = CusDatasetCPI(device=device , processed_fold = test_data_path_pro)
model, _,_ = load_model(args, data=train_data, device=device)
depth = round(len([param for param in model.node_gnn.parameters()])*float(freeze))
#freeze some PNA layers
if depth != 0:
i = 0
for param in model.node_gnn.parameters():
if i <= depth:
param.requires_grad = False
i = i+1
else:
pass
print('trainable params in model: ', sum(p.numel() for p in model.parameters() if p.requires_grad), '\n')
print('untrainable params in model: ', sum(p.numel() for p in model.parameters() if not p.requires_grad), '\n')
model.to(device)
train_loader = DataLoader(train_data, batch_size=args.batch_size, collate_fn=dti_graph_collate, shuffle=True)
val_loader = DataLoader(val_data, batch_size=args.batch_size, collate_fn=dti_graph_collate, shuffle=False)
test_loader = DataLoader(test_data, batch_size=args.batch_size, collate_fn=dti_graph_collate, shuffle=False)
metrics = {metric: metrics_dict[metric] for metric in args.metrics}
tensorboard_functions = {function: TENSORBOARD_FUNCTIONS[function] for function in args.tensorboard_functions}
trainer = Trainer(model=model, args=args, metrics=metrics, main_metric=args.main_metric,
main_metric_goal=args.main_metric_goal, optim=globals()[args.optimizer],
loss_func=globals()[args.loss_func](**args.loss_params), device=device,
tensorboard_functions=tensorboard_functions,
scheduler_step_per_batch=args.scheduler_step_per_batch)
val_metrics = trainer.train(train_loader, val_loader, test_loader)
predictions_re = []
labels = []
mean_squared_error = MSE()
ci_index= C_index()
with torch.no_grad():
for i, batch in enumerate(test_loader):
batch = move_to_device(list(batch), device)
loss, predictions, targets = trainer.process_batch(batch)
predictions_re.append(predictions.cpu().detach().numpy())
labels.append(targets.cpu().detach().numpy())
fina_re = np.concatenate(predictions_re)
labels = np.concatenate(labels)
mse_re = mean_squared_error(torch.tensor(fina_re),torch.tensor(labels)).item()
ci_re = ci_index(torch.tensor(fina_re),torch.tensor(labels)).item()
import pandas as pd
result_dir = './{}'.format(freeze)
if not os.path.exists(result_dir):
os.mkdir(result_dir)
pd.DataFrame(fina_re,columns=['predictions']).to_csv(os.path.join(result_dir,'{}{}0result_pre3.csv'.format(time_ex,result_file)))
pd.DataFrame([[mse_re, ci_re]],columns=['mse','ci']).to_csv(os.path.join(result_dir,'{}{}0result_quan3.csv'.format(time_ex,result_file)))
def get_arguments():
args = parse_arguments()
if args.config:
config_dict = yaml.load(args.config, Loader=yaml.FullLoader)
arg_dict = args.__dict__
for key, value in config_dict.items():
if isinstance(value, list):
for v in value:
arg_dict[key].append(v)
else:
arg_dict[key] = value
else:
config_dict = {}
if args.checkpoint:# overwrite args with args from checkpoint except for the args that were contained in the config file
arg_dict = args.__dict__
with open(os.path.join(os.path.dirname(args.checkpoint), 'train_arguments.yaml'), 'r') as arg_file:
checkpoint_dict = yaml.load(arg_file, Loader=yaml.FullLoader)
for key, value in checkpoint_dict.items():
if key not in config_dict.keys():
if isinstance(value, list):
for v in value:
arg_dict[key].append(v)
else:
arg_dict[key] = value
return args
if __name__ == '__main__':
args = get_arguments()
project_name = args.result_file + time_ex
run = wandb.init(project=project_name)
folder = str(args.data_path)
tasks = os.listdir(folder)
for freeze in [0.95]:
for task in tasks:
data_dir = os.path.join(folder,task)
train_cpi(args,folder=data_dir,result_file=task, freeze = freeze)
run.finish()