-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleConvModel.py
82 lines (70 loc) · 2.68 KB
/
SimpleConvModel.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
import matplotlib.pyplot as plt
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Reshape, MaxPooling2D
import tensorflow as tf
class SimpleConvModel(Model):
"""
Custom convolutional model
"""
def __init__(self, image_height, image_width, channel_count):
"""
Constructor
:param self: model
:param image_height: height of image in pixel
:param image_width: width of image in pixel
:channel_count: channel count, for example color image has 3 channels, grayscale image has only one channel
:return: void
"""
super(SimpleConvModel, self).__init__()
# Define sequential layers
self.convolution = Conv2D(32, (3,3), input_shape=(image_height, image_width, channel_count), activation="relu")
self.max_pooling = MaxPooling2D(2, 2)
self.flatten = Flatten()
self.dense = Dense(128, activation="relu")
self.softmax = Dense(10, activation="softmax")
# Keep convolutional layer output
self.convolutional_output = tf.constant(0)
# Keep max pooling layer output
self.max_pooling_output = tf.constant(0)
# Input signature for tf.saved_model.save()
self.input_signature = tf.TensorSpec(shape=[None, image_height, image_width, channel_count], dtype=tf.float32, name='prev_img')
def call(self, inputs):
"""
Forward propagation
:param self: model
:param inputs: tensor of dimension [batch_size, image_height, image_width, channel_count]
:return: predictions
"""
self.convolutional_output = self.convolution(inputs)
self.max_pooling_output = self.max_pooling(self.convolutional_output)
x = self.flatten(self.max_pooling_output)
x = self.dense(x)
return self.softmax(x)
def print_convolutions(self):
"""
Print convolution outputs
:param self: model
:return: void
"""
print("convolution shape:", self.convolutional_output.shape)
count = self.convolutional_output.shape[-1]
# print all convolution outputs
for index in range(0, count):
convolution = self.convolutional_output[0, :, :, index]
plt.title("Convolution n°{}".format(index + 1))
plt.imshow(convolution, cmap= plt.get_cmap('gray_r'))
plt.show()
def print_max_poolings(self):
"""
Print max pooling outputs
:param self: model
:return: void
"""
print("max pooling shape:", self.max_pooling_output.shape)
count = self.max_pooling_output.shape[-1]
# print all max pooling outputs
for index in range(0, count):
max_pooling = self.max_pooling_output[0, :, :, index]
plt.title("Max pooling n°{}".format(index + 1))
plt.imshow(max_pooling, cmap=plt.get_cmap('gray_r'))
plt.show()