title: Single class prediction error on certain folds of cross-validation in CNN
body: <p>I am training a CNN to predict the correct orientation of x-ray. To further validate my model, i perform stratified K-fold validation. I use K=5 as a starting point. It turns out the training will eventually meet an epoch of 0.50 accuracy (because of single class prediction) once in a while.<br><br>Even though I can run the script till I get non of the problematic epoch, the problem still bugged me.<br><br>
  The 2 classes is splitted evenly between them in both training set and validation set. This is the confusion matrix of the classification.
  <br><br>
  <a href="https://i.stack.imgur.com/ZeDyD.png" rel="nofollow noreferrer">Example of Bad Epoch</a><br>
  <a href="https://i.stack.imgur.com/12hsR.png" rel="nofollow noreferrer">Example of Good Epoch</a></p>
  <p>I need some enlightenment on how is this possible even though it is in the same script execution. What are  the possibilities? I tried the model without cross validation, it works fine.
  <br><br>
  <strong>Convolutional Neural Network code</strong></p>
  <pre><code>def get_model_cnn(input_shape=()):
  
      model = models.Sequential([
      layers.Conv2D(filters=32, kernel_size=(5, 5), strides=(1,  1), activation='relu', input_shape=input_shape),
      layers.MaxPooling2D(pool_size=(4, 4), strides=(4, 4), padding='valid'),
  
      layers.Conv2D(filters=32, kernel_size=(5, 5), strides=(1, 1), activation='relu'),
      layers.MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding='valid'),
  
      layers.Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
      layers.MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding='valid'),
  
      layers.Flatten(),
  
      layers.Dense(128, activation='relu'),
      layers.Dense(2, activation='softmax')
  ])
  
      model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
  
      return model  
  </code></pre>
  <p><strong>Stratified K-fold Code</strong></p>
  <pre><code>skfold = StratifiedKFold(n_splits = 5, shuffle = True)
  </code></pre>
  <p><strong>Model Training</strong> <em>This code is simplified. I get rid of plotting codes.</em></p>
  <pre><code>for train_idx, val_idx in list(skfold.split(train_x,train_y)):
  
     x_train_df = df.iloc[train_idx]  
     x_valid_df   = df.iloc[val_idx]
  
     print(len(x_train_df))
  
     training_set = train_datagen.flow_from_dataframe(dataframe = x_train_df,
                                                x_col=&quot;image&quot;, 
                                                y_col=&quot;type&quot;,
                                                target_size= (IMAGE_H,IMAGE_L),
                                                batch_size = 64,
                                                color_mode= &quot;grayscale&quot;, 
                                                class_mode= 'categorical',
                                                shuffle = True)
  
  
     validation_set = validation_datagen.flow_from_dataframe(dataframe = x_valid_df,
                                               x_col=&quot;image&quot;, 
                                               y_col=&quot;type&quot;,
                                               target_size= (IMAGE_H,IMAGE_L),
                                               batch_size = 64,
                                               color_mode= &quot;grayscale&quot;,
                                               class_mode= 'categorical',
                                               shuffle = False)
  
     cnn = get_model_cnn(input_shape=(IMAGE_H, IMAGE_L, CHANNEL))
  
     history = cnn.fit(training_set, epochs=EPOCHS, validation_data = validation_set)
     backend.clear_session()
  </code></pre>