import numpy as np
import pandas as pd
import os
from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold

os.chdir('/content/drive/MyDrive/data/ani')

train_dir = '/content/drive/MyDrive/data/ani/ani/train'
val_dir = '/content/drive/MyDrive/data/ani/ani/val'

data_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input,
                                             horizontal_flip=True,
                                             width_shift_range=0.2,
                                             height_shift_range=0.2)

data_generator_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input)

image_size = 224

num_classes = 7
num_folds = 5
kfold = KFold(n_splits=num_folds, shuffle=True)

results = {}
train_history = []

def build_model(with_dropout=True):
    model = Sequential()
    model.add(DenseNet121(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)))
    model.add(GlobalAveragePooling2D())
    model.add(Flatten())
    model.add(Dense(1024, activation='relu'))
    if with_dropout:
        model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=Adam(0.0001), metrics=['accuracy'])
    return model

# K-fold Cross Validation model evaluation
for fold, (train_ids, val_ids) in enumerate(kfold.split(np.zeros(10000), np.zeros(10000))):
    print(f'Training fold {fold+1}/{num_folds}')
    train_generator = data_generator_with_aug.flow_from_directory(
        train_dir,
        target_size=(image_size, image_size),
        batch_size=12,
        class_mode='categorical',
        subset='training')

    validation_generator = data_generator_no_aug.flow_from_directory(
        val_dir,  # This was previously misspelled as valcast_dir
        target_size=(image_size, image_size),
        batch_size=12,
        class_mode='categorical',
        subset='validation')

    model = build_model(with_dropout=True)
    history = model.fit(
        train_generator,
        epochs=30,
        validation_data=validation_generator
    )
    train_history.append(history)

    model_no_dropout = build_model(with_dropout=False)
    history_no_dropout = model_no_dropout.fit(  # Corrected from model_no_dropdown
        train_generator,
        epochs=30,
        validation_data=validation_generator
    )
    train_history.append(history_no_dropout)

    # Collecting Accuracy and Loss
    results[fold] = {
        'dropout': (history.history['accuracy'], history.history['loss']),
        'no_dropout': (history_no_dropout.history['accuracy'], history_no_dropout.history['loss']) # Corrected from history_no_drop
    }

# Plotting accuracy and loss for each model with and without dropout
plt.figure(figsize=(14, 6))

# Subplot for the accuracy
plt.subplot(1, 2, 1)
for key, value in results.items():
    plt.plot(value['dropout'][0], label=f'Train Acc w/ Dropout Fold {key+1}')
    plt.plot(value['no_dropout'][0], label=f'Train Acc w/o Dropout Fold {key+1}')
plt.title('Model Accuracy with/without Dropout (per fold)')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend()

# Subplot for the loss
plt.subplot(1, 2, 2)
for key, value in results.items():
    plt.plot(value['dropout'][1], label=f'Train Loss w/ Dropout Fold {key+1}')
    plt.plot(value['no_dropout'][1], label=f'Train Loss w/o Dropout Fold {key+1}')
plt.title('Model Loss with/without Dropout (per fold)')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend()

plt.show()
Training fold 1/5
Found 296 images belonging to 7 classes.
Found 0 images belonging to 7 classes.
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/densenet/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5
29084464/29084464 [==============================] - 0s 0us/step
이미지가 안불러와져