-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualizeFilteredImage.py
More file actions
77 lines (60 loc) · 2.26 KB
/
Copy pathVisualizeFilteredImage.py
File metadata and controls
77 lines (60 loc) · 2.26 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
# https://youtu.be/ho6JXE3EbZ8
"""
@author: Sreenivas Bhattiprolu
Copying VGG16 architecture and picking the conv layers of interest
to generate filtered responses.
"""
import numpy as np
from matplotlib import pyplot as plt
from keras.models import Model
from tensorflow.keras.models import load_model
import cv2
model = load_model('11_32_512_512_canny_Tamil_and_English_artificial_and_tagged_training.h5')
print(model.summary())
#Understand the filters in the model
#Let us pick the first hidden layer as the layer of interest.
layer = model.layers #Conv layers at 0, 2, 4
print(layer)
filters, biases = model.layers[0].get_weights()
print(layer[0].name, filters.shape)
# plot filters
fig1=plt.figure(figsize=(13, 7))
columns = 8
rows = 4
n_filters = columns * rows
for i in range(1, 33, +1):
f = filters[:, :, :, i-1]
fig1 =plt.subplot(rows, columns, i)
fig1.set_xticks([]) #Turn off axis
fig1.set_yticks([])
plt.imshow(f[:, :, 0], cmap='gray') #Show only the filters from 0th channel (R)
#ix += 1
plt.show()
#### Now plot filter outputs
#Define a new truncated model to only include the conv layers of interest
conv_layer_index = [0, 2, 4] #TO define a shorter model
outputs = [model.layers[i].output for i in conv_layer_index]
model_short = Model(inputs=model.inputs, outputs=outputs)
print(model_short.summary())
#Input shape to the model is 224 x 224. SO resize input image to this shape.
image = cv2.imread('0000013.TIF')
height, width, _ = np.shape(image)
#crop image to avoid extremely obvious borders
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.Canny(gray, 50, 120, apertureSize=3)
resized_image = cv2.resize(gray, (512, 512))
# expand dimensions to match the shape of model input
img = np.expand_dims(resized_image, axis=0)
# Generate feature output by predicting on the input image
feature_output = model_short.predict(img)
columns = 8
rows = 4
for ftr in feature_output:
fig=plt.figure(figsize=(13, 7))
for i in range(1, columns*rows +1):
fig =plt.subplot(rows, columns, i)
fig.set_xticks([]) #Turn off axis
fig.set_yticks([])
plt.imshow(ftr[0, :, :, i-1], cmap='gray')
#pos += 1
plt.show()