Skip to content

Commit fd13723

Browse files
authored
Add files via upload
1 parent bdd4e8d commit fd13723

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

152-visualizing_conv_layer_outputs.py

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
# https://youtu.be/ho6JXE3EbZ8
3+
"""
4+
@author: Sreenivas Bhattiprolu
5+
6+
Copying VGG16 architecture and picking the conv layers of interest
7+
to generate filtered responses.
8+
"""
9+
10+
from keras.models import Sequential
11+
from keras.layers.core import Flatten, Dense, Dropout
12+
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
13+
from keras.optimizers import SGD
14+
import numpy as np
15+
from matplotlib import pyplot as plt
16+
from keras.models import Model
17+
18+
model = Sequential()
19+
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3)))
20+
model.add(Convolution2D(64, 3, 3, activation='relu'))
21+
model.add(ZeroPadding2D((1,1)))
22+
model.add(Convolution2D(64, 3, 3, activation='relu'))
23+
model.add(MaxPooling2D((2,2), strides=(2,2)))
24+
25+
model.add(ZeroPadding2D((1,1)))
26+
model.add(Convolution2D(128, 3, 3, activation='relu'))
27+
model.add(ZeroPadding2D((1,1)))
28+
model.add(Convolution2D(128, 3, 3, activation='relu'))
29+
model.add(MaxPooling2D((2,2), strides=(2,2)))
30+
31+
model.add(ZeroPadding2D((1,1)))
32+
model.add(Convolution2D(256, 3, 3, activation='relu'))
33+
model.add(ZeroPadding2D((1,1)))
34+
model.add(Convolution2D(256, 3, 3, activation='relu'))
35+
model.add(ZeroPadding2D((1,1)))
36+
model.add(Convolution2D(256, 3, 3, activation='relu'))
37+
model.add(MaxPooling2D((2,2), strides=(2,2)))
38+
39+
model.add(ZeroPadding2D((1,1)))
40+
model.add(Convolution2D(512, 3, 3, activation='relu'))
41+
model.add(ZeroPadding2D((1,1)))
42+
model.add(Convolution2D(512, 3, 3, activation='relu'))
43+
model.add(ZeroPadding2D((1,1)))
44+
model.add(Convolution2D(512, 3, 3, activation='relu'))
45+
model.add(MaxPooling2D((2,2), strides=(2,2)))
46+
47+
model.add(ZeroPadding2D((1,1)))
48+
model.add(Convolution2D(512, 3, 3, activation='relu'))
49+
model.add(ZeroPadding2D((1,1)))
50+
model.add(Convolution2D(512, 3, 3, activation='relu'))
51+
model.add(ZeroPadding2D((1,1)))
52+
model.add(Convolution2D(512, 3, 3, activation='relu'))
53+
model.add(MaxPooling2D((2,2), strides=(2,2)))
54+
55+
model.add(Flatten())
56+
model.add(Dense(4096, activation='relu'))
57+
model.add(Dropout(0.5))
58+
model.add(Dense(4096, activation='relu'))
59+
model.add(Dropout(0.5))
60+
model.add(Dense(1000, activation='softmax'))
61+
62+
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
63+
model.compile(optimizer=sgd, loss='categorical_crossentropy')
64+
65+
print(model.summary())
66+
67+
68+
#Understand the filters in the model
69+
#Let us pick the first hidden layer as the layer of interest.
70+
layer = model.layers #Conv layers at 1, 3, 6, 8, 11, 13, 15
71+
filters, biases = model.layers[1].get_weights()
72+
print(layer[1].name, filters.shape)
73+
74+
75+
# plot filters
76+
77+
fig1=plt.figure(figsize=(8, 12))
78+
columns = 8
79+
rows = 8
80+
n_filters = columns * rows
81+
for i in range(1, n_filters +1):
82+
f = filters[:, :, :, i-1]
83+
fig1 =plt.subplot(rows, columns, i)
84+
fig1.set_xticks([]) #Turn off axis
85+
fig1.set_yticks([])
86+
plt.imshow(f[:, :, 0], cmap='gray') #Show only the filters from 0th channel (R)
87+
#ix += 1
88+
plt.show()
89+
90+
#### Now plot filter outputs
91+
92+
#Define a new truncated model to only include the conv layers of interest
93+
#conv_layer_index = [1, 3, 6, 8, 11, 13, 15]
94+
conv_layer_index = [1, 3, 6] #TO define a shorter model
95+
outputs = [model.layers[i].output for i in conv_layer_index]
96+
model_short = Model(inputs=model.inputs, outputs=outputs)
97+
print(model_short.summary())
98+
99+
#Input shape to the model is 224 x 224. SO resize input image to this shape.
100+
from keras.preprocessing.image import load_img, img_to_array
101+
img = load_img('monalisa.jpg', target_size=(224, 224)) #VGG user 224 as input
102+
103+
# convert the image to an array
104+
img = img_to_array(img)
105+
# expand dimensions to match the shape of model input
106+
img = np.expand_dims(img, axis=0)
107+
108+
# Generate feature output by predicting on the input image
109+
feature_output = model_short.predict(img)
110+
111+
112+
columns = 8
113+
rows = 8
114+
for ftr in feature_output:
115+
#pos = 1
116+
fig=plt.figure(figsize=(12, 12))
117+
for i in range(1, columns*rows +1):
118+
fig =plt.subplot(rows, columns, i)
119+
fig.set_xticks([]) #Turn off axis
120+
fig.set_yticks([])
121+
plt.imshow(ftr[0, :, :, i-1], cmap='gray')
122+
#pos += 1
123+
plt.show()
124+

0 commit comments

Comments
 (0)