K-Fold Training & Predictions 

K-Fold Cross Validation is usually performed to verify that the selected model's good performance isn't due to data bias.
This would be highly beneficial after obtaining Training Report of the models and you have selected your model architecture you would be working with.

To get started with K-Fold Cross Validation & Predictions,

Train K-Fold


Function Definition :-










Description =>
      k -> The number of folds. Usually, 5 or 10.
      tpu -> the tpu instance. To be obtained from get_strategy()

      model_name -> Name of the pretrained model architecture.
      train_tfrecs_path -> The complete path location of the tfrecord files of training dataset.
      val_tfrecs_path -> The complete path location of the tfrecord files of the validation dataset.
      test_tfrecs_path -> The complete path location of the tfrecord files of the test dataset.
      GCS_DS_PATH_labeled -> The Google Cloud Bucket Service Location of the Dataset.

      GCS_DS_PATH_unlabeled -> The Google Cloud Bucket Service Location of the Dataset.
      BATCH_SIZE -> Select the batch size of the training dataset. Usually, the value should be a multiple of 128 for efficient utilization of TPUs.

      EPOCHS -> The number of epochs to train

      classification_model -> The classification model to be attached to the base model. 

      freeze -> Default, False. Whether or not to freeze the pretrained model weights.

      input_shape -> Default, [512,512,3]. The shape of the images in the tfrecords files. 

      activation -> Default, 'softmax'. Choose an activation function.

      weights -> Default, "imagenet". Options, None, or "imagenet", "noisy-student", or any         pretrained weights.

      optimizer -> Default, "adam". Choose an optimizer for model training convergence.

      loss -> Default, "sparse_categorical_crossentropy". Options, "binary_crossentropy" or "sparse_categorical_crossentropy".

      metrics -> Default, "sparse_categorical_accuracy". Options, "accuracy" (for binary classifications) or "sparse_categorical_accuracy" (for multi-class classifications).

      callbacks -> Default, None. For callbacks available, refer to Callbacks.

      plot -> Default, False. Whether or not you want to obtain the plot of the models' folds training.

      verbose -> Default, 0. Choose among 0, 1 & 2 where 0 is for silent training, 1 for animated progress bar & 2 will keep you updated with the minor details.

Returns =>
Doesn't return anything. Saves an output file with the result of each fold training along with its validation result.


Obtain Predictions

To obtain predictions on the model folds trained using K-Fold Training, call the obtain_predictions() of the object initialized and trained via train_k_fold().

Arguments -> 

       weights - Default, None. A list of models' weights to be allotted in order to give weight to a fold over other. Note. Sum(weights) = 1. eg. for 5 fold, [0.1,0.3,0.1,0.3,0.2]

from quick_ml.k_fold_training import train_k_fold_pred

from quick_ml.k_fold_training import train_k_fold_pred

obj = train_k_fold_pred(k, tpu, n_class, model_name, train_tfrecs_path, val_tfrecs_path, test_tfrecs_path, GCS_DS_PATH_labeled, GCS_DS_PATH_unlabeled, BATCH_SIZE, EPOCHS, classification_model = 'default', freeze = False, input_shape = [512,512,3], activation = 'softmax', weights = "imagenet", optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = "sparse_categorical_accuracy", callbacks = None, plot = False, verbose = 0)

df = obj.train_k_fold()

obj.obtain_predictions(weights = None)

Was this helpful?