{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":87323,"databundleVersionId":9934427,"sourceType":"competition"}],"dockerImageVersionId":30823,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications import ResNet50V2\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom sklearn.utils.class_weight import compute_class_weight\nfrom tensorflow.keras.regularizers import l2\nfrom sklearn.metrics import roc_auc_score # Import roc_auc_score\nfrom tensorflow.keras.metrics import AUC\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:30.036314Z","iopub.execute_input":"2025-01-14T11:17:30.036661Z","iopub.status.idle":"2025-01-14T11:17:30.042305Z","shell.execute_reply.started":"2025-01-14T11:17:30.036612Z","shell.execute_reply":"2025-01-14T11:17:30.041421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv\")  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:31.240786Z","iopub.execute_input":"2025-01-14T11:17:31.241085Z","iopub.status.idle":"2025-01-14T11:17:31.248296Z","shell.execute_reply.started":"2025-01-14T11:17:31.241064Z","shell.execute_reply":"2025-01-14T11:17:31.247319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_folder = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train\" \nimage_size = 224","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:31.818010Z","iopub.execute_input":"2025-01-14T11:17:31.818286Z","iopub.status.idle":"2025-01-14T11:17:31.821905Z","shell.execute_reply.started":"2025-01-14T11:17:31.818265Z","shell.execute_reply":"2025-01-14T11:17:31.820909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = []\nlabels = []\n\nfor index, row in df.iterrows():\n    image_path = os.path.join(image_folder, row['image'])\n    img = load_img(image_path, target_size=(image_size, image_size))\n    img_array = img_to_array(img) / 255.0  # Normalize pixel values to [0, 1]\n    images.append(img_array)\n    labels.append(row['label'])\n\n# Convert to numpy arrays\nimages = np.array(images)\nlabels = np.array(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:32.761475Z","iopub.execute_input":"2025-01-14T11:17:32.761834Z","iopub.status.idle":"2025-01-14T11:17:37.647378Z","shell.execute_reply.started":"2025-01-14T11:17:32.761804Z","shell.execute_reply":"2025-01-14T11:17:37.646684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:37.648407Z","iopub.execute_input":"2025-01-14T11:17:37.648614Z","iopub.status.idle":"2025-01-14T11:17:37.653749Z","shell.execute_reply.started":"2025-01-14T11:17:37.648596Z","shell.execute_reply":"2025-01-14T11:17:37.652727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encoder = LabelEncoder()\nlabels = label_encoder.fit_transform(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:37.655429Z","iopub.execute_input":"2025-01-14T11:17:37.655632Z","iopub.status.idle":"2025-01-14T11:17:37.666468Z","shell.execute_reply.started":"2025-01-14T11:17:37.655614Z","shell.execute_reply":"2025-01-14T11:17:37.665723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:37.667405Z","iopub.execute_input":"2025-01-14T11:17:37.667596Z","iopub.status.idle":"2025-01-14T11:17:37.793158Z","shell.execute_reply.started":"2025-01-14T11:17:37.667579Z","shell.execute_reply":"2025-01-14T11:17:37.792427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_weights = compute_class_weight('balanced', classes=np.unique(y_train), y=y_train)\nclass_weights = dict(enumerate(class_weights))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:37.793938Z","iopub.execute_input":"2025-01-14T11:17:37.794157Z","iopub.status.idle":"2025-01-14T11:17:37.799265Z","shell.execute_reply.started":"2025-01-14T11:17:37.794138Z","shell.execute_reply":"2025-01-14T11:17:37.798507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\ndatagen.fit(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:37.799968Z","iopub.execute_input":"2025-01-14T11:17:37.800241Z","iopub.status.idle":"2025-01-14T11:17:37.930453Z","shell.execute_reply.started":"2025-01-14T11:17:37.800219Z","shell.execute_reply":"2025-01-14T11:17:37.929523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_counts = df['label'].value_counts()  # Count occurrences of each unique value\nprint(unique_counts)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:17:39.548052Z","iopub.execute_input":"2025-01-14T11:17:39.548330Z","iopub.status.idle":"2025-01-14T11:17:39.554259Z","shell.execute_reply.started":"2025-01-14T11:17:39.548308Z","shell.execute_reply":"2025-01-14T11:17:39.553291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.applications import InceptionV3\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.regularizers import l2\n\nIMG_SIZE = (224, 224)\n# Load pre-trained InceptionV3 (or other model)\nbase_model = keras.applications.InceptionV3(\n    include_top=False,\n    weights='imagenet',\n    input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3)\n)\n\n# Freeze the initial layers\nfor layer in base_model.layers[:100]:\n    layer.trainable = False\n\n# Add custom layers for classification\ninputs = keras.Input(shape=(IMG_SIZE[0], IMG_SIZE[1], 3))\nx = base_model(inputs, training=False)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.7)(x) # Increased dropout\nx = layers.Dense(64, activation='relu', kernel_regularizer=keras.regularizers.l2(0.02))(x) # Increased L2 regularization\nx = layers.Dropout(0.4)(x)  # Increased dropout\nx = layers.Dense(32, activation='relu', kernel_regularizer=keras.regularizers.l2(0.02))(x) # Increased L2 regularization\nx = layers.Dropout(0.4)(x)  # Increased dropout\n\noutputs = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = keras.Model(inputs, outputs)\n# Print the model summary\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:40:22.380377Z","iopub.execute_input":"2025-01-14T11:40:22.380727Z","iopub.status.idle":"2025-01-14T11:40:23.933875Z","shell.execute_reply.started":"2025-01-14T11:40:22.380696Z","shell.execute_reply":"2025-01-14T11:40:23.933019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = datagen.flow(X_train, y_train, batch_size=32)  # Adjust batch_size as needed\nval_ds = datagen.flow(X_val, y_val, batch_size=32)  # Adjust batch_size as needed\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:40:24.836203Z","iopub.execute_input":"2025-01-14T11:40:24.836507Z","iopub.status.idle":"2025-01-14T11:40:24.840631Z","shell.execute_reply.started":"2025-01-14T11:40:24.836485Z","shell.execute_reply":"2025-01-14T11:40:24.839701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\n# Learning rate schedule and optimizer\ninitial_learning_rate = 1e-4\noptimizer = keras.optimizers.AdamW(learning_rate=initial_learning_rate)\n\ndef scheduler(epoch, lr):\n  if epoch < 10:\n    return lr\n  else:\n    return (lr * tf.math.exp(-0.1)).numpy()\n\ncallback = tf.keras.callbacks.LearningRateScheduler(scheduler)\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_auc', factor=0.4, patience=4, min_lr=1e-7, mode='max') # Adjusted parameters\nes = tf.keras.callbacks.EarlyStopping(monitor='val_auc', mode='max', patience=10, restore_best_weights=True) # Adjusted parameters\n\n# compile\nmodel.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=[keras.metrics.AUC(name='auc')])\n# train the model\nEPOCHS = 50 # Increased Epoch\nhistory = model.fit(train_ds, validation_data=val_ds, epochs=EPOCHS, callbacks=[callback, reduce_lr,es])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:40:28.775775Z","iopub.execute_input":"2025-01-14T11:40:28.776089Z","iopub.status.idle":"2025-01-14T11:46:54.791197Z","shell.execute_reply.started":"2025-01-14T11:40:28.776062Z","shell.execute_reply":"2025-01-14T11:46:54.790473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ny_pred_probs = model.predict(X_val)\n\n# Calculate and print ROC AUC\nroc_auc = roc_auc_score(y_val, y_pred_probs)\nprint(f\"ROC AUC: {roc_auc}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:46:54.792482Z","iopub.execute_input":"2025-01-14T11:46:54.792795Z","iopub.status.idle":"2025-01-14T11:47:05.233279Z","shell.execute_reply.started":"2025-01-14T11:46:54.792771Z","shell.execute_reply":"2025-01-14T11:47:05.232509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_folder = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test\" \ntest_images = os.listdir(test_image_folder)\n\nsubmission = []\n\n\nfor test_image in test_images:\n    img_path = os.path.join(test_image_folder, test_image)\n    img = load_img(img_path, target_size=(224, 224)) \n    img_array = img_to_array(img) / 255.0           \n    img_array = np.expand_dims(img_array, axis=0)    \n    \n\n    prediction = model.predict(img_array)\n    label = 1 if prediction[0][0] > 0.5 else 0 \n    \n\n    submission.append({\"image\": test_image, \"label\": label})\n\n\nsubmission_df = pd.DataFrame(submission)\n\n# save to CSV\nsubmission_csv_path = \"submission.csv\"\nsubmission_df.to_csv(submission_csv_path, index=False)\n\nprint(f\"Submission file saved to {submission_csv_path}\")\nsubmission_df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:47:05.234591Z","iopub.execute_input":"2025-01-14T11:47:05.234872Z","iopub.status.idle":"2025-01-14T11:47:20.538707Z","shell.execute_reply.started":"2025-01-14T11:47:05.234851Z","shell.execute_reply":"2025-01-14T11:47:20.537992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}