{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"First import all dataset contain library you need.<br>\n\nTo install dlib without internet, add the dataset: https://www.kaggle.com/carlossouza/dlibpkg <br>\nTo install ace-recognition without internet, add the dataset: https://www.kaggle.com/minhtam/face-recognition<br>\nTo install ace-recognition without internet, add the dataset: https://www.kaggle.com/minhtam/imageio-ffmpeg<br>\n","metadata":{}},{"cell_type":"code","source":"!pip install '/kaggle/input/dlibpkg/dlib-19.19.0'\n","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:01:42.045320Z","iopub.execute_input":"2023-04-09T08:01:42.045630Z","iopub.status.idle":"2023-04-09T08:11:25.157006Z","shell.execute_reply.started":"2023-04-09T08:01:42.045568Z","shell.execute_reply":"2023-04-09T08:11:25.156009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/face-recognition/face_recognition_models-0.3.0/face_recognition_models-0.3.0'\n","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:11:28.094372Z","iopub.execute_input":"2023-04-09T08:11:28.094680Z","iopub.status.idle":"2023-04-09T08:11:52.444296Z","shell.execute_reply.started":"2023-04-09T08:11:28.094620Z","shell.execute_reply":"2023-04-09T08:11:52.443200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/face-recognition/face_recognition-0.1.5-py2.py3-none-any.whl'\n","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:12:51.052722Z","iopub.execute_input":"2023-04-09T08:12:51.053102Z","iopub.status.idle":"2023-04-09T08:12:56.567155Z","shell.execute_reply.started":"2023-04-09T08:12:51.053041Z","shell.execute_reply":"2023-04-09T08:12:56.566194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/imageio-ffmpeg/imageio_ffmpeg-0.3.0-py3-none-manylinux2010_x86_64.whl'\n","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:12:56.569054Z","iopub.execute_input":"2023-04-09T08:12:56.569376Z","iopub.status.idle":"2023-04-09T08:13:02.751645Z","shell.execute_reply.started":"2023-04-09T08:12:56.569323Z","shell.execute_reply":"2023-04-09T08:13:02.750568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n\nimport warnings\nwarnings.filterwarnings('ignore')\nPATH = '../input/deepfake-detection-challenge/'\nprint(os.listdir(PATH))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-09T08:13:18.165296Z","iopub.execute_input":"2023-04-09T08:13:18.165613Z","iopub.status.idle":"2023-04-09T08:13:26.361886Z","shell.execute_reply.started":"2023-04-09T08:13:18.165557Z","shell.execute_reply":"2023-04-09T08:13:26.361036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input/meso-pretrain'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:13:29.078895Z","iopub.execute_input":"2023-04-09T08:13:29.079235Z","iopub.status.idle":"2023-04-09T08:13:29.092117Z","shell.execute_reply.started":"2023-04-09T08:13:29.079182Z","shell.execute_reply":"2023-04-09T08:13:29.091215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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)","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:13:32.396303Z","iopub.execute_input":"2023-04-09T08:13:32.396622Z","iopub.status.idle":"2023-04-09T08:13:32.552106Z","shell.execute_reply.started":"2023-04-09T08:13:32.396567Z","shell.execute_reply":"2023-04-09T08:13:32.550903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read video","metadata":{}},{"cell_type":"markdown","source":"read video and extract frame from video","metadata":{}},{"cell_type":"code","source":"class 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","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:13:42.534311Z","iopub.execute_input":"2023-04-09T08:13:42.534628Z","iopub.status.idle":"2023-04-09T08:13:42.542602Z","shell.execute_reply.started":"2023-04-09T08:13:42.534573Z","shell.execute_reply":"2023-04-09T08:13:42.541741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Meso4","metadata":{}},{"cell_type":"code","source":"IMGWIDTH = 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\nclass 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    \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(lr = 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)","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:13:48.709307Z","iopub.execute_input":"2023-04-09T08:13:48.709659Z","iopub.status.idle":"2023-04-09T08:13:48.734908Z","shell.execute_reply.started":"2023-04-09T08:13:48.709592Z","shell.execute_reply":"2023-04-09T08:13:48.734064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"tf.test.is_gpu_available(\n    cuda_only=False,\n    min_cuda_compute_capability=None\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:13:56.563202Z","iopub.execute_input":"2023-04-09T08:13:56.563534Z","iopub.status.idle":"2023-04-09T08:13:58.181553Z","shell.execute_reply.started":"2023-04-09T08:13:56.563474Z","shell.execute_reply":"2023-04-09T08:13:58.180722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = Meso4()\nclassifier.load('/kaggle/input/meso-pretrain/Meso4_DF')\n\n# classifier = MesoInception4()\n# classifier.load('/kaggle/input/meso-pretrain/MesoInception_DF')\n\n# 0 fake\n# 1 real ","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:13:58.183467Z","iopub.execute_input":"2023-04-09T08:13:58.184010Z","iopub.status.idle":"2023-04-09T08:13:58.877677Z","shell.execute_reply.started":"2023-04-09T08:13:58.183814Z","shell.execute_reply":"2023-04-09T08:13:58.876932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{}},{"cell_type":"markdown","source":" predict video by combie image","metadata":{}},{"cell_type":"code","source":"submit = []","metadata":{"execution":{"iopub.status.busy":"2023-04-09T08:14:14.380069Z","iopub.execute_input":"2023-04-09T08:14:14.380408Z","iopub.status.idle":"2023-04-09T08:14:14.384713Z","shell.execute_reply.started":"2023-04-09T08:14:14.380353Z","shell.execute_reply":"2023-04-09T08:14:14.383483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_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    video = Video('/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4')\n    re_imgs = []\n    for i in range(0,video.__len__(),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            inp = cv2.resize(face,(256,256))/255.\n            re_img = classifier.predict(np.array([inp]))\n    #             print(vi,\": \",i , \"  :  \",classifier.predict(np.array([inp])))\n            re_imgs.append(re_img[0][0])\n    re_video = np.average(re_imgs)\n    if np.isnan(re_video):\n        re_video = 0.5\nexcept:\n    re_video = 0.5\n    submit.append([vi,1.0-re_video])\n#     submit.append([vi,re_video])\n\n#     submit[vi] = 1.0-re_video\n#     print(vi,\": \",str(1.0-re_video))\n","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(submit, columns=['filename', 'label']).fillna(0.5)\nsubmission.sort_values('filename').to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(375)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}