CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 4 - Custom Models/utils.py
Views: 13373
1
import tensorflow as tf
2
from tensorflow.keras import layers
3
4
def test_loop(test_cases):
5
6
success = 0
7
fails = 0
8
9
for test_case in test_cases:
10
try:
11
assert test_case["result"] == test_case["expected"]
12
success += 1
13
14
except:
15
fails += 1
16
print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\n')
17
18
if fails == 0:
19
print("\033[92m All public tests passed")
20
21
else:
22
print('\033[92m', success," Tests passed")
23
print('\033[91m', fails, " Tests failed")
24
raise Exception(test_case["error_message"])
25
26
27
def test_block_class(Block):
28
29
filters = 64
30
kernel_size = 3
31
padding = 'same'
32
pool_size = 3
33
repetitions = 2
34
test_block = Block(filters, kernel_size, repetitions, pool_size)
35
test_block(tf.random.uniform(shape=[2, 3, 4, 5]))
36
37
vars_test_block = vars(test_block)
38
39
test_cases = [
40
{
41
"name": "max_pool_type_check",
42
"result": type(test_block.max_pool),
43
"expected": layers.MaxPooling2D,
44
"error_message": f'Incorrect layer type for self.maxpool'
45
},
46
{
47
"name": "max_pool_size_check",
48
"result": vars_test_block['max_pool'].pool_size,
49
"expected": (pool_size, pool_size),
50
"error_message": f'max pool size incorrect. check parameters.'
51
},
52
{
53
"name": "max_pool_size_check",
54
"result": vars_test_block['max_pool'].strides,
55
"expected": (2,2),
56
"error_message": f'max pool strides incorrect. check parameters.'
57
},
58
{
59
"name": "conv2D_0_type_check",
60
"result": type(vars_test_block['conv2D_0']),
61
"expected": layers.Conv2D,
62
"error_message": f'Incorrect layer type for block_0'
63
},
64
{
65
"name": "conv2D_1_type_check",
66
"result": type(vars_test_block['conv2D_1']),
67
"expected": layers.Conv2D,
68
"error_message": f'Incorrect layer type for block_0'
69
},
70
{
71
"name": "conv2D_0_filters_check",
72
"result": vars_test_block['conv2D_0'].filters,
73
"expected": filters,
74
"error_message": f'Incorrect filters for Conv2D layer. Please check parameters.'
75
},
76
{
77
"name": "conv2D_0_kernel_size_check",
78
"result": vars_test_block['conv2D_0'].kernel_size,
79
"expected": (kernel_size, kernel_size),
80
"error_message": f'Incorrect kernel_size for Conv2D layer. Please check parameters.'
81
},
82
{
83
"name": "conv2D_0_activation_check",
84
"result": vars_test_block['conv2D_0'].activation,
85
"expected": tf.keras.activations.relu,
86
"error_message": f'Incorrect activation for Conv2D layer. Please check parameters.'
87
},
88
{
89
"name": "conv2D_0_padding_check",
90
"result": vars_test_block['conv2D_0'].padding,
91
"expected": padding,
92
"error_message": f'Incorrect padding for Conv2D layer. Please check parameters.'
93
},
94
95
]
96
97
test_loop(test_cases)
98
99
def test_myvgg_class(MyVGG, Block):
100
test_vgg = MyVGG(num_classes=2)
101
test_vgg_layers = test_vgg.layers
102
103
def get_block_params(block):
104
return (block.filters, block.kernel_size, block.repetitions)
105
106
test_cases = [
107
{
108
"name": "block_a_type_check",
109
"result": type(test_vgg.block_a),
110
"expected": Block,
111
"error_message": "self.block_a has an incorrect type. Please check declaration."
112
},
113
{
114
"name": "block_b_type_check",
115
"result": type(test_vgg.block_b),
116
"expected": Block,
117
"error_message": "self.block_b has an incorrect type. Please check declaration."
118
},
119
{
120
"name": "block_c_type_check",
121
"result": type(test_vgg.block_c),
122
"expected": Block,
123
"error_message": "self.block_c has an incorrect type. Please check declaration."
124
},
125
{
126
"name": "block_d_type_check",
127
"result": type(test_vgg.block_d),
128
"expected": Block,
129
"error_message": "self.block_d has an incorrect type. Please check declaration."
130
},
131
{
132
"name": "block_e_type_check",
133
"result": type(test_vgg.block_e),
134
"expected": Block,
135
"error_message": "self.block_e has an incorrect type. Please check declaration."
136
},
137
{
138
"name": "block_a_param_check",
139
"result": get_block_params(test_vgg.block_a),
140
"expected": (64, 3, 2),
141
"error_message": "self.block_a has incorrect parameters. Please check hints in the code comments."
142
},
143
{
144
"name": "block_b_param_check",
145
"result": get_block_params(test_vgg.block_b),
146
"expected": (128, 3, 2),
147
"error_message": "self.block_b has incorrect parameters. Please check hints in the code comments."
148
},
149
{
150
"name": "block_c_param_check",
151
"result": get_block_params(test_vgg.block_c),
152
"expected": (256, 3, 3),
153
"error_message": "self.block_c has incorrect parameters. Please check hints in the code comments."
154
},
155
{
156
"name": "block_d_param_check",
157
"result": get_block_params(test_vgg.block_d),
158
"expected": (512, 3, 3),
159
"error_message": "self.block_d has incorrect parameters. Please check hints in the code comments."
160
},
161
{
162
"name": "block_e_param_check",
163
"result": get_block_params(test_vgg.block_e),
164
"expected": (512, 3, 3),
165
"error_message": "self.block_e has incorrect parameters. Please check hints in the code comments."
166
},
167
{
168
"name": "flatten_type_check",
169
"result": type(test_vgg.flatten),
170
"expected": layers.Flatten,
171
"error_message": "self.flatten has an incorrect type. Please check declaration."
172
},
173
{
174
"name": "fc_type_check",
175
"result": type(test_vgg.fc),
176
"expected": layers.Dense,
177
"error_message": "self.fc has an incorrect type. Please check declaration."
178
},
179
{
180
"name": "fc_units_check",
181
"result": test_vgg.fc.units,
182
"expected": 256,
183
"error_message": "self.fc has an incorrect number of units. Please check declaration."
184
},
185
{
186
"name": "fc_activation_check",
187
"result": test_vgg.fc.activation,
188
"expected": tf.keras.activations.relu,
189
"error_message": "self.fc has an incorrect activation. Please check declaration."
190
},
191
{
192
"name": "classifier_type_check",
193
"result": type(test_vgg.classifier),
194
"expected": layers.Dense,
195
"error_message": "self.classifier has an incorrect type. Please check declaration."
196
},
197
{
198
"name": "fc_units_check",
199
"result": test_vgg.classifier.units,
200
"expected": 2,
201
"error_message": "self.classifier has an incorrect number of units. Please check declaration."
202
},
203
{
204
"name": "fc_activation_check",
205
"result": test_vgg.classifier.activation,
206
"expected": tf.keras.activations.softmax,
207
"error_message": "self.classifier has an incorrect activation. Please check declaration."
208
},
209
{
210
"name": "layer_0_check",
211
"result": type(test_vgg_layers[0]),
212
"expected": Block,
213
"error_message": "Layer 0 of myVGG is incorrect. Please check its call() method."
214
},
215
{
216
"name": "layer_1_check",
217
"result": type(test_vgg_layers[1]),
218
"expected": Block,
219
"error_message": "Layer 1 of myVGG is incorrect. Please check its call() method."
220
},
221
{
222
"name": "layer_2_check",
223
"result": type(test_vgg_layers[2]),
224
"expected": Block,
225
"error_message": "Layer 2 of myVGG is incorrect. Please check its call() method."
226
},
227
{
228
"name": "layer_3_check",
229
"result": type(test_vgg_layers[3]),
230
"expected": Block,
231
"error_message": "Layer 3 of myVGG is incorrect. Please check its call() method."
232
},
233
{
234
"name": "layer_4_check",
235
"result": type(test_vgg_layers[4]),
236
"expected": Block,
237
"error_message": "Layer 4 of myVGG is incorrect. Please check its call() method."
238
},
239
{
240
"name": "layer_5_check",
241
"result": type(test_vgg_layers[5]),
242
"expected": layers.Flatten,
243
"error_message": "Layer 5 of myVGG is incorrect. Please check its call() method."
244
},
245
{
246
"name": "layer_6_check",
247
"result": type(test_vgg_layers[6]),
248
"expected": layers.Dense,
249
"error_message": "Layer 6 of myVGG is incorrect. Please check its call() method."
250
},
251
{
252
"name": "layer_7_check",
253
"result": type(test_vgg_layers[7]),
254
"expected": layers.Dense,
255
"error_message": "Layer 7 of myVGG is incorrect. Please check its call() method."
256
},
257
258
]
259
260
test_loop(test_cases)
261
262