Search code examples
pythonclassificationdigits

CNN classification model.predict giving array of 0 and 1 and not the probability


I have trained a CNN model to classify digits which i working fine. But the issues i am facing is when i use the command model.predict() it gives me an array of 0 and 1 and not the probabilities.

If i pass an image to the model i want the output of model.predict to be in probabilities. For eg :-

Suppose i pass an image of digit 4. The expected output is [[0.2 0.1 0.1 0.1 0.9 ...]] But the output i am getting is [[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]

I am new to neural networks. Can someone please help. Also to quote its not case of over fitting, nor the array of 0 and 1 are probability (I have tried multiplying with 100 and changing type to float32)
Below is my model :-

(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
print(y_train)
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
print(y_train)
print(num_classes)

# define the larger model
def larger_model():
    # create model
    model = Sequential()
    model.add(Conv2D(30, (5, 5), input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(15, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

#build the model
model = larger_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)

Solution

  • One possible reason could be that your test data is not pre-processed the same way as your train/val data. You need to make sure that your test image pixel values are normalized between 0-1 before you call model.predict() as your model is being trained on normalized images.