-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_utils.py
82 lines (69 loc) · 3.19 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
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
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
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import RepeatVector
from tensorflow.keras.layers import TimeDistributed
from tensorflow.keras.layers import GRU
from tensorflow.keras.layers import Conv1D
# Compare the two inputs
def comparator(learner, instructor):
layer = 0
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']),
f"at layer: {layer}",
"\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")
layer += 1
print(colored("All tests passed!", "green"))
return True
# 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) == Conv1D):
descriptors.append(layer.padding)
descriptors.append(layer.activation.__name__)
descriptors.append(layer.strides)
descriptors.append(layer.kernel_size)
descriptors.append(layer.kernel_initializer.__class__.__name__)
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__)
if (type(layer) == LSTM):
descriptors.append(layer.input_shape)
descriptors.append(layer.activation.__name__)
if (type(layer) == RepeatVector):
descriptors.append(layer.n)
if (type(layer) == TimeDistributed):
descriptors.append(layer.layer.activation.__name__)
if (type(layer) == GRU):
descriptors.append(layer.return_sequences)
result.append(descriptors)
return result