英文:
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 "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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论