text_plain_output_1.png image_output_1.png
"""
#kfold crossvalidation
augmented_datagen = ImageDataGenerator(rescale=1./255, 
                             shear_range = 0.2 ,rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, 
                             zoom_range=0.2, horizontal_flip=True)
datagen = ImageDataGenerator(rescale=1./255)
kf = StratifiedKFold(n_splits = 5, random_state = 7, shuffle = True)
loss_values = val_loss_values = acc_values = val_acc_values = total_scores = []
i = 1
for train_index, val_index in kf.split(np.zeros(len(df)),df["label"]):
    print(f"processing fold: {i}")
    train_df = df.iloc[train_index]
    validation_df = df.iloc[val_index]
    train_generator = datagen.flow_from_dataframe(train_df,
                                                            directory = image_dir,
                                                            batch_size = batch_size,
                                                            target_size = (224,224),
                                                            x_col = "filename",
                                                            y_col = "label",
                                                            class_mode = "categorical",
                                                            shuffle = True)
    val_generator = datagen.flow_from_dataframe(validation_df,
                                                          directory = image_dir,
                                                          batch_size = batch_size,
                                                          target_size = (224,224),
                                                          x_col = "filename",
                                                          y_col = "label",
                                                          class_mode = "categorical",
                                                          shuffle = True)
    model = build_model()
    history = model.fit(train_generator,
                        epochs = epochs,
                        steps_per_epoch = train_generator.samples // batch_size,
                        callbacks = cb,
                        verbose = 1)
    val_results = model.evaluate(val_generator)
    #storing loss and acc, using the last value in history
    total_scores.append(val_results[-1])
    print(f"accuracy for fold {i}: {total_scores[-1]}")
    i+=1
print(f"mean of all accuracies: {np.mean(total_scores)}")
"""