{"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":30823,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-07T23:55:45.940376Z","iopub.execute_input":"2025-01-07T23:55:45.940697Z","iopub.status.idle":"2025-01-07T23:55:48.604333Z","shell.execute_reply.started":"2025-01-07T23:55:45.940672Z","shell.execute_reply":"2025-01-07T23:55:48.602303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv\"  # Replace with the path to your CSV file\ndf = pd.read_csv(file_path)\n\n# Map the 'label' column to binary values\ndf['label'] = df['label'].map({'real': 1, 'editada': 0})\ndf.to_csv('train_modified.csv', index=False)\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-07T23:55:56.701912Z","iopub.execute_input":"2025-01-07T23:55:56.702297Z","iopub.status.idle":"2025-01-07T23:55:56.746375Z","shell.execute_reply.started":"2025-01-07T23:55:56.702264Z","shell.execute_reply":"2025-01-07T23:55:56.745467Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-07T23:55:59.404542Z","iopub.execute_input":"2025-01-07T23:55:59.404850Z","iopub.status.idle":"2025-01-07T23:56:07.435258Z","shell.execute_reply.started":"2025-01-07T23:55:59.404828Z","shell.execute_reply":"2025-01-07T23:56:07.434375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nclass ImageLabelGenerator(Sequence):\n    def __init__(self, dataframe, batch_size, image_size, image_folder, shuffle=True,augmentation_params=None, **kwargs):\n        super().__init__(**kwargs)\n        self.dataframe = dataframe\n        self.batch_size = batch_size\n        self.image_size = image_size\n        self.image_folder = image_folder  # Folder where images are stored\n        self.shuffle = shuffle\n        self.indexes = np.arange(len(self.dataframe))  # Track indexes for shuffling\n\n        self.augmentation_params = augmentation_params\n        if self.augmentation_params:\n            self.datagen = ImageDataGenerator(**self.augmentation_params)\n        else:\n            self.datagen = ImageDataGenerator()  \n\n    def __len__(self):\n        return int(np.floor(len(self.dataframe) / self.batch_size))  # Number of batches per epoch\n\n    def __getitem__(self, index):\n        # Get the indexes of the images for this batch\n        batch_indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]\n        \n        # Get the image filenames and labels for this batch\n        batch_data = self.dataframe.iloc[batch_indexes]\n        \n        # Load images and their labels\n        images = []\n        labels = []\n        for _, row in batch_data.iterrows():\n            img_filename = row['image']  # Filename column\n            label = row['label']  # Label column\n            img_path = self.image_folder + img_filename  # Construct the full image path\n            img = image.load_img(img_path, target_size=self.image_size)\n            img = image.img_to_array(img) / 255.0  # Normalize the image\n            images.append(img)\n            labels.append(label)\n        \n        return np.array(images), np.array(labels)\n\n    def on_epoch_end(self):\n        if self.shuffle:\n            # Shuffle the indexes at the end of each epoch\n            np.random.shuffle(self.indexes)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-07T23:56:09.679604Z","iopub.execute_input":"2025-01-07T23:56:09.680164Z","iopub.status.idle":"2025-01-07T23:56:09.692794Z","shell.execute_reply.started":"2025-01-07T23:56:09.680135Z","shell.execute_reply":"2025-01-07T23:56:09.691830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Load the CSV file\n# df = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv')\n\n# Split the dataframe into training and validation sets\ntrain_df, val_df = train_test_split(df, test_size=0.2, random_state=42)\n\n# Define the folder where your images are stored\nimage_folder = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train/'  # Update this with the actual path to your images\n\n# Define batch size and image size\nbatch_size = 16\nimage_size = (224, 224)  # ResNet50 expects images of size 224x224\n\naugmentation_params = {\n    'rotation_range': 40,\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}\n# Create the data generators\ntrain_generator = ImageLabelGenerator(train_df, batch_size, image_size, image_folder,shuffle=True,\n    augmentation_params=augmentation_params)\nval_generator = ImageLabelGenerator(val_df, batch_size, image_size, image_folder,shuffle=False)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-07T23:56:15.485994Z","iopub.execute_input":"2025-01-07T23:56:15.486378Z","iopub.status.idle":"2025-01-07T23:56:15.950351Z","shell.execute_reply.started":"2025-01-07T23:56:15.486346Z","shell.execute_reply":"2025-01-07T23:56:15.949274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\n# Build a custom CNN model\nmodel = Sequential([\n    Conv2D(64, (3, 3), activation='relu', kernel_regularizer=l2(0.01), input_shape=(224, 224, 3)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(128, (3, 3), activation='relu', kernel_regularizer=l2(0.01)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(256, (3, 3), activation='relu', kernel_regularizer=l2(0.01)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(2048, activation='relu', kernel_regularizer=l2(0.01)),\n    Dropout(0.5),\n    Dense(1024, activation='relu', kernel_regularizer=l2(0.01)),\n    Dense(1, activation='sigmoid')\n])\n\n\n# Compile the model\nmodel.compile(optimizer=SGD(learning_rate=0.001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])\n\n# Set up learning rate scheduler to adjust the learning rate if validation loss plateaus\nlr_scheduler = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1)\n\n# Train the model\nhistory = model.fit(\n    train_generator,\n    epochs=12,\n    validation_data=val_generator,\n    callbacks=[lr_scheduler]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-07T23:56:23.484642Z","iopub.execute_input":"2025-01-07T23:56:23.485306Z","iopub.status.idle":"2025-01-07T23:57:52.534846Z","shell.execute_reply.started":"2025-01-07T23:56:23.485262Z","shell.execute_reply":"2025-01-07T23:57:52.533773Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom tensorflow.keras.preprocessing import image\n\ndef predict_image(img_path):\n    img = image.load_img(img_path, target_size=(224, 224))  # Load and resize the image\n    img_array = image.img_to_array(img) / 255.0  # Normalize the image\n    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension\n    prediction = model.predict(img_array)  # Make prediction\n    return 1 if prediction[0] > 0.5 else 0\n\n# Define the test folder path\ntest_folder = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test/'  # Replace with the actual path to your test folder\n\n# Get a list of all image filenames in the test folder\nimage_filenames = [f for f in os.listdir(test_folder) if f.endswith(('.jpg', '.png', '.jpeg'))]\n\n# Prepare a list to store results\nresults = []\n\n# Iterate over each image and make predictions\nfor img_name in image_filenames:\n    img_path = os.path.join(test_folder, img_name)  # Construct the full image path\n    prediction = predict_image(img_path)  # Predict if the image is real or fake\n    results.append({'image': img_name, 'label': prediction})  # Append result\n\n# Convert results to a DataFrame\nsubmission_df = pd.DataFrame(results)\n\n# Save the DataFrame to a CSV file\nsubmission_csv_path = 'submission2.csv'\nsubmission_df.to_csv(submission_csv_path, index=False)\n\nprint(f\"Predictions saved to {submission_csv_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T00:01:07.570745Z","iopub.execute_input":"2025-01-08T00:01:07.571133Z","iopub.status.idle":"2025-01-08T00:01:20.802406Z","shell.execute_reply.started":"2025-01-08T00:01:07.571101Z","shell.execute_reply":"2025-01-08T00:01:20.801353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_data = pd.read_csv(\"submission2.csv\")\nsub_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-08T00:01:24.082901Z","iopub.execute_input":"2025-01-08T00:01:24.083276Z","iopub.status.idle":"2025-01-08T00:01:24.094243Z","shell.execute_reply.started":"2025-01-08T00:01:24.083241Z","shell.execute_reply":"2025-01-08T00:01:24.093375Z"}},"outputs":[],"execution_count":null}]}