python keras.models load_model中的类型错误(“无法理解关键字参数:”,“组”)

umuewwlo  于 2023-01-08  发布在  Python
关注(0)|答案(5)|浏览(165)

在使用Google Colab训练了一个模型之后,我使用以下命令下载了它(在Google Colab内部):

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

我的问题是,当我尝试使用本地计算机(Google Colab之外)加载下载的 * model.h5 * 时,我得到以下错误:
[输入]

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

[产出]

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')

有人知道这个**'groups'**关键字参数是什么吗?我没有使用from keras.models,而是尝试使用from tensorflow.keras.models,但没有成功,我得到了同样的错误。在Google Colab和我的本地机器上,我都在运行Keras '2.4.3'

  • 提前感谢大家 *
svmlkihl

svmlkihl1#

我之前评论说我在做同样的事情时也犯了同样的错误,我只是通过在本地机器上升级tensorflow和keras来解决这个问题

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

这个错误可能是由于Colab和本地机器之间的包版本不同。希望这对你也有效。

wydwbb8l

wydwbb8l2#

我遇到了同样的问题,因为我用不同版本的tensorflow保存和加载模型。我用tf 2. 3. 0保存了一个模型,然后用tf 2. 1. 0加载它。
我确保保存和加载都使用相同的venv,这为我解决了这个问题。

wvt8vs2t

wvt8vs2t3#

我也遇到了同样的问题,所以我在谷歌合作实验室里查看了tensorflow和keras的版本,发现如下:

我通过在我的anaconda环境中使用以下命令安装tensorflow和keras解决了这个问题:

pip install tensorflow-gpu==2.4.1
pip install Keras==2.4.3
lmyy7pcs

lmyy7pcs4#

如果你想保持tf版本不变,一个变通的方法是model.load_weights("model_path"),虽然不是最好的解决方案,但是它确实有效

2ledvvac

2ledvvac5#

我在colab(谷歌)tensorflow 版本2.9.2和我的树莓4tensorflow 版本2.4.1。所以不同的版本。我在colab做了一个预训练模型VGG19与输入_形状(220,220,3)。我分类2种类型的图像。
我的答案是这样的:
在colab中(制作模型):

# 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")

然后,在我的Raspberry中创建一个模型。

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'])

然后,我从colab加载权重到Raspberry 4中创建的模型中。只有.h5文件包含权重:

model_new.load_weights('/home/pi/projects/models/model_5.h5)

相关问题