TypeError:元组索引必须是整数或切片,而不是列表-加载模型Keras时 [英] TypeError: tuple indices must be integers or slices, not list - While loading a model Keras

查看:117
本文介绍了TypeError:元组索引必须是整数或切片,而不是列表-加载模型Keras时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我有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.

In short, i have 2 trained models, one trained on 2 classes, the other on 3 classes. My code loads a model, loads an image, and predicts a classification result.

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)

The model file is of .h5 type. When loading the 2-class trained model, it works fine. When i try to load the 3-class trained model, i get the title error, Traceback is below :

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

What exactly is different between the two models? both were build and trained the same way, except the class definition. How can i go about with this issue? Thanks.

Link provided to the Git repository containing the file where the models were created, namely - modelTraining.py https://github.com/lepilmen/Car-Damage-Detection

解决方案

Your inputs must be numpy ndarrays.

这篇关于TypeError:元组索引必须是整数或切片,而不是列表-加载模型Keras时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