Keras: model.predict for a single image

Since you trained your model on mini-batches, your input is a tensor of shape [batch_size, image_width, image_height, number_of_channels].

When predicting, you have to respect this shape even if you have only one image. Your input should be of shape: [1, image_width, image_height, number_of_channels].

You can do this in numpy easily. Let’s say you have a single 5x5x3 image:

    >>> x = np.random.randint(0,10,(5,5,3))
    >>> x.shape
    >>> (5, 5, 3)
    >>> x = np.expand_dims(x, axis=0)
    >>> x.shape
    >>> (1, 5, 5, 3)

Now x is a rank 4 tensor!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)