forked from zhuhr213/HDRNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
244 lines (194 loc) · 10.4 KB
/
Copy pathmain.py
File metadata and controls
244 lines (194 loc) · 10.4 KB
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
import os
import random
import argparse
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import OneCycleLR
from utils.HDRNet import HDRNet
from utils.gen_bert_embedding import circRNABert
import torch.utils.data
from transformers import BertModel, BertTokenizer
from utils.train_loop import train, validate
from utils.utils import read_csv, myDataset, GradualWarmupScheduler, param_num, split_dataset, seq2kmer
def fix_seed(seed):
"""
Seed all necessary random number generators.
"""
if seed is None:
seed = random.randint(1, 10000)
torch.set_num_threads(1) # Suggested for issues with deadlocks, etc.
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = True
# torch.backends.cudnn.enabled = True
# print("[Info] cudnn.deterministic set to True. CUDNN-optimized code may be slow.")
def main(args):
try:
from termcolor import cprint
except ImportError:
cprint = None
try:
from pycrayon import CrayonClient
except ImportError:
CrayonClient = None
def log_print(text, color=None, on_color=None, attrs=None):
if cprint is not None:
cprint(text, color=color, on_color=on_color, attrs=attrs)
else:
print(text)
fix_seed(args.seed) # fix seed
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
max_length = 101
file_name = args.data_file
data_path = args.data_path
if args.train:
sequences, structs, label = read_csv(os.path.join(data_path, file_name+'.tsv'))
bert_model_path = args.BERT_model_path
tokenizer = BertTokenizer.from_pretrained(bert_model_path, do_lower_case=False)
model = BertModel.from_pretrained(bert_model_path)
model = model.to(device)
# model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])
model = model.eval()
bert_embedding = circRNABert(list(sequences), model, tokenizer, device, 3) # (N, 99, 768)
bert_embedding = bert_embedding.transpose([0, 2, 1]) # (N, 768, 99)
structure = np.zeros((len(structs), 1, max_length)) # (N, 1, 101)
for i in range(len(structs)):
struct = structs[i].split(',')
ti = [float(t) for t in struct]
ti = np.array(ti).reshape(1, -1)
structure[i] = np.concatenate([ti], axis=0)
[train_emb, train_struc, train_label], [test_emb, test_struc, test_label] = \
split_dataset(bert_embedding, structure, label) # , test_size=0.2, shuffle=True, stratify=label)
train_set = myDataset(train_emb, train_struc, train_label)
test_set = myDataset(test_emb, test_struc, test_label)
train_loader = DataLoader(train_set, batch_size=32, shuffle=True)
test_loader = DataLoader(test_set, batch_size=32 * 8, shuffle=False)
model = HDRNet().to(device)
# model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])
criterion = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(2))
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999), weight_decay=1e-6)
scheduler = GradualWarmupScheduler(
optimizer, multiplier=8, total_epoch=float(200), after_scheduler=None)
best_auc = 0
best_acc = 0
best_epoch = 0
model_save_path = args.model_save_path
if not os.path.exists(model_save_path):
os.makedirs(model_save_path)
early_stopping = args.early_stopping
param_num(model)
for epoch in range(1, 200):
t_met = train(model, device, train_loader, criterion, optimizer, batch_size=32)
v_met, _, _ = validate(model, device, test_loader, criterion)
scheduler.step()
lr = scheduler.get_lr()[0]
color_best = 'green'
if best_auc < v_met.auc:
best_auc = v_met.auc
best_acc = v_met.acc
best_epoch = epoch
color_best = 'red'
path_name = os.path.join(model_save_path, file_name+'.pth')
torch.save(model.state_dict(), path_name)
if epoch - best_epoch > early_stopping:
print("Early stop at %d, %s " % (epoch, 'HDRNet'))
break
line = '{} \t Train Epoch: {} avg.loss: {:.4f} Acc: {:.2f}%, AUC: {:.4f} lr: {:.6f}'.format(
file_name, epoch, t_met.other[0], t_met.acc, t_met.auc, lr)
log_print(line, color='green', attrs=['bold'])
line = '{} \t Test Epoch: {} avg.loss: {:.4f} Acc: {:.2f}%, AUC: {:.4f} ({:.4f}) {}'.format(
file_name, epoch, v_met.other[0], v_met.acc, v_met.auc, best_auc, best_epoch)
log_print(line, color=color_best, attrs=['bold'])
print("{} auc: {:.4f} acc: {:.4f}".format(file_name, best_auc, best_acc))
if args.validate: # validate only. WARNING: PLEASE FIX SEED BEFORE VALIDATION.
sequences, structs, label = read_csv(os.path.join(data_path, file_name+'.tsv'))
[train_seq, train_struc, train_label], [test_seq, test_struc, test_label] = \
split_dataset(sequences, structs, label)
bert_model_path = args.BERT_model_path
tokenizer = BertTokenizer.from_pretrained(bert_model_path, do_lower_case=False)
model = BertModel.from_pretrained(bert_model_path)
model = model.to(device)
# model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])
model = model.eval()
bert_embedding = circRNABert(list(test_seq), model, tokenizer, device, 3) # (N, 99, 768)
test_emb = bert_embedding.transpose([0, 2, 1]) # (N, 768, 99)
structure = np.zeros((len(test_struc), 1, max_length)) # (N, 1, 101)
for i in range(len(test_struc)):
struct = test_struc[i].split(',')
ti = [float(t) for t in struct]
ti = np.array(ti).reshape(1, -1)
structure[i] = np.concatenate([ti], axis=0)
test_set = myDataset(test_emb, structure, test_label)
test_loader = DataLoader(test_set, batch_size=32 * 8, shuffle=False)
model = HDRNet().to(device)
model_file = os.path.join(args.model_save_path, file_name+'.pth')
if not os.path.exists(model_file):
print('Model file does not exitsts! Please train first and save the model')
exit()
model.load_state_dict(torch.load(model_file))
criterion = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(2))
met, y_all, p_all = validate(model, device, test_loader, criterion)
best_auc = met.auc
best_acc = met.acc
print("{} auc: {:.4f} acc: {:.4f}".format(file_name, best_auc, best_acc))
if args.dynamic_validate: # perform dynamic prediction between K562 cell and HepG2 cell
# cell_list = ['K562', 'HepG2']
if file_name.endswith('K562'):
model_file = file_name.replace('K562', 'HepG2')
elif file_name.endswith('HepG2'):
model_file = file_name.replace('HepG2', 'K562')
else:
print("Dynamic prediction only performs on K562 cells and HepG2 cells!")
exit()
sequences, structs, label = read_csv(os.path.join(data_path, file_name+'.tsv'))
[train_seq, train_struc, train_label], [test_seq, test_struc, test_label] = \
split_dataset(sequences, structs, label)
bert_model_path = args.BERT_model_path
tokenizer = BertTokenizer.from_pretrained(bert_model_path, do_lower_case=False)
model = BertModel.from_pretrained(bert_model_path)
model = model.to(device)
# model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3])
model = model.eval()
bert_embedding = circRNABert(list(test_seq), model, tokenizer, device, 3) # (N, 99, 768)
test_emb = bert_embedding.transpose([0, 2, 1]) # (N, 768, 99)
structure = np.zeros((len(test_struc), 1, max_length)) # (N, 1, 101)
for i in range(len(test_struc)):
struct = test_struc[i].split(',')
ti = [float(t) for t in struct]
ti = np.array(ti).reshape(1, -1)
structure[i] = np.concatenate([ti], axis=0)
test_set = myDataset(test_emb, structure, test_label)
test_loader = DataLoader(test_set, batch_size=32 * 8, shuffle=False)
model = HDRNet().to(device)
# model_path = args.model_save_path
model_path = os.path.join(args.model_save_path, model_file+'.pth')
if not os.path.exists(model_path):
print('The dynamic predition model {} does not exist! Please train first!'.format(model_file))
exit()
model = model.load_state_dict(torch.load(model_file))
criterion = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(2))
print('Using {} model to predict {} cell'.format(model_file, file_name))
met, y_all, p_all = validate(model, device, test_loader, criterion)
best_auc = met.auc
best_acc = met.acc
print("{} auc: {:.4f} acc: {:.4f}".format(file_name, best_auc, best_acc))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Welcome to HDRNet!')
parser.add_argument('--data_file', default='TIA1_Hela', type=str, help='RBP to train or validate')
parser.add_argument('--data_path', default='./dataset', type=str, help='The data path')
parser.add_argument('--BERT_model_path', default='./BERT_Model', type=str, help='BERT model path, in case you have another BERT')
parser.add_argument('--model_save_path', default='./results/model', type=str, help='Save the trained model for dynamic prediction')
parser.add_argument('--train', default=False, action='store_true')
parser.add_argument('--validate', default=False, action='store_true')
parser.add_argument('--dynamic_validate', default=False, action='store_true')
parser.add_argument('--seed', default=1024, type=int, help='The random seed')
parser.add_argument('--early_stopping', type=int, default=20)
args = parser.parse_args()
main(args)