gpt4 book ai didi

python - TypeError : tuple indices must be integers or slices, 未列出 - 加载模型 Keras 时

转载 作者:行者123 更新时间:2023-11-30 09:02:54 25 4
gpt4 key购买 nike

简而言之,我有 2 个经过训练的模型,一个在 2 个类别上训练,另一个在 3 个类别上训练。我的代码加载模型、加载图像并预测分类结果。

finetune_model = tf.keras.models.load_model(modelPath)
model = load_model(my_file)
img = image.load_img(img_path, target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)

模型文件为.h5类型。当加载 2 类训练模型时,它工作得很好。当我尝试加载 3 类训练模型时,出现标题错误,Traceback 如下:

File "C:/Users/x/PycharmProjects/y/Learning_python.py", line 23, in <module>
dope = Prediction('Three_Classes','./1.JPEG')
File "C:\Users\x\PycharmProjects\Car_Damage_Detection_Project\Predict.py", line 37, in Prediction
model = load_model(my_file)
File "C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "C:\Users\RonShvartzburd\Miniconda3\envs\y\lib\site-packages\keras\engine\saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Users\RonShvartzburd\Miniconda3\envs\y\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\RonShvartzburd\Miniconda3\envs\y\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
printable_module_name='layer')
File "C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\network.py", line 1032, in from_config
process_node(layer, node_data)
File "C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\network.py", line 991, in process_node
layer(unpack_singleton(input_tensors), **kwargs)
File "C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\layers\normalization.py", line 94, in build
dim = input_shape[self.axis]
TypeError: tuple indices must be integers or slices, not list

这两个模型到底有什么不同?除了类定义之外,两者的构建和训练方式都相同。我该如何解决这个问题?谢谢。

提供到包含创建模型的文件的 Git 存储库的链接,即 - modelTraining.py https://github.com/lepilmen/Car-Damage-Detection

最佳答案

您的输入必须是 numpy ndarrays。

关于python - TypeError : tuple indices must be integers or slices, 未列出 - 加载模型 Keras 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com