{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":11086880,"sourceType":"datasetVersion","datasetId":6910339},{"sourceId":11086905,"sourceType":"datasetVersion","datasetId":6910356},{"sourceId":11087939,"sourceType":"datasetVersion","datasetId":6911132},{"sourceId":12507579,"sourceType":"datasetVersion","datasetId":7894303}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!python --version","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:05.195965Z","iopub.execute_input":"2025-07-18T11:47:05.196493Z","iopub.status.idle":"2025-07-18T11:47:05.324744Z","shell.execute_reply.started":"2025-07-18T11:47:05.196468Z","shell.execute_reply":"2025-07-18T11:47:05.323841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Install Required Libraries\n\n!pip install --no-deps facenet-pytorch -q\n\n!pip install --no-deps face-recognition -q\n\n!pip install --no-deps face_recognition_models -q\n\n!pip install --no-deps scikit-image -q\n\n!pip install --no-deps tensorboardX -q\n\n!pip install --no-deps pytorch_toolbelt -q\n\n!git clone https://github.com/NVIDIA/apex\n%cd apex\n!pip install -r requirements.txt -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:06.267062Z","iopub.execute_input":"2025-07-18T11:47:06.267449Z","iopub.status.idle":"2025-07-18T11:47:42.132684Z","shell.execute_reply.started":"2025-07-18T11:47:06.267372Z","shell.execute_reply":"2025-07-18T11:47:42.131923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/input/preprocessing/')\nsys.path.append('/kaggle/input/')\nsys.path.append('/kaggle/input/training/')\nsys.path.append('/kaggle/input/training/datasets/')\nsys.path.append('/kaggle/input/training/pipelines/')\nsys.path.append('/kaggle/input/training/tools/')\nsys.path.append('/kaggle/input/training/transforms/')\nsys.path.append('/kaggle/input/weights/')\nsys.path.append('/kaggle/input/weights/weights/')\nsys.path.append('/kaggle/input/weights/weights')\nsys.path.append('/kaggle/input/weights')\nsys.path.append('/kaggle/input/weights/')\nsys.path.append('/kaggle/input/weights/weights/final_111_DeepFakeClassifier_tf_efficientnet_b7_ns_0_36')\nsys.path.append('/kaggle/input/weights/weights/final_555_DeepFakeClassifier_tf_efficientnet_b7_ns_0_19')\nsys.path.append('/kaggle/input/weights/weights/final_777_DeepFakeClassifier_tf_efficientnet_b7_ns_0_29')\nsys.path.append('/kaggle/input/weights/weights/final_999_DeepFakeClassifier_tf_efficientnet_b7_ns_0_23')\nsys.path.append('/kaggle/input/weights/weights/final_888_DeepFakeClassifier_tf_efficientnet_b7_ns_0_40')\nsys.path.append('/kaggle/input/weights/weights/final_888_DeepFakeClassifier_tf_efficientnet_b7_ns_0_37')\nsys.path.append('/kaggle/input/weights/weights/final_777_DeepFakeClassifier_tf_efficientnet_b7_ns_0_31')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:42.134358Z","iopub.execute_input":"2025-07-18T11:47:42.134647Z","iopub.status.idle":"2025-07-18T11:47:42.140392Z","shell.execute_reply.started":"2025-07-18T11:47:42.134620Z","shell.execute_reply":"2025-07-18T11:47:42.139865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import training","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:42.141144Z","iopub.execute_input":"2025-07-18T11:47:42.141311Z","iopub.status.idle":"2025-07-18T11:47:42.169066Z","shell.execute_reply.started":"2025-07-18T11:47:42.141296Z","shell.execute_reply":"2025-07-18T11:47:42.168433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip uninstall albumentations -y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:42.170586Z","iopub.execute_input":"2025-07-18T11:47:42.171112Z","iopub.status.idle":"2025-07-18T11:47:43.530907Z","shell.execute_reply.started":"2025-07-18T11:47:42.171095Z","shell.execute_reply":"2025-07-18T11:47:43.530126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install albumentations==2.0.4 -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:43.531867Z","iopub.execute_input":"2025-07-18T11:47:43.532061Z","iopub.status.idle":"2025-07-18T11:47:47.477204Z","shell.execute_reply.started":"2025-07-18T11:47:43.532037Z","shell.execute_reply":"2025-07-18T11:47:47.476196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import albumentations as A\nA.__version__","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:47.478331Z","iopub.execute_input":"2025-07-18T11:47:47.478628Z","iopub.status.idle":"2025-07-18T11:47:52.773239Z","shell.execute_reply.started":"2025-07-18T11:47:47.478598Z","shell.execute_reply":"2025-07-18T11:47:52.772449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:52.774177Z","iopub.execute_input":"2025-07-18T11:47:52.774641Z","iopub.status.idle":"2025-07-18T11:47:52.906150Z","shell.execute_reply.started":"2025-07-18T11:47:52.774612Z","shell.execute_reply":"2025-07-18T11:47:52.905306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import argparse\nimport os\nimport re\nimport time\nfrom tqdm import tqdm\nimport torch\nimport pandas as pd\nfrom training.zoo.classifiers import DeepFakeClassifier\nimport os\n\nimport cv2\nimport numpy as np\nimport torch\nfrom PIL import Image\nfrom albumentations.augmentations.functional import image_compression\nfrom facenet_pytorch.models.mtcnn import MTCNN\nfrom concurrent.futures import ThreadPoolExecutor\n\nfrom torchvision.transforms import Normalize\nimport json\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:52.907182Z","iopub.execute_input":"2025-07-18T11:47:52.907491Z","iopub.status.idle":"2025-07-18T11:47:59.577211Z","shell.execute_reply.started":"2025-07-18T11:47:52.907453Z","shell.execute_reply":"2025-07-18T11:47:59.576670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean = [0.485, 0.456, 0.406]\nstd = [0.229, 0.224, 0.225]\nnormalize_transform = Normalize(mean, std)\n\n\nclass VideoReader:\n    \"\"\"Helper class for reading one or more frames from a video file.\"\"\"\n\n    def __init__(self, verbose=True, insets=(0, 0)):\n        \"\"\"Creates a new VideoReader.\n\n        Arguments:\n            verbose: whether to print warnings and error messages\n            insets: amount to inset the image by, as a percentage of\n                (width, height). This lets you \"zoom in\" to an image\n                to remove unimportant content around the borders.\n                Useful for face detection, which may not work if the\n                faces are too small.\n        \"\"\"\n        self.verbose = verbose\n        self.insets = insets\n\n    def read_frames(self, path, num_frames, jitter=0, seed=None):\n        \"\"\"Reads frames that are always evenly spaced throughout the video.\n\n        Arguments:\n            path: the video file\n            num_frames: how many frames to read, -1 means the entire video\n                (warning: this will take up a lot of memory!)\n            jitter: if not 0, adds small random offsets to the frame indices;\n                this is useful so we don't always land on even or odd frames\n            seed: random seed for jittering; if you set this to a fixed value,\n                you probably want to set it only on the first video\n        \"\"\"\n        assert num_frames > 0\n\n        capture = cv2.VideoCapture(path)\n        frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))\n        if frame_count <= 0: return None\n\n        frame_idxs = np.linspace(0, frame_count - 1, num_frames, endpoint=True, dtype=np.int32)\n        if jitter > 0:\n            np.random.seed(seed)\n            jitter_offsets = np.random.randint(-jitter, jitter, len(frame_idxs))\n            frame_idxs = np.clip(frame_idxs + jitter_offsets, 0, frame_count - 1)\n\n        result = self._read_frames_at_indices(path, capture, frame_idxs)\n        capture.release()\n        return result\n\n    def read_random_frames(self, path, num_frames, seed=None):\n        \"\"\"Picks the frame indices at random.\n\n        Arguments:\n            path: the video file\n            num_frames: how many frames to read, -1 means the entire video\n                (warning: this will take up a lot of memory!)\n        \"\"\"\n        assert num_frames > 0\n        np.random.seed(seed)\n\n        capture = cv2.VideoCapture(path)\n        frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))\n        if frame_count <= 0: return None\n\n        frame_idxs = sorted(np.random.choice(np.arange(0, frame_count), num_frames))\n        result = self._read_frames_at_indices(path, capture, frame_idxs)\n\n        capture.release()\n        return result\n\n    def read_frames_at_indices(self, path, frame_idxs):\n        \"\"\"Reads frames from a video and puts them into a NumPy array.\n\n        Arguments:\n            path: the video file\n            frame_idxs: a list of frame indices. Important: should be\n                sorted from low-to-high! If an index appears multiple\n                times, the frame is still read only once.\n\n        Returns:\n            - a NumPy array of shape (num_frames, height, width, 3)\n            - a list of the frame indices that were read\n\n        Reading stops if loading a frame fails, in which case the first\n        dimension returned may actually be less than num_frames.\n\n        Returns None if an exception is thrown for any reason, or if no\n        frames were read.\n        \"\"\"\n        assert len(frame_idxs) > 0\n        capture = cv2.VideoCapture(path)\n        result = self._read_frames_at_indices(path, capture, frame_idxs)\n        capture.release()\n        return result\n\n    def _read_frames_at_indices(self, path, capture, frame_idxs):\n        try:\n            frames = []\n            idxs_read = []\n            for frame_idx in range(frame_idxs[0], frame_idxs[-1] + 1):\n                # Get the next frame, but don't decode if we're not using it.\n                ret = capture.grab()\n                if not ret:\n                    if self.verbose:\n                        print(\"Error grabbing frame %d from movie %s\" % (frame_idx, path))\n                    break\n\n                # Need to look at this frame?\n                current = len(idxs_read)\n                if frame_idx == frame_idxs[current]:\n                    ret, frame = capture.retrieve()\n                    if not ret or frame is None:\n                        if self.verbose:\n                            print(\"Error retrieving frame %d from movie %s\" % (frame_idx, path))\n                        break\n\n                    frame = self._postprocess_frame(frame)\n                    frames.append(frame)\n                    idxs_read.append(frame_idx)\n\n            if len(frames) > 0:\n                return np.stack(frames), idxs_read\n            if self.verbose:\n                print(\"No frames read from movie %s\" % path)\n            return None\n        except:\n            if self.verbose:\n                print(\"Exception while reading movie %s\" % path)\n            return None\n\n    def read_middle_frame(self, path):\n        \"\"\"Reads the frame from the middle of the video.\"\"\"\n        capture = cv2.VideoCapture(path)\n        frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))\n        result = self._read_frame_at_index(path, capture, frame_count // 2)\n        capture.release()\n        return result\n\n    def read_frame_at_index(self, path, frame_idx):\n        \"\"\"Reads a single frame from a video.\n\n        If you just want to read a single frame from the video, this is more\n        efficient than scanning through the video to find the frame. However,\n        for reading multiple frames it's not efficient.\n\n        My guess is that a \"streaming\" approach is more efficient than a\n        \"random access\" approach because, unless you happen to grab a keyframe,\n        the decoder still needs to read all the previous frames in order to\n        reconstruct the one you're asking for.\n\n        Returns a NumPy array of shape (1, H, W, 3) and the index of the frame,\n        or None if reading failed.\n        \"\"\"\n        capture = cv2.VideoCapture(path)\n        result = self._read_frame_at_index(path, capture, frame_idx)\n        capture.release()\n        return result\n\n    def _read_frame_at_index(self, path, capture, frame_idx):\n        capture.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)\n        ret, frame = capture.read()\n        if not ret or frame is None:\n            if self.verbose:\n                print(\"Error retrieving frame %d from movie %s\" % (frame_idx, path))\n            return None\n        else:\n            frame = self._postprocess_frame(frame)\n            return np.expand_dims(frame, axis=0), [frame_idx]\n\n    def _postprocess_frame(self, frame):\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n\n        if self.insets[0] > 0:\n            W = frame.shape[1]\n            p = int(W * self.insets[0])\n            frame = frame[:, p:-p, :]\n\n        if self.insets[1] > 0:\n            H = frame.shape[1]\n            q = int(H * self.insets[1])\n            frame = frame[q:-q, :, :]\n\n        return frame\n\n\nclass FaceExtractor:\n    def __init__(self, video_read_fn):\n        self.video_read_fn = video_read_fn\n        device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n        self.detector = MTCNN(margin=0, thresholds=[0.7, 0.8, 0.8], device=device)\n\n    def process_videos(self, input_dir, filenames, video_idxs):\n        videos_read = []\n        frames_read = []\n        frames = []\n        results = []\n        for video_idx in video_idxs:\n            # Read the full-size frames from this video.\n            filename = filenames[video_idx]\n            video_path = os.path.join(input_dir, filename)\n            result = self.video_read_fn(video_path)\n            # Error? Then skip this video.\n            if result is None: continue\n\n            videos_read.append(video_idx)\n\n            # Keep track of the original frames (need them later).\n            my_frames, my_idxs = result\n\n            frames.append(my_frames)\n            frames_read.append(my_idxs)\n            for i, frame in enumerate(my_frames):\n                h, w = frame.shape[:2]\n                img = Image.fromarray(frame.astype(np.uint8))\n                img = img.resize(size=[s // 2 for s in img.size])\n\n                batch_boxes, probs = self.detector.detect(img, landmarks=False)\n\n                faces = []\n                scores = []\n                if batch_boxes is None:\n                    continue\n                for bbox, score in zip(batch_boxes, probs):\n                    if bbox is not None:\n                        xmin, ymin, xmax, ymax = [int(b * 2) for b in bbox]\n                        w = xmax - xmin\n                        h = ymax - ymin\n                        p_h = h // 3\n                        p_w = w // 3\n                        crop = frame[max(ymin - p_h, 0):ymax + p_h, max(xmin - p_w, 0):xmax + p_w]\n                        faces.append(crop)\n                        scores.append(score)\n\n                frame_dict = {\"video_idx\": video_idx,\n                              \"frame_idx\": my_idxs[i],\n                              \"frame_w\": w,\n                              \"frame_h\": h,\n                              \"faces\": faces,\n                              \"scores\": scores}\n                results.append(frame_dict)\n\n        return results\n\n    def process_video(self, video_path):\n        \"\"\"Convenience method for doing face extraction on a single video.\"\"\"\n        input_dir = os.path.dirname(video_path)\n        filenames = [os.path.basename(video_path)]\n        return self.process_videos(input_dir, filenames, [0])\n\n\n\ndef confident_strategy(pred, t=0.8):\n    pred = np.array(pred)\n    sz = len(pred)\n    fakes = np.count_nonzero(pred > t)\n    # 11 frames are detected as fakes with high probability\n    if fakes > sz // 2.5 and fakes > 11:\n        return np.mean(pred[pred > t])\n    elif np.count_nonzero(pred < 0.2) > 0.9 * sz:\n        return np.mean(pred[pred < 0.2])\n    else:\n        return np.mean(pred)\n\nstrategy = confident_strategy\n\n\ndef put_to_center(img, input_size):\n    img = img[:input_size, :input_size]\n    image = np.zeros((input_size, input_size, 3), dtype=np.uint8)\n    start_w = (input_size - img.shape[1]) // 2\n    start_h = (input_size - img.shape[0]) // 2\n    image[start_h:start_h + img.shape[0], start_w: start_w + img.shape[1], :] = img\n    return image\n\n\ndef isotropically_resize_image(img, size, interpolation_down=cv2.INTER_AREA, interpolation_up=cv2.INTER_CUBIC):\n    h, w = img.shape[:2]\n    if max(w, h) == size:\n        return img\n    if w > h:\n        scale = size / w\n        h = h * scale\n        w = size\n    else:\n        scale = size / h\n        w = w * scale\n        h = size\n    interpolation = interpolation_up if scale > 1 else interpolation_down\n    resized = cv2.resize(img, (int(w), int(h)), interpolation=interpolation)\n    return resized\n\n\ndef predict_on_video(face_extractor, video_path, batch_size, input_size, models, strategy=np.mean,\n                     apply_compression=False):\n    batch_size *= 4\n    try:\n        faces = face_extractor.process_video(video_path)\n        if len(faces) > 0:\n            x = np.zeros((batch_size, input_size, input_size, 3), dtype=np.uint8)\n            n = 0\n            for frame_data in faces:\n                for face in frame_data[\"faces\"]:\n                    resized_face = isotropically_resize_image(face, input_size)\n                    resized_face = put_to_center(resized_face, input_size)\n                    if apply_compression:\n                        resized_face = image_compression(resized_face, quality=90, image_type=\".jpg\")\n                    if n + 1 < batch_size:\n                        x[n] = resized_face\n                        n += 1\n                    else:\n                        pass\n            if n > 0:\n                device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n                x = torch.tensor(x, device=device).float()\n                # Preprocess the images.\n                x = x.permute((0, 3, 1, 2))\n                for i in range(len(x)):\n                    x[i] = normalize_transform(x[i] / 255.)\n                # Make a prediction, then take the average.\n                with torch.no_grad():\n                    preds = []\n                    for model in models:\n                        y_pred = model(x[:n].half())\n                        y_pred = torch.sigmoid(y_pred.squeeze())\n                        bpred = y_pred[:n].cpu().numpy()\n                        preds.append(strategy(bpred))\n                    return np.mean(preds)\n    except Exception as e:\n        print(\"Prediction error on video %s: %s\" % (video_path, str(e)))\n\n    return 0.5\n\ndef predict_on_video_set(face_extractor, videos, input_size, num_workers, test_dir, frames_per_video, models,\n                         strategy=np.mean,\n                         apply_compression=False):\n    def process_file(i):\n        filename = videos[i]\n        y_pred = predict_on_video(\n            face_extractor=face_extractor,\n            video_path=os.path.join(test_dir, filename),\n            input_size=input_size,\n            batch_size=frames_per_video,\n            models=models,\n            strategy=strategy,\n            apply_compression=apply_compression\n        )\n        return y_pred\n\n    with ThreadPoolExecutor(max_workers=num_workers) as ex:\n        predictions = list(tqdm(ex.map(process_file, range(len(videos))), total=len(videos), desc=\"Predicting Videos\"))\n\n    return predictions\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:59.577980Z","iopub.execute_input":"2025-07-18T11:47:59.578214Z","iopub.status.idle":"2025-07-18T11:47:59.607987Z","shell.execute_reply.started":"2025-07-18T11:47:59.578188Z","shell.execute_reply":"2025-07-18T11:47:59.607187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def deepfakedetector(video_folder_path, output_file='predictions.csv'):\n    parser = argparse.ArgumentParser(description=\"Predict test videos\")\n    arg = parser.add_argument\n    \n    arg('--weights-dir', type=str, default=\"/kaggle/input/weights\", help=\"path to directory with checkpoints\")\n\n    # Fix: Provide models as a list instead of a single string\n    arg('--models', nargs='+', default=[\n        \"final_111_DeepFakeClassifier_tf_efficientnet_b7_ns_0_36\",\n        \"final_555_DeepFakeClassifier_tf_efficientnet_b7_ns_0_19\",\n        \"final_777_DeepFakeClassifier_tf_efficientnet_b7_ns_0_29\",\n        \"final_777_DeepFakeClassifier_tf_efficientnet_b7_ns_0_31\",\n        \"final_888_DeepFakeClassifier_tf_efficientnet_b7_ns_0_37\",\n        \"final_888_DeepFakeClassifier_tf_efficientnet_b7_ns_0_40\",\n        \"final_999_DeepFakeClassifier_tf_efficientnet_b7_ns_0_23\"\n    ], help=\"List of checkpoint files\")\n\n    # Fix: Remove required=True since a default is provided\n    arg('--test-dir', type=str, default=video_folder_path, help=\"path to directory with videos\")\n\n    # arg('--output', type=str, default=\"submission.csv\", help=\"path to output csv\")\n\n    args, _ = parser.parse_known_args()\n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    models = []\n    model_paths = [os.path.join(args.weights_dir, model) for model in args.models]\n    for path in tqdm(model_paths, desc=\"Loading Models\"):\n        model = DeepFakeClassifier(encoder=\"tf_efficientnet_b7_ns\").to(device)\n        checkpoint = torch.load(path, map_location=\"cpu\", weights_only=False)\n        state_dict = checkpoint.get(\"state_dict\", checkpoint)\n        model.load_state_dict({re.sub(\"^module.\", \"\", k): v for k, v in state_dict.items()}, strict=True)\n        model.eval()\n        del checkpoint\n        models.append(model.half())\n    # for path in model_paths:\n    #     model = DeepFakeClassifier(encoder=\"tf_efficientnet_b7_ns\").to(device)\n    #     print(\"loading state dict {}\".format(path))\n    #     checkpoint = torch.load(path, map_location=\"cpu\", weights_only=False)\n    #     state_dict = checkpoint.get(\"state_dict\", checkpoint)\n    #     model.load_state_dict({re.sub(\"^module.\", \"\", k): v for k, v in state_dict.items()}, strict=True)\n    #     model.eval()\n    #     del checkpoint\n    #     models.append(model.half())\n\n    frames_per_video = 32\n    video_reader = VideoReader()\n    video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)\n    face_extractor = FaceExtractor(video_read_fn)\n    input_size = 380\n    strategy = confident_strategy\n    stime = time.time()\n\n    test_videos = sorted([x for x in os.listdir(args.test_dir) if x.endswith(\".mp4\")])\n    print(\"Predicting {} videos\".format(len(test_videos)))\n    predictions = predict_on_video_set(face_extractor=face_extractor, input_size=input_size, models=models,\n                                       strategy=strategy, frames_per_video=frames_per_video, videos=test_videos,\n                                       num_workers=1, test_dir=args.test_dir)\n    \n    pred_df = pd.DataFrame({\"filename\": test_videos, \"label\": predictions})\n\n    pred_df.to_csv(output_file, index=False)\n    print(\"Elapsed:\", time.time() - stime)\n    \n    return pred_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:47:59.609799Z","iopub.execute_input":"2025-07-18T11:47:59.609999Z","iopub.status.idle":"2025-07-18T11:47:59.626142Z","shell.execute_reply.started":"2025-07-18T11:47:59.609983Z","shell.execute_reply":"2025-07-18T11:47:59.625479Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"pred_df = deepfakedetector(\n                video_folder_path = '/kaggle/input/deepfake-detection-challenge/train_sample_videos',\n                output_file='predictions.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:13:42.278754Z","iopub.execute_input":"2025-07-18T11:13:42.279305Z","execution_failed":"2025-07-18T11:17:14.302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(pred_df.isna().any())\npred_df.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert the JSON data of videos to CSV\nwith open('/kaggle/input/deepfake-detection-challenge/train_sample_videos/metadata.json', 'r') as f:\n    json_data = json.load(f)\n\ntarget_df = pd.DataFrame.from_dict(json_data, orient='index')\ntarget_df.reset_index(inplace=True)\ntarget_df.rename(columns={'index': 'filename'}, inplace=True)\ntarget_df = target_df[['filename','label']]\ncsv_path = 'metadata.csv'\ntarget_df.to_csv(csv_path, index=False)\n\nprint(f\"CSV saved to: {csv_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:08:39.308608Z","iopub.status.idle":"2025-07-18T11:08:39.308853Z","shell.execute_reply.started":"2025-07-18T11:08:39.308720Z","shell.execute_reply":"2025-07-18T11:08:39.308736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(target_df.isna().any())\ntarget_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:08:39.310092Z","iopub.status.idle":"2025-07-18T11:08:39.310951Z","shell.execute_reply.started":"2025-07-18T11:08:39.310767Z","shell.execute_reply":"2025-07-18T11:08:39.310783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"threshold = 0.5\npred_df['pred'] = (pred_df['label'] >= threshold).astype(int)\n\ntarget_df['label'] = target_df['label'].map({'REAL': 0, 'FAKE': 1})\n\nmerged_df = pd.merge(pred_df, target_df, on='filename', how='inner', suffixes=('_pred', '_true'))\nmerged_df['target'] = merged_df['label_true'].copy(deep=True)\nmerged_df = merged_df[['filename', 'pred', 'target']]\n\nmerged_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:08:39.312878Z","iopub.status.idle":"2025-07-18T11:08:39.313210Z","shell.execute_reply.started":"2025-07-18T11:08:39.313032Z","shell.execute_reply":"2025-07-18T11:08:39.313056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Is any NaN value in any row ?\nmerged_df.isna().any()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:08:39.313988Z","iopub.status.idle":"2025-07-18T11:08:39.314266Z","shell.execute_reply.started":"2025-07-18T11:08:39.314149Z","shell.execute_reply":"2025-07-18T11:08:39.314162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"correct = (merged_df['pred'] == merged_df['target']).sum()\ntotal = len(merged_df)\naccuracy = correct / total\n\nprint(f\"Test Accuracy: {accuracy*100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:08:39.315325Z","iopub.status.idle":"2025-07-18T11:08:39.315523Z","shell.execute_reply.started":"2025-07-18T11:08:39.315429Z","shell.execute_reply":"2025-07-18T11:08:39.315437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"incorrect_df = merged_df[merged_df['pred'] != merged_df['target']]\n\nincorrect_df = pd.merge(\n    incorrect_df,\n    pred_df[['filename', 'label']],\n    on='filename',\n    how='left'\n)\nprint(incorrect_df[['filename', 'label', 'pred', 'target']])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-18T11:08:39.317511Z","iopub.status.idle":"2025-07-18T11:08:39.317807Z","shell.execute_reply.started":"2025-07-18T11:08:39.317648Z","shell.execute_reply":"2025-07-18T11:08:39.317662Z"}},"outputs":[],"execution_count":null}]}