Search code examples
tensorflowkerasdeep-learningtransfer-learning

How can i use 38 classes instead of 1000 in model.predict decode predictions


i am founding an error in plant disease detection using resnet50 deep learning model every time it raises an error message in decode_predictions

error

expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 38)"

enter code here


model = ResNet50(weights='imagenet',include_top=False,classes=38)

try:
model = load_model('/content/drive/My 
Drive/color/checkpoints/ResNet50_model_weights.h5')
print("model loaded")  
except:
print("model not loaded")

img_path = '/content/drive/My Drive/color/test/0/appleblackrot188.jpg' 
img = image.load_img(img_path, target_size=(300, 300))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds,top=3)[0])

Solution

  • You can try using the preprocesing function:

    import tensorflow as tf
    # Using the keras wrapper on tensorflow (it must be the same using just keras).
    
    IMAGE = [] # From image source, i did it from the camera.
    
    toPred = tf.keras.applications.resnet50.preprocess_input(np.expand_dims(tf.keras.preprocessing.image.img_to_array(IMAGE), axis=0))
    

    Maybe that can help :)