{"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":"code","source":"import json\nimport glob\nimport numpy as np\nimport cv2\nimport copy\nimport warnings\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"video_files = glob.glob(\"../input/deepfake-detection-challenge/train_sample_videos/*.mp4\")\nframe_count = []\nfor video_file in video_files:\n  cap = cv2.VideoCapture(video_file)\n  if(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))<150):\n    video_files.remove(video_file)\n    continue\n  frame_count.append(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))\n\nprint(\"frames\", frame_count)\nprint(\"Total number of videos: \", len(frame_count))\nprint(\"Average frame per video: \", np.mean(frame_count))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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\n\nIMGWIDTH = 256","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class 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\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(x, 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(x, y)","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install imageio-ffmpeg","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\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","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntf.test.is_gpu_available(\n    cuda_only=False,\n    min_cuda_compute_capability=None\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = MesoInception4()\nclassifier.load('/kaggle/input/meso-pretrain/MesoInception_F2F')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install CMake==3.22.2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install dlib==19.18.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install face_recognition","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import face_recognition\nsave_interval = 1\nmargin = 0.2\n\nfor vi in os.listdir('/kaggle/input/deepfake-detection-challenge/test_videos'):\n    re_video = 0.5\n    try:\n        video=Video(os.path.join('/kaggle/input/deepfake-detection-challenge/test_videos',vi))\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\n                re_imgs.append(re_img[0][0])\n\n            re_video = np.average(re_imgs)\n            if np.isnan(re_video):\n                re_video = 0.5\n    except:\n        re_video = 0.5\n    result.append([vi,1.0-re_video])\n","metadata":{"execution":{"iopub.status.busy":"2023-03-30T14:50:35.147488Z","iopub.execute_input":"2023-03-30T14:50:35.148091Z","iopub.status.idle":"2023-03-30T14:50:35.229054Z","shell.execute_reply.started":"2023-03-30T14:50:35.147947Z","shell.execute_reply":"2023-03-30T14:50:35.227895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nresult_sheet = pd.DataFrame(result, columns=['filename','label']).fillna(0.5)\nresult_sheet.sort_values('filename').to_csv('result_sheet.csv',index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.hist(result_sheet.label)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_sheet.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}