forked from kvfrans/feature-visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
256 lines (214 loc) · 9.01 KB
/
main.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
import tensorflow as tf
import numpy as np
import cPickle
from tensorflow.python.platform import gfile
from random import randint
import os
from scipy.misc import imsave
from matplotlib import pyplot as plt
def unpickle(file):
fo = open(file, 'rb')
dict = cPickle.load(fo)
fo.close()
return dict
def initWeight(shape):
weights = tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(weights)
# start with 0.1 so reLu isnt always 0
def initBias(shape):
bias = tf.constant(0.1,shape=shape)
return tf.Variable(bias)
# the convolution with padding of 1 on each side, and moves by 1.
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding="SAME")
# max pooling basically shrinks it by 2x, taking the highest value on each feature.
def maxPool2d(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
batchsize = 50;
imagesize = 32;
colors = 3;
sess = tf.InteractiveSession()
img = tf.placeholder("float",shape=[None,imagesize,imagesize,colors])
lbl = tf.placeholder("float",shape=[None,10])
# for each 5x5 area, check for 32 features over 3 color channels
wConv1 = initWeight([5,5,colors,32])
bConv1 = initBias([32])
# move the conv filter over the picture
conv1 = conv2d(img,wConv1)
# adds bias
bias1 = conv1 + bConv1
# relu = max(0,x), adds nonlinearality
relu1 = tf.nn.relu(bias1)
# maxpool to 16x16
pool1 = maxPool2d(relu1)
# second conv layer, takes a 16x16 with 32 layers, turns to 8x8 with 64 layers
wConv2 = initWeight([5,5,32,64])
bConv2 = initBias([64])
conv2 = conv2d(pool1,wConv2)
bias2 = conv2 + bConv2
relu2 = tf.nn.relu(bias2)
pool2 = maxPool2d(relu2)
# fully-connected is just a regular neural net: 8*8*64 for each training data
wFc1 = initWeight([(imagesize/4) * (imagesize/4) * 64, 1024])
bFc1 = initBias([1024])
# reduce dimensions to flatten
pool2flat = tf.reshape(pool2, [-1, (imagesize/4) * (imagesize/4) *64])
# 128 training set by 2304 data points
fc1 = tf.matmul(pool2flat,wFc1) + bFc1;
relu3 = tf.nn.relu(fc1);
# dropout removes duplicate weights
keepProb = tf.placeholder("float");
drop = tf.nn.dropout(relu3,keepProb);
wFc2 = initWeight([1024,10]);
bFc2 = initWeight([10]);
# softmax converts individual probabilities to percentages
guesses = tf.nn.softmax(tf.matmul(drop, wFc2) + bFc2);
# how wrong it is
cross_entropy = -tf.reduce_sum(lbl*tf.log(guesses + 1e-9));
# theres a lot of tensorflow optimizers such as gradient descent
# adam is one of them
optimizer = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy);
# array of bools, checking if each guess was correct
correct_prediction = tf.equal(tf.argmax(guesses,1), tf.argmax(lbl,1));
# represent the correctness as a float [1,1,0,1] -> 0.75
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"));
sess.run(tf.initialize_all_variables());
batch = unpickle("cifar-10-batches-py/data_batch_1")
validationData = batch["data"][555:batchsize+555]
validationRawLabel = batch["labels"][555:batchsize+555]
validationLabel = np.zeros((batchsize,10))
validationLabel[np.arange(batchsize),validationRawLabel] = 1
validationData = validationData/255.0
validationData = np.reshape(validationData,[-1,3,32,32])
validationData = np.swapaxes(validationData,1,3)
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(os.getcwd()+"/training/"))
# train for 20000
# print mnistbatch[0].shape
def train():
for i in range(20000):
randomint = randint(0,10000 - batchsize - 1)
trainingData = batch["data"][randomint:batchsize+randomint]
rawlabel = batch["labels"][randomint:batchsize+randomint]
trainingLabel = np.zeros((batchsize,10))
trainingLabel[np.arange(batchsize),rawlabel] = 1
trainingData = trainingData/255.0
trainingData = np.reshape(trainingData,[-1,3,32,32])
trainingData = np.swapaxes(trainingData,1,3)
if i%10 == 0:
train_accuracy = accuracy.eval(feed_dict={
img: validationData, lbl: validationLabel, keepProb: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
if i%50 == 0:
saver.save(sess, os.getcwd()+"/training/train", global_step=i)
optimizer.run(feed_dict={img: trainingData, lbl: trainingLabel, keepProb: 0.5})
print i
def unpool(value, name='unpool'):
"""N-dimensional version of the unpooling operation from
https://www.robots.ox.ac.uk/~vgg/rg/papers/Dosovitskiy_Learning_to_Generate_2015_CVPR_paper.pdf
:param value: A Tensor of shape [b, d0, d1, ..., dn, ch]
:return: A Tensor of shape [b, 2*d0, 2*d1, ..., 2*dn, ch]
"""
with tf.name_scope(name) as scope:
sh = value.get_shape().as_list()
dim = len(sh[1:-1])
out = (tf.reshape(value, [-1] + sh[-dim:]))
for i in range(dim, 0, -1):
out = tf.concat(i, [out, out])
out_size = [-1] + [s * 2 for s in sh[1:-1]] + [sh[-1]]
out = tf.reshape(out, out_size, name=scope)
return out
def display():
print "displaying"
batchsizeFeatures = 50
imageIndex = 56
inputImage = batch["data"][imageIndex:imageIndex+batchsizeFeatures]
inputImage = inputImage/255.0
inputImage = np.reshape(inputImage,[-1,3,32,32])
inputImage = np.swapaxes(inputImage,1,3)
inputLabel = np.zeros((batchsize,10))
inputLabel[np.arange(1),batch["labels"][imageIndex:imageIndex+batchsizeFeatures]] = 1;
# inputLabel = batch["labels"][54]
# prints a given image
# saves pixel-representations of features from Conv layer 1
featuresReLu1 = tf.placeholder("float",[None,32,32,32])
unReLu = tf.nn.relu(featuresReLu1)
unBias = unReLu
unConv = tf.nn.conv2d_transpose(unBias, wConv1, output_shape=[batchsizeFeatures,imagesize,imagesize,colors] , strides=[1,1,1,1], padding="SAME")
activations1 = relu1.eval(feed_dict={img: inputImage, lbl: inputLabel, keepProb: 1.0})
print np.shape(activations1)
# display features
for i in xrange(32):
isolated = activations1.copy()
isolated[:,:,:,:i] = 0
isolated[:,:,:,i+1:] = 0
print np.shape(isolated)
totals = np.sum(isolated,axis=(1,2,3))
best = np.argmin(totals,axis=0)
print best
pixelactive = unConv.eval(feed_dict={featuresReLu1: isolated})
# totals = np.sum(pixelactive,axis=(1,2,3))
# best = np.argmax(totals,axis=0)
# best = 0
saveImage(pixelactive[best],"activ"+str(i)+".png")
saveImage(inputImage[best],"activ"+str(i)+"-base.png")
# display same feature for many images
# for i in xrange(batchsizeFeatures):
# isolated = activations1.copy()
# isolated[:,:,:,:6] = 0
# isolated[:,:,:,7:] = 0
# pixelactive = unConv.eval(feed_dict={featuresReLu1: isolated})
# totals = np.sum(pixelactive,axis=(1,2,3))
# best = np.argmax(totals,axis=0)
# saveImage(pixelactive[i],"activ"+str(i)+".png")
# saveImage(inputImage[i],"activ"+str(i)+"-base.png")
# saves pixel-representations of features from Conv layer 2
featuresReLu2 = tf.placeholder("float",[None,16,16,64])
unReLu2 = tf.nn.relu(featuresReLu2)
unBias2 = unReLu2
unConv2 = tf.nn.conv2d_transpose(unBias2, wConv2, output_shape=[batchsizeFeatures,imagesize/2,imagesize/2,32] , strides=[1,1,1,1], padding="SAME")
unPool = unpool(unConv2)
unReLu = tf.nn.relu(unPool)
unBias = unReLu
unConv = tf.nn.conv2d_transpose(unBias, wConv1, output_shape=[batchsizeFeatures,imagesize,imagesize,colors] , strides=[1,1,1,1], padding="SAME")
activations1 = relu2.eval(feed_dict={img: inputImage, lbl: inputLabel, keepProb: 1.0})
print np.shape(activations1)
# display features
# for i in xrange(64):
# isolated = activations1.copy()
# isolated[:,:,:,:i] = 0
# isolated[:,:,:,i+1:] = 0
# pixelactive = unConv.eval(feed_dict={featuresReLu2: isolated})
# # totals = np.sum(pixelactive,axis=(1,2,3))
# # best = np.argmax(totals,axis=0)
# best = 0
# saveImage(pixelactive[best],"activ"+str(i)+".png")
# saveImage(inputImage[best],"activ"+str(i)+"-base.png")
# display same feature for many images
# for i in xrange(batchsizeFeatures):
# isolated = activations1.copy()
# isolated[:,:,:,:8] = 0
# isolated[:,:,:,9:] = 0
# pixelactive = unConv.eval(feed_dict={featuresReLu2: isolated})
# totals = np.sum(pixelactive,axis=(1,2,3))
# # best = np.argmax(totals,axis=0)
# # best = 0
# saveImage(pixelactive[i],"activ"+str(i)+".png")
# saveImage(inputImage[i],"activ"+str(i)+"-base.png")
def saveImage(inputImage, name):
# red = inputImage[:1024]
# green = inputImage[1024:2048]
# blue = inputImage[2048:]
# formatted = np.zeros([3,32,32])
# formatted[0] = np.reshape(red,[32,32])
# formatted[1] = np.reshape(green,[32,32])
# formatted[2] = np.reshape(blue,[32,32])
# final = np.swapaxes(formatted,0,2)/255;
final = inputImage
final = np.rot90(np.rot90(np.rot90(final)))
imsave(name,final)
def main(argv=None):
display()
# train()
if __name__ == '__main__':
tf.app.run()