This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_cnn_model.py
131 lines (111 loc) · 3.62 KB
/
test_cnn_model.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
"""Unit test related to the simple layer creation
"""
from keras.models import Model
from deeposlandia.network import ConvolutionalNeuralNetwork
def test_convolution_shape(
shapes_image_size, kernel_size, conv_depth, conv_strides
):
"""Test the convolution operation through its output layer shape
"""
cnn = ConvolutionalNeuralNetwork("test", shapes_image_size)
y = cnn.convolution(
cnn.X,
nb_filters=conv_depth,
kernel_size=kernel_size,
strides=conv_strides,
block_name="convtest",
)
m = Model(cnn.X, y)
output_shape = m.output_shape
assert len(output_shape) == 4
assert output_shape[1:] == (
shapes_image_size // conv_strides,
shapes_image_size // conv_strides,
conv_depth,
)
def test_transposed_convolution_shape(
shapes_image_size, conv_depth, kernel_size, conv_strides
):
"""Test the transposed convolution operation through its output layer shape
"""
cnn = ConvolutionalNeuralNetwork("test", shapes_image_size)
y = cnn.transposed_convolution(
cnn.X,
nb_filters=conv_depth,
kernel_size=kernel_size,
strides=conv_strides,
block_name="transconvtest",
)
m = Model(cnn.X, y)
output_shape = m.output_shape
assert len(output_shape) == 4
assert output_shape[1:] == (
shapes_image_size * conv_strides,
shapes_image_size * conv_strides,
conv_depth,
)
def test_maxpooling_shape(
shapes_image_size, nb_channels, pool_size, pool_strides
):
"""Test the max pooling operation through its output layer shape
"""
cnn = ConvolutionalNeuralNetwork("test", shapes_image_size, nb_channels)
y = cnn.maxpool(
cnn.X, pool_size=pool_size, strides=pool_strides, block_name="pooltest"
)
m = Model(cnn.X, y)
output_shape = m.output_shape
assert len(output_shape) == 4
assert output_shape[1:] == (
shapes_image_size // pool_strides,
shapes_image_size // pool_strides,
nb_channels,
)
def test_dense_shape(shapes_image_size, conv_depth):
"""Test the fully-connected layer through its output shape
"""
cnn = ConvolutionalNeuralNetwork("test", shapes_image_size)
y = cnn.dense(cnn.X, depth=conv_depth, block_name="fctest")
m = Model(cnn.X, y)
output_shape = m.output_shape
assert len(output_shape) == 4
assert output_shape[1:] == (
shapes_image_size,
shapes_image_size,
conv_depth,
)
def test_flatten_shape(shapes_image_size, nb_channels):
"""Test the flattening layer through its output shape
"""
cnn = ConvolutionalNeuralNetwork(
"test", image_size=shapes_image_size, nb_channels=nb_channels
)
y = cnn.flatten(cnn.X, block_name="flattentest")
m = Model(cnn.X, y)
output_shape = m.output_shape
assert len(output_shape) == 2
assert (
output_shape[1] == shapes_image_size * shapes_image_size * nb_channels
)
def test_layer_name(shapes_image_size, kernel_size, conv_depth, conv_strides):
"""Test the convolution operation through its output layer shape
"""
cnn = ConvolutionalNeuralNetwork("test", shapes_image_size)
y = cnn.convolution(
cnn.X,
nb_filters=conv_depth,
kernel_size=kernel_size,
strides=conv_strides,
)
y = cnn.convolution(
y, nb_filters=conv_depth, kernel_size=kernel_size, strides=conv_strides
)
m = Model(cnn.X, y)
assert [l.name for l in m.layers[1:]] == [
"conv2d_1",
"batch_normalization_1",
"activation_1",
"conv2d_2",
"batch_normalization_2",
"activation_2",
]