Teachable Machine .h5 Won't Load

I’m attempting to use an exported Teachable Machine .h5 locally in a Python program, but none of the sample programs from Google will load the model without a deserialization error. I have Tensorflow 2.16.0rc0 and Keras 3.0.5 installed. I’m using a new installation of VSCode and Python.

Code:
from keras.models import load_model # TensorFlow is required for Keras to work

from PIL import Image, ImageOps # Install pillow instead of PIL

import numpy as np

Disable scientific notation for clarity

np.set_printoptions(suppress=True)

Load the model

model = load_model(“keras_model.h5”, compile=False)

Error:
Traceback (most recent call last):
File “C:\Users\craig\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\ops\operation.py”, line 196, in from_config
return cls(**config)
^^^^^^^^^^^^^
File “C:\Users\craig\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\layers\convolutional\depthwise_conv2d.py”, line 118, in init
super().init(
File “C:\Users\craig\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\layers\convolutional\base_depthwise_conv.py”, line 106, in init
super().init(
File “C:\Users\craig\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\layers\layer.py”, line 265, in init
raise ValueError(
ValueError: Unrecognized keyword arguments passed to DepthwiseConv2D: {‘groups’: 1}

1 Like

Even I had the similar error.

ValueError: Unrecognized keyword arguments passed to DepthwiseConv2D: {‘groups’: 1}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “G:\plant Disease\app.py”, line 26, in
model = load_model(MODEL_PATH)
^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\saving\saving_api.py”, line 183, in load_model
return legacy_h5_format.load_model_from_hdf5(filepath)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\legacy\saving\legacy_h5_format.py”, line 133, in load_model_from_hdf5
model = saving_utils.model_from_config(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\legacy\saving\saving_utils.py”, line 85, in model_from_config
return serialization.deserialize_keras_object(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\legacy\saving\serialization.py”, line 494, in deserialize_keras_object
deserialized_obj = cls.from_config(
^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\models\model.py”, line 492, in from_config
return functional_from_config(
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\models\functional.py”, line 503, in functional_from_config
process_layer(layer_data)
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\models\functional.py”, line 483, in process_layer
layer = saving_utils.model_from_config(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\legacy\saving\saving_utils.py”, line 85, in model_from_config
return serialization.deserialize_keras_object(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\legacy\saving\serialization.py”, line 503, in deserialize_keras_object
deserialized_obj = cls.from_config(cls_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Om Patel\AppData\Roaming\Python\Python312\site-packages\keras\src\ops\operation.py”, line 198, in from_config
raise TypeError(
TypeError: Error when deserializing class ‘DepthwiseConv2D’ using config={‘name’: ‘block4a_dwconv2’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘kernel_size’: [3, 3], ‘strides’: [2, 2], ‘padding’: ‘same’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘linear’, ‘use_bias’: False, ‘bias_initializer’: {‘class_name’: ‘Zeros’, ‘config’: {}}, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘bias_constraint’: None, ‘depth_multiplier’: 1, ‘depthwise_initializer’: {‘class_name’: ‘VarianceScaling’, ‘config’: {‘scale’: 2.0, ‘mode’: ‘fan_out’, ‘distribution’: ‘truncated_normal’, ‘seed’: None}}, ‘depthwise_regularizer’: None, ‘depthwise_constraint’: None}.

Exception encountered: Unrecognized keyword arguments passed to DepthwiseConv2D: {‘groups’: 1}

I have the exact same issue and I cannot figure out why it’s not working.

I solved the problem by downgrading tensorflow to 2.12, feel free to try

1 Like

Any solutions here? I have the same problem.

Teachable machine’s H5 models not loading in tensorflow 2.16.1

File G:\Python\Anaconda\Lib\site-packages\keras\src\ops\operation.py:208 in from_config
return cls(**config)

File G:\Python\Anaconda\Lib\site-packages\keras\src\layers\convolutional\depthwise_conv2d.py:118 in init
super().init(

File G:\Python\Anaconda\Lib\site-packages\keras\src\layers\convolutional\base_depthwise_conv.py:106 in init
super().init(

File G:\Python\Anaconda\Lib\site-packages\keras\src\layers\layer.py:264 in init
raise ValueError(

ValueError: Unrecognized keyword arguments passed to DepthwiseConv2D: {‘groups’: 1}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File G:\Python\Anaconda\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)

File g:_leap robots\dps\atl marathon\nutrition app\identification2.py:9
model = load_model(model_path)

File G:\Python\Anaconda\Lib\site-packages\keras\src\saving\saving_api.py:183 in load_model
return legacy_h5_format.load_model_from_hdf5(filepath)

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\legacy_h5_format.py:133 in load_model_from_hdf5
model = saving_utils.model_from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\saving_utils.py:85 in model_from_config
return serialization.deserialize_keras_object(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\serialization.py:495 in deserialize_keras_object
deserialized_obj = cls.from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\models\sequential.py:326 in from_config
layer = saving_utils.model_from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\saving_utils.py:85 in model_from_config
return serialization.deserialize_keras_object(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\serialization.py:495 in deserialize_keras_object
deserialized_obj = cls.from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\models\sequential.py:326 in from_config
layer = saving_utils.model_from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\saving_utils.py:85 in model_from_config
return serialization.deserialize_keras_object(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\serialization.py:495 in deserialize_keras_object
deserialized_obj = cls.from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\models\model.py:528 in from_config
return functional_from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\models\functional.py:509 in functional_from_config
process_layer(layer_data)

File G:\Python\Anaconda\Lib\site-packages\keras\src\models\functional.py:489 in process_layer
layer = saving_utils.model_from_config(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\saving_utils.py:85 in model_from_config
return serialization.deserialize_keras_object(

File G:\Python\Anaconda\Lib\site-packages\keras\src\legacy\saving\serialization.py:504 in deserialize_keras_object
deserialized_obj = cls.from_config(cls_config)

File G:\Python\Anaconda\Lib\site-packages\keras\src\ops\operation.py:210 in from_config
raise TypeError(

TypeError: Error when deserializing class ‘DepthwiseConv2D’ using config={‘name’: ‘expanded_conv_depthwise’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘same’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘linear’, ‘use_bias’: False, ‘bias_initializer’: {‘class_name’: ‘Zeros’, ‘config’: {}}, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘bias_constraint’: None, ‘depth_multiplier’: 1, ‘depthwise_initializer’: {‘class_name’: ‘VarianceScaling’, ‘config’: {‘scale’: 1, ‘mode’: ‘fan_avg’, ‘distribution’: ‘uniform’, ‘seed’: None}}, ‘depthwise_regularizer’: None, ‘depthwise_constraint’: None}.

Exception encountered: Unrecognized keyword arguments passed to DepthwiseConv2D: {‘groups’: 1}

Had the same issue. The problem is the code that teachable machine gives (for .h5) is for older version of tensorflow. As Ajonn mentioned, downgrading it to an older version (2.12 for example) solves the issue. Also make sure that you are working with ( Python 3.8–3.11). It’s not supported for older or newer versions.