ValueError:('无法识别的关键字参数:',dict_keys(['ragged']))

时间:2020-05-21 12:12:38

标签: python tensorflow keras deep-learning

我在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错误。 张量流的版本是1.5,keras是2.2.4。我不能使用高于1.5的tensorflow版本,因为我有一个相当旧的CPU并且它不支持avx指令。 任何帮助,将不胜感激。谢谢!

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

0 个答案:

没有答案