-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbert_nc_finetune.py
457 lines (396 loc) · 21.4 KB
/
bert_nc_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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import tensorflow as tf
from tensorflow import keras
import json
from preprocessing.process_inputs import get_class_vectors, ALPHABET
from models.model import PARAMS
import numpy as np
from tensorflow.keras.utils import Sequence
from models.bert_utils import get_token_dict, seq2tokens, predict
from models.bert_utils import generate_bert_with_pretrained, generate_bert_with_pretrained_multi_tax, \
get_classes_and_weights_multi_tax
from random import shuffle, sample
from sklearn.model_selection import train_test_split
import os
import argparse
from dataclasses import dataclass, field
from typing import List, Optional
from logging import warning
import pickle
from tensorflow.keras.mixed_precision import experimental as mixed_precision
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard, EarlyStopping
from os.path import splitext
import pandas as pd
from sklearn.metrics import balanced_accuracy_score
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
mirrored_strategy = tf.distribute.MirroredStrategy()
classes = PARAMS['data']['classes'][1]
def load_dataset(filepath):
df = pd.read_csv(filepath, sep="\t")
x = df["x"]
y = df["y"]
y_species = df["tax_id"]
return x, y, y_species
def load_fragments(fragments_dir, shuffle_=True, balance=True, nr_seqs=None):
fragments = []
species_list = []
for class_ in classes:
fragments.append((class_, json.load(open(os.path.join(
fragments_dir, f'{class_}_fragments.json')))))
species_list.append([int(line.strip()) for line in
open(os.path.join(fragments_dir, f'{class_}_species_picked.txt')).readlines()])
nr_seqs_max = min(len(_[1]) for _ in fragments)
if (nr_seqs is None or nr_seqs > nr_seqs_max):
nr_seqs = nr_seqs_max
x = []
y = np.array([])
y_species = np.array([], dtype=np.int)
for index, fragments_i in enumerate(fragments):
class_, class_fragments = fragments_i
if not balance:
x.extend(class_fragments)
y = np.append(y, [class_] * len(class_fragments))
y_species = np.append(y_species, species_list[index])
else:
x_help = list(zip(class_fragments, species_list[index]))
# x.extend(sample(class_fragments, nr_seqs))
x_help = sample(x_help, nr_seqs)
x_help, y_species_help = zip(*x_help)
x.extend(x_help)
y_species = np.append(y_species, y_species_help)
y = np.append(y, [class_] * nr_seqs)
assert len(x) == len(y)
if (shuffle_):
to_shuffle = list(zip(x, y, y_species))
shuffle(to_shuffle)
x, y, y_species = zip(*to_shuffle)
print(f'{len(x)} fragments loaded in total; '
f'balanced={balance}, shuffle_={shuffle_}, nr_seqs={nr_seqs}')
return np.array(x), np.array(y), np.array(y_species)
@dataclass
class FragmentGenerator(Sequence):
x: list
y: list
seq_len: int
max_seq_len: Optional[int] = None
k: int = 3
stride: int = 3
batch_size: int = 32
classes: List = field(default_factory=lambda:
['Viruses', 'Archaea', 'Bacteria', 'Eukaryota'])
seq_len_like: Optional[np.array] = None
window: bool = False
def __post_init__(self):
self.class_vectors = get_class_vectors(self.classes)
self.token_dict = get_token_dict(ALPHABET, k=3)
if (self.max_seq_len is None):
self.max_seq_len = self.seq_len
def __len__(self):
return np.ceil(len(self.x)
/ float(self.batch_size)).astype(np.int)
def __getitem__(self, idx):
batch_fragments = self.x[idx * self.batch_size:
(idx + 1) * self.batch_size]
batch_x = [seq2tokens(seq, self.token_dict, seq_length=self.seq_len,
max_length=self.max_seq_len,
k=self.k, stride=self.stride, window=self.window,
seq_len_like=self.seq_len_like)
for seq in batch_fragments]
if (self.y is not None and len(self.y) != 0):
batch_classes = self.y[idx * self.batch_size:
(idx + 1) * self.batch_size]
batch_y = np.array([self.class_vectors[c] for c in batch_classes])
return ([np.array([_[0] for _ in batch_x]),
np.array([_[1] for _ in batch_x])], [batch_y])
else:
return [np.array([_[0] for _ in batch_x]),
np.array([_[1] for _ in batch_x])]
@dataclass
class FragmentGenerator_multi_tax(Sequence):
def __init__(self, x, y, y_species, weight_classes, classes, seq_len, tax_ranks, max_seq_len=None, k=3, stride=3,
batch_size=32, seq_len_like=None, window=False):
self.x = x
self.y = y
self.y_species = y_species
self.weight_classes = weight_classes
self.classes = classes
self.seq_len = seq_len
self.tax_ranks = tax_ranks
self.max_seq_len = max_seq_len
self.k = k
self.stride = stride
self.batch_size = batch_size
self.seq_len_like = seq_len_like
self.window = window
from utils.tax_entry import TaxidLineage
tlineage = TaxidLineage()
self.tlineage = tlineage
self.class_vectors = dict()
for tax_rank in self.classes:
self.class_vectors.update({tax_rank: get_class_vectors(self.classes[tax_rank])})
self.token_dict = get_token_dict(ALPHABET, k=3)
if (self.max_seq_len is None):
self.max_seq_len = self.seq_len
def get_class_vectors_multi_tax(self, taxid):
vector = []
weight = []
ranks = self.tlineage.get_ranks(taxid, ranks=self.tax_ranks)
# calc vector per tax and append
# if class not in dict use vector of unknown class
for index, class_tax_i in enumerate(ranks):
# try:
vector.append(self.class_vectors[self.tax_ranks[index]].get(ranks[class_tax_i][1],
self.class_vectors[self.tax_ranks[index]][
'unknown']))
# except:
# print(self.tax_ranks[index])
# print(ranks[class_tax_i][1])
# calc sample weight
for index, class_tax_i in enumerate(ranks):
weight.append(self.weight_classes[self.tax_ranks[index]].get(ranks[class_tax_i][1],
self.weight_classes[self.tax_ranks[index]][
'unknown']))
return vector, weight
# def __post_init__(self):
# # from utils.tax_entry import TaxDB
# # self.taxDB = TaxDB(data_dir="/mnt/fass2/projects/fm_read_classification_comparison/taxonomy")
def __len__(self):
return np.ceil(len(self.x)
/ float(self.batch_size)).astype(np.int)
def __getitem__(self, idx):
batch_fragments = self.x[idx * self.batch_size:
(idx + 1) * self.batch_size]
batch_x = [seq2tokens(seq, self.token_dict, seq_length=self.seq_len,
max_length=self.max_seq_len,
k=self.k, stride=self.stride, window=self.window,
seq_len_like=self.seq_len_like)
for seq in batch_fragments]
if (self.y is not None and len(self.y) != 0):
batch_classes = self.y_species[idx * self.batch_size:
(idx + 1) * self.batch_size]
batch_y, weights = zip(*[self.get_class_vectors_multi_tax(taxid) for taxid in batch_classes])
# batch_y, weights = zip(*[self.get_class_vectors_multi_tax(taxid) for taxid in batch_classes])
X = [np.array([_[0] for _ in batch_x]), np.array([_[1] for _ in batch_x])]
y = []
weights_y = []
for index in range(len(self.tax_ranks)):
y.append(np.array([_[index] for _ in batch_y]))
weights_y.append(np.array([_[index] for _ in weights]))
return (X, y, weights_y)
else:
return [np.array([_[0] for _ in batch_x]),
np.array([_[1] for _ in batch_x])]
def get_fine_model(pretrained_model_file):
# with mirrored_strategy.scope():
model_fine = generate_bert_with_pretrained(
pretrained_model_file, len(classes))
model_fine.compile(keras.optimizers.Adam(learning_rate),
loss='categorical_crossentropy',
metrics=['accuracy'])
max_length = model_fine.input_shape[0][1]
return model_fine, max_length
def get_fine_model_multi_tax(pretrained_model_file, num_classes, tax_ranks):
model_fine = generate_bert_with_pretrained_multi_tax(pretrained_model_file, num_classes, tax_ranks)
model_fine.compile(keras.optimizers.Adam(learning_rate),
loss='categorical_crossentropy',
metrics=['accuracy'])
max_length = model_fine.input_shape[0][1]
return model_fine, max_length
def prepare_training_val_weights_for_multitax(train_x, train_y, train_y_species, classes_preset=None, unknown_thr=8000,
gen_test_set=False):
"""
1. find classes of interest
2. split train and val set
3. identify which of the classes now do not match old threshold
4. adapt threshold and recalculate classes, weights, etc
unknown: set to total samples per class - samples in test set
e.g. total 10000 and 2000 in test set --> 8000
"""
if classes is None:
classes_, weight_classes_, species_list_y_ = get_classes_and_weights_multi_tax(train_y_species,
tax_ranks=tax_ranks,
unknown_thr=unknown_thr,
norm_weights=norm_weights)
else:
classes_ = classes_preset
train_x = list(zip(train_x, train_y_species))
train_x, val_x, train_y, val_y = train_test_split(train_x, train_y, test_size=0.05, stratify=train_y)
if gen_test_set:
train_x, test_x, train_y, test_y = train_test_split(train_x, train_y, test_size=0.2, stratify=train_y)
train_x, train_y_species = zip(*train_x)
val_x, val_y_species = zip(*val_x)
train_x, val_x = [np.array(i, dtype=object) for i in [train_x, val_x]]
train_y_species, val_y_species = [np.array(i) for i in [train_y_species, val_y_species]]
classes_, weight_classes_, species_list_y_ = get_classes_and_weights_multi_tax(train_y_species,
classes_preset=classes_,
tax_ranks=tax_ranks,
norm_weights=norm_weights)
if gen_test_set:
test_x, test_y_species = zip(*test_x)
return train_x, train_y, train_y_species, val_x, val_y, val_y_species, classes_, weight_classes_, species_list_y_, test_x, test_y, test_y_species
return train_x, train_y, train_y_species, val_x, val_y, val_y_species, classes_, weight_classes_, species_list_y_
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='fine-tune BERT on pre-generated fragments')
parser.add_argument('pretrained_bert')
parser.add_argument('fragments_dir')
parser.add_argument('--seq_len', help=' ', type=int, default=502)
parser.add_argument('--seq_len_like', default=None,
help='path of pickled class dict of seq lens for '
'generating sampled sequence sizes')
parser.add_argument('--k', help=' ', default=3, type=int)
parser.add_argument('--stride', help=' ', default=3, type=int)
parser.add_argument('--batch_size', help=' ', type=int, default=32)
parser.add_argument('--epochs', help=' ', type=int, default=4)
parser.add_argument('--nr_seqs', help=' ', type=int, default=100_000_000)
parser.add_argument('--learning_rate', help=' ', type=float, default=5e-5)
parser.add_argument('--save_name',
help='custom name for saved finetuned model',
default=None)
parser.add_argument('--store_predictions', help=' ', action='store_true')
parser.add_argument('--store_train_data', help=' ', action='store_true')
parser.add_argument('--roc_auc', help=' ', action='store_true')
parser.add_argument('--multi_tax', help='predict multiple taxonomic ranks with a single model, see also --tax_ranks', action='store_true')
parser.add_argument('--tax_ranks', help='taxonomic ranks to train for',
nargs='+', default=["superkingdom", "phylum"])
parser.add_argument('--only_test_model', help='don\'t train a model but evaluate the performance', action='store_true')
parser.add_argument('--use_defined_train_test_set', help='useful for benchmarking, uses output from preprocessing.make_dataset for training and performance evaluation', action='store_true')
args = parser.parse_args()
tax_ranks = args.tax_ranks
test = args.only_test_model # if you don't want to train the model but evaluate it's performance after previous training
norm_weights = True
learning_rate = args.learning_rate
if (args.seq_len_like is not None):
seq_len_dict = pickle.load(open(args.seq_len_like, 'rb'))
min_nr_seqs = min(map(len, seq_len_dict.values()))
seq_len_like = []
for k in seq_len_dict:
seq_len_like.extend(np.random.choice(seq_len_dict[k], min_nr_seqs)
// args.k)
else:
seq_len_like = None
if not args.use_defined_train_test_set:
# loading training data
if args.multi_tax:
x, y, y_species = load_fragments(args.fragments_dir, balance=False, nr_seqs=args.nr_seqs)
f_train_x, f_train_y, f_train_y_species, f_val_x, f_val_y, f_val_y_species, classes, weight_classes, species_list_y, f_test_x, f_test_y, f_test_y_species = prepare_training_val_weights_for_multitax(
x, y, y_species, unknown_thr=8000, gen_test_set=True)
else:
x, y, y_species = load_fragments(args.fragments_dir, nr_seqs=args.nr_seqs)
f_train_x, f_test_x, f_train_y, f_test_y = train_test_split(
x, y, test_size=0.2, stratify=y)
f_train_x, f_val_x, f_train_y, f_val_y = train_test_split(
f_train_x, f_train_y, test_size=0.05, stratify=f_train_y)
else:
f_test_x, f_test_y, f_test_y_species = load_dataset(os.path.join(args.fragments_dir, "test.tsv"))
f_train_x, f_train_y, f_train_y_species = load_dataset(os.path.join(args.fragments_dir, "train.tsv"))
classes = pickle.load(open(os.path.join(args.fragments_dir, "classes.pkl"), 'rb'))
f_train_x, f_train_y, f_train_y_species, f_val_x, f_val_y, f_val_y_species, classes, weight_classes, species_list_y = prepare_training_val_weights_for_multitax(
f_train_x, f_train_y, f_train_y_species, classes_preset=classes)
if test:
from models.bert_utils import load_bert
model = load_bert(args.pretrained_bert, compile_=True)
max_length = model.input_shape[0][1]
else:
# building model
if args.multi_tax:
num_classes = [len(classes[tax].keys()) for tax in classes]
model, max_length = get_fine_model_multi_tax(args.pretrained_bert, num_classes=num_classes,
tax_ranks=tax_ranks)
else:
model, max_length = get_fine_model(args.pretrained_bert)
if (args.seq_len > max_length):
warning(f'desired seq len ({args.seq_len}) is higher than possible ({max_length})'
f'setting seq len to {max_length}')
args.seq_len = max_length
generator_args = {
'max_seq_len': max_length, 'k': args.k, 'stride': args.stride,
'batch_size': args.batch_size, 'window': True,
'seq_len_like': seq_len_like}
model.summary()
if not test:
if (args.save_name is not None):
save_path = args.save_name
name = args.save_name
else:
save_path = os.path.splitext(args.pretrained_bert)[0]
name = ""
filepath1 = save_path + "_model.best.acc.h5"
filepath2 = save_path + "_model.best.loss.h5"
checkpoint1 = ModelCheckpoint(filepath1, monitor='val_phylum_out_accuracy', verbose=1, save_best_only=True,
save_weights_only=False, mode='max')
checkpoint2 = ModelCheckpoint(filepath2, monitor='val_loss', verbose=1, save_best_only=True,
save_weights_only=False, mode='min')
checkpoint3 = EarlyStopping('val_loss', min_delta=0, patience=2, restore_best_weights=True)
tensorboard_callback = TensorBoard(log_dir="./logs/run_broad_learning" + name, histogram_freq=1,
write_graph=True,
write_images=True, update_freq=100, embeddings_freq=1)
# callbacks_list = [checkpoint1, checkpoint2, checkpoint3]
callbacks_list = [checkpoint1, checkpoint2, checkpoint3, tensorboard_callback]
if (args.store_train_data):
from datetime import datetime
time_str = datetime.now().strftime('%Y%m%d_%H-%M-%S')
for kind, x, y in [('train', f_train_x, f_train_y), ('val', f_val_x, f_val_y),
('test', f_test_x, f_test_y)]:
with open(f'{time_str}_{kind}_data.json', 'w') as f:
json.dump([x, y], f)
print('saved train/test/val data.')
if args.multi_tax:
test_g = FragmentGenerator_multi_tax(f_test_x, f_test_y, f_test_y_species, weight_classes, seq_len=args.seq_len,
tax_ranks=tax_ranks, classes=classes, **generator_args)
else:
test_g = FragmentGenerator(f_test_x, f_test_y, args.seq_len, **generator_args)
if not test:
try:
if args.multi_tax:
model.fit(
FragmentGenerator_multi_tax(f_train_x, f_train_y, f_train_y_species, weight_classes,
seq_len=args.seq_len,
tax_ranks=tax_ranks, classes=classes, **generator_args),
callbacks=callbacks_list, epochs=args.epochs,
validation_data=FragmentGenerator_multi_tax(f_val_x, f_val_y, f_val_y_species, weight_classes,
seq_len=args.seq_len, tax_ranks=tax_ranks,
classes=classes,
**generator_args), verbose=1)
else:
model.fit(FragmentGenerator(f_train_x, f_train_y, args.seq_len, **generator_args),
callbacks=callbacks_list, epochs=args.epochs,
validation_data=FragmentGenerator(f_val_x, f_val_y, args.seq_len, **generator_args))
except (KeyboardInterrupt):
print("training interrupted, current status will be saved and tested, press ctrl+c to cancel this")
file_suffix = '_aborted.hdf5'
model.save(splitext(args.pretrained_bert)[0] + '_aborted.h5')
print('testing...')
result = model.evaluate(test_g)
print("test results:", *zip(model.metrics_names, result))
exit()
if (args.save_name is not None):
save_path = args.save_name + '.h5'
else:
save_path = os.path.splitext(args.pretrained_bert)[0] + '_finetuned.h5'
model.save(save_path)
print('testing...')
if (args.store_predictions or args.roc_auc):
predicted = predict(
model, test_g,
args.roc_auc, classes, return_data=args.store_predictions, calc_metrics=False)
y_true, y_pred = predicted["data"]
for i in range(len(y_pred)):
acc = balanced_accuracy_score(np.argmax(y_true[i], axis=1), np.argmax(y_pred[i], axis=1))
print(f"{test_g.tax_ranks[i]} acc:", acc)
print(pd.crosstab(np.argmax(np.array(y_true[i]), axis=1), np.argmax(np.array(y_pred[i]), axis=1),
rownames=['True'], colnames=['Predicted'], margins=True))
result = predicted['metrics']
metrics_names = predicted['metrics_names']
if (args.store_predictions):
import pickle
if test:
pickle.dump(predicted, open("/home/go96bix/projects/dna_class/resources/" + "big_trainingset_all_normed"
+ '_predictions.pkl', 'wb'))
else:
pickle.dump(predicted, open(os.path.splitext(save_path)[0]
+ '_predictions.pkl', 'wb'))
else:
result = model.evaluate(test_g)
metrics_names = model.metrics_names
print("test results:", *zip(metrics_names, result))