-
Notifications
You must be signed in to change notification settings - Fork 119
/
test_utils.py
49 lines (45 loc) · 2.04 KB
/
test_utils.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
from termcolor import colored
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import concatenate
from tensorflow.keras.layers import ZeroPadding2D
from tensorflow.keras.layers import Dense
# Compare the two inputs
def comparator(learner, instructor):
if len(learner) != len(instructor):
raise AssertionError(f"The number of layers in the model is incorrect. Expected: {len(instructor)} Found: {len(learner)}")
for a, b in zip(learner, instructor):
if tuple(a) != tuple(b):
print(colored("Test failed", attrs=['bold']),
"\n Expected value \n\n", colored(f"{b}", "green"),
"\n\n does not match the input value: \n\n",
colored(f"{a}", "red"))
raise AssertionError("Error in test")
print(colored("All tests passed!", "green"))
# extracts the description of a given model
def summary(model):
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
result = []
for layer in model.layers:
descriptors = [layer.__class__.__name__, layer.output_shape, layer.count_params()]
if (type(layer) == Conv2D):
descriptors.append(layer.padding)
descriptors.append(layer.activation.__name__)
descriptors.append(layer.kernel_initializer.__class__.__name__)
if (type(layer) == MaxPooling2D):
descriptors.append(layer.pool_size)
descriptors.append(layer.strides)
descriptors.append(layer.padding)
if (type(layer) == Dropout):
descriptors.append(layer.rate)
if (type(layer) == ZeroPadding2D):
descriptors.append(layer.padding)
if (type(layer) == Dense):
descriptors.append(layer.activation.__name__)
result.append(descriptors)
return result