{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":87323,"databundleVersionId":9934427,"sourceType":"competition"}],"dockerImageVersionId":30840,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport os\nimport cv2\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.svm import SVC\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.metrics import accuracy_score, roc_auc_score, roc_curve, auc\n\nimport tensorflow as tf\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout, BatchNormalization\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.callbacks import EarlyStopping\n\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB0\nfrom tensorflow.keras.applications.densenet import DenseNet121\nfrom tensorflow.keras.applications import InceptionV3\n\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:21:38.375873Z","iopub.status.idle":"2025-01-16T15:21:52.169806Z","shell.execute_reply.started":"2025-01-16T15:21:38.376136Z","shell.execute_reply":"2025-01-16T15:21:52.169072Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Read & explore the data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv')\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:21:52.170860Z","iopub.execute_input":"2025-01-16T15:21:52.171318Z","iopub.status.idle":"2025-01-16T15:21:52.210013Z","shell.execute_reply.started":"2025-01-16T15:21:52.171286Z","shell.execute_reply":"2025-01-16T15:21:52.209268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"real = df[df['label'] == 'real']\nfake = df[df['label'] != 'real']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:21:52.211306Z","iopub.execute_input":"2025-01-16T15:21:52.211510Z","iopub.status.idle":"2025-01-16T15:21:52.218960Z","shell.execute_reply.started":"2025-01-16T15:21:52.211491Z","shell.execute_reply":"2025-01-16T15:21:52.218013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fake.iloc[:5, 0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:21:52.219790Z","iopub.execute_input":"2025-01-16T15:21:52.220066Z","iopub.status.idle":"2025-01-16T15:21:52.236144Z","shell.execute_reply.started":"2025-01-16T15:21:52.220033Z","shell.execute_reply":"2025-01-16T15:21:52.235376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train'\ntest_dir = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:21:52.236942Z","iopub.execute_input":"2025-01-16T15:21:52.237223Z","iopub.status.idle":"2025-01-16T15:21:52.248516Z","shell.execute_reply.started":"2025-01-16T15:21:52.237195Z","shell.execute_reply":"2025-01-16T15:21:52.247717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport os\nimport random\nfrom PIL import Image\n\n\nfake_sample = fake.iloc[:5, 0]\nreal_sample = real.iloc[:5, 0]\n\nfig, axes = plt.subplots(2, 5, figsize=(15, 6))\n\nfor i, fake_image in enumerate(fake_sample):\n    img = Image.open(os.path.join(train_dir, fake_image))\n    axes[0, i].imshow(img)\n    axes[0, i].axis('off')\n    axes[0, i].set_title('Fake')\n\nfor i, real_image in enumerate(real_sample):\n    img = Image.open(os.path.join(train_dir, real_image))\n    axes[1, i].imshow(img)\n    axes[1, i].axis('off')\n    axes[1, i].set_title('Real')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:40.815146Z","iopub.execute_input":"2025-01-16T15:27:40.815466Z","iopub.status.idle":"2025-01-16T15:27:42.786747Z","shell.execute_reply.started":"2025-01-16T15:27:40.815444Z","shell.execute_reply":"2025-01-16T15:27:42.785590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:42.787822Z","iopub.execute_input":"2025-01-16T15:27:42.788053Z","iopub.status.idle":"2025-01-16T15:27:42.792948Z","shell.execute_reply.started":"2025-01-16T15:27:42.788034Z","shell.execute_reply":"2025-01-16T15:27:42.792114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"image_path\"] = df[\"image\"].apply(lambda x: os.path.join(train_dir, x))\ndf.head()\nlabel_encoder = LabelEncoder()\n\ndf[\"label_encoded\"] = label_encoder.fit_transform(df[\"label\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:43.662823Z","iopub.execute_input":"2025-01-16T15:27:43.663154Z","iopub.status.idle":"2025-01-16T15:27:43.670426Z","shell.execute_reply.started":"2025-01-16T15:27:43.663124Z","shell.execute_reply":"2025-01-16T15:27:43.669514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['label'].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:45.191713Z","iopub.execute_input":"2025-01-16T15:27:45.192037Z","iopub.status.idle":"2025-01-16T15:27:45.368045Z","shell.execute_reply.started":"2025-01-16T15:27:45.192001Z","shell.execute_reply":"2025-01-16T15:27:45.367136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_mappings = dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))\nlabel_mappings","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:45.524593Z","iopub.execute_input":"2025-01-16T15:27:45.524887Z","iopub.status.idle":"2025-01-16T15:27:45.530011Z","shell.execute_reply.started":"2025-01-16T15:27:45.524866Z","shell.execute_reply":"2025-01-16T15:27:45.529184Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train Validation split","metadata":{}},{"cell_type":"code","source":"train_df, val_df = train_test_split(df, test_size=0.15, stratify=df[\"label\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:47.368720Z","iopub.execute_input":"2025-01-16T15:27:47.369000Z","iopub.status.idle":"2025-01-16T15:27:47.378095Z","shell.execute_reply.started":"2025-01-16T15:27:47.368978Z","shell.execute_reply":"2025-01-16T15:27:47.377321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Train size: {len(train_df)}\")\nprint(f\"Validation size: {len(val_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:47.707334Z","iopub.execute_input":"2025-01-16T15:27:47.707651Z","iopub.status.idle":"2025-01-16T15:27:47.712713Z","shell.execute_reply.started":"2025-01-16T15:27:47.707596Z","shell.execute_reply":"2025-01-16T15:27:47.711710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch_size = 32\nimage_size = (224, 224, 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:48.305476Z","iopub.execute_input":"2025-01-16T15:27:48.305784Z","iopub.status.idle":"2025-01-16T15:27:48.309300Z","shell.execute_reply.started":"2025-01-16T15:27:48.305759Z","shell.execute_reply":"2025-01-16T15:27:48.308325Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Models","metadata":{}},{"cell_type":"code","source":"def train_and_eval(preprocess_input, model):\n    train_datagen = ImageDataGenerator(\n        preprocessing_function=preprocess_input\n    )\n    \n    train_generator = train_datagen.flow_from_dataframe(\n        dataframe=train_df,\n        x_col=\"image_path\",      \n        y_col=\"label_encoded\",       \n        target_size=(224, 224),  \n        batch_size=batch_size,\n        class_mode=\"raw\",        \n        shuffle=True,            \n    )\n    \n    val_datagen = ImageDataGenerator(\n        preprocessing_function=preprocess_input\n    )\n    \n    val_generator = val_datagen.flow_from_dataframe(\n        dataframe=val_df,\n        x_col=\"image_path\",      \n        y_col=\"label_encoded\",       \n        target_size=(224, 224),  \n        batch_size=batch_size,\n        class_mode=\"raw\",    \n        shuffle=False           \n    )\n    \n    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['auc'])\n    early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n    steps_per_epoch = train_generator.n // 32\n    validation_steps = val_generator.n // 32\n    \n    history = model.fit(\n        train_generator,\n        batch_size=32,\n        steps_per_epoch=steps_per_epoch,\n        validation_data=val_generator,\n        validation_steps=validation_steps,\n        epochs=30,\n        callbacks=[early_stopping]\n    )\n    plot_auc(val_generator, model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:51.514593Z","iopub.execute_input":"2025-01-16T15:27:51.514909Z","iopub.status.idle":"2025-01-16T15:27:51.520991Z","shell.execute_reply.started":"2025-01-16T15:27:51.514887Z","shell.execute_reply":"2025-01-16T15:27:51.520096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_auc(generator, model):\n    y_true = generator.labels  # True labels\n    \n    # Predict probabilities\n    y_pred = model.predict(generator, batch_size=32)\n    \n    # For binary classification\n    fpr, tpr, thresholds = roc_curve(y_true, y_pred)\n    roc_auc = auc(fpr, tpr)\n    print(f'ROC AUC: {roc_auc}')\n    # Plot the ROC curve\n    plt.figure(figsize=(8, 6))\n    plt.plot(fpr, tpr, color='blue', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')\n    plt.plot([0, 1], [0, 1], color='gray', linestyle='--')\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title('Receiver Operating Characteristic (ROC) Curve')\n    plt.legend(loc='lower right')\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:53.081498Z","iopub.execute_input":"2025-01-16T15:27:53.081828Z","iopub.status.idle":"2025-01-16T15:27:53.087190Z","shell.execute_reply.started":"2025-01-16T15:27:53.081800Z","shell.execute_reply":"2025-01-16T15:27:53.086302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess_to_hsv(image):\n    image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n    image_hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)\n    image_hsv[..., 0] = image_hsv[..., 0] / 180.0  # Hue range: 0-179 (OpenCV) to 0-1\n    image_hsv[..., 1] = image_hsv[..., 1] / 255.0  # Saturation range: 0-255 to 0-1\n    image_hsv[..., 2] = image_hsv[..., 2] / 255.0  # Value range: 0-255 to 0-1\n    \n    return image_hsv","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:27:53.563320Z","iopub.execute_input":"2025-01-16T15:27:53.563609Z","iopub.status.idle":"2025-01-16T15:27:53.568371Z","shell.execute_reply.started":"2025-01-16T15:27:53.563585Z","shell.execute_reply":"2025-01-16T15:27:53.567581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 1. EfficientNet","metadata":{}},{"cell_type":"code","source":"base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\nbase_model.trainable = False\n\nefficientnet_model = models.Sequential([\n    base_model,\n    layers.GlobalAveragePooling2D(),\n    layers.Dense(1024, activation='relu'),\n    layers.Dropout(0.5),\n    layers.Dense(1, activation='sigmoid')  \n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:46:12.655555Z","iopub.execute_input":"2025-01-15T22:46:12.655931Z","iopub.status.idle":"2025-01-15T22:46:13.602683Z","shell.execute_reply.started":"2025-01-15T22:46:12.655896Z","shell.execute_reply":"2025-01-15T22:46:13.602016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_and_eval(preprocess_to_hsv, efficientnet_model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:46:14.255981Z","iopub.execute_input":"2025-01-15T22:46:14.256272Z","iopub.status.idle":"2025-01-15T22:47:15.422382Z","shell.execute_reply.started":"2025-01-15T22:46:14.256250Z","shell.execute_reply":"2025-01-15T22:47:15.421529Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## GoogleNet","metadata":{}},{"cell_type":"code","source":"base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\nx = base_model.output\nx = GlobalAveragePooling2D()(x)  \nx = Dense(1024, activation='relu')(x)  \npredictions = Dense(1, activation='sigmoid')(x)  \n\ninception_model = Model(inputs=base_model.input, outputs=predictions)\n\nfor layer in base_model.layers:\n    layer.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:48:39.682558Z","iopub.execute_input":"2025-01-15T22:48:39.682909Z","iopub.status.idle":"2025-01-15T22:48:41.265943Z","shell.execute_reply.started":"2025-01-15T22:48:39.682879Z","shell.execute_reply":"2025-01-15T22:48:41.265241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_and_eval(preprocess_to_hsv, inception_model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:48:41.267142Z","iopub.execute_input":"2025-01-15T22:48:41.267444Z","iopub.status.idle":"2025-01-15T22:49:31.531273Z","shell.execute_reply.started":"2025-01-15T22:48:41.267413Z","shell.execute_reply":"2025-01-15T22:49:31.530517Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Resnet","metadata":{}},{"cell_type":"code","source":"base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\nbase_model.trainable = False\n\nx = base_model.output\nx = Flatten()(x)  \nx = Dense(128, activation='relu')(x)\nx = Dense(1, activation='sigmoid')(x)  \n\nresnet_model = Model(inputs=base_model.input, outputs=x)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:49:45.446105Z","iopub.execute_input":"2025-01-15T22:49:45.446416Z","iopub.status.idle":"2025-01-15T22:49:47.287637Z","shell.execute_reply.started":"2025-01-15T22:49:45.446390Z","shell.execute_reply":"2025-01-15T22:49:47.286984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_and_eval(preprocess_to_hsv, resnet_model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:49:52.209825Z","iopub.execute_input":"2025-01-15T22:49:52.210133Z","iopub.status.idle":"2025-01-15T22:51:36.922694Z","shell.execute_reply.started":"2025-01-15T22:49:52.210109Z","shell.execute_reply":"2025-01-15T22:51:36.921824Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Densenet","metadata":{}},{"cell_type":"code","source":"base_model = DenseNet121(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))\n\nbase_model.trainable = False\n\nx = base_model.output\nx = Flatten()(x)  \nx = Dense(128, activation='relu')(x)\nx = Dense(1, activation='sigmoid')(x)  \n\ndensenet_model = Model(inputs=base_model.input, outputs=x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:56:09.893520Z","iopub.execute_input":"2025-01-15T22:56:09.893864Z","iopub.status.idle":"2025-01-15T22:56:11.557585Z","shell.execute_reply.started":"2025-01-15T22:56:09.893836Z","shell.execute_reply":"2025-01-15T22:56:11.556689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_and_eval(preprocess_to_hsv, densenet_model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T22:56:48.142310Z","iopub.execute_input":"2025-01-15T22:56:48.142747Z","iopub.status.idle":"2025-01-15T22:59:00.872482Z","shell.execute_reply.started":"2025-01-15T22:56:48.142712Z","shell.execute_reply":"2025-01-15T22:59:00.871539Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Pretrained Feature Extractor + SVM","metadata":{}},{"cell_type":"markdown","source":"#### Function for saving csv predictions file for this part","metadata":{}},{"cell_type":"code","source":"def predict_and_save_csv(preprocess_to_hsv, feature_extractor, svm_model, csv_path):\n    image_paths = []\n    for filename in os.listdir(test_dir):\n        if filename.endswith(\".jpg\") or filename.endswith(\".png\"):  # or other image formats\n            image_paths.append(os.path.join(test_dir, filename))\n    \n    test_df = pd.DataFrame(image_paths, columns=[\"image_path\"])\n    \n    test_df.head()\n    test_datagen = ImageDataGenerator(\n        preprocessing_function=preprocess_to_hsv \n    )\n    \n    # Assuming you have a dataframe `test_df` with the image paths and labels\n    test_generator = test_datagen.flow_from_dataframe(\n        dataframe=test_df,                # DataFrame with image paths\n        x_col=\"image_path\",                # Column containing image paths\n        target_size=(224, 224),            # Resize to match VGG input size\n        batch_size=batch_size,             # Batch size for predictions\n        class_mode=None,                   # No labels for prediction\n        shuffle=False                      # Do not shuffle since we need the original order\n    )\n    X_test_features = feature_extractor_densenet.predict(test_generator)\n    print(X_test_features.shape)\n    \n    y_pred = svm_model.predict(X_test_features)\n    test_df['image'] = test_df['image_path'].str.split('/').str[-1]\n    test_df['label'] = y_pred\n    \n    test_df = test_df[['image', 'label']]\n    test_df.to_csv(csv_path, index=False)\n    \n    print(\"Predictions saved to predictions.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:28:04.020197Z","iopub.execute_input":"2025-01-16T15:28:04.020527Z","iopub.status.idle":"2025-01-16T15:28:04.026512Z","shell.execute_reply.started":"2025-01-16T15:28:04.020499Z","shell.execute_reply":"2025-01-16T15:28:04.025553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        preprocessing_function=preprocess_to_hsv\n    )\n    \ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    x_col=\"image_path\",      \n    y_col=\"label_encoded\",       \n    target_size=(224, 224),  \n    batch_size=batch_size,\n    class_mode=\"raw\",        \n    shuffle=True,            \n)\n\nval_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_to_hsv\n)\n\nval_generator = val_datagen.flow_from_dataframe(\n    dataframe=val_df,\n    x_col=\"image_path\",      \n    y_col=\"label_encoded\",       \n    target_size=(224, 224),  \n    batch_size=batch_size,\n    class_mode=\"raw\",    \n    shuffle=False           \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:28:31.464173Z","iopub.execute_input":"2025-01-16T15:28:31.464461Z","iopub.status.idle":"2025-01-16T15:28:32.011128Z","shell.execute_reply.started":"2025-01-16T15:28:31.464439Z","shell.execute_reply":"2025-01-16T15:28:32.010297Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Densenet + SVM","metadata":{}},{"cell_type":"code","source":"base_model = DenseNet121(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))\nx = GlobalAveragePooling2D()(base_model.output)\nfeature_extractor_densenet = Model(inputs=base_model.input, outputs=x)\n\nfor layer in base_model.layers:\n    layer.trainable = False\n\ndef extract_features(generator, model):\n    features = []\n    labels = []\n    \n    for batch in generator:\n        batch_features = model.predict(batch[0], batch_size=32)\n        features.append(batch_features)\n        labels.append(batch[1])\n        \n        if len(features) * batch_size >= generator.samples:\n            break\n    \n    return np.concatenate(features, axis=0), np.concatenate(labels, axis=0)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:29:03.961840Z","iopub.execute_input":"2025-01-16T15:29:03.962156Z","iopub.status.idle":"2025-01-16T15:29:10.024790Z","shell.execute_reply.started":"2025-01-16T15:29:03.962131Z","shell.execute_reply":"2025-01-16T15:29:10.023895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train_features, y_train = extract_features(train_generator, feature_extractor_densenet)\nX_val_features, y_val = extract_features(val_generator, feature_extractor_densenet)\n\nprint(X_train_features.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:29:10.026220Z","iopub.execute_input":"2025-01-16T15:29:10.026545Z","iopub.status.idle":"2025-01-16T15:29:51.685958Z","shell.execute_reply.started":"2025-01-16T15:29:10.026513Z","shell.execute_reply":"2025-01-16T15:29:51.685126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nsvm_model = make_pipeline(StandardScaler(), SVC(kernel='rbf', probability=True))\nsvm_model.fit(X_train_features, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:29:51.687311Z","iopub.execute_input":"2025-01-16T15:29:51.687548Z","iopub.status.idle":"2025-01-16T15:29:52.355183Z","shell.execute_reply.started":"2025-01-16T15:29:51.687526Z","shell.execute_reply":"2025-01-16T15:29:52.354411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, roc_curve\n\ny_pred_prob = svm_model.predict_proba(X_val_features)[:, 1]  # Get probability of positive class\n\nauc_score = roc_auc_score(y_val, y_pred_prob)\nprint(f\"AUC Score: {auc_score:.2f}\")\n\nfpr, tpr, thresholds = roc_curve(y_val, y_pred_prob)\n\nplt.figure()\nplt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {auc_score:.2f})')\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('Receiver Operating Characteristic (ROC)')\nplt.legend(loc=\"lower right\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:29:52.356012Z","iopub.execute_input":"2025-01-16T15:29:52.356243Z","iopub.status.idle":"2025-01-16T15:29:52.563027Z","shell.execute_reply.started":"2025-01-16T15:29:52.356225Z","shell.execute_reply":"2025-01-16T15:29:52.562222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict_and_save_csv(preprocess_to_hsv, feature_extractor_densenet, svm_model, csv_path='dense_svm_hsv_wiht_preprocess_input_predictions.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:29:52.563792Z","iopub.execute_input":"2025-01-16T15:29:52.564084Z","iopub.status.idle":"2025-01-16T15:30:12.817803Z","shell.execute_reply.started":"2025-01-16T15:29:52.564061Z","shell.execute_reply":"2025-01-16T15:30:12.816855Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Final Submission: Densenet + SVM (trained on the whole dataset)","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        preprocessing_function=preprocess_to_hsv\n    )\n    \ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=df,\n    x_col=\"image_path\",      \n    y_col=\"label_encoded\",       \n    target_size=(224, 224),  \n    batch_size=batch_size,\n    class_mode=\"raw\",        \n    shuffle=True,            \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:32:55.493997Z","iopub.execute_input":"2025-01-16T15:32:55.494292Z","iopub.status.idle":"2025-01-16T15:32:55.585371Z","shell.execute_reply.started":"2025-01-16T15:32:55.494269Z","shell.execute_reply":"2025-01-16T15:32:55.584711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = DenseNet121(weights=\"imagenet\", include_top=False, input_shape=(224, 224, 3))\nx = GlobalAveragePooling2D()(base_model.output)\nfeature_extractor_densenet = Model(inputs=base_model.input, outputs=x)\n\nfor layer in base_model.layers:\n    layer.trainable = False\n\ndef extract_features(generator, model):\n    features = []\n    labels = []\n    \n    for batch in generator:\n        batch_features = model.predict(batch[0], batch_size=32)\n        features.append(batch_features)\n        labels.append(batch[1])\n        \n        if len(features) * batch_size >= generator.samples:\n            break\n    \n    return np.concatenate(features, axis=0), np.concatenate(labels, axis=0)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:32:56.887525Z","iopub.execute_input":"2025-01-16T15:32:56.887906Z","iopub.status.idle":"2025-01-16T15:32:58.543014Z","shell.execute_reply.started":"2025-01-16T15:32:56.887878Z","shell.execute_reply":"2025-01-16T15:32:58.542086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train_features, y_train = extract_features(train_generator, feature_extractor_densenet)\n\nprint(X_train_features.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:32:58.544187Z","iopub.execute_input":"2025-01-16T15:32:58.544506Z","iopub.status.idle":"2025-01-16T15:33:22.397145Z","shell.execute_reply.started":"2025-01-16T15:32:58.544477Z","shell.execute_reply":"2025-01-16T15:33:22.396264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"svm_model = make_pipeline(StandardScaler(), SVC(kernel='rbf', probability=True))\nsvm_model.fit(X_train_features, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:33:22.398749Z","iopub.execute_input":"2025-01-16T15:33:22.398990Z","iopub.status.idle":"2025-01-16T15:33:23.211590Z","shell.execute_reply.started":"2025-01-16T15:33:22.398967Z","shell.execute_reply":"2025-01-16T15:33:23.210870Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_prob = svm_model.predict_proba(X_val_features)[:, 1]  # Get probability of positive class\n\nauc_score = roc_auc_score(y_val, y_pred_prob)\nprint(f\"AUC Score: {auc_score:.2f}\")\n\nfpr, tpr, thresholds = roc_curve(y_val, y_pred_prob)\n\nplt.figure()\nplt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {auc_score:.2f})')\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('Receiver Operating Characteristic (ROC)')\nplt.legend(loc=\"lower right\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:33:23.212634Z","iopub.execute_input":"2025-01-16T15:33:23.212899Z","iopub.status.idle":"2025-01-16T15:33:23.415441Z","shell.execute_reply.started":"2025-01-16T15:33:23.212875Z","shell.execute_reply":"2025-01-16T15:33:23.414526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict_and_save_csv(preprocess_to_hsv, feature_extractor_densenet, svm_model, csv_path='dense_svm_hsv_wiht_preprocess_input_with_all_df_predictions.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-16T15:33:23.416376Z","iopub.execute_input":"2025-01-16T15:33:23.416709Z","iopub.status.idle":"2025-01-16T15:33:38.184628Z","shell.execute_reply.started":"2025-01-16T15:33:23.416674Z","shell.execute_reply":"2025-01-16T15:33:38.183890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}