cancel
Showing results for 
Search instead for 
Did you mean: 

Unrecognized keyword arguments: ['batch_shape'] with loading keras model in X-CUBE-AI

gosal
Associate III

I have this working keras model: https://www.dropbox.com/scl/fi/544ma5tsndw3cjv8djem0/model.keras?rlkey=87845alu8vki9y81kb7tjgrab&dl=1 and I am trying to load it into X-CUBE-AI 8.1.0 

 

When I hit Analyze I get this error: 

Neural Network Tools for STM32 family v1.7.0 (stm.ai v8.1.0-19520) 
E010(InvalidModelError): Couldn't load Keras model C:\[redacted]\model.keras, 
Error when deserializing class 'InputLayer' using config={'batch_shape': [None, 128, 173, 1], 'dtype': 'float32', 'sparse': False, 'name': 'input_layer_1'}. 
Exception encountered: Unrecognized keyword arguments: ['batch_shape']

How can I solve this?

1 ACCEPTED SOLUTION

Accepted Solutions
jean-michel.d
ST Employee

Hi Flare,

The referenced "model.keras" is a model generated and saved with a version of Keras 3 (3.0.5). In X-CUBE-AI, the version of TensorFlow/Keras which is embedded/used is 2.12.0. This version is not able to load a model generated with Keras 3.0.5.

 

import tensorflow as tf
new_model = tf.keras.models.load_model('model.keras')

...
TypeError: Error when deserializing class 'InputLayer' using config={'batch_shape': [None, 128, 173, 1], 'dtype': 'float32', 'sparse': False, 'name': 'input_layer_1'}.
Exception encountered: Unrecognized keyword arguments: ['batch_shape']

The issue is not related to the batch parameter.

Possible workaround to use X-CUBE-AI, is to convert your model to TFLite format.

br,

Jean-Michel

View solution in original post

3 REPLIES 3
Flare
Associate II
You have to set a fixed batch size. Assuming you just want to process one input at a time, something like this:

 

batch_size = 1
model.input.set_shape((batch_size,) + model.input.shape[1:])

 

gosal
Associate III

Thank you, I tried  this and got past that error but now getting the below error, ...am I trying to use a too complex model?

 

