{"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":"code","source":"# TensorFlow and Keras Imports\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers,models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.applications import EfficientNetB0\n\n# Data Handling Libraries\nimport numpy as np\nimport pandas as pd\nfrom sklearn.utils.class_weight import compute_class_weight\n\n# Visualization Libraries\nimport matplotlib.pyplot as plt\n\n# File and Operating System Libraries\nimport os\n\n# Warnings Management\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# GPU Configuration\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\nprint(tf.config.list_physical_devices('GPU'))\n\nTRAIN_IMG_PATH = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train/'\nTEST_IMG_PATH = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test/'\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:16:43.423209Z","iopub.execute_input":"2025-01-18T15:16:43.423488Z","iopub.status.idle":"2025-01-18T15:16:43.429219Z","shell.execute_reply.started":"2025-01-18T15:16:43.423466Z","shell.execute_reply":"2025-01-18T15:16:43.428193Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_metadata = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv')\ntest_metadata = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:16:45.599831Z","iopub.execute_input":"2025-01-18T15:16:45.600171Z","iopub.status.idle":"2025-01-18T15:16:45.620341Z","shell.execute_reply.started":"2025-01-18T15:16:45.600147Z","shell.execute_reply":"2025-01-18T15:16:45.619728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_metadata.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:16:46.010106Z","iopub.execute_input":"2025-01-18T15:16:46.010415Z","iopub.status.idle":"2025-01-18T15:16:46.039335Z","shell.execute_reply.started":"2025-01-18T15:16:46.010391Z","shell.execute_reply":"2025-01-18T15:16:46.038512Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_metadata.label.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:16:48.186059Z","iopub.execute_input":"2025-01-18T15:16:48.186354Z","iopub.status.idle":"2025-01-18T15:16:48.193964Z","shell.execute_reply.started":"2025-01-18T15:16:48.186332Z","shell.execute_reply":"2025-01-18T15:16:48.193164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compute class weights based on the training labels\nclass_weights = compute_class_weight(\n    'balanced',  # This will automatically adjust for class imbalance\n    classes=np.unique(train_metadata.label),  # Unique classes in the target\n    y=train_metadata.label  # The target class labels for training\n)\n\n# Create a dictionary mapping each class to its weight\nclass_weight_dict = {i: class_weights[i] for i in range(len(class_weights))}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:16:48.461061Z","iopub.execute_input":"2025-01-18T15:16:48.461370Z","iopub.status.idle":"2025-01-18T15:16:48.493738Z","shell.execute_reply.started":"2025-01-18T15:16:48.461348Z","shell.execute_reply":"2025-01-18T15:16:48.493087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def custom_preprocessing(image):\n    # Normalize with mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]\n    image = image / 255.0  # Rescale to [0, 1]\n    mean = np.array([0.485, 0.456, 0.406])\n    std = np.array([0.229, 0.224, 0.225])\n    image = (image - mean) / std\n    return image\n\ntrain_datagen = ImageDataGenerator(\n    rotation_range=15,            # Random rotation\n    horizontal_flip=True,         # Random horizontal flip\n    brightness_range=[0.8, 1.2],  # Approximation for brightness jitter\n    preprocessing_function=custom_preprocessing,  # Custom normalization\n    validation_split=0.2          # Validation split\n)\n\ndatagen = ImageDataGenerator(rescale=1./255)\n\ntrain_img = train_datagen.flow_from_dataframe(train_metadata,\n    directory=TRAIN_IMG_PATH,\n    x_col='image',\n    y_col='label',\n    weight_col=None,\n    target_size=(512, 512),\n    batch_size=8,\n    class_mode='binary',\n    subset='training')\n\nval_img = train_datagen.flow_from_dataframe(train_metadata,\n    directory=TRAIN_IMG_PATH,\n    x_col='image',\n    y_col='label',\n    weight_col=None,\n    target_size=(512, 512),\n    batch_size=8,\n    class_mode='binary',\n    subset='validation')\n\ntest_img = datagen.flow_from_dataframe(test_metadata,\n    directory=TEST_IMG_PATH,\n    x_col='image',\n    y_col=None,\n    weight_col=None,\n    target_size=(512, 512),\n    batch_size=8,\n    class_mode=None,\n    shuffle='False')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:17:32.875440Z","iopub.execute_input":"2025-01-18T15:17:32.875762Z","iopub.status.idle":"2025-01-18T15:17:34.498701Z","shell.execute_reply.started":"2025-01-18T15:17:32.875737Z","shell.execute_reply":"2025-01-18T15:17:34.497971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = {v: k for k, v in train_img.class_indices.items()}\nclass_names","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:17:39.872410Z","iopub.execute_input":"2025-01-18T15:17:39.872718Z","iopub.status.idle":"2025-01-18T15:17:39.878243Z","shell.execute_reply.started":"2025-01-18T15:17:39.872694Z","shell.execute_reply":"2025-01-18T15:17:39.877323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"_, ax = plt.subplots(2,4,figsize=(8,4))\nax = ax.flatten()\nimgs, clss = next(iter(train_img))\nfor i in range(8):\n    ax[i].imshow(imgs[i])\n    ax[i].set_title(class_names[clss[i]])\n    ax[i].set_xticks([])\n    ax[i].set_yticks([])\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:18:28.612108Z","iopub.execute_input":"2025-01-18T15:18:28.612427Z","iopub.status.idle":"2025-01-18T15:18:29.558188Z","shell.execute_reply.started":"2025-01-18T15:18:28.612403Z","shell.execute_reply":"2025-01-18T15:18:29.557191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_shape = (512,512,3)\nnum_classes = 1\n\nimage_input = layers.Input(shape=input_shape)\neffnet = EfficientNetB0(weights='imagenet', include_top=False, input_shape=input_shape)\nx = effnet(image_input)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Flatten()(x)  \nx = layers.Dense(1024, activation='relu')(x) \nx = layers.Dense(num_classes,activation='sigmoid')(x)\nmodel =  models.Model(inputs=image_input, outputs=x)\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:19:19.341629Z","iopub.execute_input":"2025-01-18T15:19:19.341949Z","iopub.status.idle":"2025-01-18T15:19:22.401479Z","shell.execute_reply.started":"2025-01-18T15:19:19.341924Z","shell.execute_reply":"2025-01-18T15:19:22.400673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compiling the model with the combined loss and metrics\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4), \n              loss = tf.keras.losses.BinaryCrossentropy(from_logits=False),\n              metrics=['accuracy','AUC']) \n\nchkpnt_loss = ModelCheckpoint(\n    'best_model_loss.keras',            # Path to save the model\n    monitor='val_loss',         # Metric to monitor \n    verbose=1,                  # Print messages when saving the model\n    save_best_only=True,        # Save only the best model (with highest metric)\n    mode='min',                 \n    save_weights_only=False,     # Save the entire model (not just weights)\n)\n\nchkpnt_auc = ModelCheckpoint(\n    'best_model_auc.keras',            # Path to save the model\n    monitor='val_AUC',         # Metric to monitor \n    verbose=1,                  # Print messages when saving the model\n    save_best_only=True,        # Save only the best model (with highest metric)\n    mode='max',                 # 'max' means the model with the highest metric score will be saved\n    save_weights_only=False,     # Save the entire model (not just weights)\n)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:19:34.594335Z","iopub.execute_input":"2025-01-18T15:19:34.594688Z","iopub.status.idle":"2025-01-18T15:19:34.610275Z","shell.execute_reply.started":"2025-01-18T15:19:34.594641Z","shell.execute_reply":"2025-01-18T15:19:34.609305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_img,\n                    validation_data=val_img,\n                    epochs=64,\n                    class_weight=class_weight_dict,\n                    callbacks=[chkpnt_loss,chkpnt_auc])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T15:19:44.144496Z","iopub.execute_input":"2025-01-18T15:19:44.144802Z","iopub.status.idle":"2025-01-18T15:23:20.819524Z","shell.execute_reply.started":"2025-01-18T15:19:44.144769Z","shell.execute_reply":"2025-01-18T15:23:20.818735Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training & validation loss values\nplt.figure(figsize=(8,6))\nplt.subplot(221)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.subplot(222)\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model Accuracy')\nplt.ylabel('Metric Value')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.subplot(223)\nplt.plot(history.history['AUC'], label='Train AUC')\nplt.plot(history.history['val_AUC'], label='Validation AUC')\nplt.title('Model AUC')\nplt.ylabel('Metric Value')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T16:28:09.134465Z","iopub.execute_input":"2025-01-18T16:28:09.134769Z","iopub.status.idle":"2025-01-18T16:28:09.712984Z","shell.execute_reply.started":"2025-01-18T16:28:09.134747Z","shell.execute_reply":"2025-01-18T16:28:09.712075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imgs,clss = next(iter(val_img))\npred_prob = np.squeeze(model.predict(imgs,verbose=0))\n\nfig, ax = plt.subplots(2,4,figsize=(8,4))\nax = ax.flatten()\nfor i in range(8):\n    ax[i].imshow(imgs[i])\n    ax[i].set_xticks([])\n    ax[i].set_yticks([])\n\n    # Get the actual class from one-hot encoding (if it's one-hot encoded)\n    ax[i].set_title(f'Actual: {class_names[clss[i]]}\\n'\n                    f'Pred: {class_names[np.round(pred_prob[i])]} ({pred_prob[i]:0.2f})', fontsize=9)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T16:24:51.771448Z","iopub.execute_input":"2025-01-18T16:24:51.771803Z","iopub.status.idle":"2025-01-18T16:24:52.663719Z","shell.execute_reply.started":"2025-01-18T16:24:51.771772Z","shell.execute_reply":"2025-01-18T16:24:52.662668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_prob = np.squeeze(model.predict(test_img,verbose=0))\ntest_metadata.label = pred_prob\ntest_metadata.to_csv('submision.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-18T16:25:21.062433Z","iopub.execute_input":"2025-01-18T16:25:21.062799Z","iopub.status.idle":"2025-01-18T16:25:34.893209Z","shell.execute_reply.started":"2025-01-18T16:25:21.062770Z","shell.execute_reply":"2025-01-18T16:25:34.892310Z"}},"outputs":[],"execution_count":null}]}