首页 > 解决方案 > ValueError: ('无法识别的关键字参数:', dict_keys(['ragged']))

问题描述

我在 google colab 上训练了一个细胞分割模型,一个是 tensorflow 版本 1.x,另一个是 2.x。我下载了这个模型,现在正在尝试加载它并预测我下载的预选图像。训练和构建模型,我使用了 tf.keras.layers.layername 代码如下:

import numpy as np
import cv2
import tensorflow as tf
import keras
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.utils import CustomObjectScope
from tensorflow.python.keras.initializers import glorot_uniform


def prepare(filepath):
    IMG_SIZE = 100
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

#def load_model():
with CustomObjectScope({'GlorotUniform':glorot_uniform()}):
    model = tf.keras.models.load_model("./models/nuclei_1-15.h5", compile=False)

def predict():
    prediction = model.predict([prepare("./images/cell.jpeg")])
    print(prediction)

predict()

出现以下错误:

Traceback (most recent call last):
  File "model_werk.py", line 20, in <module>
    model = tf.keras.models.load_model("./models/nuclei_1-15.h5", compile=False)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 246, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 324, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/layers/serialization.py", line 63, in deserialize
    printable_module_name='layer')
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/generic_utils.py", line 164, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 975, in from_config
    process_layer(layer_data)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 961, in process_layer
    layer = deserialize_layer(layer_data, custom_objects=custom_objects)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/layers/serialization.py", line 63, in deserialize
    printable_module_name='layer')
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/utils/generic_utils.py", line 166, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 483, in from_config
    return cls(**config)
  File "/home/hmrbcnt/Documents/thesis/try_2.1.5/trial/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 524, in __init__
    raise ValueError('Unrecognized keyword arguments:', kwargs.keys())
ValueError: ('Unrecognized keyword arguments:', dict_keys(['ragged']))

我使用 tensorflow.python.keras 而不是 tensorflow.keras 的原因是因为使用 tensorflow.keras 返回 ModuleNotFound 错误。tensorflow 的版本是 1.5,keras 是 2.2.4。我不能使用高于 1.5 的 tensorflow 版本,因为我有一个非常旧的 CPU 并且它不支持 avx 指令。任何帮助,将不胜感激。谢谢!

编辑:更改错误和一行代码,因为我忽略了一些东西,现在得到类似但不同的错误

标签: pythontensorflowkerasdeep-learning

解决方案


推荐阅读