|
| 1 | +from hls4ml.converters.keras_to_hls import keras_to_hls |
| 2 | +import pytest |
| 3 | +import hls4ml |
| 4 | +import numpy as np |
| 5 | +from sklearn.metrics import accuracy_score |
| 6 | +import tensorflow as tf |
| 7 | +from tensorflow.keras.models import model_from_json |
| 8 | +import yaml |
| 9 | + |
| 10 | +@pytest.fixture(scope='module') |
| 11 | +def data(): |
| 12 | + X = np.random.rand(100,100,7) |
| 13 | + return X |
| 14 | + |
| 15 | +@pytest.fixture(scope='module') |
| 16 | +def keras_model(): |
| 17 | + jsons = open('../../example-models/keras/KERAS_conv1d.json','r').read() |
| 18 | + model = model_from_json(jsons) |
| 19 | + model.load_weights('../../example-models/keras/KERAS_conv1d_weights.h5') |
| 20 | + return model |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +@pytest.mark.parametrize('settings', [('io_parallel', 'latency'), |
| 24 | + ('io_parallel', 'resource'), |
| 25 | + ('io_stream', 'latency'), |
| 26 | + ('io_stream', 'resource')]) |
| 27 | +def hls_model(settings): |
| 28 | + io_type = settings[0] |
| 29 | + strategy = settings[1] |
| 30 | + config = hls4ml.converters.create_config(output_dir = 'hls4mlprj_conv1d_{}_{}'.format(io_type, strategy)) |
| 31 | + config['KerasJson'] = '../../example-models/keras/KERAS_conv1d.json' |
| 32 | + config['KerasH5'] = '../../example-models/keras/KERAS_conv1d_weights.h5' |
| 33 | + config['OutputDir'] = 'hls4mlprj_conv1d_{}_{}'.format(io_type, strategy) |
| 34 | + config['IOType'] = io_type |
| 35 | + |
| 36 | + hls_config = {'Model' : {'Strategy' : strategy, |
| 37 | + 'ReuseFactor' : 1, |
| 38 | + 'Precision' : 'ap_fixed<16,3,AP_RND_CONV,AP_SAT>'}} |
| 39 | + # Some model specific precision tuning |
| 40 | + config['LayerName'] = {} |
| 41 | + config['LayerName']['fc1_relu'] = {'Precision':{'weight' : 'ap_fixed<16,3>', 'result' : 'ap_fixed<16,6,AP_RND_CONV,AP_SAT>'}} |
| 42 | + config['LayerName']['output_softmax'] = {'Precision':{'weight' : 'ap_fixed<16,6>', 'result' : 'ap_fixed<16,6,AP_RND_CONV,AP_SAT>'}} |
| 43 | + config['LayerName']['output_softmax_softmax'] = {'Strategy':'Stable'} |
| 44 | + config['HLSConfig'] = hls_config |
| 45 | + hls_model = keras_to_hls(config) |
| 46 | + hls_model.compile() |
| 47 | + return hls_model |
| 48 | + |
| 49 | +@pytest.mark.parametrize('settings', [('io_parallel', 'latency'), |
| 50 | + ('io_parallel', 'resource'), |
| 51 | + ('io_stream', 'latency'), |
| 52 | + ('io_stream', 'resource')]) |
| 53 | +def test_accuracy(data, keras_model, hls_model): |
| 54 | + X = data |
| 55 | + model = keras_model |
| 56 | + # model under test predictions and accuracy |
| 57 | + y_keras = model.predict(X) |
| 58 | + y_hls4ml = hls_model.predict(X) |
| 59 | + # "accuracy" of hls4ml predictions vs keras |
| 60 | + rel_acc = accuracy_score(np.argmax(y_keras, axis=1), np.argmax(y_hls4ml, axis=1)) |
| 61 | + |
| 62 | + print('hls4ml accuracy relative to keras: {}'.format(rel_acc)) |
| 63 | + |
| 64 | + assert rel_acc > 0.98 |
0 commit comments