Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: 'batch_input_shape' #1135

Closed
4 tasks done
franhervas99 opened this issue Nov 21, 2024 · 4 comments
Closed
4 tasks done

KeyError: 'batch_input_shape' #1135

franhervas99 opened this issue Nov 21, 2024 · 4 comments
Labels

Comments

@franhervas99
Copy link

Prerequisites

Please make sure to check off these prerequisites before submitting a bug report.

  • Version 0.8.1 from pip.
  • Check that the issue hasn't already been reported, by checking the currently open issues.
  • If there are steps to reproduce the problem, make sure to write them down below.
  • If relevant, please include the hls4ml project files, which were created directly before and/or after the bug.

Quick summary

I have a keras model which has, in the input layer, in config key, a key that is "batch_shape", but when I run config_from_keras_model function, an error is thrown "KeyError: 'batch_input_shape'". Changing the lines 11 and 21 from core.py file, to be batch_shape instead of batch_input_shape, fixes the error.

Details

Please add to the following sections to describe the bug as accurately as possible.

Steps to Reproduce

  1. Download the model:
    test_model.zip
  2. Run typical script to get the config:
keras_model = keras.models.load_model(model_path)
config = hls4ml.utils.config_from_keras_model(keras_model, granularity='name')

Expected behavior

Generate the model config for converting the model to HLS.

Actual behavior

An error is thrown:

Traceback (most recent call last):
  File "/home/fran/work_repository/machine_learning_fpga/python/hls4ml_dev.py", line 40, in <module>
    config = hls4ml.utils.config_from_keras_model(keras_model, granularity='name')
  File "/home/fran/.local/lib/python3.10/site-packages/hls4ml/utils/config.py", line 159, in config_from_keras_model
    layer_list, _, _, _ = hls4ml.converters.parse_keras_model(model_arch, reader)
  File "/home/fran/.local/lib/python3.10/site-packages/hls4ml/converters/keras_to_hls.py", line 288, in parse_keras_model
    layer, output_shape = layer_handlers[keras_class](keras_layer, input_names, input_shapes, reader)
  File "/home/fran/.local/lib/python3.10/site-packages/hls4ml/converters/keras/core.py", line 11, in parse_input_layer
    layer['input_shape'] = keras_layer['config']['batch_input_shape'][1:]
KeyError: 'batch_input_shape'

Optional

Possible fix

  • Go to this file: hls4ml/converters/keras/core.py
  • Edit the line 11 from layer['input_shape'] = keras_layer['config']['batch_input_shape'][1:] to layer['input_shape'] = keras_layer['config']['batch_shape'][1:]
  • Edit the line 21 from output_shape = keras_layer['config']['batch_input_shape'] to output_shape = keras_layer['config']['batch_shape']
@vloncar
Copy link
Contributor

vloncar commented Nov 21, 2024

Same answer as #1133. 0.8.1 is really old by now and there are significant improvements in the current main branch. You can install it with pip install git+https://github.com/fastmachinelearning/hls4ml.git

@franhervas99
Copy link
Author

Updating to the last version on main branch gives me the same error.

@vloncar
Copy link
Contributor

vloncar commented Nov 22, 2024

Upon investigation, this is caused by your environment being based on new TF and Keras v3, which are not supported. There's some progress on v3 support in #1116. To install usable hls4ml from the main branch, you will need python 3.10 or 3.11 and TF 2.8 to 2.14. See #1136 for a list of dependencies of the current version.

@franhervas99
Copy link
Author

Hello @vloncar ,
Chaning TF version to 2.12 solves the issue. Thank you so much for your solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants