-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfl_heatmap.py
204 lines (157 loc) · 7.6 KB
/
tfl_heatmap.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
'''
@author: Vignesh Srinivasan
@author: Sebastian Lapuschkin
@author: Gregoire Montavon
@maintainer: Vignesh Srinivasan
@maintainer: Sebastian Lapuschkin
@contact: vignesh.srinivasan@hhi.fraunhofer.de
@date: 20.12.2016
@version: 1.0+
@copyright: Copyright (c) 2016-2017, Vignesh Srinivasan, Sebastian Lapuschkin, Alexander Binder, Gregoire Montavon, Klaus-Robert Mueller, Wojciech Samek
@license : BSD-2-Clause
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append("..")
from modules.sequential import Sequential
from modules.linear import Linear
from modules.softmax import Softmax
from modules.relu import Relu
from modules.tanh import Tanh
from modules.convolution import Convolution
from modules.avgpool import AvgPool
from modules.maxpool import MaxPool
from modules.utils import Utils, Summaries, plot_relevances
import modules.render as render
import tensorflow as tf
import numpy as np
import pdb
import scipy.io as sio
from TFL_Data import TFLData
from MNIST_Data import MnistData
flags = tf.flags
logging = tf.logging
flags.DEFINE_integer("max_steps", 200,'Number of steps to run trainer.')
flags.DEFINE_integer("batch_size", 100,'Number of steps to run trainer.')
flags.DEFINE_integer("test_every", 10,'Number of steps to run trainer.')
flags.DEFINE_integer("image_dim", 56,'Width of square input images')
flags.DEFINE_integer("output_dim", 2,'Dimension of output layer')
flags.DEFINE_float("learning_rate", 0.01,'Initial learning rate')
flags.DEFINE_float("dropout", 0.9, 'Keep probability for training dropout.')
flags.DEFINE_string("data_dir", 'data','Directory for storing data')
flags.DEFINE_string("summaries_dir", 'tfl_convolutional_logs','Summaries directory')
flags.DEFINE_boolean("relevance", False,'Compute relevances')
flags.DEFINE_string("relevance_method", 'simple','relevance methods: simple/eps/w^2/alphabeta')
flags.DEFINE_boolean("save_model", False,'Save the trained model')
flags.DEFINE_boolean("reload_model", False,'Restore the trained model')
#flags.DEFINE_string("checkpoint_dir", 'mnist_convolution_model','Checkpoint dir')
flags.DEFINE_string("checkpoint_dir", 'tfl_model','Checkpoint dir')
flags.DEFINE_string("checkpoint_reload_dir", 'tfl_model','Checkpoint dir')
FLAGS = flags.FLAGS
def nn():
return Sequential([Convolution(output_depth=FLAGS.output_dim,input_depth=1,batch_size=FLAGS.batch_size, input_dim=FLAGS.image_dim, act ='relu', stride_size=1, pad='VALID'),
AvgPool(),
Convolution(output_depth=25,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(output_depth=25,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(kernel_size=4,output_depth=100,stride_size=1, act ='relu', pad='VALID'),
AvgPool(),
Convolution(kernel_size=1, output_depth=FLAGS.output_dim,stride_size=1, pad='VALID'),
Softmax()])
def feed_dict(tfl_data, train):
if train:
xs, ys = tfl_data.NextTrainBatch(FLAGS.batch_size)
k = FLAGS.dropout
else:
xs, ys = tfl_data.NextTestBatch(FLAGS.batch_size)
k = 1.0
return (2*xs)-1, ys, k
#return xs, ys, k
def train():
# Import data
train_file_path = str(FLAGS.image_dim)+"_train_y.csv"
test_file_path = str(FLAGS.image_dim)+"_test_y.csv"
mnist = TFLData( (train_file_path,test_file_path) )
# train_file_path = "mnist_train.csv"
# test_file_path = "mnist_test.csv"
# mnist = MnistData( (train_file_path,test_file_path) )
config = tf.ConfigProto(allow_soft_placement = True)
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
#with tf.Session() as sess:
# Input placeholders
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, [None, FLAGS.image_dim*FLAGS.image_dim], name='x-input')
y_ = tf.placeholder(tf.float32, [None, FLAGS.output_dim], name='y-input')
keep_prob = tf.placeholder(tf.float32)
with tf.variable_scope('model'):
net = nn()
inp = tf.pad(tf.reshape(x, [FLAGS.batch_size,FLAGS.image_dim,FLAGS.image_dim,1]), [[0,0],[2,2],[2,2],[0,0]])
op = net.forward(inp)
y = tf.squeeze(op)
trainer = net.fit(output=y,ground_truth=y_,loss='softmax_crossentropy',optimizer='adam', opt_params=[FLAGS.learning_rate])
with tf.variable_scope('relevance'):
if FLAGS.relevance:
LRP = net.lrp(op, FLAGS.relevance_method, 1e-8)
# LRP layerwise
relevance_layerwise = []
# R = y
# for layer in net.modules[::-1]:
# R = net.lrp_layerwise(layer, R, 'simple')
# relevance_layerwise.append(R)
else:
LRP=[]
relevance_layerwise = []
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)), tf.float32))
tf.summary.scalar('accuracy', accuracy)
# Merge all the summaries and write them out to /tmp/mnist_logs (by default)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train', sess.graph)
test_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/test')
tf.global_variables_initializer().run()
utils = Utils(sess, FLAGS.checkpoint_reload_dir)
if FLAGS.reload_model:
utils.reload_model()
for i in range(FLAGS.max_steps):
if i % FLAGS.test_every == 0: # test-set accuracy
d = feed_dict(mnist, False)
test_inp = {x:d[0], y_: d[1], keep_prob: d[2]}
#pdb.set_trace()
summary, acc , relevance_test, rel_layer= sess.run([merged, accuracy, LRP, relevance_layerwise], feed_dict=test_inp)
test_writer.add_summary(summary, i)
print('Accuracy at step %s: %f' % (i, acc))
# print([np.sum(rel) for rel in rel_layer])
# print(np.sum(relevance_test))
# save model if required
if FLAGS.save_model:
utils.save_model()
else:
d = feed_dict(mnist, True)
inp = {x:d[0], y_: d[1], keep_prob: d[2]}
summary, _ , relevance_train,op, rel_layer= sess.run([merged, trainer.train, LRP,y, relevance_layerwise], feed_dict=inp)
train_writer.add_summary(summary, i)
# relevances plotted with visually pleasing color schemes
if FLAGS.relevance:
#pdb.set_trace()
relevance_test = relevance_test[:,2:FLAGS.image_dim+2,2:FLAGS.image_dim+2,:]
# plot test images with relevances overlaid
images = test_inp[test_inp.keys()[0]].reshape([FLAGS.batch_size,FLAGS.image_dim,FLAGS.image_dim,1])
#images = (images + 1)/2.0
plot_relevances(relevance_test.reshape([FLAGS.batch_size,FLAGS.image_dim,FLAGS.image_dim,1]), images, test_writer )
# plot train images with relevances overlaid
# relevance_train = relevance_train[:,2:30,2:30,:]
# images = inp[inp.keys()[0]].reshape([FLAGS.batch_size,28,28,1])
# plot_relevances(relevance_train.reshape([FLAGS.batch_size,28,28,1]), images, train_writer )
train_writer.close()
test_writer.close()
def main(_):
if tf.gfile.Exists(FLAGS.summaries_dir):
tf.gfile.DeleteRecursively(FLAGS.summaries_dir)
tf.gfile.MakeDirs(FLAGS.summaries_dir)
train()
if __name__ == '__main__':
tf.app.run()