{"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":"none","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"}],"dockerImageVersionId":29844,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-22T16:56:44.967444Z","iopub.execute_input":"2024-03-22T16:56:44.96772Z","iopub.status.idle":"2024-03-22T16:56:44.973282Z","shell.execute_reply.started":"2024-03-22T16:56:44.967679Z","shell.execute_reply":"2024-03-22T16:56:44.972071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lis = os.listdir(\"/kaggle/input/deepfake-detection-challenge/train_sample_videos\")\nm  = None\nfor i in lis:\n    if \".mp4\" not in i:\n        m = i\nm = pd.read_json(\"/kaggle/input/deepfake-detection-challenge/train_sample_videos/\"+ m)\nm=m.T\nm.reset_index(inplace=True)\nm.rename(columns = {\"index\" : \"files\"} , inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:56:45.322722Z","iopub.execute_input":"2024-03-22T16:56:45.323313Z","iopub.status.idle":"2024-03-22T16:56:45.757964Z","shell.execute_reply.started":"2024-03-22T16:56:45.32297Z","shell.execute_reply":"2024-03-22T16:56:45.756625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = pd.DataFrame(m)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:56:46.315071Z","iopub.execute_input":"2024-03-22T16:56:46.315337Z","iopub.status.idle":"2024-03-22T16:56:46.319847Z","shell.execute_reply.started":"2024-03-22T16:56:46.315301Z","shell.execute_reply":"2024-03-22T16:56:46.318692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m.drop(\"split\",axis = 1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:56:46.646468Z","iopub.execute_input":"2024-03-22T16:56:46.647073Z","iopub.status.idle":"2024-03-22T16:56:46.658397Z","shell.execute_reply.started":"2024-03-22T16:56:46.646702Z","shell.execute_reply":"2024-03-22T16:56:46.657245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:56:46.939016Z","iopub.execute_input":"2024-03-22T16:56:46.939414Z","iopub.status.idle":"2024-03-22T16:56:46.960533Z","shell.execute_reply.started":"2024-03-22T16:56:46.939295Z","shell.execute_reply":"2024-03-22T16:56:46.959219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nfrom glob import glob\nimport IPython.display as ipd\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:18:22.518536Z","iopub.execute_input":"2024-03-22T14:18:22.518947Z","iopub.status.idle":"2024-03-22T14:18:22.547119Z","shell.execute_reply.started":"2024-03-22T14:18:22.518878Z","shell.execute_reply":"2024-03-22T14:18:22.54551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cap = cv2.VideoCapture('/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4')","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:20:36.087368Z","iopub.execute_input":"2024-03-22T14:20:36.087685Z","iopub.status.idle":"2024-03-22T14:20:36.215663Z","shell.execute_reply.started":"2024-03-22T14:20:36.087628Z","shell.execute_reply":"2024-03-22T14:20:36.214863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cap.get(cv2.cv2.CAP_PROP_FPS)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:21:11.163566Z","iopub.execute_input":"2024-03-22T14:21:11.164063Z","iopub.status.idle":"2024-03-22T14:21:11.171011Z","shell.execute_reply.started":"2024-03-22T14:21:11.164006Z","shell.execute_reply":"2024-03-22T14:21:11.170087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cap.release()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:21:29.295275Z","iopub.execute_input":"2024-03-22T14:21:29.295774Z","iopub.status.idle":"2024-03-22T14:21:29.302902Z","shell.execute_reply.started":"2024-03-22T14:21:29.295722Z","shell.execute_reply":"2024-03-22T14:21:29.301291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cap = cv2.VideoCapture('/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4')\n\nret,img = cap.read()\nprint(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\ndef get_frames(video_path, duration=5):\n    frames = []\n    capture = cv2.VideoCapture(video_path)\n    fps = capture.get(cv2.CAP_PROP_FPS)\n    num_frames_to_read = int(fps * duration)\n    capture.set(cv2.CAP_PROP_POS_FRAMES, 0)\n    \n    for _ in range(num_frames_to_read):\n        ret, frame = capture.read()\n        if not ret:\n            break\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        frame = cv2.resize(frame, (224,224))\n        frames.append(frame)\n        \n    capture.release()\n    return np.array(frames)/255","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:03.51506Z","iopub.execute_input":"2024-03-22T16:57:03.515636Z","iopub.status.idle":"2024-03-22T16:57:03.769149Z","shell.execute_reply.started":"2024-03-22T16:57:03.515577Z","shell.execute_reply":"2024-03-22T16:57:03.767929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"direc = \"/kaggle/input/deepfake-detection-challenge/train_sample_videos\"","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:35:37.391296Z","iopub.execute_input":"2024-03-22T14:35:37.391569Z","iopub.status.idle":"2024-03-22T14:35:37.396051Z","shell.execute_reply.started":"2024-03-22T14:35:37.391536Z","shell.execute_reply":"2024-03-22T14:35:37.394588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in direc:\n    print(i)\n    break","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:35:50.778558Z","iopub.execute_input":"2024-03-22T14:35:50.779117Z","iopub.status.idle":"2024-03-22T14:35:50.784809Z","shell.execute_reply.started":"2024-03-22T14:35:50.779058Z","shell.execute_reply":"2024-03-22T14:35:50.784101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"videos = []\n\ndirectory_path = '/kaggle/input/deepfake-detection-challenge/train_sample_videos'\n\nfor file in os.listdir(directory_path):\n    file_path = os.path.join(directory_path,file)\n    videos.append(get_frames(file_path))\n    break","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:13.615431Z","iopub.execute_input":"2024-03-22T16:57:13.615685Z","iopub.status.idle":"2024-03-22T16:57:14.739597Z","shell.execute_reply.started":"2024-03-22T16:57:13.615645Z","shell.execute_reply":"2024-03-22T16:57:14.738142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T14:45:37.921672Z","iopub.execute_input":"2024-03-22T14:45:37.922354Z","iopub.status.idle":"2024-03-22T14:45:37.934339Z","shell.execute_reply.started":"2024-03-22T14:45:37.922284Z","shell.execute_reply":"2024-03-22T14:45:37.932754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now working on tensorflow model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom keras import layers","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:20.556971Z","iopub.execute_input":"2024-03-22T16:57:20.557318Z","iopub.status.idle":"2024-03-22T16:57:20.562008Z","shell.execute_reply.started":"2024-03-22T16:57:20.557256Z","shell.execute_reply":"2024-03-22T16:57:20.560625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = tf.convert_to_tensor(videos)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:21.52944Z","iopub.execute_input":"2024-03-22T16:57:21.529694Z","iopub.status.idle":"2024-03-22T16:57:28.306481Z","shell.execute_reply.started":"2024-03-22T16:57:21.529654Z","shell.execute_reply":"2024-03-22T16:57:28.305159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 224\nWIDTH = 224","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:29.714455Z","iopub.execute_input":"2024-03-22T16:57:29.71471Z","iopub.status.idle":"2024-03-22T16:57:29.719774Z","shell.execute_reply.started":"2024-03-22T16:57:29.714672Z","shell.execute_reply":"2024-03-22T16:57:29.718757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Conv2Plus1D(keras.layers.Layer):\n  def __init__(self, filters, kernel_size, padding):\n    \"\"\"\n      A sequence of convolutional layers that first apply the convolution operation over the\n      spatial dimensions, and then the temporal dimension. \n    \"\"\"\n    super().__init__()\n    self.seq = keras.Sequential([  \n        # Spatial decomposition\n        layers.Conv3D(filters=filters,\n                      kernel_size=(1, kernel_size[1], kernel_size[2]),\n                      padding=padding),\n        # Temporal decomposition\n        layers.Conv3D(filters=filters, \n                      kernel_size=(kernel_size[0], 1, 1),\n                      padding=padding)\n        ])\n\n  def call(self, x):\n    return self.seq(x)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:32.139166Z","iopub.execute_input":"2024-03-22T16:57:32.139465Z","iopub.status.idle":"2024-03-22T16:57:32.147585Z","shell.execute_reply.started":"2024-03-22T16:57:32.139421Z","shell.execute_reply":"2024-03-22T16:57:32.146317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResidualMain(keras.layers.Layer):\n  \"\"\"\n    Residual block of the model with convolution, layer normalization, and the\n    activation function, ReLU.\n  \"\"\"\n  def __init__(self, filters, kernel_size):\n    super().__init__()\n    self.seq = keras.Sequential([\n        Conv2Plus1D(filters=filters,\n                    kernel_size=kernel_size,\n                    padding='same'),\n        layers.LayerNormalization(),\n        layers.ReLU(),\n        Conv2Plus1D(filters=filters, \n                    kernel_size=kernel_size,\n                    padding='same'),\n        layers.LayerNormalization()\n    ])\n\n  def call(self, x):\n    return self.seq(x)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:32.615146Z","iopub.execute_input":"2024-03-22T16:57:32.615494Z","iopub.status.idle":"2024-03-22T16:57:32.622258Z","shell.execute_reply.started":"2024-03-22T16:57:32.61543Z","shell.execute_reply":"2024-03-22T16:57:32.621052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Project(keras.layers.Layer):\n  \"\"\"\n    Project certain dimensions of the tensor as the data is passed through different \n    sized filters and downsampled. \n  \"\"\"\n  def __init__(self, units):\n    super().__init__()\n    self.seq = keras.Sequential([\n        layers.Dense(units),\n        layers.LayerNormalization()\n    ])\n\n  def call(self, x):\n    return self.seq(x)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:33.816275Z","iopub.execute_input":"2024-03-22T16:57:33.816569Z","iopub.status.idle":"2024-03-22T16:57:33.822569Z","shell.execute_reply.started":"2024-03-22T16:57:33.816517Z","shell.execute_reply":"2024-03-22T16:57:33.821444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_residual_block(input, filters, kernel_size):\n  \"\"\"\n    Add residual blocks to the model. If the last dimensions of the input data\n    and filter size does not match, project it such that last dimension matches.\n  \"\"\"\n  out = ResidualMain(filters, \n                     kernel_size)(input)\n\n  res = input\n  # Using the Keras functional APIs, project the last dimension of the tensor to\n  # match the new filter size\n  if out.shape[-1] != input.shape[-1]:\n    res = Project(out.shape[-1])(res)\n\n  return layers.add([res, out])","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:57:34.31529Z","iopub.execute_input":"2024-03-22T16:57:34.315582Z","iopub.status.idle":"2024-03-22T16:57:34.322004Z","shell.execute_reply.started":"2024-03-22T16:57:34.315536Z","shell.execute_reply":"2024-03-22T16:57:34.320321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (None, 1, HEIGHT, WIDTH, 3)\ninput = layers.Input(shape=(input_shape[1:]))\nx = input\n\nx = Conv2Plus1D(filters=16, kernel_size=(3, 7, 7), padding='same')(x)\nx = layers.BatchNormalization()(x)\nx = layers.ReLU()(x)\nx = ResizeVideo(HEIGHT // 2, WIDTH // 2)(x)\n\n# Block 1\nx = add_residual_block(x, 16, (3, 3, 3))\nx = ResizeVideo(HEIGHT // 4, WIDTH // 4)(x)\n\n# Block 2\nx = add_residual_block(x, 32, (3, 3, 3))\nx = ResizeVideo(HEIGHT // 8, WIDTH // 8)(x)\n\n# Block 3\nx = add_residual_block(x, 64, (3, 3, 3))\nx = ResizeVideo(HEIGHT // 16, WIDTH // 16)(x)\n\n# Block 4\nx = add_residual_block(x, 128, (3, 3, 3))\n\nx = layers.GlobalAveragePooling3D()(x)\nx = layers.Flatten()(x)\nx = layers.Dense(10)(x)\n\nmodel = keras.Model(input, x)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T16:58:07.331542Z","iopub.execute_input":"2024-03-22T16:58:07.331821Z","iopub.status.idle":"2024-03-22T16:58:07.401841Z","shell.execute_reply.started":"2024-03-22T16:58:07.331764Z","shell.execute_reply":"2024-03-22T16:58:07.399556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build(videos[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"using pretrained MovieNet model","metadata":{}},{"cell_type":"code","source":"!pip install tf-models-official","metadata":{"execution":{"iopub.status.busy":"2024-03-22T17:13:29.690459Z","iopub.execute_input":"2024-03-22T17:13:29.690718Z","iopub.status.idle":"2024-03-22T17:13:40.892462Z","shell.execute_reply.started":"2024-03-22T17:13:29.690682Z","shell.execute_reply":"2024-03-22T17:13:40.890896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\n\n# Import the MoViNet model from TensorFlow Models (tf-models-official) for the MoViNet model\nfrom official.projects.movinet.modeling import movinet\nfrom official.projects.movinet.modeling import movinet_model","metadata":{"execution":{"iopub.status.busy":"2024-03-22T17:13:56.701392Z","iopub.execute_input":"2024-03-22T17:13:56.701676Z","iopub.status.idle":"2024-03-22T17:13:56.729769Z","shell.execute_reply.started":"2024-03-22T17:13:56.701621Z","shell.execute_reply":"2024-03-22T17:13:56.728466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_id = 'a0'\nresolution = 224\n\ntf.keras.backend.clear_session()\n\nbackbone = movinet.Movinet(model_id=model_id)\nbackbone.trainable = False\n\n# Set num_classes=600 to load the pre-trained weights from the original model\nmodel = movinet_model.MovinetClassifier(backbone=backbone, num_classes=600)\nmodel.build([None, None, None, None, 3])\n\n# Load pre-trained weights\n!wget https://storage.googleapis.com/tf_model_garden/vision/movinet/movinet_a0_base.tar.gz -O movinet_a0_base.tar.gz -q\n!tar -xvf movinet_a0_base.tar.gz\n\ncheckpoint_dir = f'movinet_{model_id}_base'\ncheckpoint_path = tf.train.latest_checkpoint(checkpoint_dir)\ncheckpoint = tf.train.Checkpoint(model=model)\nstatus = checkpoint.restore(checkpoint_path)\nstatus.assert_existing_objects_matched()","metadata":{"execution":{"iopub.status.busy":"2024-03-22T17:07:03.506453Z","iopub.execute_input":"2024-03-22T17:07:03.506727Z","iopub.status.idle":"2024-03-22T17:07:03.538244Z","shell.execute_reply.started":"2024-03-22T17:07:03.506688Z","shell.execute_reply":"2024-03-22T17:07:03.537061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}