问题
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:
Your inputs must be numpy ndarrays.
回答2:
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.
来源:https://stackoverflow.com/questions/59580562/typeerror-tuple-indices-must-be-integers-or-slices-not-list-while-loading-a