Neural Network Tools for STM32 family v1.7.0 (stm.ai v8.1.0-19520) 
E010(InvalidModelError): Couldn't load Keras model C:\[redacted]\model.keras, 
Could not deserialize class 'Functional' because its parent module keras.src.models.functional cannot be imported. Full object config: {'module': 'keras.src.models.functional', 'class_name': 'Functional', 'config': {'name': 'functional_1', 'trainable': True, 'layers': [{'module': 'keras.layers', 'class_name': 'InputLayer', 'config': {'batch_shape': [1, 128, 173, 1], 'dtype': 'float32', 'sparse': False, 'name': 'input_layer'}, 'registered_name': None, 'name': 'input_layer', 'inbound_nodes': []}, {'module': 'keras.layers', 'class_name': 'Conv2D', 'config': {'name': 'conv2d', 'trainable': True, 'dtype': 'float32', 'filters': 16, 'kernel_size': [3, 3], 'strides': [1, 1], 'padding': 'valid', 'data_format': 'channels_last', 'dilation_rate': [1, 1], 'groups': 1, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'module': 'keras.initializers', 'class_name': 'GlorotUniform', 'config': {'seed': None}, 'registered_name': None}, 'bias_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}, 'registered_name': None, 'build_config': {'input_shape': [1, 128, 173, 1]}, 'name': 'conv2d', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 128, 173, 1], 'dtype': 'float32', 'keras_history': ['input_layer', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'MaxPooling2D', 'config': {'name': 'max_pooling2d', 'trainable': True, 'dtype': 'float32', 'pool_size': [2, 2], 'padding': 'valid', 'strides': [2, 2], 'data_format': 'channels_last'}, 'registered_name': None, 'build_config': {'input_shape': [1, 126, 171, 16]}, 'name': 'max_pooling2d', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 126, 171, 16], 'dtype': 'float32', 'keras_history': ['conv2d', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'Conv2D', 'config': {'name': 'conv2d_1', 'trainable': True, 'dtype': 'float32', 'filters': 32, 'kernel_size': [3, 3], 'strides': [1, 1], 'padding': 'valid', 'data_format': 'channels_last', 'dilation_rate': [1, 1], 'groups': 1, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'module': 'keras.initializers', 'class_name': 'GlorotUniform', 'config': {'seed': None}, 'registered_name': None}, 'bias_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}, 'registered_name': None, 'build_config': {'input_shape': [1, 63, 85, 16]}, 'name': 'conv2d_1', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 63, 85, 16], 'dtype': 'float32', 'keras_history': ['max_pooling2d', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'MaxPooling2D', 'config': {'name': 'max_pooling2d_1', 'trainable': True, 'dtype': 'float32', 'pool_size': [2, 2], 'padding': 'valid', 'strides': [2, 2], 'data_format': 'channels_last'}, 'registered_name': None, 'build_config': {'input_shape': [1, 61, 83, 32]}, 'name': 'max_pooling2d_1', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 61, 83, 32], 'dtype': 'float32', 'keras_history': ['conv2d_1', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'Flatten', 'config': {'name': 'flatten', 'trainable': True, 'dtype': 'float32', 'data_format': 'channels_last'}, 'registered_name': None, 'build_config': {'input_shape': [1, 30, 41, 32]}, 'name': 'flatten', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 30, 41, 32], 'dtype': 'float32', 'keras_history': ['max_pooling2d_1', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'Dense', 'config': {'name': 'dense', 'trainable': True, 'dtype': 'float32', 'units': 64, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'module': 'keras.initializers', 'class_name': 'GlorotUniform', 'config': {'seed': None}, 'registered_name': None}, 'bias_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'kernel_regularizer': None, 'bias_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}, 'registered_name': None, 'build_config': {'input_shape': [1, 39360]}, 'name': 'dense', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 39360], 'dtype': 'float32', 'keras_history': ['flatten', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'Dense', 'config': {'name': 'dense_1', 'trainable': True, 'dtype': 'float32', 'units': 22144, 'activation': 'linear', 'use_bias': True, 'kernel_initializer': {'module': 'keras.initializers', 'class_name': 'GlorotUniform', 'config': {'seed': None}, 'registered_name': None}, 'bias_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'kernel_regularizer': None, 'bias_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}, 'registered_name': None, 'build_config': {'input_shape': [1, 64]}, 'name': 'dense_1', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 64], 'dtype': 'float32', 'keras_history': ['dense', 0, 0]}}], 'kwargs': {}}]}, {'module': 'keras.layers', 'class_name': 'Reshape', 'config': {'name': 'reshape', 'trainable': True, 'dtype': 'float32', 'target_shape': [128, 173, 1]}, 'registered_name': None, 'build_config': {'input_shape': [1, 22144]}, 'name': 'reshape', 'inbound_nodes': [{'args': [{'class_name': '__keras_tensor__', 'config': {'shape': [1, 22144], 'dtype': 'float32', 'keras_history': ['dense_1', 0, 0]}}], 'kwargs': {}}]}], 'input_layers': [['input_layer', 0, 0]], 'output_layers': [['reshape', 0, 0]]}, 'registered_name': 'Functional', 'build_config': {'input_shape': None}, 'compile_config': None}

jean-michel.d
ST Employee

Hi Flare,

The referenced "model.keras" is a model generated and saved with a version of Keras 3 (3.0.5). In X-CUBE-AI, the version of TensorFlow/Keras which is embedded/used is 2.12.0. This version is not able to load a model generated with Keras 3.0.5.

 

import tensorflow as tf
new_model = tf.keras.models.load_model('model.keras')

...
TypeError: Error when deserializing class 'InputLayer' using config={'batch_shape': [None, 128, 173, 1], 'dtype': 'float32', 'sparse': False, 'name': 'input_layer_1'}.
Exception encountered: Unrecognized keyword arguments: ['batch_shape']

The issue is not related to the batch parameter.

Possible workaround to use X-CUBE-AI, is to convert your model to TFLite format.

br,

Jean-Michel