Search code examples
pythonctensorflowkeras

How to use keras2c?


I am using paper: Keras2c: A library for converting Keras neural networks to real-time compatible C to convert a model made in Keras to C. Because I need to compile it on hardware with little computational power. How API transforms? basically it loads the weights from the extension model (.h) and transforms it into three different files:

<function_name>.c that when called replicates the forward pass (inference) through the neural network

<function_name>.h is the header file associated with the generated source file, containing function declarations.

<function_name>_test_suite.c contains a main program to run sample inputs through the generated code to ensure that it produces the same outputs as the original python model.

Thus, the keras2c documentation shows its use. However, when I execute the following function:

from keras2c import k2c
k2c("classification_model.h5", "modelC", malloc=False, num_tests=10, verbose=True)

It displays the following error:

Traceback (most recent call last):
  File "c:\Users\dossa\Desktop\keras2c-master\convert.py", line 2, in <module>
    k2c("classification_model.h5", "modelC", malloc=False, num_tests=10, verbose=True)
  File "c:\Users\dossa\Desktop\keras2c-master\keras2c\keras2c_main.py", line 216, in k2c       
    check_model(model, function_name)
  File "c:\Users\dossa\Desktop\keras2c-master\keras2c\check_model.py", line 232, in check_model
    raise AssertionError(log)
AssertionError: The following errors were found: 
Functional' is not supported at this time. 

Has anyone used the API, could you help me solve this problem?


Solution

  • I'm not familiar with keras2c (cool idea though), but I can probably tell you why the error happens and how to fix it.

    Keras has two APIs: the functional API https://keras.io/guides/functional_api/ and the sequential API https://keras.io/guides/sequential_model/

    From the error message, it seems that keras2c only supports the sequential API.

    Try to run keras2c on a simple sequential model, and see if that works first:

    import keras
    from keras import layers
    model = keras.Sequential(
        [
            layers.Dense(2, activation="relu"),
            layers.Dense(3, activation="relu"),
            layers.Dense(4),
        ]
    )
    
    from keras2c import k2c
    k2c(model, "modelC", malloc=False, num_tests=10, verbose=True)
    

    If that works, you'd have to figure out if the model you really want to use can be rewritten as an equivalent sequential model.

    UPDATED It seems the error posted comes from this line https://github.com/f0uriest/keras2c/blob/708b7635d2dc7215a86ccd5d0bcb980be7672452/keras2c/check_model.py#L84 via https://github.com/f0uriest/keras2c/blob/708b7635d2dc7215a86ccd5d0bcb980be7672452/keras2c/check_model.py#L230

    where layer_type(layer) is just layer.__class__.__name__

    So basically the code runs into a layer where layer.__class__.__name__ is "Functional", checks whether it has functions to handle that (Weights2C._write_weights_Functional and Layer2C._write_layer_Functional), and upon not finding them raises an exception