-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparison.py
153 lines (116 loc) · 5.1 KB
/
comparison.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import glob
import os
import numpy as np
from keras.models import load_model
import utils
def compare_models(model1, model2):
"""Compares the layers and weights of the two models and returns a tuple. The
first element is the result of the comparison. The other two are the
offending indices of the layer and weight, if any.
"""
if len(model1.layers) != len(model2.layers):
return False
for i_layer, layer in enumerate(model1.layers):
weights = layer.get_weights()
for i_array, array in enumerate(weights):
if not ((array == model2.layers[i_layer].get_weights()[i_array]).all()):
return (False, i_layer, i_array)
return (True, -1, -1)
def compare_xy(xy_train1, xy_train2):
"""Compares the XY training data and returns True if they are the same, False
otherwise.
"""
if not np.array_equal(xy_train1['x_train'], xy_train2['x_train']):
return (False, "X")
if not np.array_equal(xy_train1['y_train'], xy_train2['y_train']):
return (False, "Y")
return (True, "")
def compare_arrays(array1, array2):
"""Compares the arrays saved by the neural network and returns True if they are
the same, False otherwise.
"""
return np.array_equal(np.array(array1), np.array(array2))
def check_same_models(d, stub_fmt, num_runs):
"""Checks if all models in this directory with the provided stub are the same.
"""
all_same = True
num_checked = 0
messages = []
for i_run in range(num_runs):
f = os.path.join(d, stub_fmt % i_run, "cifar10_ResNet29v2_model.h5")
model = load_model(f)
num_checked += 1
# Save and skip if it's the first model
if 0 == i_run:
first_model = model
continue
# Check all the layers and weights in the model
same_model, i_layer, i_array = compare_models(model, first_model)
if not same_model:
all_same = False
messages.append("- Different model (layers and weights compared with run #0) in this run: #%d (file = %s, layer = %d, array = %d)" % (i_run, f, i_layer, i_array))
return ["Models all the same: " + str(all_same) +
" (number of models checked: %d/%d)" % (num_checked, utils.get_num_runs())] + messages
def check_same_xy_train(d, stub_fmt, num_runs):
"""Checks if all the XY training data are the same.
"""
all_same = True
num_checked = 0
messages = []
for i_run in range(num_runs):
f = os.path.join(d, stub_fmt % i_run, "xy_train.npz")
xy_train = np.load(f)
num_checked += 1
if 0 == i_run:
first_xy_train = xy_train
continue
same, offending = compare_xy(xy_train, first_xy_train)
if not same:
all_same = False
messages.append("- Different XY training data (compared with run #0) in this run: #%d (file = %s, offending = %s)" % (i_run, batches_file, offending))
return ["XY training data all the same: " + str(all_same) +
"(number of models checked: %d/%d)" % (num_checked, utils.get_num_runs())] + messages
def check_same_batch_arrays(d, stub_fmt, num_runs, num_epochs):
"""Checks if all the batch shuffles are the same.
"""
all_same = True
num_checked = 0
messages = []
for i_run in range(num_runs):
arrays = []
for i_epoch in range(num_epochs):
f = os.path.join(d, stub_fmt % i_run, "array %d.txt" % i_epoch)
arrays.append(np.loadtxt(f, delimiter = ","))
num_checked += 1
# Save and skip if it's the first model
if 0 == i_run:
first_arrays = arrays
continue
same = compare_arrays(arrays, first_arrays)
if not same:
all_same = False
messages.append("- Different batches array (compared with run #0) in this run: #%d" % i_run)
return ["Batches all the same: " + str(all_same) +
"(number of models checked: %d/%d)" % (num_checked, utils.get_num_runs())] + messages
def compare_all(d = utils.SAVED_MODELS_DIR):
"""Compare models and batches when run with the same or different seeds.
"""
num_runs = utils.get_num_runs()
num_epochs = utils.get_num_epochs()
# Iterate on same or different seeds
full_message = ""
for same_seed in range(2):
messages = []
stub_fmt = utils.seed_to_str_fmt(same_seed)
messages.extend(check_same_models(d = d, stub_fmt = stub_fmt, num_runs = num_runs))
messages.extend(check_same_xy_train(d = d, stub_fmt = stub_fmt, num_runs = num_runs))
messages.extend(check_same_batch_arrays(d = d, stub_fmt = stub_fmt, num_runs = num_runs, num_epochs = num_epochs))
seed_str = utils.seed_to_str(same_seed)
full_message += "Comparison results for %s:\n\n" % seed_str + "\n".join(messages) + "\n\n"
return full_message
def main():
message = compare_all(d = utils.SAVED_MODELS)
print(utils.PRINT_START + message + utils.PRINT_END)
if "__main__" == __name__:
print("Current directory: " + os.getcwd())
main()