-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinetune.py
187 lines (155 loc) · 7.63 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
#!/usr/bin/env python3
import random
import os
import sys
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dropout, Activation, Reshape, Dense
from tensorflow.keras.models import Model
import tensorflow_addons as tfa
import json
from custom_layers import CombineConcat, Edge2Node, BilinearFusion
from utils import IMAGE_SIZE, scale_hic, normalise_graphs, get_split_dataset
from metrics import compute_auc
def ds_generator(images, features, graphs, flatten_y):
"""
Returns tuple of (inputs,outputs) where
inputs = (inp1,inp2,inp2)
outputs = (out1,out2)
"""
def g():
for i in range(len(images)):
inputs = (images[i], features[i], graphs[i])
outputs = (flatten_y[i], flatten_y[i])
yield inputs, outputs
return g
def finetune_run(chroms, run_id, seed, dataset_name, epoch=50):
dataset_dir = os.path.join('dataset', dataset_name)
print('#' * 10 + ' Fine-tuning ' + '#' * 10)
# seed = hash(run_id)
train_images, train_graphs, train_features, train_y, val_images, val_graphs, val_features, val_y, test_images, \
test_graphs, test_features, test_y = get_split_dataset(dataset_dir, IMAGE_SIZE, seed, chroms)
graph_upper_bound = np.quantile(train_graphs, 0.996)
train_graphs = normalise_graphs(scale_hic(train_graphs, graph_upper_bound))
val_graphs = normalise_graphs(scale_hic(val_graphs, graph_upper_bound))
test_graphs = normalise_graphs(scale_hic(test_graphs, graph_upper_bound))
FEATURE_DIM = train_features.shape[2]
def crop_and_mutual_concat(input_tensor, graph_size, image_size, feature_num):
t = Reshape((graph_size, feature_num, 1))(input_tensor)
t1 = tf.keras.layers.Cropping2D(cropping=((0, image_size), (0, 0)))(t)
t1 = Reshape((image_size, feature_num))(t1)
t2 = tf.keras.layers.Cropping2D(cropping=((image_size, 0), (0, 0)))(t)
t2 = Reshape((image_size, feature_num))(t2)
t = CombineConcat(image_size)([t1, t2])
return t
Complete_METRICS = [
tf.keras.metrics.BinaryAccuracy(name='binary_accuracy', threshold=0.5),
tf.keras.metrics.AUC(curve="ROC", name='ROC_AUC'),
tf.keras.metrics.AUC(curve="PR", name='PR_AUC')
]
complete_learning_rate = 0.0001
bs = 8
if len(train_images) % bs == 0:
steps_per_epoch = int(len(train_images) / bs)
else:
steps_per_epoch = int(len(train_images) / bs) + 1
# Data preparation (convert to tensors)
# train_images_tensor = tf.convert_to_tensor(train_images, dtype=tf.float32)
# train_features_tensor = tf.convert_to_tensor(train_features, dtype=tf.float32)
# train_graphs_tensor = tf.convert_to_tensor(train_graphs, dtype=tf.float32)
#
# val_images_tensor = tf.convert_to_tensor(val_images, dtype=tf.float32)
# val_features_tensor = tf.convert_to_tensor(val_features, dtype=tf.float32)
# val_graphs_tensor = tf.convert_to_tensor(val_graphs, dtype=tf.float32)
#
# train_x_tensors = [train_images_tensor, train_features_tensor, train_graphs_tensor]
# val_x_tensors = [val_images_tensor, val_features_tensor, val_graphs_tensor]
flatten_train_y = train_y.reshape((-1, IMAGE_SIZE * IMAGE_SIZE))[..., np.newaxis]
flatten_val_y = val_y.reshape((-1, IMAGE_SIZE * IMAGE_SIZE))[..., np.newaxis]
train_ds = tf.data.Dataset.from_generator(
ds_generator(train_images, train_features, train_graphs, flatten_train_y),
output_signature=
(
(
tf.TensorSpec(shape=train_images.shape[1:], dtype=tf.float32),
tf.TensorSpec(shape=train_features.shape[1:], dtype=tf.float32),
tf.TensorSpec(shape=train_graphs.shape[1:], dtype=tf.float32)
),
(
tf.TensorSpec(shape=flatten_train_y.shape[1:], dtype=tf.int64),
tf.TensorSpec(shape=flatten_train_y.shape[1:], dtype=tf.int64)
)
)
)
train_ds = train_ds.batch(bs).repeat(epoch).prefetch(tf.data.AUTOTUNE)
val_x_ds = tf.data.Dataset.from_tensor_slices((val_images, val_features, val_graphs))
val_y_ds = tf.data.Dataset.from_tensor_slices((flatten_val_y, flatten_val_y))
val_ds = tf.data.Dataset.zip((val_x_ds, val_y_ds)).batch(bs)
# Batch size setup
GNN = tf.keras.models.load_model(
'models/{}_GNN'.format(run_id)
)
GNN = Model(inputs=GNN.inputs, outputs=GNN.get_layer('gnn_embedding').output, name='GNN')
CNN = tf.keras.models.load_model(
'models/{}_CNN'.format(run_id)
)
CNN = Model(inputs=CNN.inputs, outputs=CNN.get_layer('cnn_embedding').output, name='CNN')
I = Input(CNN.inputs[0].get_shape()[1:])
F = Input(GNN.inputs[0].get_shape()[1:])
A = Input(GNN.inputs[1].get_shape()[1:])
combined_decoded = BilinearFusion()([CNN([I]), GNN([F, A])])
combined_decoded = Dropout(0.3)(combined_decoded)
combined_decoded = Dense(512, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.0001))(
combined_decoded)
combined_decoded = Dropout(0.3)(combined_decoded)
combined_decoded = Dense(256, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.0001))(
combined_decoded)
combined_decoded = Dropout(0.3)(combined_decoded)
combined_decoded = Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.0001))(
combined_decoded)
combined_decoded = Dropout(0.3)(combined_decoded)
combined_decoded = Dense(16, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.0001))(
combined_decoded)
combined_decoded = Dropout(0.3)(combined_decoded)
combined_decoded = Dense(1, name='logits_flattened', kernel_regularizer=tf.keras.regularizers.l2(0.0001))(
combined_decoded)
flattened_decoded = combined_decoded
sig_flattened = Activation('sigmoid', name='sigmoid_flattened')(flattened_decoded)
model = Model(inputs=[I, F, A], outputs=[flattened_decoded, sig_flattened])
model.compile(
loss={
'sigmoid_flattened': tfa.losses.SigmoidFocalCrossEntropy(from_logits=False, alpha=0.5, gamma=1.2,
reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE)
},
loss_weights={'sigmoid_flattened': IMAGE_SIZE * IMAGE_SIZE},
optimizer=tf.keras.optimizers.Adam(learning_rate=complete_learning_rate),
metrics={
'sigmoid_flattened': Complete_METRICS
}
)
inputs = train_ds
history = model.fit(
inputs,
epochs=epoch,
validation_data=val_ds,
steps_per_epoch=steps_per_epoch,
callbacks=[
tf.keras.callbacks.EarlyStopping(
monitor='val_sigmoid_flattened_' + 'PR_AUC', # use validation AUC of precision-recall for stopping
min_delta=0.0001, patience=5,
verbose=1, mode='max')
],
verbose=2
)
train_y_pred = np.asarray(model.predict(train_ds, steps=steps_per_epoch)[1])
val_y_pred = np.asarray(model.predict(val_ds)[1])
test_y_pred = np.asarray(model.predict([test_images, test_features, test_graphs])[1])
train_auc, train_ap = compute_auc(train_y_pred, train_y.astype('bool'))
val_auc, val_ap = compute_auc(val_y_pred, val_y.astype('bool'))
test_auc, test_ap = compute_auc(test_y_pred, test_y.astype('bool'))
print('=' * 30)
print('*******Finetune**********')
print('Train AUC is {}. Train AP is {}.'.format(train_auc, train_ap))
print('Validation AUC is {}. Validation AP is {}.'.format(val_auc, val_ap))
print('Test AUC is {}. Test AP is {}.'.format(test_auc, test_ap))
model.save(os.path.join('models', '{}_Finetune'.format(run_id)))