forked from bm2-lab/DeepCRISPR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepcrispr.py
244 lines (202 loc) · 10.2 KB
/
deepcrispr.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
import tensorflow as tf
import sonnet as snt
from tensorflow.contrib import slim
def create_init_op():
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
return init_op
def load_ckpt(sess, model_dir, variables_to_restore=None):
ckpt = tf.train.get_checkpoint_state(model_dir)
model_path = ckpt.model_checkpoint_path
if variables_to_restore is None:
variables_to_restore = slim.get_variables_to_restore()
restore_op, restore_fd = slim.assign_from_checkpoint(
model_path, variables_to_restore)
sess.run(restore_op, feed_dict=restore_fd)
print(f'{model_path} loaded')
def build_ontar_model(inputs_sg, scope='ontar'):
with tf.variable_scope(scope):
channel_size = [8, 32, 64, 64, 256, 256]
betas = [None] + [tf.Variable(0.0 * tf.ones(channel_size[i]), name=f'beta_{i}') for i in range(1, len(channel_size))]
e1 = snt.Conv2D(channel_size[1], kernel_shape=[1, 3], name='e_1')
ebn1u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_1u')
e2 = snt.Conv2D(channel_size[2], kernel_shape=[1, 3], stride=2, name='e_2')
ebn2u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_2u')
e3 = snt.Conv2D(channel_size[3], kernel_shape=[1, 3], name='e_3')
ebn3u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_3u')
e4 = snt.Conv2D(channel_size[4], kernel_shape=[1, 3], stride=2, name='e_4')
ebn4u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_4u')
e5 = snt.Conv2D(channel_size[5], kernel_shape=[1, 3], name='e_5')
ebn5u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_5u')
encoder = [None, e1, e2, e3, e4, e5]
encoder_bn_u = [None, ebn1u, ebn2u, ebn3u, ebn4u, ebn5u]
hu0 = inputs_sg
u_lst = [hu0]
hu_lst = [hu0]
for i in range(1, len(channel_size) - 1):
hu_pre = hu_lst[i - 1]
pre_u = encoder[i](hu_pre)
u = encoder_bn_u[i](pre_u, False, test_local_stats=False)
hu = tf.nn.relu(u + betas[i])
u_lst.append(u)
hu_lst.append(hu)
hu_m1 = hu_lst[-1]
pre_u_last = encoder[-1](hu_m1)
u_last = encoder_bn_u[-1](pre_u_last, False, test_local_stats=False)
u_last = u_last + betas[-1]
hu_last = tf.nn.relu(u_last)
u_lst.append(u_last)
hu_lst.append(hu_last)
# classifier
cls_channel_size = [512, 512, 1024, 2]
e6 = snt.Conv2D(cls_channel_size[0], kernel_shape=[1, 3], stride=2, name='e_6')
ebn6l = snt.BatchNorm(decay_rate=0.99, name='ebn_6l')
e7 = snt.Conv2D(cls_channel_size[1], kernel_shape=[1, 3], name='e_7')
ebn7l = snt.BatchNorm(decay_rate=0.99, name='ebn_7l')
e8 = snt.Conv2D(cls_channel_size[2], kernel_shape=[1, 3], padding='VALID', name='e_8')
ebn8l = snt.BatchNorm(decay_rate=0.99, name='ebn_8l')
e9 = snt.Conv2D(cls_channel_size[3], kernel_shape=[1, 1], name='e_9')
cls_layers = [None, e6, e7, e8, e9]
cls_bn_layers = [None, ebn6l, ebn7l, ebn8l]
hl0 = hu_last
l_lst = [hl0]
hl_lst = [hl0]
for i in range(1, len(cls_channel_size)):
hl_pre = hl_lst[i - 1]
pre_l = cls_layers[i](hl_pre)
l = cls_bn_layers[i](pre_l, False, test_local_stats=False)
hl = tf.nn.relu(l)
l_lst.append(l)
hl_lst.append(hl)
hl_m1 = hl_lst[-1]
l_last = cls_layers[-1](hl_m1)
hl_last = tf.nn.softmax(l_last)
l_lst.append(l_last)
hl_lst.append(hl_last)
sig_l = tf.squeeze(hl_last, axis=[1, 2])[:, 1]
return sig_l
def build_offtar_model(inputs_sg, inputs_ot, scope='offtar'):
with tf.variable_scope(scope):
channel_size = [8, 32, 64, 64, 256, 256]
with tf.variable_scope('sg'):
betas_sg = [None] + [tf.Variable(0.0 * tf.ones(channel_size[i]), name=f'beta_{i}') for i in
range(1, len(channel_size))]
e1 = snt.Conv2D(channel_size[1], kernel_shape=[1, 3], name='e_1')
ebn1u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_1u')
e2 = snt.Conv2D(channel_size[2], kernel_shape=[1, 3], stride=2, name='e_2')
ebn2u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_2u')
e3 = snt.Conv2D(channel_size[3], kernel_shape=[1, 3], name='e_3')
ebn3u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_3u')
e4 = snt.Conv2D(channel_size[4], kernel_shape=[1, 3], stride=2, name='e_4')
ebn4u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_4u')
e5 = snt.Conv2D(channel_size[5], kernel_shape=[1, 3], name='e_5')
ebn5u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_5u')
encoder_sg = [None, e1, e2, e3, e4, e5]
encoder_bn_u_sg = [None, ebn1u, ebn2u, ebn3u, ebn4u, ebn5u]
with tf.variable_scope('ot'):
betas_ot = [None] + [tf.Variable(0.0 * tf.ones(channel_size[i]), name=f'beta_{i}') for i in
range(1, len(channel_size))]
e1 = snt.Conv2D(channel_size[1], kernel_shape=[1, 3], name='e_1')
ebn1u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_1u')
e2 = snt.Conv2D(channel_size[2], kernel_shape=[1, 3], stride=2, name='e_2')
ebn2u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_2u')
e3 = snt.Conv2D(channel_size[3], kernel_shape=[1, 3], name='e_3')
ebn3u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_3u')
e4 = snt.Conv2D(channel_size[4], kernel_shape=[1, 3], stride=2, name='e_4')
ebn4u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_4u')
e5 = snt.Conv2D(channel_size[5], kernel_shape=[1, 3], name='e_5')
ebn5u = snt.BatchNorm(decay_rate=0, offset=False, name='ebn_5u')
encoder_ot = [None, e1, e2, e3, e4, e5]
encoder_bn_u_ot = [None, ebn1u, ebn2u, ebn3u, ebn4u, ebn5u]
# sg first layer
hu0_sg = inputs_sg
u_lst_sg = [hu0_sg]
hu_lst_sg = [hu0_sg]
# ot first layer
hu0_ot = inputs_ot
u_lst_ot = [hu0_ot]
hu_lst_ot = [hu0_ot]
for i in range(1, len(channel_size) - 1):
# sg layer building
hu_pre_sg = hu_lst_sg[i - 1]
pre_u_sg = encoder_sg[i](hu_pre_sg)
u_sg = encoder_bn_u_sg[i](pre_u_sg, False, test_local_stats=False)
hu_sg = tf.nn.relu(u_sg + betas_sg[i])
u_lst_sg.append(u_sg)
hu_lst_sg.append(hu_sg)
# ot layer building
hu_pre_ot = hu_lst_ot[i - 1]
pre_u_ot = encoder_ot[i](hu_pre_ot)
u_ot = encoder_bn_u_ot[i](pre_u_ot, False, test_local_stats=False)
hu_ot = tf.nn.relu(u_ot + betas_ot[i])
u_lst_ot.append(u_ot)
hu_lst_ot.append(hu_ot)
# sg last layer
hu_m1_sg = hu_lst_sg[-1]
pre_u_last_sg = encoder_sg[-1](hu_m1_sg)
u_last_sg = encoder_bn_u_sg[-1](pre_u_last_sg, False, test_local_stats=False)
u_last_sg = u_last_sg + betas_sg[-1]
hu_last_sg = tf.nn.relu(u_last_sg)
u_lst_sg.append(u_last_sg)
hu_lst_sg.append(hu_last_sg)
# ot last layer
hu_m1_ot = hu_lst_ot[-1]
pre_u_last_ot = encoder_ot[-1](hu_m1_ot)
u_last_ot = encoder_bn_u_ot[-1](pre_u_last_ot, False, test_local_stats=False)
u_last_ot = u_last_ot + betas_ot[-1]
hu_last_ot = tf.nn.relu(u_last_ot)
u_lst_ot.append(u_last_ot)
hu_lst_ot.append(hu_last_ot)
hu_last = tf.concat([hu_last_sg, hu_last_ot], axis=3)
cls_channel_size = [512, 512, 1024, 2]
e6 = snt.Conv2D(cls_channel_size[0], kernel_shape=[1, 3], stride=2, name='e_6')
ebn6l = snt.BatchNorm(decay_rate=0.99, name='ebn_6l')
e7 = snt.Conv2D(cls_channel_size[1], kernel_shape=[1, 3], name='e_7')
ebn7l = snt.BatchNorm(decay_rate=0.99, name='ebn_7l')
e8 = snt.Conv2D(cls_channel_size[2], kernel_shape=[1, 3], padding='VALID', name='e_8')
ebn8l = snt.BatchNorm(decay_rate=0.99, name='ebn_8l')
e9 = snt.Conv2D(cls_channel_size[3], kernel_shape=[1, 1], name='e_9')
cls_layers = [None, e6, e7, e8, e9]
cls_bn_layers = [None, ebn6l, ebn7l, ebn8l]
hl0 = hu_last
l_lst = [hl0]
hl_lst = [hl0]
for i in range(1, len(cls_channel_size)):
hl_pre = hl_lst[i - 1]
pre_l = cls_layers[i](hl_pre)
l = cls_bn_layers[i](pre_l, False, test_local_stats=False)
hl = tf.nn.relu(l)
l_lst.append(l)
hl_lst.append(hl)
hl_m1 = hl_lst[-1]
l_last = cls_layers[-1](hl_m1)
hl_last = tf.nn.softmax(l_last)
l_lst.append(l_last)
hl_lst.append(hl_last)
sig_l = tf.squeeze(hl_last, axis=[1, 2])[:, 1]
return sig_l
class DCModel:
def __init__(self, sess, ontar_model_dir, offtar_model_dir):
self.sess = sess
self.inputs_sg = tf.placeholder(dtype=tf.float32, shape=[None, 1, 23, 8])
self.inputs_ot = tf.placeholder(dtype=tf.float32, shape=[None, 1, 23, 8])
self.pred_ontar = build_ontar_model(self.inputs_sg)
self.pred_offtar = build_offtar_model(self.inputs_sg, self.inputs_ot)
all_vars = slim.get_variables_to_restore()
on_vars = {v.op.name[6:]: v for v in all_vars if v.name.startswith('ontar')}
off_vars = {v.op.name[7:]: v for v in all_vars if v.name.startswith('offtar')}
sess.run(create_init_op())
load_ckpt(sess, ontar_model_dir, variables_to_restore=on_vars)
load_ckpt(sess, offtar_model_dir, variables_to_restore=off_vars)
def ontar_predict(self, x, channel_first=True):
if channel_first:
x = x.transpose([0, 2, 3, 1])
fd = {self.inputs_sg: x}
yp = self.sess.run(self.pred_ontar, feed_dict=fd)
return yp.ravel()
def offtar_predict(self, xsg, xot, channel_first=True):
if channel_first:
xsg = xsg.transpose([0, 2, 3, 1])
xot = xot.transpose([0, 2, 3, 1])
fd = {self.inputs_sg: xsg, self.inputs_ot: xot}
yp = self.sess.run(self.pred_offtar, feed_dict=fd)
return yp.ravel()