1

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

11
  • How are you loading the 3 class model ? Commented Jan 3, 2020 at 14:53
  • 1
    We cannot tell you what is different between your models, you should tell us, please provide code for both models Commented Jan 3, 2020 at 15:01
  • Both models trained with the same code, same infrastructures and same layers, ResNet50 with a softmax classifier. i load the model using "load_model" as stated above. Commented Jan 3, 2020 at 17:44
  • 1
    Furthermore you wrote: "Link provided to the Git repository containing the file where the models were created, namely - modelTraining.py", what link? Commented Jan 3, 2020 at 18:21
  • 1
    I edited and added the link, my bad. Didn't mean to sound defensive, i realized i should add the codes in question right away, my apologies. Commented Jan 3, 2020 at 19:50

2 Answers 2

2

Your inputs must be numpy ndarrays.

Sign up to request clarification or add additional context in comments.

3 Comments

Why does the input even vary between the 2 models? which were trained by the same code ?
Not sure about the reason, might be some module related problem. I had faced a similar problem and the above solution worked. Did it work for you?
I am not sure, you are suggesting to resend the image to the model as an ndarray? isn't that what i am doing by np.exand_dims?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.