title: Sci-kit Learn Confusion Matrix: Found input variables with inconsistent numbers of samples
body: <p>I'm trying to plot a confusion matrix between the predicted test labels and the actual ones, but I'm getting this error</p>
  
  <blockquote>
    <p>ValueError: Found input variables with inconsistent numbers of samples: [1263, 12630]</p>
  </blockquote>
  
  <p>Dataset: GTSRB</p>
  
  <p>Code used</p>
  
  <p><strong>Image augmentation</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>train_generator and test_generator</strong></p>
  
  <pre><code>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
  )
  </code></pre>
  
  <p>Output of that code</p>
  
  <blockquote>
    <p>Found 39209 images belonging to 43 classes.</p>
    
    <p>Found 12630 images belonging to 43 classes.</p>
  </blockquote>
  
  <p>Then, I used a VGG-16 model and replaced the latest Dense layer with a <code>Dense(43, activation='softmax')</code></p>
  
  <p><strong>Model summary</strong></p>
  
  <pre><code>_________________________________________________________________ 
  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
  _________________________________________________________________
  </code></pre>
  
  <p><strong>Compile the model</strong></p>
  
  <pre><code>my_sgd = SGD(lr=0.01)
  
  model.compile(
      optimizer=my_sgd,
      loss='categorical_crossentropy',
      metrics=['accuracy']
  )
  </code></pre>
  
  <p><strong>Train the model</strong></p>
  
  <pre><code>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
  )
  </code></pre>
  
  <p><strong>Predictions</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Output</p>
  
  <blockquote>
    <p>1263/1263 [==============================] - 229s 181ms/step</p>
  </blockquote>
  
  <p>Predictions shape
      print(predictions.shape)</p>
  
  <blockquote>
    <p>(12630, 43)</p>
  </blockquote>
  
  <p>Getting the test_data and test_labels</p>
  
  <pre><code>test_data = []
  test_labels = []
  batch_index = 0
  
  while batch_index &lt;= 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)
  </code></pre>
  
  <p>Shape of test_data_array and test_labels_array</p>
  
  <pre><code>test_data_array.shape
  </code></pre>
  
  <blockquote>
    <p>(1263, 10, 224, 224, 3)</p>
  </blockquote>
  
  <pre><code>test_labels_array.shape
  </code></pre>
  
  <blockquote>
    <p>(1263, 10, 43)</p>
  </blockquote>
  
  <p><strong>Confusion Matrix</strong></p>
  
  <pre><code>from sklearn.metrics import confusion_matrix
  cm = confusion_matrix(test_labels_array, predictions)
  </code></pre>
  
  <p>I get the output</p>
  
  <blockquote>
    <p>ValueError: Found input variables with inconsistent numbers of samples: [1263, 12630]</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>PS: If anyone has any tips on how to increase the training accuracy while we're at it, that would be brilliant.</p>
  
  <p>Thanks! </p>