TypeError: 元组索引必须是整数或切片,而不是列表 – 在加载Keras模型时

huangapple go评论76阅读模式
英文:

TypeError: tuple indices must be integers or slices, not list - While loading a model 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类别训练的模型时我收到以下错误错误跟踪如下

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

希望这对您有所帮助。如果您需要进一步的解释或帮助,请告诉我。

英文:

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 &quot;C:/Users/x/PycharmProjects/y/Learning_python.py&quot;, line 23, in &lt;module&gt;
dope = Prediction(&#39;Three_Classes&#39;,&#39;./1.JPEG&#39;)
File &quot;C:\Users\x\PycharmProjects\Car_Damage_Detection_Project\Predict.py&quot;, line 37, in Prediction
model = load_model(my_file)
File &quot;C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\saving.py&quot;, line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File &quot;C:\Users\RonShvartzburd\Miniconda3\envs\y\lib\site-packages\keras\engine\saving.py&quot;, line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File &quot;C:\Users\RonShvartzburd\Miniconda3\envs\y\lib\site-packages\keras\engine\saving.py&quot;, line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File &quot;C:\Users\RonShvartzburd\Miniconda3\envs\y\lib\site-packages\keras\layers\__init__.py&quot;, line 55, in deserialize
printable_module_name=&#39;layer&#39;)
File &quot;C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\utils\generic_utils.py&quot;, line 145, in deserialize_keras_object
list(custom_objects.items())))
File &quot;C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\network.py&quot;, line 1032, in from_config
process_node(layer, node_data)
File &quot;C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\network.py&quot;, line 991, in process_node
layer(unpack_singleton(input_tensors), **kwargs)
File &quot;C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\engine\base_layer.py&quot;, line 431, in __call__
self.build(unpack_singleton(input_shapes))
File &quot;C:\Users\x\Miniconda3\envs\y\lib\site-packages\keras\layers\normalization.py&quot;, 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

答案1

得分: 2

你的输入必须是numpy ndarrays。

英文:

Your inputs must be numpy ndarrays.

答案2

得分: 0

与@Geeocode交流后,我再次使用相同的代码重新训练了模型,新模型没有产生错误。也许是之前的模型出了问题,导致了输入层的混乱。他使用了3张图像重新创建了一个新模型,每个类别1张图像,但也无法再现问题。这意味着问题已解决。感谢您花费的所有时间来帮助我。

英文:

After conversing with @Geeocode ,
I retrained the model again with the same code, and the new model did not produce an error.
Perhaps something happened to the previous model, and it messed up the input layer.
He reproduced a new model with 3 images, 1 per class, and also couldn't recreate the problem.
Meaning it's solved. Thanks for all the time spend helping me.

huangapple
  • 本文由 发表于 2020年1月3日 22:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定