Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

machine learning - ValueError: `decode_predictions` expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 40)

I have trained the model on 40 classes and I have trained it by removing the top layer but it is throwing an error . Now In the next block of colab if I load the model for predictions it is not giving results

    from keras.applications.inception_resnet_v2 import preprocess_input
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications import imagenet_utils
    
    
    def prepare_image(file):
        img_path = '/content/drive/MyDrive/test imgs/'
        img = image.load_img(img_path + file, target_size=(224, 224))
        img_array = image.img_to_array(img)
        img_array_expanded_dims = np.expand_dims(img_array, axis=0)
        return tf.keras.applications.inception_resnet_v2.preprocess_input(img_array_expanded_dims)
        
    model = tf.keras.models.load_model("CNN ResNet.h5")
    preprocessed_image = prepare_image('mcd.jpg')
    predictions = model.predict(preprocessed_image)
    print("Predictions are",predictions)
    results = imagenet_utils.decode_predictions(predictions)
question from:https://stackoverflow.com/questions/65897940/valueerror-decode-predictions-expects-a-batch-of-predictions-i-e-a-2d-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...