|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
1 | 4 | import pytest
|
| 5 | +from tensorflow.keras.layers import GlobalAveragePooling1D, GlobalAveragePooling2D, GlobalMaxPooling1D, GlobalMaxPooling2D |
2 | 6 | from tensorflow.keras.models import Sequential
|
3 |
| -from tensorflow.keras.layers import GlobalAveragePooling1D, GlobalMaxPooling1D, GlobalAveragePooling2D, GlobalMaxPooling2D |
4 |
| -import numpy as np |
| 7 | + |
5 | 8 | import hls4ml
|
6 |
| -from pathlib import Path |
7 | 9 |
|
8 | 10 | test_root_path = Path(__file__).parent
|
9 | 11 |
|
10 | 12 | in_shape = 18
|
11 | 13 | in_filt = 6
|
12 | 14 | atol = 5e-3
|
13 | 15 |
|
| 16 | + |
14 | 17 | @pytest.fixture(scope='module')
|
15 | 18 | def data_1d():
|
16 | 19 | return np.random.rand(100, in_shape, in_filt)
|
17 | 20 |
|
18 |
| -@pytest.fixture(scope='module') |
19 |
| -def keras_model_max_1d(): |
20 |
| - model = Sequential() |
21 |
| - model.add(GlobalMaxPooling1D(input_shape=(in_shape, in_filt))) |
22 |
| - model.compile() |
23 |
| - return model |
24 | 21 |
|
25 | 22 | @pytest.fixture(scope='module')
|
26 |
| -def keras_model_avg_1d(): |
| 23 | +def keras_model_1d(request): |
| 24 | + model_type = request.param['model_type'] |
| 25 | + keepdims = request.param['keepdims'] |
27 | 26 | model = Sequential()
|
28 |
| - model.add(GlobalAveragePooling1D(input_shape=(in_shape, in_filt))) |
| 27 | + if model_type == 'avg': |
| 28 | + model.add(GlobalAveragePooling1D(input_shape=(in_shape, in_filt), keepdims=keepdims)) |
| 29 | + elif model_type == 'max': |
| 30 | + model.add(GlobalMaxPooling1D(input_shape=(in_shape, in_filt), keepdims=keepdims)) |
29 | 31 | model.compile()
|
30 |
| - return model |
31 |
| - |
| 32 | + return model, model_type, keepdims |
| 33 | + |
32 | 34 |
|
33 | 35 | @pytest.mark.parametrize('backend', ['Quartus', 'Vivado'])
|
34 |
| -@pytest.mark.parametrize('model_type', ['max', 'avg']) |
| 36 | +@pytest.mark.parametrize( |
| 37 | + 'keras_model_1d', |
| 38 | + [ |
| 39 | + {'model_type': 'max', 'keepdims': True}, |
| 40 | + {'model_type': 'max', 'keepdims': False}, |
| 41 | + {'model_type': 'avg', 'keepdims': True}, |
| 42 | + {'model_type': 'avg', 'keepdims': False}, |
| 43 | + ], |
| 44 | + ids=[ |
| 45 | + 'model_type-max-keepdims-True', |
| 46 | + 'model_type-max-keepdims-False', |
| 47 | + 'model_type-avg-keepdims-True', |
| 48 | + 'model_type-avg-keepdims-False', |
| 49 | + ], |
| 50 | + indirect=True, |
| 51 | +) |
35 | 52 | @pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream'])
|
36 |
| -def test_global_pool1d(backend, keras_model_max_1d, keras_model_avg_1d, data_1d, model_type, io_type): |
37 |
| - if model_type == 'avg': |
38 |
| - model = keras_model_avg_1d |
39 |
| - else: |
40 |
| - model = keras_model_max_1d |
41 |
| - |
| 53 | +def test_global_pool1d(backend, keras_model_1d, data_1d, io_type): |
| 54 | + |
| 55 | + model, model_type, keepdims = keras_model_1d |
| 56 | + |
42 | 57 | config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,9>', granularity='name')
|
43 | 58 |
|
44 |
| - hls_model = hls4ml.converters.convert_from_keras_model(model, |
45 |
| - hls_config=config, |
46 |
| - io_type=io_type, |
47 |
| - output_dir=str(test_root_path / f'hls4mlprj_globalplool1d_{backend}_{io_type}_{model_type}'), |
48 |
| - backend=backend) |
| 59 | + hls_model = hls4ml.converters.convert_from_keras_model( |
| 60 | + model, |
| 61 | + hls_config=config, |
| 62 | + io_type=io_type, |
| 63 | + output_dir=str(test_root_path / f'hls4mlprj_globalplool1d_{backend}_{io_type}_{model_type}_keepdims{keepdims}'), |
| 64 | + backend=backend, |
| 65 | + ) |
49 | 66 | hls_model.compile()
|
50 |
| - |
51 |
| - y_keras = np.squeeze(model.predict(data_1d)) |
52 |
| - y_hls = hls_model.predict(data_1d) |
| 67 | + |
| 68 | + y_keras = model.predict(data_1d) |
| 69 | + y_hls = hls_model.predict(data_1d).reshape(y_keras.shape) |
53 | 70 | np.testing.assert_allclose(y_keras, y_hls, rtol=0, atol=atol, verbose=True)
|
54 | 71 |
|
| 72 | + |
55 | 73 | @pytest.fixture(scope='module')
|
56 | 74 | def data_2d():
|
57 | 75 | return np.random.rand(100, in_shape, in_shape, in_filt)
|
58 | 76 |
|
59 |
| -@pytest.fixture(scope='module') |
60 |
| -def keras_model_max_2d(): |
61 |
| - model = Sequential() |
62 |
| - model.add(GlobalMaxPooling2D(input_shape=(in_shape, in_shape, in_filt))) |
63 |
| - model.compile() |
64 |
| - return model |
65 | 77 |
|
66 | 78 | @pytest.fixture(scope='module')
|
67 |
| -def keras_model_avg_2d(): |
| 79 | +def keras_model_2d(request): |
| 80 | + model_type = request.param['model_type'] |
| 81 | + keepdims = request.param['keepdims'] |
68 | 82 | model = Sequential()
|
69 |
| - model.add(GlobalAveragePooling2D(input_shape=(in_shape, in_shape, in_filt))) |
| 83 | + if model_type == 'avg': |
| 84 | + model.add(GlobalAveragePooling2D(input_shape=(in_shape, in_shape, in_filt), keepdims=keepdims)) |
| 85 | + elif model_type == 'max': |
| 86 | + model.add(GlobalMaxPooling2D(input_shape=(in_shape, in_shape, in_filt), keepdims=keepdims)) |
70 | 87 | model.compile()
|
71 |
| - return model |
| 88 | + return model, model_type, keepdims |
| 89 | + |
72 | 90 |
|
73 | 91 | @pytest.mark.parametrize('backend', ['Quartus', 'Vivado'])
|
74 |
| -@pytest.mark.parametrize('model_type', ['max', 'avg']) |
| 92 | +@pytest.mark.parametrize( |
| 93 | + 'keras_model_2d', |
| 94 | + [ |
| 95 | + {'model_type': 'max', 'keepdims': True}, |
| 96 | + {'model_type': 'max', 'keepdims': False}, |
| 97 | + {'model_type': 'avg', 'keepdims': True}, |
| 98 | + {'model_type': 'avg', 'keepdims': False}, |
| 99 | + ], |
| 100 | + ids=[ |
| 101 | + 'model_type-max-keepdims-True', |
| 102 | + 'model_type-max-keepdims-False', |
| 103 | + 'model_type-avg-keepdims-True', |
| 104 | + 'model_type-avg-keepdims-False', |
| 105 | + ], |
| 106 | + indirect=True, |
| 107 | +) |
75 | 108 | @pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream'])
|
76 |
| -def test_global_pool2d(backend, keras_model_max_2d, keras_model_avg_2d, data_2d, model_type, io_type): |
77 |
| - |
78 |
| - if model_type == 'avg': |
79 |
| - model = keras_model_avg_2d |
80 |
| - else: |
81 |
| - model = keras_model_max_2d |
82 |
| - |
| 109 | +def test_global_pool2d(backend, keras_model_2d, data_2d, io_type): |
| 110 | + |
| 111 | + model, model_type, keepdims = keras_model_2d |
| 112 | + |
83 | 113 | config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,9>', granularity='name')
|
84 | 114 |
|
85 |
| - hls_model = hls4ml.converters.convert_from_keras_model(model, |
86 |
| - hls_config=config, |
87 |
| - io_type=io_type, |
88 |
| - output_dir=str(test_root_path / f'hls4mlprj_globalplool2d_{backend}_{io_type}_{model_type}'), |
89 |
| - backend=backend) |
| 115 | + hls_model = hls4ml.converters.convert_from_keras_model( |
| 116 | + model, |
| 117 | + hls_config=config, |
| 118 | + io_type=io_type, |
| 119 | + output_dir=str(test_root_path / f'hls4mlprj_globalplool2d_{backend}_{io_type}_{model_type}_keepdims{keepdims}'), |
| 120 | + backend=backend, |
| 121 | + ) |
90 | 122 | hls_model.compile()
|
91 |
| - |
92 |
| - y_keras = np.squeeze(model.predict(data_2d)) |
93 |
| - y_hls = hls_model.predict(data_2d) |
| 123 | + |
| 124 | + y_keras = model.predict(data_2d) |
| 125 | + y_hls = hls_model.predict(data_2d).reshape(y_keras.shape) |
94 | 126 | np.testing.assert_allclose(y_keras, y_hls, rtol=0, atol=atol, verbose=True)
|
0 commit comments