|
| 1 | +import pytest |
| 2 | +import hls4ml |
| 3 | +import tensorflow as tf |
| 4 | +import numpy as np |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +test_root_path = Path(__file__).parent |
| 9 | + |
| 10 | +test_root_path = Path('/tmp') |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize('backend', ['Vivado', 'Quartus']) |
| 14 | +def test_keras_h5_loader(backend): |
| 15 | + input_shape = (10,) |
| 16 | + model = tf.keras.models.Sequential([ |
| 17 | + tf.keras.layers.InputLayer(input_shape=input_shape), |
| 18 | + tf.keras.layers.Activation(activation='relu'), |
| 19 | + ]) |
| 20 | + |
| 21 | + hls_config = hls4ml.utils.config_from_keras_model(model, granularity='name') |
| 22 | + |
| 23 | + config = {'OutputDir': 'KerasH5_loader_test', |
| 24 | + 'ProjectName': 'KerasH5_loader_test', |
| 25 | + 'Backend': backend, |
| 26 | + 'ClockPeriod': 25.0, |
| 27 | + 'IOType': 'io_parallel', |
| 28 | + 'HLSConfig': hls_config, |
| 29 | + 'KerasH5': str(test_root_path / 'KerasH5_loader_test.h5'), |
| 30 | + 'output_dir': str(test_root_path / 'KerasH5_loader_test')} |
| 31 | + |
| 32 | + model.save(config['KerasH5']) |
| 33 | + hls_model = hls4ml.converters.keras_to_hls(config) |
| 34 | + hls_model.compile() |
| 35 | + data = np.random.rand(1000, 10).astype(np.float32) |
| 36 | + pred = hls_model.predict(data) |
| 37 | + np.testing.assert_allclose(pred, model.predict(data), rtol=5e-3, atol=5e-3) |
0 commit comments