{"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":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":845111,"sourceType":"datasetVersion","datasetId":446509},{"sourceId":896209,"sourceType":"datasetVersion","datasetId":479256},{"sourceId":903208,"sourceType":"datasetVersion","datasetId":448076},{"sourceId":9614787,"sourceType":"datasetVersion","datasetId":5867358},{"sourceId":9614805,"sourceType":"datasetVersion","datasetId":5867372},{"sourceId":9615813,"sourceType":"datasetVersion","datasetId":5868157},{"sourceId":9619483,"sourceType":"datasetVersion","datasetId":5870872},{"sourceId":244122,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":208543,"modelId":230233}],"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)\nimport cv2 as cv\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-28T12:03:21.101624Z","iopub.execute_input":"2025-01-28T12:03:21.101899Z","iopub.status.idle":"2025-01-28T12:03:22.303453Z","shell.execute_reply.started":"2025-01-28T12:03:21.101873Z","shell.execute_reply":"2025-01-28T12:03:22.302535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install mediapipe","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-28T12:03:22.30476Z","iopub.execute_input":"2025-01-28T12:03:22.30523Z","iopub.status.idle":"2025-01-28T12:03:34.581886Z","shell.execute_reply.started":"2025-01-28T12:03:22.305192Z","shell.execute_reply":"2025-01-28T12:03:34.580788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Install libraries\n!pip install utils\n!pip install python-utils","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:16:08.65628Z","iopub.execute_input":"2025-01-29T10:16:08.65661Z","iopub.status.idle":"2025-01-29T10:16:28.159228Z","shell.execute_reply.started":"2025-01-29T10:16:08.656545Z","shell.execute_reply":"2025-01-29T10:16:28.158302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Шлях до досліджуваного відофайлу\nPATH_TO_VIDEOFILE = \"/kaggle/input/deepfake-detection-challenge/train_sample_videos/bilnggbxgu.mp4\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:16:28.161413Z","iopub.execute_input":"2025-01-29T10:16:28.161699Z","iopub.status.idle":"2025-01-29T10:16:28.165617Z","shell.execute_reply.started":"2025-01-29T10:16:28.161671Z","shell.execute_reply":"2025-01-29T10:16:28.164842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ПІДРАХУНОК КІЛЬКОСТІ КЛІПАНЬ\n\n\n\nimport mediapipe as mp\nimport time\nimport utils, math\nimport numpy as np\n# variables \nframe_counter =0\nCEF_COUNTER =0\nSIMPLE_BLINK = 0\nTOTAL_BLINKS =0\nDECISION_STATUS = 0\n# constants\nCLOSED_EYES_FRAME =3\nFONTS =cv.FONT_HERSHEY_COMPLEX\n\n# face bounder indices \nFACE_OVAL=[ 10, 338, 297, 332, 284, 251, 389, 356, 454, 323, 361, 288, 397, 365, 379, 378, 400, 377, 152, 148, 176, 149, 150, 136, 172, 58, 132, 93, 234, 127, 162, 21, 54, 103,67, 109]\n\n# lips indices for Landmarks\nLIPS=[ 61, 146, 91, 181, 84, 17, 314, 405, 321, 375,291, 308, 324, 318, 402, 317, 14, 87, 178, 88, 95,185, 40, 39, 37,0 ,267 ,269 ,270 ,409, 415, 310, 311, 312, 13, 82, 81, 42, 183, 78 ]\nLOWER_LIPS =[61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 308, 324, 318, 402, 317, 14, 87, 178, 88, 95]\nUPPER_LIPS=[ 185, 40, 39, 37,0 ,267 ,269 ,270 ,409, 415, 310, 311, 312, 13, 82, 81, 42, 183, 78] \n# Left eyes indices \nLEFT_EYE =[ 362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385,384, 398 ]\nLEFT_EYEBROW =[ 336, 296, 334, 293, 300, 276, 283, 282, 295, 285 ]\n\n# right eyes indices\nRIGHT_EYE=[ 33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161 , 246 ]  \nRIGHT_EYEBROW=[ 70, 63, 105, 66, 107, 55, 65, 52, 53, 46 ]\n\nmap_face_mesh = mp.solutions.face_mesh\n\ncamera = cv.VideoCapture(PATH_TO_VIDEOFILE)\n\ndef landmarksDetection(img, results, draw=False):\n    img_height, img_width= img.shape[:2]\n    # list[(x,y), (x,y)....]\n    mesh_coord = [(int(point.x * img_width), int(point.y * img_height)) for point in results.multi_face_landmarks[0].landmark]\n    if draw :\n        [cv.circle(img, p, 2, (0,255,0), -1) for p in mesh_coord]\n\n    # returning the list of tuples for each landmarks \n    return mesh_coord\n\n# Euclaidean distance \ndef euclaideanDistance(point, point1):\n    x, y = point\n    x1, y1 = point1\n    distance = math.sqrt((x1 - x)**2 + (y1 - y)**2)\n    return distance\n\n# Blinking Ratio\ndef blinkRatio(img, landmarks, right_indices, left_indices):\n    # Right eyes \n    # horizontal line \n    rh_right = landmarks[right_indices[0]]\n    rh_left = landmarks[right_indices[8]]\n    # vertical line \n    rv_top = landmarks[right_indices[12]]\n    rv_bottom = landmarks[right_indices[4]]\n    \n    lh_right = landmarks[left_indices[0]]\n    lh_left = landmarks[left_indices[8]]\n\n    # vertical line \n    lv_top = landmarks[left_indices[12]]\n    lv_bottom = landmarks[left_indices[4]]\n\n    rhDistance = euclaideanDistance(rh_right, rh_left)\n    rvDistance = euclaideanDistance(rv_top, rv_bottom)\n\n    lvDistance = euclaideanDistance(lv_top, lv_bottom)\n    lhDistance = euclaideanDistance(lh_right, lh_left)\n\n    reRatio = rhDistance/rvDistance\n    leRatio = lhDistance/lvDistance\n\n    ratio = (reRatio+leRatio)/2\n    return ratio \n\n\nwith map_face_mesh.FaceMesh(min_detection_confidence =0.5, min_tracking_confidence=0.5) as face_mesh:\n\n    # starting time here \n    start_time = time.time()\n    # starting Video loop here.\n    while True:\n        frame_counter +=1 # frame counter\n        ret, frame = camera.read() # getting frame from camera \n        if not ret: \n            break # no more frames break\n        #  resizing frame\n        \n        frame = cv.resize(frame, None, fx=1.5, fy=1.5, interpolation=cv.INTER_CUBIC)\n        frame_height, frame_width= frame.shape[:2]\n        rgb_frame = cv.cvtColor(frame, cv.COLOR_RGB2BGR)\n        results  = face_mesh.process(rgb_frame)\n        if results.multi_face_landmarks:\n            mesh_coords = landmarksDetection(frame, results, False)\n            ratio = blinkRatio(frame, mesh_coords, RIGHT_EYE, LEFT_EYE)\n            \n            if ratio >5.5:\n                CEF_COUNTER +=1\n                SIMPLE_BLINK +=1\n            else:\n                if CEF_COUNTER>CLOSED_EYES_FRAME:\n                    TOTAL_BLINKS +=1\n                    CEF_COUNTER =0\n\n        # calculating  frame per seconds FPS\n        end_time = time.time()-start_time\n        fps = frame_counter/end_time\n\n    # змінна, що відповідає за обчислення обтимальної кількості кліпань\n    NORMAL_BLINK_COUNT = 0    \n    # умова кількості кліпань (в середньому відбувається одне кліпання на 5 секунд)\n    if end_time < 5:\n        #якщо відео коротке, то вважаємо що 1 кліпання достатньо, щоби вважати відео оригінальним\n        NORMAL_BLINK_COUNT = 1\n    else:\n        # якщо відео довше 5 секунд, то підраховувємо співвідношення (к-сть секунд відео / середній інтервал кліпань\n        NORMAL_BLINK_COUNT = end_time/5\n    \n    if(TOTAL_BLINKS < NORMAL_BLINK_COUNT):\n        # Імовірність діпфейку - середня \n        DECISION_STATUS = \"Імовірність діпфейку - середня\"\n    else:\n        # Імовірність діпфейку - низька\n        DECISION_STATUS = \"Імовірність діпфейку - низька\"\n\n    print(\"Тривалість відео: \" + str(end_time))\n    print(\"simple blink: \" + str(SIMPLE_BLINK))\n    print(\"Кількість кліпань: \" + str(TOTAL_BLINKS))\n    print(\"Рішення \" + DECISION_STATUS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T20:11:50.52734Z","iopub.execute_input":"2024-11-11T20:11:50.527641Z","iopub.status.idle":"2024-11-11T20:12:10.945375Z","shell.execute_reply.started":"2024-11-11T20:11:50.527608Z","shell.execute_reply":"2024-11-11T20:12:10.944471Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/input/dlibpkg\n!ls /kaggle/input/face-recognition-0-1-5-py2-py3-none-any \n!ls /kaggle/input/imageio-ffmpeg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T20:12:10.946806Z","iopub.execute_input":"2024-11-11T20:12:10.947402Z","iopub.status.idle":"2024-11-11T20:12:14.028787Z","shell.execute_reply.started":"2024-11-11T20:12:10.947365Z","shell.execute_reply":"2024-11-11T20:12:14.027849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import kagglehub\ndlibpkg_path = kagglehub.dataset_download('carlossouza/dlibpkg')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T20:12:14.030211Z","iopub.execute_input":"2024-11-11T20:12:14.030541Z","iopub.status.idle":"2024-11-11T20:12:14.799212Z","shell.execute_reply.started":"2024-11-11T20:12:14.030504Z","shell.execute_reply":"2024-11-11T20:12:14.798399Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install dlib\n!pip install face_recognition\n!pip install imageio_ffmpeg\n!pip show dlib\n!pip show face_recognition\n!pip show imageio_ffmpeg","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T20:53:31.402597Z","iopub.execute_input":"2025-02-17T20:53:31.402881Z","iopub.status.idle":"2025-02-17T20:53:49.344845Z","shell.execute_reply.started":"2025-02-17T20:53:31.402858Z","shell.execute_reply":"2025-02-17T20:53:49.343753Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport cv2\nimport imageio\nimport face_recognition\nimport tensorflow as tf\nimport pandas as pd\nfrom tensorflow.keras.models import load_model\nfrom tqdm import tqdm\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\n# Завантаження моделі автоенкодера\nautoencoder = load_model('/kaggle/input/autoencoder_model/keras/autoencoder/1/autoencoder_model.h5', compile=False)\n\n# Шлях до тестового набору\nTEST_DIR = \"/kaggle/input/deepfake-detection-challenge/test_videos\"\nLABELS_FILE = \"/kaggle/input/deepfake-detection-challenge/sample_submission.csv\"\n\nFRAMES_TO_EXTRACT = 10  # Скільки кадрів витягати з кожного відео\nIMG_SIZE = 224  # Розмір зображень\nBATCH_SIZE = 50  # Обробка відео батчами\nOUTPUT_CSV = \"/kaggle/working/predictions.csv\"  # Файл для збереження передбачень\n\n# Завантаження міток\ndef load_labels(labels_file):\n    \"\"\"\n    Завантажує мітки із CSV файлу.\n    \"\"\"\n    labels_df = pd.read_csv(labels_file)\n    labels = {row['filename']: 1 if row['label'] == 'FAKE' else 0 for _, row in labels_df.iterrows()}\n    return labels\n\n# Клас для обробки відео\nclass Video:\n    def __init__(self, path):\n        self.path = path\n        self.container = imageio.get_reader(path, 'ffmpeg')\n        self.length = self.container.count_frames()\n    \n    def get(self, key):\n        return self.container.get_data(key)\n    \n    def __len__(self):\n        return self.length\n\n# Клас для автоенкодера\nclass AutoencoderClassifier:\n    def __init__(self, autoencoder_model):\n        self.model = autoencoder_model\n    \n    def predict(self, x):\n        encoded_features = self.model.predict(x)\n        avg_pixel_value = np.mean(encoded_features, axis=(1, 2, 3))\n        return (avg_pixel_value > 0.5).astype(int)\n\n# Ініціалізація автоенкодера\nclassifier = AutoencoderClassifier(autoencoder)\n\n# Функція для обробки одного відео\ndef process_video(video_path):\n    try:\n        video = Video(video_path)\n        num_frames = FRAMES_TO_EXTRACT\n        frame_indices = np.linspace(0, len(video) - 1, num_frames, dtype=int)\n        margin = 0.2\n        re_imgs = []\n\n        for i in frame_indices:\n            img = video.get(i)\n            face_positions = face_recognition.face_locations(img)\n\n            if len(face_positions) == 0:\n                continue  # Пропускаємо кадри без облич\n\n            for face_position in face_positions:\n                offset = round(margin * (face_position[2] - face_position[0]))\n                y0 = max(face_position[0] - offset, 0)\n                x1 = min(face_position[1] + offset, img.shape[1])\n                y1 = min(face_position[2] + offset, img.shape[0])\n                x0 = max(face_position[3] - offset, 0)\n                face = img[y0:y1, x0:x1]\n\n                inp = cv2.resize(face, (IMG_SIZE, IMG_SIZE)) / 255.0\n                inp = np.expand_dims(inp, axis=0)\n\n                prediction = classifier.predict(inp)\n                re_imgs.append(prediction[0])\n\n        if len(re_imgs) > 0:\n            return np.mean(re_imgs)\n        else:\n            return 0.5  # Якщо немає облич\n\n    except Exception as e:\n        print(f\"Error processing video {video_path}: {e}\")\n        return None  # Пропускаємо відео у разі помилки\n\n# Завантаження тестових міток\nlabels = load_labels(LABELS_FILE)\n\n# Отримуємо список відеофайлів у тестовому наборі\nvideo_files = sorted([f for f in os.listdir(TEST_DIR) if f.endswith('.mp4')])\n\n# Обробка батчами по 50 відео\ny_true = []\ny_pred = []\nresults = []\n\nfor i in range(0, len(video_files), BATCH_SIZE):\n    batch_videos = video_files[i:i + BATCH_SIZE]\n    print(f\"\\nОбробка батчу {i // BATCH_SIZE + 1}/{len(video_files) // BATCH_SIZE + 1}...\")\n\n    batch_true = []\n    batch_pred = []\n\n    for video_file in tqdm(batch_videos):\n        video_path = os.path.join(TEST_DIR, video_file)\n        prediction = process_video(video_path)\n\n        if prediction is not None:\n            batch_true.append(labels.get(video_file, 0))  # Якщо мітки немає, вважаємо Real (0)\n            if prediction <= 0.2:\n                batch_pred.append(1)  # Fake\n            elif prediction <= 0.5:\n                batch_pred.append(1)  # Maybe Fake → Fake\n            elif prediction <= 0.8:\n                batch_pred.append(0)  # Maybe Original → Real\n            else:\n                batch_pred.append(0)  # Original → Real\n\n            results.append([video_file, prediction])\n\n    # Додаємо результати поточного батчу до загальних списків\n    y_true.extend(batch_true)\n    y_pred.extend(batch_pred)\n\n    # Обчислюємо метрики для поточного батчу\n    if len(batch_true) > 0:\n        accuracy = accuracy_score(batch_true, batch_pred)\n        precision = precision_score(batch_true, batch_pred)\n        recall = recall_score(batch_true, batch_pred)\n        f1 = f1_score(batch_true, batch_pred)\n\n        print(f\"Batch Accuracy: {accuracy:.4f}\")\n        print(f\"Batch Precision: {precision:.4f}\")\n        print(f\"Batch Recall: {recall:.4f}\")\n        print(f\"Batch F1 Score: {f1:.4f}\")\n\n# Збереження результатів у CSV\ndf_results = pd.DataFrame(results, columns=['filename', 'prediction'])\ndf_results.to_csv(OUTPUT_CSV, index=False)\n\n# Остаточні метрики\nif len(y_true) > 0:\n    final_accuracy = accuracy_score(y_true, y_pred)\n    final_precision = precision_score(y_true, y_pred)\n    final_recall = recall_score(y_true, y_pred)\n    final_f1 = f1_score(y_true, y_pred)\n\n    print(\"\\n=== Фінальні результати ===\")\n    print(f\"Final Accuracy: {final_accuracy:.4f}\")\n    print(f\"Final Precision: {final_precision:.4f}\")\n    print(f\"Final Recall: {final_recall:.4f}\")\n    print(f\"Final F1 Score: {final_f1:.4f}\")\n\nprint(f\"\\nПередбачення збережені у {OUTPUT_CSV}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T20:54:00.745115Z","iopub.execute_input":"2025-02-17T20:54:00.745469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport keras\nimport glob\nimport cv2\nfrom albumentations import *\nfrom tqdm import tqdm_notebook as tqdm\nimport gc\n\nfrom keras.models import Model as KerasModel\nfrom keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout, Reshape, Concatenate, LeakyReLU\nfrom keras.optimizers import Adam\nimport face_recognition\nimport imageio\nimport tensorflow as tf\nfrom keras.models import load_model\nimport warnings\nwarnings.filterwarnings('ignore')\nPATH = '../input/deepfake-detection-challenge/'\nprint(os.listdir(PATH))\n\nfor dirname, _, filenames in os.walk('/kaggle/input/meso-pretrain'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nfrom IPython.display import HTML\nfrom base64 import b64encode\nvid1 = open('/kaggle/input/deepfake-detection-challenge/test_videos/ytddugrwph.mp4','rb').read()\ndata_url = \"data:video/mp4;base64,\" + b64encode(vid1).decode()\nHTML(\"\"\"\n<video width=600 controls>\n      <source src=\"%s\" type=\"video/mp4\">\n</video>\n\"\"\" % data_url)\n\n\n\n\nclass Video:\n    def __init__(self, path):\n        self.path = path\n        self.container = imageio.get_reader(path, 'ffmpeg')\n        self.length = self.container.count_frames()\n#         self.length = self.container.get_meta_data()['nframes']\n        self.fps = self.container.get_meta_data()['fps']\n    \n    def init_head(self):\n        self.container.set_image_index(0)\n    \n    def next_frame(self):\n        self.container.get_next_data()\n    \n    def get(self, key):\n        return self.container.get_data(key)\n    \n    def __call__(self, key):\n        return self.get(key)\n    \n    def __len__(self):\n        return self.length\n    \n    \n\n    \n    \n    \n    \n    \n    \nIMGWIDTH = 256\n\nclass Classifier:\n    def __init__():\n        self.model = 0\n    \n    def predict(self, x):\n        return self.model.predict(x)\n    \n    def fit(self, x, y):\n        return self.model.train_on_batch(x, y)\n    \n    def get_accuracy(self, x, y):\n        return self.model.test_on_batch(x, y)\n    \n    def load(self, path):\n        self.model.load_weights(path)\n\n\n#class Meso4(Classifier):\n#    def __init__(self, learning_rate = 0.001):\n#        self.model = self.init_model()\n#        optimizer = Adam(lr = learning_rate)\n#        self.model.compile(optimizer = optimizer, loss = 'mean_squared_error', metrics = ['accuracy'])\n\nclass Meso4(Classifier):\n    def __init__(self, learning_rate = 0.001):\n        self.model = self.init_model()\n        optimizer = Adam(learning_rate=learning_rate)  # Замість lr=learning_rate\n        self.model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['accuracy'])\n    \n    def init_model(self): \n        x = Input(shape = (IMGWIDTH, IMGWIDTH, 3))\n        \n        x1 = Conv2D(8, (3, 3), padding='same', activation = 'relu')(x)\n        x1 = BatchNormalization()(x1)\n        x1 = MaxPooling2D(pool_size=(2, 2), padding='same')(x1)\n        \n        x2 = Conv2D(8, (5, 5), padding='same', activation = 'relu')(x1)\n        x2 = BatchNormalization()(x2)\n        x2 = MaxPooling2D(pool_size=(2, 2), padding='same')(x2)\n        \n        x3 = Conv2D(16, (5, 5), padding='same', activation = 'relu')(x2)\n        x3 = BatchNormalization()(x3)\n        x3 = MaxPooling2D(pool_size=(2, 2), padding='same')(x3)\n        \n        x4 = Conv2D(16, (5, 5), padding='same', activation = 'relu')(x3)\n        x4 = BatchNormalization()(x4)\n        x4 = MaxPooling2D(pool_size=(4, 4), padding='same')(x4)\n        \n        y = Flatten()(x4)\n        y = Dropout(0.5)(y)\n        y = Dense(16)(y)\n        y = LeakyReLU(alpha=0.1)(y)\n        y = Dropout(0.5)(y)\n        y = Dense(1, activation = 'sigmoid')(y)\n\n        return KerasModel(inputs = x, outputs = y)\n\nclass MesoInception4(Classifier):\n    def __init__(self, learning_rate = 0.001):\n        self.model = self.init_model()\n        optimizer = Adam(learning_rate = learning_rate)\n        self.model.compile(optimizer = optimizer, loss = 'mean_squared_error', metrics = ['accuracy'])\n    \n    def InceptionLayer(self, a, b, c, d):\n        def func(x):\n            x1 = Conv2D(a, (1, 1), padding='same', activation='relu')(x)\n            \n            x2 = Conv2D(b, (1, 1), padding='same', activation='relu')(x)\n            x2 = Conv2D(b, (3, 3), padding='same', activation='relu')(x2)\n            \n            x3 = Conv2D(c, (1, 1), padding='same', activation='relu')(x)\n            x3 = Conv2D(c, (3, 3), dilation_rate = 2, strides = 1, padding='same', activation='relu')(x3)\n            \n            x4 = Conv2D(d, (1, 1), padding='same', activation='relu')(x)\n            x4 = Conv2D(d, (3, 3), dilation_rate = 3, strides = 1, padding='same', activation='relu')(x4)\n\n            y = Concatenate(axis = -1)([x1, x2, x3, x4])\n            \n            return y\n        return func\n    \n    def init_model(self):\n        x = Input(shape = (IMGWIDTH, IMGWIDTH, 3))\n        \n        x1 = self.InceptionLayer(1, 4, 4, 2)(x)\n        x1 = BatchNormalization()(x1)\n        x1 = MaxPooling2D(pool_size=(2, 2), padding='same')(x1)\n        \n        x2 = self.InceptionLayer(2, 4, 4, 2)(x1)\n        x2 = BatchNormalization()(x2)\n        x2 = MaxPooling2D(pool_size=(2, 2), padding='same')(x2)        \n        \n        x3 = Conv2D(16, (5, 5), padding='same', activation = 'relu')(x2)\n        x3 = BatchNormalization()(x3)\n        x3 = MaxPooling2D(pool_size=(2, 2), padding='same')(x3)\n        \n        x4 = Conv2D(16, (5, 5), padding='same', activation = 'relu')(x3)\n        x4 = BatchNormalization()(x4)\n        x4 = MaxPooling2D(pool_size=(4, 4), padding='same')(x4)\n        \n        y = Flatten()(x4)\n        y = Dropout(0.5)(y)\n        y = Dense(16)(y)\n        y = LeakyReLU(alpha=0.1)(y)\n        y = Dropout(0.5)(y)\n        y = Dense(1, activation = 'sigmoid')(y)\n\n        return KerasModel(inputs = x, outputs = y)\n    \n    \n    \n    \ntf.test.is_gpu_available(\n    cuda_only=False,\n    min_cuda_compute_capability=None\n)\n\n#classifier.load('/kaggle/input/meso-pretrain/MesoInception_DF')\n#classifier.load('/kaggle/input/meso-pretrain/mesonet41.h5')\nclassifier = Meso4()  # Створюємо простішу модель\n#classifier.model.load_weights('/kaggle/input/meso-pretrain/mesonet4_kaggle.h5')\nclassifier.model.load_weights('/kaggle/input/autoencoder_model/keras/autoencoder/1/autoencoder_model.h5')\n\n# 0 fake\n# 1 real \n\n\nsubmit = []\n\n\n\nsave_interval = 1 # perform face detection every {save_interval} frames\nmargin = 0.2\n# for vi in os.listdir('/kaggle/input/deepfake-detection-challenge/test_videos'):\n#     print(os.path.join(\"/kaggle/input/deepfake-detection-challenge/test_videos/\", vi))\nre_video = 0.5\ntry:\n    video1 = Video('/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4')\n    re_imgs = []\n    for i in range(0,video1.__len__(),save_interval):\n        img = video1.get(i)\n        face_positions = face_recognition.face_locations(img)\n        for face_position in face_positions:\n            offset = round(margin * (face_position[2] - face_position[0]))\n            y0 = max(face_position[0] - offset, 0)\n            x1 = min(face_position[1] + offset, img.shape[1])\n            y1 = min(face_position[2] + offset, img.shape[0])\n            x0 = max(face_position[3] - offset, 0)\n            face = img[y0:y1,x0:x1]\n\n            inp = cv2.resize(face,(256,256))/255.\n            re_img = classifier.predict(np.array([inp]))\n            print(video1,\": \",i , \"  :  \",classifier.predict(np.array([inp])))\n            re_imgs.append(re_img[0][0])\n    re_video = np.average(re_imgs)\n    print(\"re_video = \" + str(re_video))\n    if np.isnan(re_video):\n        re_video = 0.5\nexcept:\n    re_video = 0.5\n    print(\"re_video = \" + str(re_video))\n    submit.append([video1,1.0-re_video])\n#     submit.append([vi,re_video])\n\n#     submit[vi] = 1.0-re_video\ndef plot_training_history(history):\n    \"\"\"\n    Візуалізація графіків точності та втрат під час навчання.\n    \"\"\"\n    # Отримуємо значення втрат і точності\n    loss = history.history['loss']\n    val_loss = history.history.get('val_loss', [])\n    accuracy = history.history.get('accuracy', [])\n    val_accuracy = history.history.get('val_accuracy', [])\n    \n    # Графік втрат\n    plt.figure(figsize=(12, 5))\n    plt.subplot(1, 2, 1)\n    plt.plot(loss, label='Training Loss')\n    if val_loss:\n        plt.plot(val_loss, label='Validation Loss')\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    \n    # Графік точності\n    plt.subplot(1, 2, 2)\n    plt.plot(accuracy, label='Training Accuracy')\n    if val_accuracy:\n        plt.plot(val_accuracy, label='Validation Accuracy')\n    plt.title('Training and Validation Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    \n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-28T12:13:24.881887Z","iopub.execute_input":"2025-01-28T12:13:24.882451Z","iopub.status.idle":"2025-01-28T12:13:28.17667Z","shell.execute_reply.started":"2025-01-28T12:13:24.882416Z","shell.execute_reply":"2025-01-28T12:13:28.175404Z"},"collapsed":true,"jupyter":{"source_hidden":true,"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#structure with autoencoder v1.1 oll frames\nfrom tensorflow.keras.models import load_model\nimport numpy as np\nimport cv2\nimport imageio\nimport face_recognition\nimport tensorflow as tf\nimport warnings\nwarnings.filterwarnings('ignore')\n\nautoencoder = load_model('/kaggle/input/autoencoder_model/keras/autoencoder/1/autoencoder_model.h5', compile=False)\n\n\n# Клас для обробки відео\nclass Video:\n    def __init__(self, path):\n        self.path = path\n        self.container = imageio.get_reader(path, 'ffmpeg')\n        self.length = self.container.count_frames()\n        self.fps = self.container.get_meta_data()['fps']\n    \n    def init_head(self):\n        self.container.set_image_index(0)\n    \n    def next_frame(self):\n        return self.container.get_next_data()\n    \n    def get(self, key):\n        return self.container.get_data(key)\n    \n    def __call__(self, key):\n        return self.get(key)\n    \n    def __len__(self):\n        return self.length\n\n# Клас для використання автоенкодера як класифікатора\nclass AutoencoderClassifier:\n    def __init__(self, autoencoder_model):\n        self.model = autoencoder_model\n    \n    def predict(self, x):\n        \"\"\"\n        Використовуємо вихід енкодера для класифікації (аналітична частина).\n        \"\"\"\n        encoded_features = self.model.predict(x)  # Отримуємо реконструйовані зображення\n        avg_pixel_value = np.mean(encoded_features, axis=(1, 2, 3))  # Середнє значення пікселів\n        return (avg_pixel_value > 0.5).astype(int)  # Простий поріг: >0.5 -> клас 1 (real)\n\n# Ініціалізація класифікатора\nclassifier = AutoencoderClassifier(autoencoder)\n\n# Обробка відео та класифікація\nPATH_TO_VIDEO = '/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4'\nvideo = Video(PATH_TO_VIDEO)\n\nsave_interval = 1  # Частота обробки кадрів\nmargin = 0.2  # Розширення рамки обличчя\nresults = []\n\ntry:\n    re_imgs = []\n    for i in range(0, len(video), save_interval):\n        img = video.get(i)\n        face_positions = face_recognition.face_locations(img)  # Визначення облич\n        for face_position in face_positions:\n            offset = round(margin * (face_position[2] - face_position[0]))\n            y0 = max(face_position[0] - offset, 0)\n            x1 = min(face_position[1] + offset, img.shape[1])\n            y1 = min(face_position[2] + offset, img.shape[0])\n            x0 = max(face_position[3] - offset, 0)\n            face = img[y0:y1, x0:x1]\n\n            # Підготовка кадру до передбачення\n            inp = cv2.resize(face, (224, 224)) / 255.0\n            inp = np.expand_dims(inp, axis=0)  # Додаємо вимір batch\n\n            # Передбачення\n            prediction = classifier.predict(inp)\n            re_imgs.append(prediction[0])\n            print(f\"Frame {i}: Prediction = {prediction[0]}\")\n\n    re_video = np.mean(re_imgs)  # Середнє значення для всього відео\n    print(f\"Final Video Prediction: {re_video}\")\n    results.append([PATH_TO_VIDEO, re_video])\nexcept Exception as e:\n    print(f\"Error processing video: {e}\")\n    results.append([PATH_TO_VIDEO, 0.5])  # У разі помилки встановлюємо середнє значення\n\n# Вивід результатів\nprint(\"Results:\", results)\n\nif results >0.5:\n    print(\"Fake\")\nelse:\n    print(\"Original\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-28T12:49:43.352415Z","iopub.execute_input":"2025-01-28T12:49:43.35321Z","iopub.status.idle":"2025-01-28T12:55:46.077346Z","shell.execute_reply.started":"2025-01-28T12:49:43.353178Z","shell.execute_reply":"2025-01-28T12:55:46.076401Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#structure with autoencoder v1.2 10 frames per video\nfrom tensorflow.keras.models import load_model\nimport numpy as np\nimport cv2\nimport imageio\nimport face_recognition\nimport tensorflow as tf\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Завантаження автоенкодера\nautoencoder = load_model('/kaggle/input/autoencoder_model/keras/autoencoder/1/autoencoder_model.h5', compile=False)\n\n# Клас для обробки відео\nclass Video:\n    def __init__(self, path):\n        self.path = path\n        self.container = imageio.get_reader(path, 'ffmpeg')\n        self.length = self.container.count_frames()\n        self.fps = self.container.get_meta_data()['fps']\n    \n    def get(self, key):\n        return self.container.get_data(key)\n    \n    def __len__(self):\n        return self.length\n\n# Клас для використання автоенкодера як класифікатора\nclass AutoencoderClassifier:\n    def __init__(self, autoencoder_model):\n        self.model = autoencoder_model\n    \n    def predict(self, x):\n        \"\"\" Використовуємо вихід енкодера для класифікації. \"\"\"\n        encoded_features = self.model.predict(x)  # Отримуємо реконструйовані зображення\n        avg_pixel_value = np.mean(encoded_features, axis=(1, 2, 3))  # Середнє значення пікселів\n        return (avg_pixel_value > 0.5).astype(int)  # Поріг класифікації: >0.5 → Fake, ≤0.5 → Real\n\n# Ініціалізація класифікатора\nclassifier = AutoencoderClassifier(autoencoder)\n\n# Обробка відео та класифікація\nPATH_TO_VIDEO = \"/kaggle/input/deepfake-detection-challenge/train_sample_videos/bilnggbxgu.mp4\"\nvideo = Video(PATH_TO_VIDEO)\n\nnum_frames = 10  # Обробляємо рівномірно 10 кадрів\nframe_indices = np.linspace(0, len(video) - 1, num_frames, dtype=int)  # Вибираємо рівномірно розподілені кадри\n\nmargin = 0.2  # Розширення рамки обличчя\nre_imgs = []  # Список передбачень\n\ntry:\n    for i in frame_indices:\n        img = video.get(i)\n        face_positions = face_recognition.face_locations(img)  # Визначення облич\n\n        if len(face_positions) == 0:\n            print(f\"Frame {i}: No face detected\")\n            continue  # Пропускаємо кадри без облич\n\n        for face_position in face_positions:\n            offset = round(margin * (face_position[2] - face_position[0]))\n            y0 = max(face_position[0] - offset, 0)\n            x1 = min(face_position[1] + offset, img.shape[1])\n            y1 = min(face_position[2] + offset, img.shape[0])\n            x0 = max(face_position[3] - offset, 0)\n            face = img[y0:y1, x0:x1]\n\n            # Підготовка кадру для автоенкодера\n            inp = cv2.resize(face, (224, 224)) / 255.0\n            inp = np.expand_dims(inp, axis=0)  # Додаємо batch-розмірність\n\n            # Передбачення\n            prediction = classifier.predict(inp)\n            re_imgs.append(prediction[0])\n            print(f\"Frame {i}: Prediction = {prediction[0]}\")\n\n    if len(re_imgs) > 0:\n        re_video = np.mean(re_imgs)  # Середнє значення для відео\n    else:\n        re_video = 0.5  # Якщо не було знайдено жодного обличчя\n\n    #print(f\"\\nVideo Prediction: {re_video}\")\n    \n    # Класифікація відео\n    if re_video <= 0.2:\n        print(f\"\\nVideo Prediction: {re_video}\"\"Fake\")\n    if re_video <= 0.5 and re_video > 0.2:\n        print(f\"\\nVideo Prediction: {re_video}\"\"Maybe Fake\")\n    if re_video >0.5 and re_video <=0.8:\n        print(f\"\\nVideo Prediction: {re_video}\"\"Maybe Original\")\n    if re_video >0.8:\n        print(f\"\\nVideo Prediction: {re_video}\"\"Original\")\n    \nexcept Exception as e:\n    print(f\"Error processing video: {e}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T12:32:58.931462Z","iopub.execute_input":"2025-01-29T12:32:58.931846Z","iopub.status.idle":"2025-01-29T12:33:20.085006Z","shell.execute_reply.started":"2025-01-29T12:32:58.931816Z","shell.execute_reply":"2025-01-29T12:33:20.083994Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#New code\nimport os\nimport json\nimport numpy as np\nimport cv2\nimport imageio\nimport face_recognition\nimport tensorflow as tf\nimport pandas as pd\nfrom tensorflow.keras.models import load_model\nfrom tqdm import tqdm\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\n# Завантаження моделі автоенкодера\nautoencoder = load_model('/kaggle/input/autoencoder_model/keras/autoencoder/1/autoencoder_model.h5', compile=False)\n\n# Шлях до тестового набору\nTEST_DIR = \"/kaggle/input/deepfake-detection-challenge/test_videos\"\nLABELS_FILE = \"/kaggle/input/deepfake-detection-challenge/sample_submission.csv\"\n\nFRAMES_TO_EXTRACT = 10  # Скільки кадрів витягати з кожного відео\nIMG_SIZE = 224  # Розмір зображень\nBATCH_SIZE = 50  # Обробка відео батчами\nOUTPUT_CSV = \"/kaggle/working/predictions.csv\"  # Файл для збереження передбачень\n\n# Завантаження міток\ndef load_labels(labels_file):\n    \"\"\"\n    Завантажує мітки із CSV файлу.\n    \"\"\"\n    labels_df = pd.read_csv(labels_file)\n    labels = {row['filename']: 1 if row['label'] == 'FAKE' else 0 for _, row in labels_df.iterrows()}\n    return labels\n\n# Клас для обробки відео\nclass Video:\n    def __init__(self, path):\n        self.path = path\n        self.container = imageio.get_reader(path, 'ffmpeg')\n        self.length = self.container.count_frames()\n    \n    def get(self, key):\n        return self.container.get_data(key)\n    \n    def __len__(self):\n        return self.length\n\n# Клас для автоенкодера\nclass AutoencoderClassifier:\n    def __init__(self, autoencoder_model):\n        self.model = autoencoder_model\n    \n    def predict(self, x):\n        encoded_features = self.model.predict(x)\n        avg_pixel_value = np.mean(encoded_features, axis=(1, 2, 3))\n        return (avg_pixel_value > 0.5).astype(int)\n\n# Ініціалізація автоенкодера\nclassifier = AutoencoderClassifier(autoencoder)\n\n# Функція для обробки одного відео\ndef process_video(video_path):\n    try:\n        video = Video(video_path)\n        num_frames = FRAMES_TO_EXTRACT\n        frame_indices = np.linspace(0, len(video) - 1, num_frames, dtype=int)\n        margin = 0.2\n        re_imgs = []\n\n        for i in frame_indices:\n            img = video.get(i)\n            face_positions = face_recognition.face_locations(img)\n\n            if len(face_positions) == 0:\n                continue  # Пропускаємо кадри без облич\n\n            for face_position in face_positions:\n                offset = round(margin * (face_position[2] - face_position[0]))\n                y0 = max(face_position[0] - offset, 0)\n                x1 = min(face_position[1] + offset, img.shape[1])\n                y1 = min(face_position[2] + offset, img.shape[0])\n                x0 = max(face_position[3] - offset, 0)\n                face = img[y0:y1, x0:x1]\n\n                inp = cv2.resize(face, (IMG_SIZE, IMG_SIZE)) / 255.0\n                inp = np.expand_dims(inp, axis=0)\n\n                prediction = classifier.predict(inp)\n                re_imgs.append(prediction[0])\n\n        if len(re_imgs) > 0:\n            return np.mean(re_imgs)\n        else:\n            return 0.5  # Якщо немає облич\n\n    except Exception as e:\n        print(f\"Error processing video {video_path}: {e}\")\n        return None  # Пропускаємо відео у разі помилки\n\n# Завантаження тестових міток\nlabels = load_labels(LABELS_FILE)\n\n# Отримуємо список відеофайлів у тестовому наборі\nvideo_files = sorted([f for f in os.listdir(TEST_DIR) if f.endswith('.mp4')])\n\n# Обробка батчами по 50 відео\ny_true = []\ny_pred = []\nresults = []\n\nfor i in range(0, len(video_files), BATCH_SIZE):\n    batch_videos = video_files[i:i + BATCH_SIZE]\n    print(f\"\\nОбробка батчу {i // BATCH_SIZE + 1}/{len(video_files) // BATCH_SIZE + 1}...\")\n\n    batch_true = []\n    batch_pred = []\n\n    for video_file in tqdm(batch_videos):\n        video_path = os.path.join(TEST_DIR, video_file)\n        prediction = process_video(video_path)\n\n        if prediction is not None:\n            batch_true.append(labels.get(video_file, 0))  # Якщо мітки немає, вважаємо Real (0)\n            if prediction <= 0.2:\n                batch_pred.append(1)  # Fake\n            elif prediction <= 0.5:\n                batch_pred.append(1)  # Maybe Fake → Fake\n            elif prediction <= 0.8:\n                batch_pred.append(0)  # Maybe Original → Real\n            else:\n                batch_pred.append(0)  # Original → Real\n\n            results.append([video_file, prediction])\n\n    # Додаємо результати поточного батчу до загальних списків\n    y_true.extend(batch_true)\n    y_pred.extend(batch_pred)\n\n    # Обчислюємо метрики для поточного батчу\n    if len(batch_true) > 0:\n        accuracy = accuracy_score(batch_true, batch_pred)\n        precision = precision_score(batch_true, batch_pred)\n        recall = recall_score(batch_true, batch_pred)\n        f1 = f1_score(batch_true, batch_pred)\n\n        print(f\"Batch Accuracy: {accuracy:.4f}\")\n        print(f\"Batch Precision: {precision:.4f}\")\n        print(f\"Batch Recall: {recall:.4f}\")\n        print(f\"Batch F1 Score: {f1:.4f}\")\n\n# Збереження результатів у CSV\ndf_results = pd.DataFrame(results, columns=['filename', 'prediction'])\ndf_results.to_csv(OUTPUT_CSV, index=False)\n\n# Остаточні метрики\nif len(y_true) > 0:\n    final_accuracy = accuracy_score(y_true, y_pred)\n    final_precision = precision_score(y_true, y_pred)\n    final_recall = recall_score(y_true, y_pred)\n    final_f1 = f1_score(y_true, y_pred)\n\n    print(\"\\n=== Фінальні результати ===\")\n    print(f\"Final Accuracy: {final_accuracy:.4f}\")\n    print(f\"Final Precision: {final_precision:.4f}\")\n    print(f\"Final Recall: {final_recall:.4f}\")\n    print(f\"Final F1 Score: {final_f1:.4f}\")\n\nprint(f\"\\nПередбачення збережені у {OUTPUT_CSV}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T19:57:57.424563Z","iopub.execute_input":"2025-02-17T19:57:57.424917Z","iopub.status.idle":"2025-02-17T19:57:57.428934Z","shell.execute_reply.started":"2025-02-17T19:57:57.424883Z","shell.execute_reply":"2025-02-17T19:57:57.427973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict_video(video_path, model, margin=0.2, img_width=256, save_interval=1):\n    \"\"\"\n    Функція для виконання передбачень для кожного кадру у відео.\n    \"\"\"\n    video = Video(video_path)\n    re_imgs = []  # Зберігає передбачення для кожного кадру з обличчям\n\n    for i in range(0, len(video), save_interval):\n        img = video.get(i)\n        face_positions = face_recognition.face_locations(img)\n        \n        for face_position in face_positions:\n            offset = round(margin * (face_position[2] - face_position[0]))\n            y0 = max(face_position[0] - offset, 0)\n            x1 = min(face_position[1] + offset, img.shape[1])\n            y1 = min(face_position[2] + offset, img.shape[0])\n            x0 = max(face_position[3] - offset, 0)\n            face = img[y0:y1, x0:x1]\n\n            # Підготовка кадру обличчя для моделі\n            inp = cv2.resize(face, (img_width, img_width)) / 255.0\n            re_img = model.predict(np.array([inp]))[0][0]\n            re_imgs.append(re_img)\n    \n    # Усереднене значення для усього відео\n    re_video = np.mean(re_imgs) if re_imgs else 0.5  # Якщо не виявлено облич, повертаємо 0.5\n    return re_video\n\n# Виклик функції для передбачення на конкретному відео\nvideo_path = '/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4'\nresult = predict_video(video_path, classifier.model)\n\n# Інтерпретація результату\nprint(f\"Video Prediction Score (0=Fake, 1=Real): {result}\")\nprint(\"Video is Real\" if result > 0.5 else \"Video is Fake\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T21:07:27.852434Z","iopub.execute_input":"2024-11-11T21:07:27.852764Z","iopub.status.idle":"2024-11-11T21:13:47.078199Z","shell.execute_reply.started":"2024-11-11T21:07:27.852725Z","shell.execute_reply":"2024-11-11T21:13:47.077036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\n\ndef plot_training_history(history):\n    \"\"\"\n    Візуалізація графіків точності та втрат під час навчання.\n    \"\"\"\n    # Отримуємо значення втрат і точності\n    loss = history.history['loss']\n    val_loss = history.history.get('val_loss', [])\n    accuracy = history.history.get('accuracy', [])\n    val_accuracy = history.history.get('val_accuracy', [])\n    \n    # Графік втрат\n    plt.figure(figsize=(12, 5))\n    plt.subplot(1, 2, 1)\n    plt.plot(loss, label='Training Loss')\n    if val_loss:\n        plt.plot(val_loss, label='Validation Loss')\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend()\n    \n    # Графік точності\n    plt.subplot(1, 2, 2)\n    plt.plot(accuracy, label='Training Accuracy')\n    if val_accuracy:\n        plt.plot(val_accuracy, label='Validation Accuracy')\n    plt.title('Training and Validation Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    \n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T20:49:33.835328Z","iopub.execute_input":"2024-11-11T20:49:33.835714Z","iopub.status.idle":"2024-11-11T20:49:33.848468Z","shell.execute_reply.started":"2024-11-11T20:49:33.835675Z","shell.execute_reply":"2024-11-11T20:49:33.847568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def visualize_predictions_on_video(video_path, model, margin=0.2, img_width=256, save_interval=1):\n    video = Video(video_path)\n    plt.figure(figsize=(15, 15))\n    frame_predictions = []\n\n    for i in range(0, len(video), save_interval):\n        img = video.get(i)\n        face_positions = face_recognition.face_locations(img)\n        \n        for face_position in face_positions:\n            offset = round(margin * (face_position[2] - face_position[0]))\n            y0 = max(face_position[0] - offset, 0)\n            x1 = min(face_position[1] + offset, img.shape[1])\n            y1 = min(face_position[2] + offset, img.shape[0])\n            x0 = max(face_position[3] - offset, 0)\n            face = img[y0:y1, x0:x1]\n\n            # Підготовка кадру обличчя для моделі\n            inp = cv2.resize(face, (img_width, img_width)) / 255.0\n            prediction = model.predict(np.array([inp]))[0][0]\n            frame_predictions.append(prediction)\n            \n            # Візуалізація кадру з передбаченням\n            plt.imshow(face)\n            plt.title(f\"Prediction: {'Real' if prediction > 0.5 else 'Fake'} ({prediction:.2f})\")\n            plt.axis('off')\n            plt.show()\n    \n    print(f\"Average Prediction for Video: {'Real' if np.mean(frame_predictions) > 0.5 else 'Fake'} ({np.mean(frame_predictions):.2f})\")\n\n# Виклик функції для візуалізації передбачень на конкретному відео\nvisualize_predictions_on_video(video_path, classifier.model)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T21:15:30.920046Z","iopub.execute_input":"2024-11-11T21:15:30.921029Z","iopub.status.idle":"2024-11-11T21:16:09.899233Z","shell.execute_reply.started":"2024-11-11T21:15:30.920981Z","shell.execute_reply":"2024-11-11T21:16:09.897829Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = classifier.model.fit(train_data, epochs=10, validation_data=val_data)\n\n# Виклик функції для побудови графіків\nplot_training_history(history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T20:50:22.788381Z","iopub.execute_input":"2024-11-11T20:50:22.788782Z","iopub.status.idle":"2024-11-11T20:50:22.826162Z","shell.execute_reply.started":"2024-11-11T20:50:22.78874Z","shell.execute_reply":"2024-11-11T20:50:22.825053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\n# Для документації - АРХІТЕКТУРА ЗГОРТКОВОЇ НЕЙРОННОЇ МЕРЕЖІ\n\n# Функція опису архітектури згорткової нейронної мережі\n# для класифікації діпфейків\n# на вхід подається зображення розміром 256 x 256 пікселів\ndef define_model(shape=(256,256,3)):\n    inputImage = Input(shape = shape)\n    \n    # перший блок\n    firstBlock_1 = InceptionLayer(1, 4, 4, 2)(inputImage)\n    firstBlock_2 = BatchNormalization()(firstBlock_1)\n    firstBlock_3 = MaxPooling2D(pool_size=(2, 2), padding='same')(firstBlock_2)\n    \n    # другий блок\n    secondBlock_1 = InceptionLayer(2, 4, 4, 2)(firstBlock_3)\n    secondBlock_2 = BatchNormalization()(secondBlock_1)        \n    secondBlock_3 = MaxPooling2D(pool_size=(2, 2), padding='same')(secondBlock_2)   \n    \n    # третій блок    \n    thirdBlock_1 = Conv2D(16, (3, 3), padding='same', activation = 'elu')(secondBlock_3)\n    thirdBlock_2 = BatchNormalization()(thirdBlock_1)\n    thirdBlock_3 = MaxPooling2D(pool_size=(2, 2), padding='same')(thirdBlock_2)\n    \n    # четвертий блок\n    fourthBlock_1 = Conv2D(16, (3, 3), padding='same', activation = 'elu')(thirdBlock_3)\n    fourthBlock_2 = BatchNormalization()(fourthBlock_1)\n    fourthBlock_3 = MaxPooling2D(pool_size=(2, 2), padding='same')(fourthBlock_2)\n\n    # п'ятий блок\n    fifthBlock_1 = Conv2D(32, (5, 5), padding='same', activation = 'elu')(fourthBlock_3)\n    fifthBlock_2 = BatchNormalization()(fifthBlock_1)\n    fifthBlock_3 = MaxPooling2D(pool_size=(2, 2), padding='same')(fifthBlock_2)\n    \n    final = Flatten()(fifthBlock_3)\n    final = Dropout(0.5)(final)\n    final = Dense(16)(final)\n    final = LeakyReLU(alpha=0.1)(final)\n    final = Dropout(0.5)(final)\n    final = Dense(1, activation = 'sigmoid')(final)\n    \n    model=Model(inputs = x, outputs = y)\n    model.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-4))\n    return model\n'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-11T20:27:32.67243Z","iopub.execute_input":"2024-11-11T20:27:32.672757Z","iopub.status.idle":"2024-11-11T20:27:32.695794Z","shell.execute_reply.started":"2024-11-11T20:27:32.672722Z","shell.execute_reply":"2024-11-11T20:27:32.69484Z"}},"outputs":[],"execution_count":null}]}