Welcome to OGeek Q&A 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...