How to predict input image using trained model in Keras?

前端 未结 5 1381
刺人心
刺人心 2020-12-04 06:40

I\'m only beginning with keras and machine learning in general.

I trained a model to classify images from 2 classes and saved it using model.save(). Her

相关标签:
5条回答
  • 2020-12-04 07:28

    keras predict_classes (docs) outputs A numpy array of class predictions. Which in your model case, the index of neuron of highest activation from your last(softmax) layer. [[0]] means that your model predicted that your test data is class 0. (usually you will be passing multiple image, and the result will look like [[0], [1], [1], [0]] )

    You must convert your actual label (e.g. 'cancer', 'not cancer') into binary encoding (0 for 'cancer', 1 for 'not cancer') for binary classification. Then you will interpret your sequence output of [[0]] as having class label 'cancer'

    0 讨论(0)
  • 2020-12-04 07:29

    You can use model.predict() to predict the class of a single image as follows [doc]:

    # load_model_sample.py
    from keras.models import load_model
    from keras.preprocessing import image
    import matplotlib.pyplot as plt
    import numpy as np
    import os
    
    
    def load_image(img_path, show=False):
    
        img = image.load_img(img_path, target_size=(150, 150))
        img_tensor = image.img_to_array(img)                    # (height, width, channels)
        img_tensor = np.expand_dims(img_tensor, axis=0)         # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
        img_tensor /= 255.                                      # imshow expects values in the range [0, 1]
    
        if show:
            plt.imshow(img_tensor[0])                           
            plt.axis('off')
            plt.show()
    
        return img_tensor
    
    
    if __name__ == "__main__":
    
        # load model
        model = load_model("model_aug.h5")
    
        # image path
        img_path = '/media/data/dogscats/test1/3867.jpg'    # dog
        #img_path = '/media/data/dogscats/test1/19.jpg'      # cat
    
        # load a single image
        new_image = load_image(img_path)
    
        # check prediction
        pred = model.predict(new_image)
    

    In this example, a image is loaded as a numpy array with shape (1, height, width, channels). Then, we load it into the model and predict its class, returned as a real value in the range [0, 1] (binary classification in this example).

    0 讨论(0)
  • 2020-12-04 07:29

    Forwarding the example by @ritiek, I'm a beginner in ML too, maybe this kind of formatting will help see the name instead of just class number.

    images = np.vstack([x, y])
    
    prediction = model.predict(images)
    
    print(prediction)
    
    i = 1
    
    for things in prediction:  
        if(things == 0):
            print('%d.It is cancer'%(i))
        else:
            print('%d.Not cancer'%(i))
        i = i + 1
    
    0 讨论(0)
  • 2020-12-04 07:32

    That's because you're getting the numeric value associated with the class. For example if you have two classes cats and dogs, Keras will associate them numeric values 0 and 1. To get the mapping between your classes and their associated numeric value, you can use

    >>> classes = train_generator.class_indices    
    >>> print(classes)
        {'cats': 0, 'dogs': 1}
    

    Now you know the mapping between your classes and indices. So now what you can do is

    if classes[0][0] == 1: prediction = 'dog' else: prediction = 'cat'

    0 讨论(0)
  • 2020-12-04 07:37

    If someone is still struggling to make predictions on images, here is the optimized code to load the saved model and make predictions:

    # Modify 'test1.jpg' and 'test2.jpg' to the images you want to predict on
    
    from keras.models import load_model
    from keras.preprocessing import image
    import numpy as np
    
    # dimensions of our images
    img_width, img_height = 320, 240
    
    # load the model we saved
    model = load_model('model.h5')
    model.compile(loss='binary_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])
    
    # predicting images
    img = image.load_img('test1.jpg', target_size=(img_width, img_height))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    
    images = np.vstack([x])
    classes = model.predict_classes(images, batch_size=10)
    print classes
    
    # predicting multiple images at once
    img = image.load_img('test2.jpg', target_size=(img_width, img_height))
    y = image.img_to_array(img)
    y = np.expand_dims(y, axis=0)
    
    # pass the list of multiple images np.vstack()
    images = np.vstack([x, y])
    classes = model.predict_classes(images, batch_size=10)
    
    # print the classes, the images belong to
    print classes
    print classes[0]
    print classes[0][0]
    
    0 讨论(0)
提交回复
热议问题