-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_keras_api.py
483 lines (407 loc) · 25.6 KB
/
test_keras_api.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import pytest
import hls4ml
import tensorflow as tf
import numpy as np
from tensorflow.keras import optimizers
from tensorflow.keras.layers import Input, Dense, Activation, Conv1D, Conv2D, \
Reshape, ELU, LeakyReLU, ThresholdedReLU, \
PReLU, BatchNormalization, Add, Subtract, \
Multiply, Average, Maximum, Minimum, Concatenate, \
MaxPooling1D, MaxPooling2D, AveragePooling1D, \
AveragePooling2D
import math
from tensorflow.keras import backend as K
# ALMOST DONE
# TODO Consider BinaryDense ve TernaryDense layers
def test_dense():
model = tf.keras.models.Sequential()
model.add(Dense(2,
input_shape=(1,),
name='Dense',
use_bias=True,
kernel_initializer= tf.keras.initializers.RandomUniform(minval=1, maxval=10),
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None))
model.add(Activation(activation='elu', name='Activation'))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(1,)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
assert round(np.average(np.subtract(np.abs(keras_prediction), np.abs(hls_prediction)))) < 3
assert len(model.layers) + 1 == len(hls_model.get_layers())
assert list(hls_model.get_layers())[0].attributes['class_name'] == "InputLayer"
assert list(hls_model.get_layers())[1].attributes["class_name"] == model.layers[0]._name
assert list(hls_model.get_layers())[2].attributes['class_name'] == model.layers[1]._name
assert list(hls_model.get_layers())[0].attributes['input_shape'] == list(model.layers[0].input_shape[1:])
assert list(hls_model.get_layers())[1].attributes['n_in'] == model.layers[0].input_shape[1:][0]
assert list(hls_model.get_layers())[1].attributes['n_out'] == model.layers[0].output_shape[1:][0]
assert list(hls_model.get_layers())[2].attributes['activation'] == str(model.layers[1].activation).split()[1]
assert list(hls_model.get_layers())[1].attributes['activation'] == str(model.layers[0].activation).split()[1]
# # DONE
# def test_reshape():
# model = tf.keras.models.Sequential()
# model.add(Dense(12,
# input_shape=(1,),
# name='Dense',
# use_bias=True,
# kernel_initializer= tf.keras.initializers.RandomUniform(minval=1, maxval=10),
# bias_initializer='zeros',
# kernel_regularizer=None,
# bias_regularizer=None,
# activity_regularizer=None,
# kernel_constraint=None,
# bias_constraint=None))
# model.add(Reshape((3,4)))
# model.add(Activation(activation="elu", name='Activation'))
# model.compile(optimizer='adam', loss='mse')
# # X_input = np.random.rand(12,)
# X_input = np.ndarray(shape=(12,), dtype=float)
# keras_prediction = model.predict(X_input)
# config = hls4ml.utils.config_from_keras_model(model)
# hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
# hls_model.compile()
# hls_prediction = hls_model.predict(X_input)
# # assert round(np.average(np.subtract(np.abs(keras_prediction), np.abs(hls_prediction)))) < 3
# # hls_model = hls4ml.converters.convert_from_keras_model(model)
# assert len(model.layers) + 1 == len(hls_model.get_layers())
# assert list(hls_model.get_layers())[2].attributes["name"] == model.layers[1]._name
# assert list(hls_model.get_layers())[2].attributes['target_shape'] == list(model.layers[1].target_shape)
# DONE
keras_activation_functions = [LeakyReLU, ELU]
@pytest.mark.parametrize("activation_functions", keras_activation_functions)
def test_activation_leakyrelu_elu(activation_functions):
model = tf.keras.models.Sequential()
model.add(Dense(64,
input_shape=(1,),
name='Dense',
kernel_initializer='lecun_uniform',
kernel_regularizer=None))
model.add(activation_functions(alpha=1.0))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(1)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
assert round(np.average(np.subtract(np.abs(keras_prediction), np.abs(hls_prediction)))) < 3
assert len(model.layers) + 1 == len(hls_model.get_layers())
if activation_functions == 'ELU':
assert list(hls_model.get_layers())[2].attributes['class_name'] == 'ELU'
elif activation_functions == 'LeakyReLU':
assert list(hls_model.get_layers())[2].attributes['class_name'] == 'LeakyReLU'
# DONE
keras_activation_functions = [ThresholdedReLU]
@pytest.mark.parametrize("activation_functions", keras_activation_functions)
def test_activation_thresholdedrelu(activation_functions):
model = tf.keras.models.Sequential()
model.add(Dense(64,
input_shape=(1,),
name='Dense',
kernel_initializer='lecun_uniform',
kernel_regularizer=None))
model.add(activation_functions(theta=1.0))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(1,)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
assert round(np.average(np.subtract(np.abs(keras_prediction), np.abs(hls_prediction)))) < 3
assert len(model.layers) + 1 == len(hls_model.get_layers())
if activation_functions == 'ThresholdedReLU':
assert list(hls_model.get_layers())[2].attributes['class_name'] == 'ThresholdedReLU'
# DONE
keras_activation_functions = [PReLU]
@pytest.mark.parametrize("activation_functions", keras_activation_functions)
def test_activation_prelu(activation_functions):
model = tf.keras.models.Sequential()
model.add(Dense(64,
input_shape=(1,),
name='Dense',
kernel_initializer='lecun_uniform',
kernel_regularizer=None))
model.add(activation_functions(alpha_initializer="zeros",))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(1,)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
assert round(np.average(np.subtract(np.abs(keras_prediction), np.abs(hls_prediction)))) < 3
assert len(model.layers) + 1 == len(hls_model.get_layers())
if activation_functions == 'PReLU':
assert list(hls_model.get_layers())[2].attributes['class_name'] == 'PReLU'
# DONE
keras_activation_functions = [Activation]
@pytest.mark.parametrize("activation_functions", keras_activation_functions)
def test_activation(activation_functions):
model = tf.keras.models.Sequential()
model.add(Dense(64,
input_shape=(1,),
name='Dense',
kernel_initializer='lecun_uniform',
kernel_regularizer=None))
model.add(Activation(activation='relu', name='Activation'))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(1,)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
assert round(np.average(np.subtract(np.abs(keras_prediction), np.abs(hls_prediction)))) < 3
assert len(model.layers) + 1 == len(hls_model.get_layers())
if activation_functions == 'Activation':
assert list(hls_model.get_layers())[2].attributes["activation"] == str(model.layers[1].activation).split()[1]
# DONE
keras_conv1d = [Conv1D]
padds_options = ['same', 'valid']
@pytest.mark.parametrize("conv1d", keras_conv1d)
@pytest.mark.parametrize("padds", padds_options)
def test_conv1d(conv1d, padds):
model = tf.keras.models.Sequential()
input_shape = (10, 128, 4)
model.add(conv1d(filters=32,
kernel_size=3,
strides=1,
padding=padds,
activation='relu',
input_shape=input_shape[1:],
kernel_initializer='normal',
use_bias=False,
data_format='channels_last'))
model.add(Activation(activation='relu'))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(10, 128,4)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
assert len(model.layers) + 2 == len(hls_model.get_layers())
assert list(hls_model.get_layers())[1].attributes['name'] == model.layers[0]._name
if conv1d == 'Conv1D':
assert list(hls_model.get_layers())[1].attributes['class_name'] == 'Conv1D'
assert list(hls_model.get_layers())[1].attributes['activation'] == str(model.layers[0].activation).split()[1]
assert list(hls_model.get_layers())[1].attributes["n_in"] == model.layers[0]._batch_input_shape[1]
assert list(hls_model.get_layers())[1].attributes['filt_width'] == model.layers[0].kernel_size[0]
assert list(hls_model.get_layers())[1].attributes['n_chan'] == model.layers[0].input_shape[2]
assert list(hls_model.get_layers())[1].attributes['n_filt'] == model.layers[0].filters
assert list(hls_model.get_layers())[1].attributes['stride'] == model.layers[0].strides[0]
assert list(hls_model.get_layers())[1].attributes['padding'] == model.layers[0].padding
assert list(hls_model.get_layers())[1].attributes['data_format'] == model.layers[0].data_format
assert list(hls_model.get_layers())[1].attributes["n_out"] == list(model.layers[0].output_shape)[1]
out_width = math.ceil(float(model.layers[0]._batch_input_shape[2]) / float(model.layers[0].strides[0]))
pad_along_width = max((out_width - 1) * model.layers[0].strides[0] + model.layers[0].kernel_size[0] - model.layers[0]._batch_input_shape[2], 0)
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left
out_valid = math.ceil(float(model.layers[0]._batch_input_shape[1] - model.layers[0].kernel_size[0] + 1) / float(model.layers[0].strides[0]))
if model.layers[0].padding == 'same':
assert list(hls_model.get_layers())[1].attributes['pad_left'] == pad_left
assert list(hls_model.get_layers())[1].attributes['pad_right'] == pad_right
elif model.layers[0].padding == 'valid':
assert list(hls_model.get_layers())[1].attributes['pad_left'] == 0
assert list(hls_model.get_layers())[1].attributes['pad_right'] == 0
# # DONE
# keras_conv2d = [Conv2D]
# padds_options = ['same', 'valid']
# chans_options = ['channels_first', 'channels_last']
# @pytest.mark.parametrize("conv2d", keras_conv2d)
# @pytest.mark.parametrize("chans", chans_options)
# @pytest.mark.parametrize("padds", padds_options)
# def test_conv2d(conv2d, chans, padds):
# model = tf.keras.models.Sequential()
# input_shape = (4, 4, 28, 30)
# model.add(conv2d(filters=32,
# kernel_size=(4,4),
# strides=(4,4),
# padding=padds,
# activation='relu',
# input_shape=input_shape[1:],
# kernel_initializer='normal',
# use_bias=False,
# data_format=chans
# ))
# model.add(Activation(activation='relu'))
# model.compile(optimizer='adam', loss='mse')
# X_input = np.random.rand(4, 4, 28, 30)
# keras_prediction = model.predict(X_input)
# config = hls4ml.utils.config_from_keras_model(model)
# hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
# hls_model.compile()
# hls_prediction = hls_model.predict(X_input)
# # hls_model = hls4ml.converters.convert_from_keras_model(model)
# assert len(model.layers) + 2 == len(hls_model.get_layers())
# assert list(hls_model.get_layers())[1].attributes['name'] == model.layers[0]._name
# if conv2d == 'Conv2D':
# assert list(hls_model.get_layers())[1].attributes['class_name'] == 'Conv2D'
# assert list(hls_model.get_layers())[1].attributes['activation'] == str(model.layers[0].activation).split()[1]
# assert list(hls_model.get_layers())[1].attributes['filt_width'] == model.layers[0].kernel_size[1]
# assert list(hls_model.get_layers())[1].attributes['filt_height'] == model.layers[0].kernel_size[0]
# assert list(hls_model.get_layers())[1].attributes['n_filt'] == model.layers[0].filters
# assert list(hls_model.get_layers())[1].attributes['stride_width'] == model.layers[0].strides[1]
# assert list(hls_model.get_layers())[1].attributes['stride_height'] == model.layers[0].strides[0]
# assert list(hls_model.get_layers())[1].attributes['padding'] == model.layers[0].padding
# assert list(hls_model.get_layers())[1].attributes['data_format'] == model.layers[0].data_format
# if model.layers[0].data_format == 'channels_first':
# assert list(hls_model.get_layers())[1].attributes['n_chan'] == model.layers[0]._batch_input_shape[1]
# assert list(hls_model.get_layers())[1].attributes['in_height'] == model.layers[0]._batch_input_shape[2]
# assert list(hls_model.get_layers())[1].attributes['in_width'] == model.layers[0]._batch_input_shape[3]
# assert list(hls_model.get_layers())[1].attributes['out_height'] == model.layers[0].output_shape[2]
# assert list(hls_model.get_layers())[1].attributes['out_width'] == model.layers[0].output_shape[3]
# elif model.layers[0].data_format == 'channels_last':
# assert list(hls_model.get_layers())[1].attributes['n_chan'] == model.layers[0]._batch_input_shape[3]
# assert list(hls_model.get_layers())[1].attributes['in_height'] == model.layers[0]._batch_input_shape[1]
# assert list(hls_model.get_layers())[1].attributes['in_width'] == model.layers[0]._batch_input_shape[2]
# assert list(hls_model.get_layers())[1].attributes['out_height'] == model.layers[0].output_shape[1]
# assert list(hls_model.get_layers())[1].attributes['out_width'] == model.layers[0].output_shape[2]
# if model.layers[0].padding =='same':
# if model.layers[0].data_format == 'channels_first':
# out_height = model.layers[0].output_shape[2]
# out_width = model.layers[0].output_shape[3]
# pad_along_height = max((out_height - 1) * model.layers[0].strides[0] + model.layers[0].kernel_size[0] - model.layers[0]._batch_input_shape[2], 0)
# pad_along_width = max((out_width - 1) * model.layers[0].strides[1] + model.layers[0].kernel_size[1] - model.layers[0]._batch_input_shape[3], 0)
# elif model.layers[0].data_format == 'channels_last':
# out_height = model.layers[0].output_shape[1]
# out_width = model.layers[0].output_shape[2]
# pad_along_height = max((out_height - 1) * model.layers[0].strides[0] + model.layers[0].kernel_size[0] - model.layers[0]._batch_input_shape[1], 0)
# pad_along_width = max((out_width - 1) * model.layers[0].strides[1] + model.layers[0].kernel_size[1] - model.layers[0]._batch_input_shape[2], 0)
# pad_top = pad_along_height // 2
# pad_bottom = pad_along_height - pad_top
# pad_left = pad_along_width // 2
# pad_right = pad_along_width - pad_left
# assert list(hls_model.get_layers())[1].attributes['pad_top'] == pad_top
# assert list(hls_model.get_layers())[1].attributes['pad_bottom'] == pad_bottom
# assert list(hls_model.get_layers())[1].attributes['pad_left'] == pad_left
# assert list(hls_model.get_layers())[1].attributes['pad_right'] == pad_right
# elif model.layers[0].padding =='valid':
# assert list(hls_model.get_layers())[1].attributes['pad_top'] == 0
# assert list(hls_model.get_layers())[1].attributes['pad_bottom'] == 0
# assert list(hls_model.get_layers())[1].attributes['pad_left'] == 0
# assert list(hls_model.get_layers())[1].attributes['pad_right'] == 0
# DONE
pooling_layers = [MaxPooling1D, MaxPooling2D, AveragePooling1D, AveragePooling2D]
padds_options = ['same', 'valid']
chans_options = ['channels_first', 'channels_last']
@pytest.mark.parametrize("poolings", pooling_layers)
@pytest.mark.parametrize("padds", padds_options)
@pytest.mark.parametrize("chans", chans_options)
def test_pooling(poolings, padds, chans):
model = tf.keras.models.Sequential()
if poolings == 'MaxPooling2D' or poolings == 'AveragePooling2D':
model.add(Conv2D(1, (3,3), activation='relu', input_shape=(8, 8, 1)))
model.add(AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None))
model.add(MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(10, 128,4)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
for i in range(2):
assert list(hls_model.get_layers())[i + 3].attributes['name'] == model.layers[i + 1]._name
assert list(hls_model.get_layers())[i + 3].attributes['class_name'][-2] == str(2)
assert list(hls_model.get_layers())[i + 3].attributes['stride_height'] == model.layers[i + 1].strides[0]
assert list(hls_model.get_layers())[i + 3].attributes['stride_width'] == model.layers[i + 1].strides[1]
assert list(hls_model.get_layers())[i + 3].attributes['pool_height'] == model.layers[i + 1].pool_size[1]
assert list(hls_model.get_layers())[i + 3].attributes['pool_width'] == model.layers[i + 1].pool_size[0]
assert list(hls_model.get_layers())[i + 3].attributes['padding'] == model.layers[i + 1].padding
if list(hls_model.get_layers())[i + 3].attributes['data_format'] == 'channels_last':
assert list(hls_model.get_layers())[i + 3].attributes['in_height'] == model.layers[i + 1].input_shape[1]
assert list(hls_model.get_layers())[i + 3].attributes['in_width'] == model.layers[i + 1].input_shape[2]
assert list(hls_model.get_layers())[i + 3].attributes['n_filt'] == model.layers[i + 1].input_shape[3]
elif list(hls_model.get_layers())[i + 3].attributes['data_format'] == 'channels_first':
assert list(hls_model.get_layers())[i + 3].attributes['in_height'] == model.layers[i + 1].input_shape[2]
assert list(hls_model.get_layers())[i + 3].attributes['in_width'] == model.layers[i + 1].input_shape[3]
assert list(hls_model.get_layers())[i + 3].attributes['n_filt'] == model.layers[i + 1].input_shape[1]
if list(hls_model.get_layers())[i + 3].attributes['padding'] == 'same':
# Height
in_height = model.layers[i + 1].input_shape[1]
if model.layers[i + 1].data_format == 'channels_first':
in_height = model.layers[i + 1].input_shape[2]
out_height = int(math.ceil(float(in_height) / float(model.layers[i + 1].strides[0])))
assert out_height == list(hls_model.get_layers())[i + 3].attributes['out_height']
if in_height % model.layers[i + 1].strides[0] == 0:
pad_along_height = max(model.layers[i + 1].pool_size[1] - model.layers[i + 1].strides[0], 0)
else:
pad_along_height = max(model.layers[i + 1].pool_size[1] - (in_height % model.layers[i + 1].strides[0]), 0)
pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
assert pad_bottom == list(hls_model.get_layers())[i + 3].attributes['pad_bottom']
assert pad_top == list(hls_model.get_layers())[i + 3].attributes['pad_top']
# Width
in_width = model.layers[i + 1].input_shape[2]
if model.layers[i + 1].data_format == 'channels_first':
in_height = model.layers[1].input_shape[i + 3]
out_width = int(math.ceil(float(in_width) / float(model.layers[i + 1].strides[1])))
assert out_width == list(hls_model.get_layers())[i + 3].attributes['out_width']
if in_width % model.layers[i + 1].strides[1] == 0:
pad_along_width = max(model.layers[i + 1].pool_size[0] - model.layers[i + 1].strides[1], 0)
else:
pad_along_width = max(model.layers[i + 1].pool_size[0] - (in_width % model.layers[i + 1].strides[1]), 0)
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left
assert pad_left == list(hls_model.get_layers())[i + 3].attributes['pad_left']
assert pad_right == list(hls_model.get_layers())[i + 3].attributes['pad_right']
elif list(hls_model.get_layers())[i + 3].attributes['padding'] == 'valid':
if list(hls_model.get_layers())[i + 3].attributes['data_format'] == 'channels_first':
in_height = model.layers[i + 1].input_shape[2]
in_width = model.layers[i + 1].input_shape[3]
elif list(hls_model.get_layers())[i + 3].attributes['data_format'] == 'channels_last':
in_height = model.layers[i + 1].input_shape[1]
in_width = model.layers[i + 1].input_shape[2]
out_width = int(math.ceil(float(in_width - model.layers[i + 1].pool_size[0] + 1) / float(model.layers[i + 1].strides[1])))
out_height = int(math.ceil(float(in_height - model.layers[i + 1].pool_size[1] + 1) / float(model.layers[i + 1].strides[0])))
assert list(hls_model.get_layers())[i + 3].attributes['out_height'] == out_height
assert list(hls_model.get_layers())[i + 3].attributes['out_width'] == out_width
assert list(hls_model.get_layers())[i + 3].attributes['pad_top'] == 0
assert list(hls_model.get_layers())[i + 3].attributes['pad_bottom'] == 0
assert list(hls_model.get_layers())[i + 3].attributes['pad_left'] == 0
assert list(hls_model.get_layers())[i + 3].attributes['pad_right'] == 0
elif poolings == 'MaxPooling1D' or poolings == 'AveragePooling2D':
input_shape = (10, 128, 4)
model.add(Conv1D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu',
input_shape=input_shape[1:], kernel_initializer='normal', use_bias=False,
data_format='channels_last'))
model.add(MaxPooling1D(pool_size=2, strides=None, padding=padds, data_format=chans))
model.add(AveragePooling1D(pool_size=2, strides=None, padding=padds, data_format=chans))
model.compile(optimizer='adam', loss='mse')
X_input = np.random.rand(10, 128,4)
keras_prediction = model.predict(X_input)
config = hls4ml.utils.config_from_keras_model(model)
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config)
hls_model.compile()
hls_prediction = hls_model.predict(X_input)
for i in range(2):
assert list(hls_model.get_layers())[i + 3].attributes['name'] == model.layers[i + 1]._name
assert list(hls_model.get_layers())[i + 3].attributes['class_name'][-2] == str(1)
assert list(hls_model.get_layers())[i + 3].attributes['n_in'] == model.layers[i + 1].input_shape[1]
assert list(hls_model.get_layers())[i + 3].attributes['n_filt'] == model.layers[i + 1].input_shape[2]
assert list(hls_model.get_layers())[i + 3].attributes['pool_size'] == model.layers[i + 1].pool_size[0]
assert list(hls_model.get_layers())[i + 3].attributes['stride'] == model.layers[i + 1].strides[0]
assert list(hls_model.get_layers())[i + 3].attributes['padding'] == model.layers[i + 1].padding
out_same = math.ceil(float(model.layers[i + 1].input_shape[1]) / float(model.layers[i + 1].strides[0]))
out_valid = math.ceil(float(model.layers[i + 1].input_shape[1] - model.layers[i + 1].pool_size[0] + 1) / model.layers[i + 1].strides[0])
if list(hls_model.get_layers())[i + 3].attributes['padding'] == 'same':
assert list(hls_model.get_layers())[i + 3].attributes['n_out'] == out_same
if model.layers[i + 1].input_shape[1] % model.layers[i + 1].strides[0] == 0:
pad_along_width = max(model.layers[i + 1].pool_size[0] - model.layers[i + 1].strides[0], 0)
else:
pad_along_width = max(model.layers[i + 1].pool_size[0] - (model.layers[i + 1].input_shape[1] % model.layers[i + 1].strides[0]), 0)
assert list(hls_model.get_layers())[i + 3].attributes['pad_left'] == pad_along_width // 2
assert list(hls_model.get_layers())[i + 3].attributes['pad_right'] == pad_along_width - pad_along_width // 2
elif list(hls_model.get_layers())[i + 3].attributes['padding'] == 'valid':
assert list(hls_model.get_layers())[i + 3].attributes['n_out'] == out_valid
assert list(hls_model.get_layers())[i + 3].attributes['pad_left'] == 0
assert list(hls_model.get_layers())[i + 3].attributes['pad_right'] == 0