-
Notifications
You must be signed in to change notification settings - Fork 57
/
make_tests.py
326 lines (245 loc) · 9.13 KB
/
make_tests.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import numpy as np
import pprint
from keras.models import Sequential
from keras.layers import Convolution2D, Dense, Flatten, Activation, MaxPooling2D, Dropout
from keras.layers.recurrent import LSTM
from keras.layers.advanced_activations import ELU
from keras.layers.embeddings import Embedding
from kerasify import export_model
np.set_printoptions(precision=25, threshold=np.nan)
def c_array(a):
s = pprint.pformat(a.flatten())
s = s.replace('[', '{').replace(']', '}').replace('array(', '').replace(')', '').replace(', dtype=float32', '')
shape = ''
if a.shape == ():
s = '{%s}' % s
shape = '(1)'
else:
shape = repr(a.shape).replace(',)', ')')
return shape, s
TEST_CASE = '''
bool test_%s(double* load_time, double* apply_time)
{
printf("TEST %s\\n");
KASSERT(load_time, "Invalid double");
KASSERT(apply_time, "Invalid double");
Tensor in%s;
in.data_ = %s;
Tensor out%s;
out.data_ = %s;
KerasTimer load_timer;
load_timer.Start();
KerasModel model;
KASSERT(model.LoadModel("test_%s.model"), "Failed to load model");
*load_time = load_timer.Stop();
KerasTimer apply_timer;
apply_timer.Start();
Tensor predict = out;
KASSERT(model.Apply(&in, &out), "Failed to apply");
*apply_time = apply_timer.Stop();
for (int i = 0; i < out.dims_[0]; i++)
{
KASSERT_EQ(out(i), predict(i), %s);
}
return true;
}
'''
def output_testcase(model, test_x, test_y, name, eps):
print("Processing %s" % name)
model.compile(loss='mean_squared_error', optimizer='adamax')
model.fit(test_x, test_y, nb_epoch=1, verbose=False)
predict_y = model.predict(test_x).astype('f')
print(model.summary())
export_model(model, 'test_%s.model' % name)
with open('test_%s.h' % name, 'w') as f:
x_shape, x_data = c_array(test_x[0])
y_shape, y_data = c_array(predict_y[0])
f.write(TEST_CASE % (name, name, x_shape, x_data, y_shape, y_data, name, eps))
''' Dense 1x1 '''
test_x = np.arange(10)
test_y = test_x * 10 + 1
model = Sequential()
model.add(Dense(1, input_dim=1))
output_testcase(model, test_x, test_y, 'dense_1x1', '1e-6')
''' Dense 10x1 '''
test_x = np.random.rand(10, 10).astype('f')
test_y = np.random.rand(10).astype('f')
model = Sequential()
model.add(Dense(1, input_dim=10))
output_testcase(model, test_x, test_y, 'dense_10x1', '1e-6')
''' Dense 2x2 '''
test_x = np.random.rand(10, 2).astype('f')
test_y = np.random.rand(10).astype('f')
model = Sequential()
model.add(Dense(2, input_dim=2))
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'dense_2x2', '1e-6')
''' Dense 10x10 '''
test_x = np.random.rand(10, 10).astype('f')
test_y = np.random.rand(10).astype('f')
model = Sequential()
model.add(Dense(10, input_dim=10))
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'dense_10x10', '1e-6')
''' Dense 10x10x10 '''
test_x = np.random.rand(10, 10).astype('f')
test_y = np.random.rand(10, 10).astype('f')
model = Sequential()
model.add(Dense(10, input_dim=10))
model.add(Dense(10))
output_testcase(model, test_x, test_y, 'dense_10x10x10', '1e-6')
''' Conv 2x2 '''
test_x = np.random.rand(10, 1, 2, 2).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(Convolution2D(1, 2, 2, input_shape=(1, 2, 2)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'conv_2x2', '1e-6')
''' Conv 3x3 '''
test_x = np.random.rand(10, 1, 3, 3).astype('f').astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(Convolution2D(1, 3, 3, input_shape=(1, 3, 3)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'conv_3x3', '1e-6')
''' Conv 3x3x3 '''
test_x = np.random.rand(10, 3, 10, 10).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(Convolution2D(3, 3, 3, input_shape=(3, 10, 10)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'conv_3x3x3', '1e-6')
''' Activation ELU '''
test_x = np.random.rand(1, 10).astype('f')
test_y = np.random.rand(1, 1).astype('f')
model = Sequential()
model.add(Dense(10, input_dim=10))
model.add(ELU(alpha=0.5))
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'elu_10', '1e-6')
''' Activation relu '''
test_x = np.random.rand(1, 10).astype('f')
test_y = np.random.rand(1, 10).astype('f')
model = Sequential()
model.add(Dense(10, input_dim=10))
model.add(Activation('relu'))
output_testcase(model, test_x, test_y, 'relu_10', '1e-6')
''' Dense relu '''
test_x = np.random.rand(1, 10).astype('f')
test_y = np.random.rand(1, 10).astype('f')
model = Sequential()
model.add(Dense(10, input_dim=10, activation='relu'))
model.add(Dense(10, input_dim=10, activation='relu'))
model.add(Dense(10, input_dim=10, activation='relu'))
output_testcase(model, test_x, test_y, 'dense_relu_10', '1e-6')
''' Dense relu '''
test_x = np.random.rand(1, 10).astype('f')
test_y = np.random.rand(1, 10).astype('f')
model = Sequential()
model.add(Dense(10, input_dim=10, activation='tanh'))
model.add(Dense(10, input_dim=10, activation='tanh'))
model.add(Dense(10, input_dim=10, activation='tanh'))
output_testcase(model, test_x, test_y, 'dense_tanh_10', '1e-6')
''' Conv softplus '''
test_x = np.random.rand(10, 1, 2, 2).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(Convolution2D(1, 2, 2, input_shape=(1, 2, 2), activation='softplus'))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'conv_softplus_2x2', '1e-6')
''' Conv hardsigmoid '''
test_x = np.random.rand(10, 1, 2, 2).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(Convolution2D(1, 2, 2, input_shape=(1, 2, 2), activation='hard_sigmoid'))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'conv_hard_sigmoid_2x2', '1e-6')
''' Conv sigmoid '''
test_x = np.random.rand(10, 1, 2, 2).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(Convolution2D(1, 2, 2, input_shape=(1, 2, 2), activation='sigmoid'))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'conv_sigmoid_2x2', '1e-6')
''' Maxpooling2D 1x1'''
test_x = np.random.rand(10, 1, 10, 10).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(MaxPooling2D(pool_size=(1, 1), input_shape=(1, 10, 10)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'maxpool2d_1x1', '1e-6')
''' Maxpooling2D 2x2'''
test_x = np.random.rand(10, 1, 10, 10).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(MaxPooling2D(pool_size=(2, 2), input_shape=(1, 10, 10)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'maxpool2d_2x2', '1e-6')
''' Maxpooling2D 3x2x2'''
test_x = np.random.rand(10, 3, 10, 10).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(MaxPooling2D(pool_size=(2, 2), input_shape=(3, 10, 10)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'maxpool2d_3x2x2', '1e-6')
''' Maxpooling2D 3x3x3'''
test_x = np.random.rand(10, 3, 10, 10).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(MaxPooling2D(pool_size=(3, 3), input_shape=(3, 10, 10)))
model.add(Flatten())
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'maxpool2d_3x3x3', '1e-6')
''' LSTM simple 7x20 '''
test_x = np.random.rand(10, 7, 20).astype('f')
test_y = np.random.rand(10, 3).astype('f')
model = Sequential()
model.add(LSTM(3, return_sequences=False, input_shape=(7, 20)))
output_testcase(model, test_x, test_y, 'lstm_simple_7x20', '1e-6')
''' LSTM simple stacked 20x9 '''
test_x = np.random.rand(10, 20, 9).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(LSTM(32, return_sequences=False, input_shape=(20, 9)))
model.add(Dense(3, input_dim=32, activation='tanh'))
model.add(Dense(1))
output_testcase(model, test_x, test_y, 'lstm_simple_stacked20x9', '1e-6')
''' LSTM stacked 150x83 '''
test_x = np.random.rand(10, 150, 83).astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(150, 83)))
model.add(LSTM(32, return_sequences=False))
model.add(Dense(1, activation='sigmoid'))
output_testcase(model, test_x, test_y, 'lstm_stacked150x83', '1e-6')
''' Embedding 64 '''
np.random.seed(10)
test_x = np.random.randint(100, size=(32, 10)).astype('f')
test_y = np.random.rand(32, 20).astype('f')
model = Sequential()
model.add(Embedding(100, 64, input_length=10))
model.add(Flatten())
#model.add(Dropout(0.5))
model.add(Dense(20, activation='sigmoid'))
output_testcase(model, test_x, test_y, 'embedding64', '1e-6')
''' Benchmark '''
test_x = np.random.rand(1, 3, 128, 128).astype('f')
test_y = np.random.rand(1, 10).astype('f')
model = Sequential()
model.add(Convolution2D(16, 7, 7, input_shape=(3, 128, 128), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(ELU())
model.add(Convolution2D(8, 3, 3))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(10))
output_testcase(model, test_x, test_y, 'benchmark', '1e-3')