{"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":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\n\n# ----------------- Data Loading and Preprocessing ------------------\ntrain = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv')\ntest = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/sample_submission.csv')\nsub = pd.read_csv('/kaggle/input/cidaut-ai-fake-scene-classification-2024/sample_submission.csv')\n\ntrain_path = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train/'\ntest_path = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test/'\n\n# label binary encoding\ncls_to_idx = {'editada': 0, 'real': 1}\n\ntrain['label'] = [cls_to_idx[x] for x in train['label'].values]\ntrain['image'] = train_path + train['image']\ntest['image'] = test_path + test['image']\n\n\nprint(f\"Training set size: {len(train)}\")\n\n\ndef preprocess_image(image_path, label):\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, (224, 224))\n    image = tf.cast(image, tf.float32) / 255.0  # normalized\n    image = tf.image.per_image_standardization(image)\n    return image, label\n\n\n# create tf dataset\ntrain_ds = tf.data.Dataset.from_tensor_slices((train['image'].values, train['label'].values))\ntest_ds = tf.data.Dataset.from_tensor_slices(test['image'].values)\n\n\nBATCH_SIZE = 32\ntrain_ds = train_ds.map(preprocess_image).shuffle(buffer_size=1000).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n\n#preprocess test data\ndef preprocess_test_image(image_path):\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, (224, 224))\n    image = tf.cast(image, tf.float32) / 255.0  # normalized\n    image = tf.image.per_image_standardization(image)\n    return image\n\ntest_ds= test_ds.map(preprocess_test_image).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n\n\n\n# Add rotation layer\nrotation_layer = layers.RandomRotation(factor=0.3)\n\n\ndef augment_image(image, label):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    image = rotation_layer(image)  # apply rotation\n    return image, label\n\n\ntrain_ds = train_ds.map(augment_image)\n\n# sanity check\nfor images, labels in train_ds.take(1):\n    print('image batch shape: ', images.shape)\n    print('labels batch shape', labels.shape)\n    for i in range(10):\n        plt.subplot(2, 5, i + 1)\n        plt.imshow(images[i])\n        plt.title(f'label {labels[i]}')\n    plt.show()\n\n\n# ------------------Model Building and Training------------------------\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, kernel_regularizer=keras.regularizers.l2(0.02))(x)  # Dense layer before activation\nx = layers.BatchNormalization()(x)  # Added Batch Normalization\nx = layers.ReLU()(x)  # Activation function after Batch Normalization\nx = layers.Dropout(0.4)(x)  # Increased dropout\noutputs = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = keras.Model(inputs, outputs)\n\n# Learning rate schedule and optimizer\ninitial_learning_rate = 1e-4\noptimizer = keras.optimizers.AdamW(learning_rate=initial_learning_rate)\n\n\ndef scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return (lr * tf.math.exp(-0.1)).numpy()\n\n\ncallback = tf.keras.callbacks.LearningRateScheduler(scheduler)\n\n\n# Compile\nmodel.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=[keras.metrics.AUC(name='auc')])\n\n# Train the model\nEPOCHS = 50  # Increased Epoch\nhistory = model.fit(train_ds, epochs=EPOCHS, callbacks=[callback])\n\n\n# Prediction\npreds = model.predict(test_ds)\nsub['label'] = preds\n\nsub.to_csv('submission.csv', index=False)\n\nprint(\"Submission file created: submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-14T11:59:34.822358Z","iopub.execute_input":"2025-01-14T11:59:34.822672Z","iopub.status.idle":"2025-01-14T12:06:04.439688Z","shell.execute_reply.started":"2025-01-14T11:59:34.822645Z","shell.execute_reply":"2025-01-14T12:06:04.438598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}