-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRunModel.py
303 lines (252 loc) · 12.1 KB
/
RunModel.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
# -*- coding:utf-8 -*-
'''
Author: MrZQAQ
Date: 2022-03-29 14:06
LastEditTime: 2023-03-01 22:24
LastEditors: MrZQAQ
Description: turely model execute file
FilePath: /MCANet/RunModel.py
'''
import os
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from prefetch_generator import BackgroundGenerator
from sklearn.metrics import (accuracy_score, auc, precision_recall_curve,
precision_score, recall_score, roc_auc_score)
from torch.utils.data import DataLoader
from tqdm import tqdm
from config import hyperparameter
from model import MCANet
from utils.DataPrepare import get_kfold_data, shuffle_dataset
from utils.DataSetsFunction import CustomDataSet, collate_fn
from utils.EarlyStoping import EarlyStopping
from LossFunction import CELoss, PolyLoss
from utils.TestModel import test_model
from utils.ShowResult import show_result
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def run_model(SEED, DATASET, MODEL, K_Fold, LOSS):
'''set random seed'''
random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
'''init hyperparameters'''
hp = hyperparameter()
'''load dataset from text file'''
assert DATASET in ["DrugBank", "KIBA", "Davis", "Enzyme", "GPCRs", "ion_channel"]
print("Train in " + DATASET)
print("load data")
dir_input = ('./DataSets/{}.txt'.format(DATASET))
with open(dir_input, "r") as f:
data_list = f.read().strip().split('\n')
print("load finished")
'''set loss function weight'''
if DATASET == "Davis":
weight_loss = torch.FloatTensor([0.3, 0.7]).to(DEVICE)
elif DATASET == "KIBA":
weight_loss = torch.FloatTensor([0.2, 0.8]).to(DEVICE)
else:
weight_loss = None
'''shuffle data'''
print("data shuffle")
data_list = shuffle_dataset(data_list, SEED)
'''split dataset to train&validation set and test set'''
split_pos = len(data_list) - int(len(data_list) * 0.2)
train_data_list = data_list[0:split_pos]
test_data_list = data_list[split_pos:-1]
print('Number of Train&Val set: {}'.format(len(train_data_list)))
print('Number of Test set: {}'.format(len(test_data_list)))
'''metrics'''
Accuracy_List_stable, AUC_List_stable, AUPR_List_stable, Recall_List_stable, Precision_List_stable = [], [], [], [], []
for i_fold in range(K_Fold):
print('*' * 25, 'No.', i_fold + 1, '-fold', '*' * 25)
train_dataset, valid_dataset = get_kfold_data(
i_fold, train_data_list, k=K_Fold)
train_dataset = CustomDataSet(train_dataset)
valid_dataset = CustomDataSet(valid_dataset)
test_dataset = CustomDataSet(test_data_list)
train_size = len(train_dataset)
train_dataset_loader = DataLoader(train_dataset, batch_size=hp.Batch_size, shuffle=True, num_workers=0,
collate_fn=collate_fn, drop_last=True)
valid_dataset_loader = DataLoader(valid_dataset, batch_size=hp.Batch_size, shuffle=False, num_workers=0,
collate_fn=collate_fn, drop_last=True)
test_dataset_loader = DataLoader(test_dataset, batch_size=hp.Batch_size, shuffle=False, num_workers=0,
collate_fn=collate_fn, drop_last=True)
""" create model"""
model = MODEL(hp).to(DEVICE)
"""Initialize weights"""
weight_p, bias_p = [], []
for p in model.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
for name, p in model.named_parameters():
if 'bias' in name:
bias_p += [p]
else:
weight_p += [p]
"""create optimizer and scheduler"""
optimizer = optim.AdamW(
[{'params': weight_p, 'weight_decay': hp.weight_decay}, {'params': bias_p, 'weight_decay': 0}], lr=hp.Learning_rate)
scheduler = optim.lr_scheduler.CyclicLR(optimizer, base_lr=hp.Learning_rate, max_lr=hp.Learning_rate*10, cycle_momentum=False,
step_size_up=train_size // hp.Batch_size)
if LOSS == 'PolyLoss':
Loss = PolyLoss(weight_loss=weight_loss,
DEVICE=DEVICE, epsilon=hp.loss_epsilon)
else:
Loss = CELoss(weight_CE=weight_loss, DEVICE=DEVICE)
"""Output files"""
save_path = "./" + DATASET + "/{}".format(i_fold+1)
if not os.path.exists(save_path):
os.makedirs(save_path)
file_results = save_path + '/' + 'The_results_of_whole_dataset.txt'
early_stopping = EarlyStopping(
savepath=save_path, patience=hp.Patience, verbose=True, delta=0)
"""Start training."""
print('Training...')
for epoch in range(1, hp.Epoch + 1):
if early_stopping.early_stop == True:
break
train_pbar = tqdm(
enumerate(BackgroundGenerator(train_dataset_loader)),
total=len(train_dataset_loader))
"""train"""
train_losses_in_epoch = []
model.train()
for train_i, train_data in train_pbar:
train_compounds, train_proteins, train_labels = train_data
train_compounds = train_compounds.to(DEVICE)
train_proteins = train_proteins.to(DEVICE)
train_labels = train_labels.to(DEVICE)
optimizer.zero_grad()
predicted_interaction = model(train_compounds, train_proteins)
train_loss = Loss(predicted_interaction, train_labels)
train_losses_in_epoch.append(train_loss.item())
train_loss.backward()
optimizer.step()
scheduler.step()
train_loss_a_epoch = np.average(
train_losses_in_epoch) # 一次epoch的平均训练loss
"""valid"""
valid_pbar = tqdm(
enumerate(BackgroundGenerator(valid_dataset_loader)),
total=len(valid_dataset_loader))
valid_losses_in_epoch = []
model.eval()
Y, P, S = [], [], []
with torch.no_grad():
for valid_i, valid_data in valid_pbar:
valid_compounds, valid_proteins, valid_labels = valid_data
valid_compounds = valid_compounds.to(DEVICE)
valid_proteins = valid_proteins.to(DEVICE)
valid_labels = valid_labels.to(DEVICE)
valid_scores = model(valid_compounds, valid_proteins)
valid_loss = Loss(valid_scores, valid_labels)
valid_losses_in_epoch.append(valid_loss.item())
valid_labels = valid_labels.to('cpu').data.numpy()
valid_scores = F.softmax(
valid_scores, 1).to('cpu').data.numpy()
valid_predictions = np.argmax(valid_scores, axis=1)
valid_scores = valid_scores[:, 1]
Y.extend(valid_labels)
P.extend(valid_predictions)
S.extend(valid_scores)
Precision_dev = precision_score(Y, P)
Reacll_dev = recall_score(Y, P)
Accuracy_dev = accuracy_score(Y, P)
AUC_dev = roc_auc_score(Y, S)
tpr, fpr, _ = precision_recall_curve(Y, S)
PRC_dev = auc(fpr, tpr)
valid_loss_a_epoch = np.average(valid_losses_in_epoch)
epoch_len = len(str(hp.Epoch))
print_msg = (f'[{epoch:>{epoch_len}}/{hp.Epoch:>{epoch_len}}] ' +
f'train_loss: {train_loss_a_epoch:.5f} ' +
f'valid_loss: {valid_loss_a_epoch:.5f} ' +
f'valid_AUC: {AUC_dev:.5f} ' +
f'valid_PRC: {PRC_dev:.5f} ' +
f'valid_Accuracy: {Accuracy_dev:.5f} ' +
f'valid_Precision: {Precision_dev:.5f} ' +
f'valid_Reacll: {Reacll_dev:.5f} ')
print(print_msg)
'''save checkpoint and make decision when early stop'''
early_stopping(Accuracy_dev, model, epoch)
'''load best checkpoint'''
model.load_state_dict(torch.load(
early_stopping.savepath + '/valid_best_checkpoint.pth'))
'''test model'''
trainset_test_stable_results, _, _, _, _, _ = test_model(
model, train_dataset_loader, save_path, DATASET, Loss, DEVICE, dataset_class="Train", FOLD_NUM=1)
validset_test_stable_results, _, _, _, _, _ = test_model(
model, valid_dataset_loader, save_path, DATASET, Loss, DEVICE, dataset_class="Valid", FOLD_NUM=1)
testset_test_stable_results, Accuracy_test, Precision_test, Recall_test, AUC_test, PRC_test = test_model(
model, test_dataset_loader, save_path, DATASET, Loss, DEVICE, dataset_class="Test", FOLD_NUM=1)
AUC_List_stable.append(AUC_test)
Accuracy_List_stable.append(Accuracy_test)
AUPR_List_stable.append(PRC_test)
Recall_List_stable.append(Recall_test)
Precision_List_stable.append(Precision_test)
with open(save_path + '/' + "The_results_of_whole_dataset.txt", 'a') as f:
f.write("Test the stable model" + '\n')
f.write(trainset_test_stable_results + '\n')
f.write(validset_test_stable_results + '\n')
f.write(testset_test_stable_results + '\n')
show_result(DATASET, Accuracy_List_stable, Precision_List_stable,
Recall_List_stable, AUC_List_stable, AUPR_List_stable, Ensemble=False)
def ensemble_run_model(SEED, DATASET, K_Fold):
'''set random seed'''
random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
'''init hyperparameters'''
hp = hyperparameter()
'''load dataset from text file'''
assert DATASET in ["DrugBank", "KIBA", "Davis"]
print("Train in " + DATASET)
print("load data")
dir_input = ('./DataSets/{}.txt'.format(DATASET))
with open(dir_input, "r") as f:
data_list = f.read().strip().split('\n')
print("load finished")
'''set loss function weight'''
if DATASET == "Davis":
weight_loss = torch.FloatTensor([0.3, 0.7]).to(DEVICE)
elif DATASET == "KIBA":
weight_loss = torch.FloatTensor([0.2, 0.8]).to(DEVICE)
else:
weight_loss = None
'''shuffle data'''
print("data shuffle")
data_list = shuffle_dataset(data_list, SEED)
'''split dataset to train&validation set and test set'''
split_pos = len(data_list) - int(len(data_list) * 0.2)
test_data_list = data_list[split_pos:-1]
print('Number of Test set: {}'.format(len(test_data_list)))
save_path = f"./{DATASET}/ensemble"
if not os.path.exists(save_path):
os.makedirs(save_path)
test_dataset = CustomDataSet(test_data_list)
test_dataset_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=0,
collate_fn=collate_fn, drop_last=True)
model = []
for i in range(K_Fold):
model.append(MCANet(hp).to(DEVICE))
'''MCANet K-Fold train process is necessary'''
try:
model[i].load_state_dict(torch.load(
f'./{DATASET}/{i+1}' + '/valid_best_checkpoint.pth', map_location=torch.device(DEVICE)))
except FileNotFoundError as e:
print('-'* 25 + 'ERROR' + '-'*25)
error_msg = 'Load pretrained model error: \n' + \
str(e) + \
'\n' + 'MCANet K-Fold train process is necessary'
print(error_msg)
print('-'* 55)
exit(1)
Loss = PolyLoss(weight_loss=weight_loss,
DEVICE=DEVICE, epsilon=hp.loss_epsilon)
testdataset_results, Accuracy_test, Precision_test, Recall_test, AUC_test, PRC_test = test_model(
model, test_dataset_loader, save_path, DATASET, Loss, DEVICE, dataset_class="Test", save=True, FOLD_NUM=K_Fold)
show_result(DATASET, Accuracy_test, Precision_test,
Recall_test, AUC_test, PRC_test, Ensemble=True)