0
votes

I trained a model on Keras in CNN, it supposes to get the input of image with shape (-1,32,32,1) after training, I tested the model by inputting new images to the model and get the prediction using model.predict_class and check if the prediction matches the desired label, but the model returns an array of 3 elements for example ([3 12 22]) why it doesn't return one class '

this is the preprocessing code of the image before the prediction

        read_img = cv2.imread('test-images/' + file + '/' + img)
        read_img = read_img.reshape( -1,32, 32, 1)
        read_img = read_img.astype('float32')/255
        maxind = model.predict_classes(read_img)

and this is the output of the prediction

[22 12  4]

the code used to build the model :

model = Sequential()

model.add(Conv2D(filters=32, kernel_size=2,padding='same',activation='relu',input_shape=(32,32,1)))

model.add(MaxPooling2D(pool_size=2))

model.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))

model.add(MaxPooling2D(pool_size=2))

model.add(Flatten())

model.add(Dense(1024, activation='relu'))

model.add(Dropout(0.2))

model.add(Dense(24, activation='softmax'))

model.summary()

model inputs, outputs and image shape

model.inputs = [tf.Tensor 'conv2d_1_input:0' shape=(?, 32, 32, 1) dtype=float32]

model.outputs = [tf.Tensor 'dense_2/Softmax:0' shape=(?, 24) dtype=float32]

read_img.shape = (3, 32, 32, 1)
1
the output depends on the last layer of your keras model. Can you include the code you used to built the model - ArunJose_Intel
sure I will edit the question above - Hassan_mohammad
Please show your network architecture - pawols
@ I edited the question the architecture is above - Hassan_mohammad
What is shape of read_img? - Vivek Mehta

1 Answers

1
votes

The short answer is: you've got vector of 3 classes because you put into your network 3 images. Please see the shape of your input tensor/

read_img.shape = (3, 32, 32, 1)

The first dimension - 3 - it's batch size which means that your network makes prediction for examples - and that's the reason why you your prediction has 3 classes. Please check the dimension of your input image to be sure for how many photos you make prediction. If you'd like to make prediction for one photo your input have to has the shape (1,32,32,1).