{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"}],"dockerImageVersionId":29845,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install tensorflow==2.19.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T12:23:32.741961Z","iopub.execute_input":"2025-05-04T12:23:32.742251Z","iopub.status.idle":"2025-05-04T12:23:36.47138Z","shell.execute_reply.started":"2025-05-04T12:23:32.742194Z","shell.execute_reply":"2025-05-04T12:23:36.470565Z"}},"outputs":[{"name":"stdout","text":"\u001b[31mERROR: Could not find a version that satisfies the requirement tensorflow==2.19.0 (from versions: 0.12.1, 1.0.0, 1.0.1, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.4.0, 1.4.1, 1.5.0, 1.5.1, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.9.0, 1.10.0, 1.10.1, 1.11.0, 1.12.0, 1.12.2, 1.12.3, 1.13.1, 1.13.2, 1.14.0, 1.15.0, 1.15.2, 1.15.3, 1.15.4, 1.15.5, 2.0.0, 2.0.1, 2.0.2, 2.0.3, 2.0.4, 2.1.0, 2.1.1, 2.1.2, 2.1.3, 2.1.4, 2.2.0, 2.2.1, 2.2.2, 2.2.3, 2.3.0, 2.3.1, 2.3.2, 2.3.3, 2.3.4, 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.5.0, 2.5.1, 2.5.2, 2.6.0rc0, 2.6.0rc1, 2.6.0rc2, 2.6.0, 2.6.1, 2.6.2)\u001b[0m\n\u001b[31mERROR: No matching distribution found for tensorflow==2.19.0\u001b[0m\nNote: you may need to restart the kernel to use updated packages.\n","output_type":"stream"}],"execution_count":1},{"cell_type":"code","source":"!pip install mtcnn tensorflow opencv-python pandas numpy scikit-learn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T09:50:22.652762Z","iopub.execute_input":"2025-05-04T09:50:22.653075Z","iopub.status.idle":"2025-05-04T09:50:29.686337Z","shell.execute_reply.started":"2025-05-04T09:50:22.653018Z","shell.execute_reply":"2025-05-04T09:50:29.685628Z"}},"outputs":[{"name":"stdout","text":"Collecting mtcnn\n\u001b[?25l  Downloading https://files.pythonhosted.org/packages/09/d1/2a4269e387edb97484157b872fa8a1953b53dcafbe4842a1967f549ac5ea/mtcnn-0.1.1-py3-none-any.whl (2.3MB)\n\u001b[K     |████████████████████████████████| 2.3MB 5.3MB/s eta 0:00:01\n\u001b[?25hRequirement already satisfied: tensorflow in /opt/conda/lib/python3.6/site-packages (2.1.0rc0)\nRequirement already satisfied: opencv-python in /opt/conda/lib/python3.6/site-packages (4.1.2.30)\nRequirement already satisfied: pandas in /opt/conda/lib/python3.6/site-packages (0.25.3)\nRequirement already satisfied: numpy in /opt/conda/lib/python3.6/site-packages (1.17.4)\nRequirement already satisfied: scikit-learn in /opt/conda/lib/python3.6/site-packages (0.21.3)\nRequirement already satisfied: keras>=2.0.0 in /opt/conda/lib/python3.6/site-packages (from mtcnn) (2.3.1)\nRequirement already satisfied: keras-preprocessing>=1.1.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (1.1.0)\nRequirement already satisfied: tensorboard<2.1.0,>=2.0.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (2.0.2)\nRequirement already satisfied: wheel>=0.26; python_version >= \"3\" in /opt/conda/lib/python3.6/site-packages (from tensorflow) (0.33.6)\nRequirement already satisfied: protobuf>=3.8.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (3.11.0)\nRequirement already satisfied: gast==0.2.2 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (0.2.2)\nRequirement already satisfied: wrapt>=1.11.1 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (1.11.2)\nRequirement already satisfied: absl-py>=0.7.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (0.8.1)\nRequirement already satisfied: astor>=0.6.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (0.8.0)\nRequirement already satisfied: six>=1.12.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (1.13.0)\nRequirement already satisfied: grpcio>=1.8.6 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (1.25.0)\nRequirement already satisfied: termcolor>=1.1.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (1.1.0)\nRequirement already satisfied: keras-applications>=1.0.8 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (1.0.8)\nRequirement already satisfied: opt-einsum>=2.3.2 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (3.1.0)\nRequirement already satisfied: google-pasta>=0.1.6 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (0.1.8)\nRequirement already satisfied: tensorflow-estimator<2.1.0,>=2.0.0 in /opt/conda/lib/python3.6/site-packages (from tensorflow) (2.0.1)\nRequirement already satisfied: python-dateutil>=2.6.1 in /opt/conda/lib/python3.6/site-packages (from pandas) (2.8.0)\nRequirement already satisfied: pytz>=2017.2 in /opt/conda/lib/python3.6/site-packages (from pandas) (2019.3)\nRequirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.6/site-packages (from scikit-learn) (0.14.0)\nRequirement already satisfied: scipy>=0.17.0 in /opt/conda/lib/python3.6/site-packages (from scikit-learn) (1.3.3)\nRequirement already satisfied: h5py in /opt/conda/lib/python3.6/site-packages (from keras>=2.0.0->mtcnn) (2.9.0)\nRequirement already satisfied: pyyaml in /opt/conda/lib/python3.6/site-packages (from keras>=2.0.0->mtcnn) (5.1.2)\nRequirement already satisfied: requests<3,>=2.21.0 in /opt/conda/lib/python3.6/site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow) (2.22.0)\nRequirement already satisfied: markdown>=2.6.8 in /opt/conda/lib/python3.6/site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow) (3.1.1)\nRequirement already satisfied: setuptools>=41.0.0 in /opt/conda/lib/python3.6/site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow) (42.0.1.post20191125)\nRequirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/conda/lib/python3.6/site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow) (0.4.1)\nRequirement already satisfied: google-auth<2,>=1.6.3 in /opt/conda/lib/python3.6/site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow) (1.7.1)\nRequirement already satisfied: werkzeug>=0.11.15 in /opt/conda/lib/python3.6/site-packages (from tensorboard<2.1.0,>=2.0.0->tensorflow) (0.16.0)\nRequirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.6/site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow) (2019.9.11)\nRequirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/lib/python3.6/site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow) (1.24.2)\nRequirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/lib/python3.6/site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow) (3.0.4)\nRequirement already satisfied: idna<2.9,>=2.5 in /opt/conda/lib/python3.6/site-packages (from requests<3,>=2.21.0->tensorboard<2.1.0,>=2.0.0->tensorflow) (2.8)\nRequirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/lib/python3.6/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.1.0,>=2.0.0->tensorflow) (1.3.0)\nRequirement already satisfied: cachetools<3.2,>=2.0.0 in /opt/conda/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow) (3.1.1)\nRequirement already satisfied: rsa<4.1,>=3.1.4 in /opt/conda/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow) (4.0)\nRequirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow) (0.2.7)\nRequirement already satisfied: oauthlib>=3.0.0 in /opt/conda/lib/python3.6/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.1.0,>=2.0.0->tensorflow) (3.1.0)\nRequirement already satisfied: pyasn1>=0.1.3 in /opt/conda/lib/python3.6/site-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.1.0,>=2.0.0->tensorflow) (0.4.8)\nInstalling collected packages: mtcnn\nSuccessfully installed mtcnn-0.1.1\n","output_type":"stream"}],"execution_count":1},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom mtcnn import MTCNN\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import roc_curve, auc, confusion_matrix\nimport seaborn as sns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T09:50:29.688962Z","iopub.execute_input":"2025-05-04T09:50:29.689282Z","iopub.status.idle":"2025-05-04T09:50:35.693567Z","shell.execute_reply.started":"2025-05-04T09:50:29.689224Z","shell.execute_reply":"2025-05-04T09:50:35.692976Z"}},"outputs":[],"execution_count":2},{"cell_type":"code","source":"# Initialize MTCNN face detector\ndetector = MTCNN()\n\n# Paths and setup\nvideo_folder = '../input/deepfake-detection-challenge/train_sample_videos/'\nmetadata_path = '../input/deepfake-detection-challenge/train_sample_videos/metadata.json'\n\ntrain_sample_metadata = pd.read_json(metadata_path).T\n\ntrain_metadata, val_metadata = train_test_split(train_sample_metadata, test_size=0.2, random_state=42)\n\nclass VideoFrameGenerator(Sequence):\n    def __init__(self, metadata, batch_size=32, target_size=(224, 224), shuffle=True):\n        self.metadata = metadata\n        self.batch_size = batch_size\n        self.target_size = target_size\n        self.shuffle = shuffle\n        self.indexes = np.arange(len(self.metadata))\n        self.on_epoch_end()\n    \n    def __len__(self):\n        return int(np.ceil(len(self.metadata) / self.batch_size))\n    \n    def __getitem__(self, index):\n        batch_indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        batch_metadata = self.metadata.iloc[batch_indexes]\n        \n        X, y_labels = self.__data_generation(batch_metadata)\n        return X, y_labels\n    \n    def on_epoch_end(self):\n        if self.shuffle:\n            np.random.shuffle(self.indexes)\n    \n    def __data_generation(self, batch_metadata):\n        X = []\n        y_labels = []\n        \n        for video_name, row in batch_metadata.iterrows():\n            video_path = os.path.join(video_folder, video_name)\n            label = 1 if row['label'] == 'FAKE' else 0\n            \n            cap = cv2.VideoCapture(video_path)\n            while cap.isOpened():\n                ret, frame = cap.read()\n                if not ret:\n                    break\n                \n                frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n                faces = detector.detect_faces(frame_rgb)\n                \n                for face in faces:\n                    x, y, width, height = face['box']\n                    face_img = frame_rgb[y:y+height, x:x+width]\n                    face_img = cv2.resize(face_img, self.target_size)  # Resize face to target_size\n                    face_array = img_to_array(face_img) / 255.0  # Normalize pixel values\n                    \n                    X.append(face_array)\n                    y_labels.append(label)\n                    \n                    if len(X) >= self.batch_size:\n                        cap.release()\n                        return np.array(X), np.array(y_labels)\n            \n            cap.release()\n        \n        while len(X) < self.batch_size:\n            X.append(X[0])\n            y_labels.append(y_labels[0])\n        \n        return np.array(X), np.array(y_labels)\n\nbatch_size = 32\ntrain_generator = VideoFrameGenerator(train_metadata, batch_size=batch_size)\nval_generator = VideoFrameGenerator(val_metadata, batch_size=batch_size)\n\n# Build the model\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')\n])\n\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(\n    train_generator,\n    epochs=5,\n    validation_data=val_generator\n)\n\n# Evaluate the model \nloss, accuracy = model.evaluate(val_generator)\nprint(f\"Validation Loss: {loss}\")\nprint(f\"Validation Accuracy: {accuracy}\")\n\nmodel_save_path = '/kaggle/working/deepfake_detection_model.h5'  \nmodel.save(model_save_path)\nprint(f\"Model saved to {model_save_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T09:50:35.695344Z","iopub.execute_input":"2025-05-04T09:50:35.695667Z","iopub.status.idle":"2025-05-04T10:10:37.990613Z","shell.execute_reply.started":"2025-05-04T09:50:35.69561Z","shell.execute_reply":"2025-05-04T10:10:37.989721Z"}},"outputs":[{"name":"stdout","text":"Train for 10 steps, validate for 3 steps\nEpoch 1/5\n10/10 [==============================] - 221s 22s/step - loss: 2.9999 - accuracy: 0.5562 - val_loss: 0.7916 - val_accuracy: 0.3333\nEpoch 2/5\n10/10 [==============================] - 217s 22s/step - loss: 0.7235 - accuracy: 0.3000 - val_loss: 0.6929 - val_accuracy: 0.6667\nEpoch 3/5\n10/10 [==============================] - 218s 22s/step - loss: 0.6925 - accuracy: 0.6281 - val_loss: 0.6907 - val_accuracy: 0.6667\nEpoch 4/5\n10/10 [==============================] - 217s 22s/step - loss: 0.6899 - accuracy: 0.7000 - val_loss: 0.6802 - val_accuracy: 0.6667\nEpoch 5/5\n10/10 [==============================] - 218s 22s/step - loss: 0.6801 - accuracy: 0.7000 - val_loss: 0.6557 - val_accuracy: 0.6667\n3/3 [==============================] - 52s 17s/step - loss: 0.6557 - accuracy: 0.6667\nValidation Loss: 0.6556945443153381\nValidation Accuracy: 0.6666666865348816\nModel saved to /kaggle/working/deepfake_detection_model.h5\n","output_type":"stream"}],"execution_count":3},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nfrom mtcnn import MTCNN\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.preprocessing.image import img_to_array\n\n# Initialize MTCNN face detector\ndetector = MTCNN()\n\n# Load the trained model\n# model = load_model('path/to/your/trained_model.h5')  # Update with the actual path to your model\n\n# Function to detect and preprocess faces from a video\ndef extract_faces_from_video(video_path, target_size=(224, 224)):\n    cap = cv2.VideoCapture(video_path)\n    faces = []\n\n    while cap.isOpened():\n        ret, frame = cap.read()\n        if not ret:\n            break\n\n        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        detected_faces = detector.detect_faces(frame_rgb)\n\n        for face in detected_faces:\n            x, y, width, height = face['box']\n            face_img = frame_rgb[y:y+height, x:x+width]\n            face_img = cv2.resize(face_img, target_size)\n            face_array = img_to_array(face_img) / 255.0\n            faces.append(face_array)\n    \n    cap.release()\n    return np.array(faces)\n\n# Function to predict if the video is fake or real\ndef predict_video(video_path):\n    faces = extract_faces_from_video(video_path)\n    if len(faces) == 0:\n        print(\"No faces detected in the video.\")\n        return None\n\n    predictions = model.predict(faces)\n    avg_prediction = np.mean(predictions)\n\n    if avg_prediction > 0.5:\n        print(f\"The video '{video_path}' is predicted to be FAKE.\")\n    else:\n        print(f\"The video '{video_path}' is predicted to be REAL.\")\n\n    return avg_prediction\n\n# Test the prediction function with a sample video\nvideo_path = '/kaggle/input/deepfake-detection-challenge/test_videos/aassnaulhq.mp4'  # Update with the actual path to the test video\npredict_video(video_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T10:10:37.992304Z","iopub.execute_input":"2025-05-04T10:10:37.992634Z","iopub.status.idle":"2025-05-04T10:13:20.293051Z","shell.execute_reply.started":"2025-05-04T10:10:37.992562Z","shell.execute_reply":"2025-05-04T10:13:20.292353Z"}},"outputs":[{"name":"stdout","text":"The video '/kaggle/input/deepfake-detection-challenge/test_videos/aassnaulhq.mp4' is predicted to be FAKE.\n","output_type":"stream"},{"execution_count":4,"output_type":"execute_result","data":{"text/plain":"0.5585739"},"metadata":{}}],"execution_count":4},{"cell_type":"markdown","source":"Xception\n","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom mtcnn import MTCNN\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nimport matplotlib.pyplot as plt\n\n# Initialize MTCNN face detector\ndetector = MTCNN()\n\n# Paths and setup\nvideo_folder = '../input/deepfake-detection-challenge/train_sample_videos/'\nmetadata_path = '../input/deepfake-detection-challenge/train_sample_videos/metadata.json'\n\ntrain_sample_metadata = pd.read_json(metadata_path).T\n\ntrain_metadata, val_metadata = train_test_split(train_sample_metadata, test_size=0.2, random_state=42)\n\nclass VideoFrameGenerator(Sequence):\n    def __init__(self, metadata, batch_size=32, target_size=(160, 160), shuffle=True):\n        self.metadata = metadata\n        self.batch_size = batch_size\n        self.target_size = target_size\n        self.shuffle = shuffle\n        self.indexes = np.arange(len(self.metadata))\n        self.on_epoch_end()\n    \n    def __len__(self):\n        return int(np.ceil(len(self.metadata) / self.batch_size))\n    \n    def __getitem__(self, index):\n        batch_indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        batch_metadata = self.metadata.iloc[batch_indexes]\n        \n        X, y_labels = self.__data_generation(batch_metadata)\n        return X, y_labels\n    \n    def on_epoch_end(self):\n        if self.shuffle:\n            np.random.shuffle(self.indexes)\n    \n    def __data_generation(self, batch_metadata):\n        X = []\n        y_labels = []\n        \n        for video_name, row in batch_metadata.iterrows():\n            video_path = os.path.join(video_folder, video_name)\n            label = 1 if row['label'] == 'FAKE' else 0\n            \n            cap = cv2.VideoCapture(video_path)\n            while cap.isOpened():\n                ret, frame = cap.read()\n                if not ret:\n                    break\n                \n                frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n                faces = detector.detect_faces(frame_rgb)\n                \n                for face in faces:\n                    x, y, width, height = face['box']\n                    face_img = frame_rgb[y:y+height, x:x+width]\n                    face_img = cv2.resize(face_img, self.target_size)  # Resize face to target_size\n                    face_array = img_to_array(face_img) / 255.0  # Normalize pixel values\n                    \n                    X.append(face_array)\n                    y_labels.append(label)\n                    \n                    if len(X) >= self.batch_size:\n                        cap.release()\n                        return np.array(X), np.array(y_labels)\n            \n            cap.release()\n        \n        while len(X) < self.batch_size:\n            X.append(X[0])\n            y_labels.append(y_labels[0])\n        \n        return np.array(X), np.array(y_labels)\n\nbatch_size = 32\ntrain_generator = VideoFrameGenerator(train_metadata, batch_size=batch_size)\nval_generator = VideoFrameGenerator(val_metadata, batch_size=batch_size)\n\n# Create the directory to save models if it doesn't exist\nos.makedirs('/kaggle/working/models', exist_ok=True)\n\n# Import Xception model and apply transfer learning\nxception = Xception(\n    include_top=False,\n    weights='imagenet',\n    input_shape=(160, 160, 3)\n)\n\n# Build the model\nxception_model = Sequential([\n    xception,\n    GlobalAveragePooling2D(),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')\n])\n\n# Freeze the layers of the base model\nfor layer in xception_model.layers[0].layers:\n    layer.trainable = False\n\n# Compile the model\nxception_model.compile(\n    loss='binary_crossentropy', \n    optimizer='adam', \n    metrics=['binary_accuracy', 'Recall', 'Precision', 'AUC']\n)\n\n# Define callbacks\nmc = ModelCheckpoint('/kaggle/working/models/xception_model.001.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True)\nes = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)\n\n# Train the model\nxception_history = xception_model.fit(\n    train_generator, \n    epochs=10, \n    validation_data=val_generator, \n    callbacks=[es, mc]\n)\n\n# Evaluate the model\nloss, accuracy, recall, precision, auc = xception_model.evaluate(val_generator)\nprint(f\"Validation Loss: {loss}\")\nprint(f\"Validation Accuracy: {accuracy}\")\nprint(f\"Validation Recall: {recall}\")\nprint(f\"Validation Precision: {precision}\")\nprint(f\"Validation AUC: {auc}\")\n\n# Save the model\nmodel_save_path = '/kaggle/working/models/xception_model.h5'\nxception_model.save(model_save_path)\nprint(f\"Model saved to {model_save_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T10:13:20.294357Z","iopub.execute_input":"2025-05-04T10:13:20.294605Z","iopub.status.idle":"2025-05-04T11:18:10.813002Z","shell.execute_reply.started":"2025-05-04T10:13:20.294557Z","shell.execute_reply":"2025-05-04T11:18:10.812071Z"}},"outputs":[{"name":"stdout","text":"Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5\n83689472/83683744 [==============================] - 1s 0us/step\nTrain for 10 steps, validate for 3 steps\nEpoch 1/10\n 9/10 [==========================>...] - ETA: 26s - loss: 0.6879 - binary_accuracy: 0.6354 - Recall: 0.7634 - Precision: 0.7668 - AUC: 0.3833\nEpoch 00001: val_loss improved from inf to 0.30090, saving model to /kaggle/working/models/xception_model.001.h5\n10/10 [==============================] - 456s 46s/step - loss: 0.6515 - binary_accuracy: 0.6687 - Recall: 0.7891 - Precision: 0.7953 - AUC: 0.4311 - val_loss: 0.3009 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 2/10\n 9/10 [==========================>...] - ETA: 26s - loss: 0.5464 - binary_accuracy: 0.7743 - Recall: 0.9955 - Precision: 0.7770 - AUC: 0.5464      \nEpoch 00002: val_loss improved from 0.30090 to 0.23332, saving model to /kaggle/working/models/xception_model.001.h5\n10/10 [==============================] - 455s 46s/step - loss: 0.5181 - binary_accuracy: 0.7969 - Recall: 0.9961 - Precision: 0.7994 - AUC: 0.5607 - val_loss: 0.2333 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 3/10\n 9/10 [==========================>...] - ETA: 26s - loss: 0.5301 - binary_accuracy: 0.7812 - Recall: 1.0000 - Precision: 0.7805 - AUC: 0.5892    \nEpoch 00003: val_loss improved from 0.23332 to 0.20276, saving model to /kaggle/working/models/xception_model.001.h5\n10/10 [==============================] - 459s 46s/step - loss: 0.5077 - binary_accuracy: 0.8031 - Recall: 1.0000 - Precision: 0.8025 - AUC: 0.5744 - val_loss: 0.2028 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 4/10\n 9/10 [==========================>...] - ETA: 26s - loss: 0.5711 - binary_accuracy: 0.7743 - Recall: 0.9955 - Precision: 0.7770 - AUC: 0.5145\nEpoch 00004: val_loss improved from 0.20276 to 0.18235, saving model to /kaggle/working/models/xception_model.001.h5\n10/10 [==============================] - 457s 46s/step - loss: 0.5353 - binary_accuracy: 0.7969 - Recall: 0.9961 - Precision: 0.7994 - AUC: 0.5195 - val_loss: 0.1823 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 5/10\n 9/10 [==========================>...] - ETA: 26s - loss: 0.3765 - binary_accuracy: 0.8889 - Recall: 1.0000 - Precision: 0.8889 - AUC: 0.5837    \nEpoch 00005: val_loss improved from 0.18235 to 0.13355, saving model to /kaggle/working/models/xception_model.001.h5\n10/10 [==============================] - 457s 46s/step - loss: 0.5554 - binary_accuracy: 0.8000 - Recall: 1.0000 - Precision: 0.8000 - AUC: 0.5140 - val_loss: 0.1336 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 6/10\n 9/10 [==========================>...] - ETA: 27s - loss: 0.4063 - binary_accuracy: 0.8889 - Recall: 1.0000 - Precision: 0.8889 - AUC: 0.5069\nEpoch 00006: val_loss did not improve from 0.13355\n10/10 [==============================] - 455s 45s/step - loss: 0.5202 - binary_accuracy: 0.8000 - Recall: 1.0000 - Precision: 0.8000 - AUC: 0.5816 - val_loss: 0.1811 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 7/10\n 9/10 [==========================>...] - ETA: 26s - loss: 0.5516 - binary_accuracy: 0.7778 - Recall: 1.0000 - Precision: 0.7778 - AUC: 0.5583\nEpoch 00007: val_loss did not improve from 0.13355\n10/10 [==============================] - 451s 45s/step - loss: 0.5236 - binary_accuracy: 0.8000 - Recall: 1.0000 - Precision: 0.8000 - AUC: 0.5477 - val_loss: 0.2587 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\nEpoch 8/10\n 9/10 [==========================>...] - ETA: 27s - loss: 0.4423 - binary_accuracy: 0.8854 - Recall: 0.9961 - Precision: 0.8885 - AUC: 0.4641\nEpoch 00008: val_loss did not improve from 0.13355\n10/10 [==============================] - 449s 45s/step - loss: 0.5218 - binary_accuracy: 0.7969 - Recall: 0.9961 - Precision: 0.7994 - AUC: 0.5506 - val_loss: 0.2821 - val_binary_accuracy: 1.0000 - val_Recall: 1.0000 - val_Precision: 1.0000 - val_AUC: 0.0000e+00\n3/3 [==============================] - 195s 65s/step - loss: 0.1336 - binary_accuracy: 1.0000 - Recall: 1.0000 - Precision: 1.0000 - AUC: 0.0000e+00\nValidation Loss: 0.13355112572511038\nValidation Accuracy: 1.0\nValidation Recall: 1.0\nValidation Precision: 1.0\nValidation AUC: 0.0\nModel saved to /kaggle/working/models/xception_model.h5\n","output_type":"stream"}],"execution_count":5},{"cell_type":"markdown","source":"**ResNet**","metadata":{}},{"cell_type":"code","source":"# Initialize MTCNN face detector\ndetector = MTCNN()\n\n# Paths and setup\nvideo_folder = '../input/deepfake-detection-challenge/train_sample_videos/'\nmetadata_path = '../input/deepfake-detection-challenge/train_sample_videos/metadata.json'\n\ntrain_sample_metadata = pd.read_json(metadata_path).T\ntrain_metadata, val_metadata = train_test_split(train_sample_metadata, test_size=0.2, random_state=42)\n\nclass VideoFrameGenerator(Sequence):\n    def __init__(self, metadata, batch_size=32, target_size=(224, 224), shuffle=True):\n        self.metadata = metadata\n        self.batch_size = batch_size\n        self.target_size = target_size\n        self.shuffle = shuffle\n        self.indexes = np.arange(len(self.metadata))\n        self.on_epoch_end()\n    \n    def __len__(self):\n        return int(np.ceil(len(self.metadata) / self.batch_size))\n    \n    def __getitem__(self, index):\n        batch_indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        batch_metadata = self.metadata.iloc[batch_indexes]\n        \n        X, y_labels = self.__data_generation(batch_metadata)\n        return X, y_labels\n    \n    def on_epoch_end(self):\n        if self.shuffle:\n            np.random.shuffle(self.indexes)\n    \n    def __data_generation(self, batch_metadata):\n        X = []\n        y_labels = []\n        \n        for video_name, row in batch_metadata.iterrows():\n            video_path = os.path.join(video_folder, video_name)\n            label = 1 if row['label'] == 'FAKE' else 0\n            \n            cap = cv2.VideoCapture(video_path)\n            while cap.isOpened():\n                ret, frame = cap.read()\n                if not ret:\n                    break\n                \n                frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n                faces = detector.detect_faces(frame_rgb)\n                \n                for face in faces:\n                    x, y, width, height = face['box']\n                    face_img = frame_rgb[y:y+height, x:x+width]\n                    face_img = cv2.resize(face_img, self.target_size)  # Resize face to target_size\n                    face_array = img_to_array(face_img)\n                    face_array = preprocess_input(face_array)  # Preprocess for ResNet\n                    \n                    X.append(face_array)\n                    y_labels.append(label)\n                    \n                    if len(X) >= self.batch_size:\n                        cap.release()\n                        return np.array(X), np.array(y_labels)\n            \n            cap.release()\n        \n        while len(X) < self.batch_size:\n            X.append(X[0])\n            y_labels.append(y_labels[0])\n        \n        return np.array(X), np.array(y_labels)\n\n# Data Generators\nbatch_size = 16\ntrain_generator = VideoFrameGenerator(train_metadata, batch_size=batch_size)\nval_generator = VideoFrameGenerator(val_metadata, batch_size=batch_size)\n\n# Build the ResNet-50 Model\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\nmodel = Sequential([\n    base_model,\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')\n])\n\nbase_model.trainable = False  # Freeze the base model for transfer learning\n\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(\n    train_generator,\n    epochs=3,\n    validation_data=val_generator\n)\n\n# Evaluate the model\nloss, accuracy = model.evaluate(val_generator)\nprint(f\"Validation Loss: {loss}\")\nprint(f\"Validation Accuracy: {accuracy}\")\n\n# Save the model\nmodel_save_path = '/kaggle/working/deepfake_detection_resnet_model.h5'\nmodel.save(model_save_path)\nprint(f\"Model saved to {model_save_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T11:18:10.814396Z","iopub.execute_input":"2025-05-04T11:18:10.814614Z","iopub.status.idle":"2025-05-04T11:29:42.583263Z","shell.execute_reply.started":"2025-05-04T11:18:10.814576Z","shell.execute_reply":"2025-05-04T11:29:42.582403Z"}},"outputs":[{"name":"stdout","text":"Downloading data from https://github.com/keras-team/keras-applications/releases/download/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\n94773248/94765736 [==============================] - 0s 0us/step\nTrain for 20 steps, validate for 5 steps\nEpoch 1/3\n20/20 [==============================] - 206s 10s/step - loss: 41.2065 - accuracy: 0.6469 - val_loss: 2.2019 - val_accuracy: 0.4250\nEpoch 2/3\n20/20 [==============================] - 204s 10s/step - loss: 0.8146 - accuracy: 0.8781 - val_loss: 0.0668 - val_accuracy: 1.0000\nEpoch 3/3\n20/20 [==============================] - 204s 10s/step - loss: 0.2159 - accuracy: 0.8875 - val_loss: 0.4155 - val_accuracy: 0.7000\n5/5 [==============================] - 45s 9s/step - loss: 0.4155 - accuracy: 0.7000\nValidation Loss: 0.41553472727537155\nValidation Accuracy: 0.699999988079071\nModel saved to /kaggle/working/deepfake_detection_resnet_model.h5\n","output_type":"stream"}],"execution_count":6},{"cell_type":"code","source":"print(tf.__version__)\nprint(tf.keras.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T11:36:52.136849Z","iopub.execute_input":"2025-05-04T11:36:52.137178Z","iopub.status.idle":"2025-05-04T11:36:52.141418Z","shell.execute_reply.started":"2025-05-04T11:36:52.137131Z","shell.execute_reply":"2025-05-04T11:36:52.140699Z"}},"outputs":[{"name":"stdout","text":"2.1.0-rc0\n2.2.4-tf\n","output_type":"stream"}],"execution_count":7},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}