TypeError('Keyword argument not understood:', 'groups') in keras.models load_model

Question:

After training a model using Google Colab, I downloaded it using the following command (inside Google Colab):

model.save('model.h5')
from google.colab import files
files.download('model.h5')

My problem is that when I try to load the downloaded model.h5 using my local machine (outside Google Colab), I get the following error:

[input]

from keras.models import load_model
model = load_model(model.h5)

[output]

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    model = load_model(filepath = 'saved_model/model2.h5',custom_objects=None,compile=True, )
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 184, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 177, in load_model_from_hdf5
    model = model_config_lib.model_from_config(model_config,
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py", line 55, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py", line 105, in deserialize
    return deserialize_keras_object(
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 369, in deserialize_keras_object
    return cls.from_config(
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py", line 397, in from_config
    layer = layer_module.deserialize(layer_config,
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py", line 105, in deserialize
    return deserialize_keras_object(
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 375, in deserialize_keras_object
    return cls.from_config(cls_config)
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 655, in from_config
    return cls(**config)
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py", line 582, in __init__
    super(Conv2D, self).__init__(
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py", line 121, in __init__
    super(Conv, self).__init__(
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 294, in __init__
    generic_utils.validate_kwargs(kwargs, allowed_kwargs)
  File "/home/lucasmirachi/anaconda3/envs/myenviron/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 792, in validate_kwargs
    raise TypeError(error_message, kwarg)
TypeError: ('Keyword argument not understood:', 'groups')

Does anyone know what is this ‘groups’ keyword argument not understood?
Instead of using from keras.models I have tried using from tensorflow.keras.models but I had no success, I got the same error.
In both Google Colab and on my local machine I’m running Keras ‘2.4.3’

Thank you all in advance!

Asked By: Lucas Mirachi

||

Answers:

I commented earlier saying I had the same exact error from doing the same exact thing. I just solved it by upgrading both tensorflow and keras on my local machine

pip install --upgrade tensorflow
pip install --upgrade keras

The error was probably due to differing versions of the packages between Colab and local machine. Hope this works for you, too.

Answered By: Sam Skinner

I had the same issue because I was saving and loading the model with different versions of tensorflow. I saved a model with tf 2.3.0 then loaded it with tf 2.1.0.

I made sure that both saving and loading use the same venv which fixed the issue for me.

Answered By: jost

I faced the same issue so I checked for the versions of tensorflow and keras in google-colaboratory and found the following:

image

I solved the issue by installing tensorflow and keras with the commands below in my anaconda environment:

pip install tensorflow-gpu==2.4.1
pip install Keras==2.4.3
Answered By: Tejaswari

If you want to stay on the same tf version, a workaround is model.load_weights("model_path"). Not the best solution, but it works

Answered By: Shreyas Agrawal

I have in colab (Google) tensorflow version 2.9.2
and in my Raspberry 4 tensorflow versio 2.4.1. So diferent versions.
I made in colab a pre-training model VGG19 with input_shape(220,220,3). And I classificated 2 types image.

I SOLVED like this:

In colab (making model):

# serialize model to JSON
model_json =  loaded_model2.to_json()
with open('/content/drive/MyDrive/dataset/extract/model_5.json', "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5

loaded_model2.save_weights('/content/drive/MyDrive/model_5.h5')
print("Saved model to disk")

Then, in my Raspberry I create a model.

model_new = tf.keras.Sequential()
model_new.add(tf.keras.applications.VGG19(include_top=false, weights='imagenet',pooling='avg',input_shape=(220,220,3)))
model_new.add(tf.keras.layers.Dense(2,activation="softmax"))
opt = tf.keras.optimizers.SGC(0,004)
model_new.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])

And then, I load weights from colab in this model made in my Raspberry 4. Only .h5 file with weights:

model_new.load_weights('/home/pi/projects/models/model_5.h5)
Answered By: sruizvargasp3