0
votes

i want to evaluate the accuracy of the model but also implement the confusion matrix of all 10 classes of the cifar10 dataset, and i got this error message "Error when checking input: expected conv2d_9_input to have 4 dimensions, but got array with shape (10000, 10)"

def run_test_harness():

# load dataset
trainX, trainY, testX, testY = load_dataset()
# prepare pixel data
trainX, testX = prep_pixels(trainX, testX)
# define model
model = define_model()
# fit model
history = model.fit(trainX, trainY, epochs=100, batch_size=64, validation_data=(testX, testY), verbose=0)

 # fig
y_pred=model.predict_classes(testY)
con_mat = tf.math.confusion_matrix(labels=y_true, predictions=y_pred).numpy()
con_mat_norm = np.around(con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis], decimals=2)
con_mat_df = pd.DataFrame(con_mat_norm, index = classes, columns = classes)
figure = plt.figure(figsize=(8, 8))
sns.heatmap(con_mat_df, annot=True,cmap=plt.cm.Blues)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

  accuracy, precision, recall = model.evaluate(testX, testY, verbose=0)

print ("recall")
print ('> %.3f' % (recall * 100.0))
print ("accuracy")
print('> %.3f' % (accuracy * 100.0))
print ("precision")
print('> %.3f' % (precision * 100.0))

# learning curves # end

 accuracy, precision, recall = model.evaluate(testX, testY, verbose=0)

print ("recall")
print ('> %.3f' % (recall * 100.0))
print ("accuracy")
print('> %.3f' % (accuracy * 100.0))
print ("precision")
print('> %.3f' % (precision * 100.0))

this is the implementation on the Cnn

def define_model():

model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(32, 32, 3)))
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='softmax'))
# compile model

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',precision_m, recall_m])
return model
1
where is prep_pixel_caode? it seems you are giving your test_Y to the model. The problem might be in your load_dataset or prepr_pixel? could you post them too, as far as i know cifar10.load_data() returns two tuples that can extracted as you did with Train_x, train_y, = load_dataset - Eliethesaiyan

1 Answers

0
votes

My guess is that the trainX or testX is being swapped for the y data somewhere. The expected input shape to fit for trainX is: (batchsize, 32, 32, 3), where I think the batch size is 10000. The trainY probably has shape (10000,10). You might want to check the shapes of trainX, trainY, testX, and testY right before the call to model.fit to make sure there has not been a swap or other corruption. I hope this helps.