-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFCN.py
More file actions
367 lines (294 loc) · 16.6 KB
/
Copy pathFCN.py
File metadata and controls
367 lines (294 loc) · 16.6 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
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
from tensorflow.python.ops import nn_ops, gen_nn_ops
import tensorflow as tf
import numpy as np
class CNN_MC_dropout:
def __init__(self, std=0.01, batch_size=64,width=500, height =1, input_channel=3, nb_classes=2, l_rate =1e-6,reuse = False):
self.std=std
self.batch_size=batch_size
self.height = height
self.width = width
self.input_channel = input_channel
self.l_rate = l_rate
self.nb_classes = nb_classes
with tf.name_scope('Classifier'):
self.y = tf.placeholder(tf.float32, [None, self.nb_classes], name='y')
self.x = tf.placeholder(tf.float32, [None, self.height,self.width,self.input_channel], name='x')
self.keep_prob = tf.placeholder(tf.float32)
self.is_dropout = tf.placeholder(tf.bool)
# keep prob가 1보다 작으면, 드랍아웃을 한다는 의미
#self.dropout_bool = True#tf.cond(self.keep_prob < 1.0, lambda: tf.constant(True), lambda: tf.constant(False))
self.logits = self.build_model()
# Define loss and optimizer, minimize the squared error
self.cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.y, logits=self.logits)
self.cost =tf.reduce_mean(self.cross_entropy)
self.optimizer = tf.train.AdamOptimizer(self.l_rate).minimize(self.cost)
self.correct_pred = tf.equal(tf.argmax(self.prediction,1),tf.argmax(self.y,1))
self.accuracy = tf.reduce_mean(tf.cast(self.correct_pred, tf.float32))
init = tf.global_variables_initializer()
# Launch the session
self.session_conf = tf.ConfigProto()
self.session_conf.gpu_options.allow_growth = True
self.sess = tf.InteractiveSession(config=self.session_conf)
self.sess.run(init)
self.saver = tf.train.Saver(max_to_keep=None)
def build_model(self):
with tf.variable_scope('layer0'):
#b, h, w, c
self.input = self.x
# Convolutional Layer #1 and Pooling Layer #1
with tf.variable_scope('layer1'):
self.conv1 = tf.layers.conv2d(self.input, 128, [8,1], padding='SAME')
self.batch1 = tf.layers.batch_normalization(self.conv1)
self.relu1 = tf.nn.relu(self.batch1)
self.dropout1 = tf.nn.dropout(self.relu1, self.keep_prob)
# Convolutional Layer #1 and Pooling Layer #2
with tf.variable_scope('layer2'):
self.conv2 = tf.layers.conv2d(self.dropout1, 256, [5,1], padding='SAME')
self.batch2 = tf.layers.batch_normalization(self.conv2)
self.relu2 = tf.nn.relu(self.batch2)
self.dropout2 = tf.nn.dropout(self.relu2, self.keep_prob)
# Convolutional Layer #1 and Pooling Layer #3
with tf.variable_scope('layer3'):
self.conv3 = tf.layers.conv2d(self.dropout2, 128, [3,1], padding='SAME')
self.batch3 = tf.layers.batch_normalization(self.conv3)
self.relu3 = tf.nn.relu(self.batch3)
# Dense Layer with Relu
with tf.variable_scope('layer4'):
#Global Average Pooling
self.GAP = tf.reduce_mean(self.relu3, axis=[1,2])# b,h,w,c
self.logits = tf.layers.dense(self.GAP,self.nb_classes)
self.prediction = tf.nn.softmax(self.logits)
return self.logits
def train(self, data, target, keep_prob,is_dropout):
opt, cost ,acc = self.sess.run((self.optimizer, self.cost, self.accuracy ),
feed_dict={self.y: target,
self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout})
return cost,acc
def test(self, data, target, keep_prob,is_dropout):
cost,acc = self.sess.run((self.cost,self.accuracy),
feed_dict={self.y: target,
self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout})
return cost,acc
def get_last_conv_output(self, data, keep_prob,is_dropout):
relu3 = self.sess.run((self.relu3),
feed_dict={self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout})
return relu3
def predict(self, data, keep_prob,is_dropout):
prediction = self.sess.run((self.prediction),
feed_dict={self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout})
return prediction
def save(self, save_path='./model.ckpt'):
saved_path = self.saver.save(self.sess, save_path)
print("Model saved in file: %s"%saved_path)
def load(self, load_path = './model.ckpt'):
self.saver.restore(self.sess, load_path)
print("Model restored")
def terminate(self):
self.sess.close()
tf.reset_default_graph()
class CNN_MC_dropout_last_conv_turnoff:
def __init__(self, std=0.01, batch_size=64,width=500, height =1, input_channel=3, nb_classes=2, l_rate =1e-6,reuse = False):
self.std=std
self.batch_size=batch_size
self.height = height
self.width = width
self.input_channel = input_channel
self.l_rate = l_rate
self.nb_classes = nb_classes
with tf.name_scope('Classifier'):
self.y = tf.placeholder(tf.float32, [None, self.nb_classes], name='y')
self.x = tf.placeholder(tf.float32, [None, self.height,self.width,self.input_channel], name='x')
self.keep_prob = tf.placeholder(tf.float32)
self.is_dropout = tf.placeholder(tf.bool)
self.threshold_weight = tf.placeholder(tf.float32, [None,1,128,128])
# keep prob가 1보다 작으면, 드랍아웃을 한다는 의미
#self.dropout_bool = True#tf.cond(self.keep_prob < 1.0, lambda: tf.constant(True), lambda: tf.constant(False))
self.logits = self.build_model()
# Define loss and optimizer, minimize the squared error
self.cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.y, logits=self.logits)
self.cost =tf.reduce_mean(self.cross_entropy)
self.optimizer = tf.train.AdamOptimizer(self.l_rate).minimize(self.cost)
self.correct_pred = tf.equal(tf.argmax(self.prediction,1),tf.argmax(self.y,1))
self.accuracy = tf.reduce_mean(tf.cast(self.correct_pred, tf.float32))
init = tf.global_variables_initializer()
# Launch the session
self.session_conf = tf.ConfigProto()
self.session_conf.gpu_options.allow_growth = True
self.sess = tf.InteractiveSession(config=self.session_conf)
self.sess.run(init)
self.saver = tf.train.Saver(max_to_keep=None)
def build_model(self):
with tf.variable_scope('layer0'):
#b, h, w, c
self.input = self.x
# Convolutional Layer #1 and Pooling Layer #1
with tf.variable_scope('layer1'):
self.conv1 = tf.layers.conv2d(self.input, 128, [8,1], padding='SAME')
self.batch1 = tf.layers.batch_normalization(self.conv1)
self.relu1 = tf.nn.relu(self.batch1)
self.dropout1 = tf.nn.dropout(self.relu1, self.keep_prob)
# Convolutional Layer #1 and Pooling Layer #2
with tf.variable_scope('layer2'):
self.conv2 = tf.layers.conv2d(self.dropout1, 256, [5,1], padding='SAME')
self.batch2 = tf.layers.batch_normalization(self.conv2)
self.relu2 = tf.nn.relu(self.batch2)
self.dropout2 = tf.nn.dropout(self.relu2, self.keep_prob)
# Convolutional Layer #1 and Pooling Layer #3
with tf.variable_scope('layer3'):
self.conv3 = tf.layers.conv2d(self.dropout2, 128, [3,1], padding='SAME')
self.batch3 = tf.layers.batch_normalization(self.conv3)
self.relu3 = tf.nn.relu(self.batch3)
self.threshold_relu3 = tf.multiply(self.relu3 , self.threshold_weight)
# Dense Layer with Relu
with tf.variable_scope('layer4'):
#Global Average Pooling
self.GAP = tf.reduce_mean(self.threshold_relu3, axis=[1,2])# b,h,w,c
self.logits = tf.layers.dense(self.GAP,self.nb_classes)
self.prediction = tf.nn.softmax(self.logits)
return self.logits
def train(self, data, target, keep_prob,is_dropout):
opt, cost ,acc = self.sess.run((self.optimizer, self.cost, self.accuracy ),
feed_dict={self.y: target,
self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight : np.ones(data.shape[0]*128*128).reshape(-1,1,128,128)})
return cost,acc
def test(self, data, target, keep_prob,is_dropout):
cost,acc = self.sess.run((self.cost,self.accuracy),
feed_dict={self.y: target,
self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight :np.ones(data.shape[0]*128*128).reshape(-1,1,128,128)})
return cost,acc
def get_last_conv_output(self, data, keep_prob,is_dropout):
relu3 = self.sess.run((self.relu3),
feed_dict={self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight : np.ones(data.shape[0]*128*128).reshape(-1,1,128,128)})
return relu3
def predict(self, data, keep_prob,is_dropout,threshold_weight):
prediction = self.sess.run((self.prediction),
feed_dict={self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight : threshold_weight})
return prediction
def save(self, save_path='./model.ckpt'):
saved_path = self.saver.save(self.sess, save_path)
print("Model saved in file: %s"%saved_path)
def load(self, load_path = './model.ckpt'):
self.saver.restore(self.sess, load_path)
print("Model restored")
def terminate(self):
self.sess.close()
tf.reset_default_graph()
class CNN_MC_dropout_input_turnoff:
def __init__(self, std=0.01, batch_size=64,width=500, height =1, input_channel=3, nb_classes=2, l_rate =1e-6,reuse = False):
self.std=std
self.batch_size=batch_size
self.height = height
self.width = width
self.input_channel = input_channel
self.l_rate = l_rate
self.nb_classes = nb_classes
with tf.name_scope('Classifier'):
self.y = tf.placeholder(tf.float32, [None, self.nb_classes], name='y')
self.x = tf.placeholder(tf.float32, [None, self.height,self.width,self.input_channel], name='x')
self.keep_prob = tf.placeholder(tf.float32)
self.is_dropout = tf.placeholder(tf.bool)
self.threshold_weight = tf.placeholder(tf.float32, [None, self.height,self.width,self.input_channel])
# keep prob가 1보다 작으면, 드랍아웃을 한다는 의미
#self.dropout_bool = True#tf.cond(self.keep_prob < 1.0, lambda: tf.constant(True), lambda: tf.constant(False))
self.logits = self.build_model()
# Define loss and optimizer, minimize the squared error
self.cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.y, logits=self.logits)
self.cost =tf.reduce_mean(self.cross_entropy)
self.optimizer = tf.train.AdamOptimizer(self.l_rate).minimize(self.cost)
self.correct_pred = tf.equal(tf.argmax(self.prediction,1),tf.argmax(self.y,1))
self.accuracy = tf.reduce_mean(tf.cast(self.correct_pred, tf.float32))
init = tf.global_variables_initializer()
# Launch the session
self.session_conf = tf.ConfigProto()
self.session_conf.gpu_options.allow_growth = True
self.sess = tf.InteractiveSession(config=self.session_conf)
self.sess.run(init)
self.saver = tf.train.Saver(max_to_keep=None)
def build_model(self):
with tf.variable_scope('layer0'):
#b, h, w, c
self.input = self.x
self.threshold_input = tf.multiply(self.input , self.threshold_weight)
# Convolutional Layer #1 and Pooling Layer #1
with tf.variable_scope('layer1'):
self.conv1 = tf.layers.conv2d(self.threshold_input, 128, [8,1], padding='SAME')
self.batch1 = tf.layers.batch_normalization(self.conv1)
self.relu1 = tf.nn.relu(self.batch1)
self.dropout1 = tf.nn.dropout(self.relu1, self.keep_prob)
# Convolutional Layer #1 and Pooling Layer #2
with tf.variable_scope('layer2'):
self.conv2 = tf.layers.conv2d(self.dropout1, 256, [5,1], padding='SAME')
self.batch2 = tf.layers.batch_normalization(self.conv2)
self.relu2 = tf.nn.relu(self.batch2)
self.dropout2 = tf.nn.dropout(self.relu2, self.keep_prob)
# Convolutional Layer #1 and Pooling Layer #3
with tf.variable_scope('layer3'):
self.conv3 = tf.layers.conv2d(self.dropout2, 128, [3,1], padding='SAME')
self.batch3 = tf.layers.batch_normalization(self.conv3)
self.relu3 = tf.nn.relu(self.batch3)
# Dense Layer with Relu
with tf.variable_scope('layer4'):
#Global Average Pooling
self.GAP = tf.reduce_mean(self.relu3, axis=[1,2])# b,h,w,c
self.logits = tf.layers.dense(self.GAP,self.nb_classes)
self.prediction = tf.nn.softmax(self.logits)
return self.logits
def train(self, data, target, keep_prob,is_dropout):
opt, cost ,acc = self.sess.run((self.optimizer, self.cost, self.accuracy ),
feed_dict={self.y: target,
self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight : np.ones(data.shape[0]*self.height*self.width*self.input_channel).reshape(-1,self.height,self.width,self.input_channel)})
return cost,acc
def test(self, data, target, keep_prob,is_dropout):
cost,acc = self.sess.run((self.cost,self.accuracy),
feed_dict={self.y: target,
self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight :np.ones(data.shape[0]*self.height*self.width*self.input_channel).reshape(-1,self.height,self.width,self.input_channel)})
return cost,acc
def get_last_conv_output(self, data, keep_prob,is_dropout):
relu3 = self.sess.run((self.relu3),
feed_dict={self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight : np.ones(data.shape[0]*self.height*self.width*self.input_channel).reshape(-1,self.height,self.width,self.input_channel)})
return relu3
def predict(self, data, keep_prob,is_dropout,threshold_weight):
prediction = self.sess.run((self.prediction),
feed_dict={self.x: data,
self.keep_prob: keep_prob,
self.is_dropout : is_dropout,
self.threshold_weight : threshold_weight})
return prediction
def save(self, save_path='./model.ckpt'):
saved_path = self.saver.save(self.sess, save_path)
print("Model saved in file: %s"%saved_path)
def load(self, load_path = './model.ckpt'):
self.saver.restore(self.sess, load_path)
print("Model restored")
def terminate(self):
self.sess.close()
tf.reset_default_graph()