Stack Exchange
Sci-kit Learn Confusion Matrix: Found input variables with inconsistent numbers of samples
I'm trying to plot a confusion matrix between the predicted test labels and the actual ones, but I'm getting this error

ValueError: Found input variables with inconsistent numbers of samples: [1263, 12630]

Dataset: GTSRB
Code used
Image augmentation
train_datagen = ImageDataGenerator(rescale=1./255,
                            rotation_range=20,
                            horizontal_flip=True,
                            width_shift_range=0.1,
                            height_shift_range=0.1,
                            shear_range=0.01,
                            zoom_range=[0.9, 1.25],
                            brightness_range=[0.5, 1.5])

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator and test_generator
batch_size = 10

train_generator = train_datagen.flow_from_directory(
    directory=train_path,
    target_size=(224, 224),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True,
    seed=42
)

test_generator = test_datagen.flow_from_directory(
    directory=test_path,
    target_size=(224, 224),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=False,
    seed=42
)

Output of that code

Found 39209 images belonging to 43 classes.
Found 12630 images belonging to 43 classes.

Then, I used a VGG-16 model and replaced the latest Dense layer with a Dense(43, activation='softmax')
Model summary
_________________________________________________________________ 
Layer (type)                 Output Shape              Param #   
================================================================= 
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________ 
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________ 
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________ 
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________ 
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________ 
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________ 
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________ 
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________ 
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________ 
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________ 
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________ 
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________ 
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________ 
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________ 
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________ 
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________ 
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________ 
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________ 
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________ 
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________ 
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________ 
predictions (Dense)          (None, 1000)              4097000   
_________________________________________________________________ 
dense_1 (Dense)              (None, 43)                43043     
================================================================= 
Total params: 138,400,587 
Trainable params: 43,043 
Non-trainable params: 138,357,544
_________________________________________________________________

Compile the model
my_sgd = SGD(lr=0.01)

model.compile(
    optimizer=my_sgd,
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

Train the model
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
epochs=10
model.fit_generator(generator=train_generator,
                    steps_per_epoch=STEP_SIZE_TRAIN,
                    epochs=epochs, 
                    verbose=1
)

Predictions
STEP_SIZE_TEST=test_generator.n//test_generator.batch_size
test_generator.reset()

predictions = model.predict_generator(test_generator, steps=STEP_SIZE_TEST, verbose=1)

Output

1263/1263 [==============================] - 229s 181ms/step

Predictions shape
    print(predictions.shape)

(12630, 43)

Getting the test_data and test_labels
test_data = []
test_labels = []
batch_index = 0

while batch_index <= test_generator.batch_index:
    data = next(test_generator)
    test_data.append(data[0])
    test_labels.append(data[1])
    batch_index = batch_index + 1

test_data_array = np.asarray(test_data)
test_labels_array = np.asarray(test_labels)

Shape of test_data_array and test_labels_array
test_data_array.shape


(1263, 10, 224, 224, 3)

test_labels_array.shape


(1263, 10, 43)

Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(test_labels_array, predictions)

I get the output

ValueError: Found input variables with inconsistent numbers of samples: [1263, 12630]

I understand that this error is because the test_labels_array size isn't equal to the predictions; 1263 and 12630 respectively, but I don't really know what I'm doing wrong.
Any help would be much appreciated.
PS: If anyone has any tips on how to increase the training accuracy while we're at it, that would be brilliant.
Thanks! 

You should reshape test_data_array and test_labels_array as follows:
data_count, batch_count, w, h, c = test_data_array.shape

test_data_array=np.reshape(test_data_array, (data_count*batch_count, w, h, c))
test_labels_array = np.reshape(test_labels_array , (data_count*batch_count, -1))

the way you are appending the results of test_generator is the reason. In fact the first call of your test_generator will generate 10 data with shape of (224, 224, 3). For the next call again your test_generator will generate 10 data with shape of (224, 224, 3). So now you should have 20 data of shape (224, 224, 3) while the way you are appending the results would cause that you came up with 2 data of shape (10, 224, 224, 3). which is not what you are expecting.

Thanks for the quick respose. I tried your answer and now I'm getting this error 'ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets'

probably that's because your prediction outputs are float number and probably you should round each entry and then use your metric function.

I tried pred = predictions.round() but I got the same error. I also tried using a class mode of 'sparse' instead of 'categorical'  in the flow_from_directory function and that didn't work either.

Update: using argmax worked fine cm = confusion_matrix(test_labels_array.argmax(axis=1), pred.argmax(axis=1)) thank you for your help!

Yes you are right. Sorry for my wrong about rounding :(.

Not at all. Thank you so much, sir!
