{"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":"none","dataSources":[{"sourceId":87323,"databundleVersionId":9934427,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Use pretrained models available in tensorflow.keras.applications, such as:**\n\n\n**EfficientNet:** Scalable and highly accurate.\n\n**InceptionV3:** Excellent for handling subtle image features.\n\n**Xception:** Robust for complex datasets.","metadata":{}},{"cell_type":"markdown","source":"# **🌟Introduction🚗**","metadata":{}},{"cell_type":"markdown","source":" \n**This is Part 3 of my Autonomous Driving Scene Classification project. The goal is to classify scenes as real or fake using deep learning. In previous parts, I used PyTorch for model building, training, and evaluation. In this part, I’m switching to Keras to simplify model creation and deployment.**\n\nThe notebook covers:\n\n**Data Preprocessing:** Loading, augmenting, and splitting the data.\n\n**Model Definition:** Using the pre-trained EfficientNetB0 model.\n\n**Callbacks:** Using EarlyStopping, ModelCheckpoint, and ReduceLROnPlateau.\n\n**Training and Fine-Tuning:** Training the model and evaluating it with AUC-ROC.\n\n**Submission:** Generating predictions and creating the final submission.\n\n**This notebook demonstrates how to use Keras for effective real vs. fake scene classification, optimizing performance with AUC-ROC.**","metadata":{}},{"cell_type":"markdown","source":"# **📚Import Libraries**","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:28.165066Z","iopub.execute_input":"2025-01-19T13:43:28.165347Z","iopub.status.idle":"2025-01-19T13:43:43.915824Z","shell.execute_reply.started":"2025-01-19T13:43:28.165320Z","shell.execute_reply":"2025-01-19T13:43:43.915055Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **🧹📊Data Preparation**","metadata":{}},{"cell_type":"code","source":"# Paths\ndf = pd.read_csv(\"/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv\")\ntrain_dir = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train\"\ntest_dir = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test\"\nsample_submission_csv = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/sample_submission.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:43.917005Z","iopub.execute_input":"2025-01-19T13:43:43.917498Z","iopub.status.idle":"2025-01-19T13:43:43.933632Z","shell.execute_reply.started":"2025-01-19T13:43:43.917473Z","shell.execute_reply":"2025-01-19T13:43:43.932795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['label'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:43.935616Z","iopub.execute_input":"2025-01-19T13:43:43.935967Z","iopub.status.idle":"2025-01-19T13:43:43.959175Z","shell.execute_reply.started":"2025-01-19T13:43:43.935902Z","shell.execute_reply":"2025-01-19T13:43:43.958271Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**🎨 Feature Encoding 🔢**","metadata":{}},{"cell_type":"code","source":"# Encode labels (real/editada -> 1/0)\nlabel_encoder = LabelEncoder()\ndf['label'] = label_encoder.fit_transform(df['label'])\nprint(f\"Label Mapping: {dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:43.960538Z","iopub.execute_input":"2025-01-19T13:43:43.960839Z","iopub.status.idle":"2025-01-19T13:43:43.977521Z","shell.execute_reply.started":"2025-01-19T13:43:43.960815Z","shell.execute_reply":"2025-01-19T13:43:43.976570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:43.978486Z","iopub.execute_input":"2025-01-19T13:43:43.978859Z","iopub.status.idle":"2025-01-19T13:43:44.015099Z","shell.execute_reply.started":"2025-01-19T13:43:43.978818Z","shell.execute_reply":"2025-01-19T13:43:44.014020Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **🔄Train-Test Split**","metadata":{}},{"cell_type":"code","source":"train_data,val_data=train_test_split(df,test_size=0.20,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:44.016231Z","iopub.execute_input":"2025-01-19T13:43:44.016506Z","iopub.status.idle":"2025-01-19T13:43:44.024849Z","shell.execute_reply.started":"2025-01-19T13:43:44.016480Z","shell.execute_reply":"2025-01-19T13:43:44.023769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save split datasets to temporary CSV files (optional)\ntrain_data.to_csv(\"train_split.csv\", index=False)\nval_data.to_csv(\"val_split.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:44.025752Z","iopub.execute_input":"2025-01-19T13:43:44.026028Z","iopub.status.idle":"2025-01-19T13:43:44.053683Z","shell.execute_reply.started":"2025-01-19T13:43:44.026004Z","shell.execute_reply":"2025-01-19T13:43:44.052774Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **📊 Image Generators🖼️**","metadata":{}},{"cell_type":"code","source":"# Image data augmentation\ntrain_datagen = ImageDataGenerator(\n    rescale=1.0/255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True\n)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:44.056445Z","iopub.execute_input":"2025-01-19T13:43:44.056750Z","iopub.status.idle":"2025-01-19T13:43:44.065335Z","shell.execute_reply.started":"2025-01-19T13:43:44.056723Z","shell.execute_reply":"2025-01-19T13:43:44.064291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_datagen = ImageDataGenerator(rescale=1.0/255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:44.067495Z","iopub.execute_input":"2025-01-19T13:43:44.067796Z","iopub.status.idle":"2025-01-19T13:43:44.082192Z","shell.execute_reply.started":"2025-01-19T13:43:44.067770Z","shell.execute_reply":"2025-01-19T13:43:44.081091Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Prepare Generators**","metadata":{}},{"cell_type":"code","source":"# Convert 'label' column values to strings\ntrain_data['label'] = train_data['label'].astype(str)\nval_data['label'] = val_data['label'].astype(str)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_data,\n    directory=train_dir,\n    x_col='image',\n    y_col='label',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:44.083253Z","iopub.execute_input":"2025-01-19T13:43:44.083648Z","iopub.status.idle":"2025-01-19T13:43:45.119163Z","shell.execute_reply.started":"2025-01-19T13:43:44.083607Z","shell.execute_reply":"2025-01-19T13:43:45.118032Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_generator = val_datagen.flow_from_dataframe(\n    val_data,\n    directory=train_dir,\n    x_col='image',\n    y_col='label',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='binary'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:45.120263Z","iopub.execute_input":"2025-01-19T13:43:45.120566Z","iopub.status.idle":"2025-01-19T13:43:45.352377Z","shell.execute_reply.started":"2025-01-19T13:43:45.120538Z","shell.execute_reply":"2025-01-19T13:43:45.351328Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **🧠🔧Define Model**","metadata":{}},{"cell_type":"code","source":"# Step 3: Define the Model\nbase_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\nx = GlobalAveragePooling2D()(base_model.output)\nx = Dense(256, activation='relu')(x)\noutput = Dense(1, activation='sigmoid')(x)  # Binary classification\nmodel = Model(inputs=base_model.input, outputs=output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:45.353346Z","iopub.execute_input":"2025-01-19T13:43:45.353715Z","iopub.status.idle":"2025-01-19T13:43:47.078254Z","shell.execute_reply.started":"2025-01-19T13:43:45.353678Z","shell.execute_reply":"2025-01-19T13:43:47.077089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Freeze base model initially\nbase_model.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:47.079293Z","iopub.execute_input":"2025-01-19T13:43:47.079601Z","iopub.status.idle":"2025-01-19T13:43:47.087873Z","shell.execute_reply.started":"2025-01-19T13:43:47.079574Z","shell.execute_reply":"2025-01-19T13:43:47.086910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['AUC'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:47.089044Z","iopub.execute_input":"2025-01-19T13:43:47.089427Z","iopub.status.idle":"2025-01-19T13:43:47.114213Z","shell.execute_reply.started":"2025-01-19T13:43:47.089388Z","shell.execute_reply":"2025-01-19T13:43:47.113086Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **🚦💾Call Back📉**","metadata":{}},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_auc', mode='max', patience=10, restore_best_weights=True, verbose=1)\nmodel_checkpoint = ModelCheckpoint('best_model.keras', monitor='val_auc', mode='max', save_best_only=True, verbose=1)\nlr_scheduler = ReduceLROnPlateau(monitor='val_auc', mode='max', factor=0.5, patience=5, min_lr=1e-6, verbose=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:47.115295Z","iopub.execute_input":"2025-01-19T13:43:47.115688Z","iopub.status.idle":"2025-01-19T13:43:47.121289Z","shell.execute_reply.started":"2025-01-19T13:43:47.115654Z","shell.execute_reply":"2025-01-19T13:43:47.120251Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **🧠 Fine-Tuning🚀🔧**","metadata":{}},{"cell_type":"code","source":"base_model.trainable = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:47.122373Z","iopub.execute_input":"2025-01-19T13:43:47.122745Z","iopub.status.idle":"2025-01-19T13:43:47.143006Z","shell.execute_reply.started":"2025-01-19T13:43:47.122706Z","shell.execute_reply":"2025-01-19T13:43:47.141516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Recompile with a lower learning rate\nmodel.compile(optimizer=Adam(learning_rate=1e-5), loss='binary_crossentropy', metrics=['AUC'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:47.144194Z","iopub.execute_input":"2025-01-19T13:43:47.144610Z","iopub.status.idle":"2025-01-19T13:43:47.167195Z","shell.execute_reply.started":"2025-01-19T13:43:47.144569Z","shell.execute_reply":"2025-01-19T13:43:47.166127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Fine-tune the model\nfine_tune_history = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=10,\n    callbacks=[early_stopping, model_checkpoint, lr_scheduler]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-19T13:43:47.168248Z","iopub.execute_input":"2025-01-19T13:43:47.168536Z","execution_failed":"2025-01-19T14:38:12.148Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **📸Predictions**","metadata":{}},{"cell_type":"code","source":"def preprocess_test_images(test_dir):\n    test_images = []\n    test_filenames = []\n    for img_name in sorted(os.listdir(test_dir)):\n        img_path = os.path.join(test_dir, img_name)\n        if os.path.isfile(img_path):\n            img = load_img(img_path, target_size=(224, 224))\n            img_array = img_to_array(img) / 255.0\n            test_images.append(img_array)\n            test_filenames.append(img_name)\n    return np.array(test_images), test_filenames\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-01-19T14:38:12.149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_images, test_filenames = preprocess_test_images(test_dir)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-01-19T14:38:12.149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Generate predictions\ntest_predictions = model.predict(test_images)\ntest_labels = (test_predictions >= 0.5).astype(int).ravel()\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-01-19T14:38:12.150Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **💾📜Save Result**","metadata":{}},{"cell_type":"code","source":"# Step 9: Create Submission File\nsubmission = pd.DataFrame({'image': test_filenames, 'label': test_labels})\nsubmission.to_csv('submission_csv', index=False)\n","metadata":{"trusted":true,"execution":{"execution_failed":"2025-01-19T14:38:12.150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true,"execution":{"execution_failed":"2025-01-19T14:38:12.150Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}}]}