{"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 os, sys, time\nimport cv2\nimport numpy as np\nimport pandas as pd\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\nimport matplotlib.pyplot as plt\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-05-29T11:55:42.440823Z","iopub.execute_input":"2021-05-29T11:55:42.441141Z","iopub.status.idle":"2021-05-29T11:55:42.446161Z","shell.execute_reply.started":"2021-05-29T11:55:42.441082Z","shell.execute_reply":"2021-05-29T11:55:42.445415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = \"/kaggle/input/deepfake-detection-challenge/test_videos/\"\n\ntest_videos = sorted([x for x in os.listdir(test_dir) if x[-4:] == \".mp4\"])\nframe_h = 5\nframe_l = 5\nlen(test_videos)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T11:59:57.110925Z","iopub.execute_input":"2021-05-29T11:59:57.111235Z","iopub.status.idle":"2021-05-29T11:59:57.119212Z","shell.execute_reply.started":"2021-05-29T11:59:57.111184Z","shell.execute_reply":"2021-05-29T11:59:57.118452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpu = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\ngpu","metadata":{"execution":{"iopub.status.busy":"2021-05-29T11:39:37.97112Z","iopub.execute_input":"2021-05-29T11:39:37.971422Z","iopub.status.idle":"2021-05-29T11:39:37.977172Z","shell.execute_reply.started":"2021-05-29T11:39:37.971375Z","shell.execute_reply":"2021-05-29T11:39:37.976433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(0, \"/kaggle/input/blazeface-pytorch\")\nsys.path.insert(0, \"/kaggle/input/deepfakes-inference-demo\")","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:03:34.823518Z","iopub.execute_input":"2021-05-29T12:03:34.823816Z","iopub.status.idle":"2021-05-29T12:03:34.830927Z","shell.execute_reply.started":"2021-05-29T12:03:34.823768Z","shell.execute_reply":"2021-05-29T12:03:34.830129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from blazeface import BlazeFace\nfacedet = BlazeFace().to(gpu)\nfacedet.load_weights(\"/kaggle/input/blazeface-pytorch/blazeface.pth\")\nfacedet.load_anchors(\"/kaggle/input/blazeface-pytorch/anchors.npy\")\n_ = facedet.train(False)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:03:35.439743Z","iopub.execute_input":"2021-05-29T12:03:35.440017Z","iopub.status.idle":"2021-05-29T12:03:35.471556Z","shell.execute_reply.started":"2021-05-29T12:03:35.439967Z","shell.execute_reply":"2021-05-29T12:03:35.47094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from helpers.read_video_1 import VideoReader\nfrom helpers.face_extract_1 import FaceExtractor\n\nframes_per_video = 64 \nvideo_reader = VideoReader()\nvideo_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)\nface_extractor = FaceExtractor(video_read_fn, facedet)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:03:36.835984Z","iopub.execute_input":"2021-05-29T12:03:36.836317Z","iopub.status.idle":"2021-05-29T12:03:36.844467Z","shell.execute_reply.started":"2021-05-29T12:03:36.836264Z","shell.execute_reply":"2021-05-29T12:03:36.843574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_size = 224","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:03:38.21485Z","iopub.execute_input":"2021-05-29T12:03:38.215156Z","iopub.status.idle":"2021-05-29T12:03:38.221294Z","shell.execute_reply.started":"2021-05-29T12:03:38.215105Z","shell.execute_reply":"2021-05-29T12:03:38.220501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import Normalize\n\nmean = [0.485, 0.456, 0.406]\nstd = [0.229, 0.224, 0.225]\nnormalize = Normalize(mean, std)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T13:02:19.516267Z","iopub.execute_input":"2021-05-29T13:02:19.516592Z","iopub.status.idle":"2021-05-29T13:02:19.521784Z","shell.execute_reply.started":"2021-05-29T13:02:19.516528Z","shell.execute_reply":"2021-05-29T13:02:19.520731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_image(img, size, resample=cv2.INTER_AREA):\n    h, w = img.shape[:2]\n    if w > h:\n        h = h * size // w\n        w = size\n    else:\n        w = w * size // h\n        h = size\n\n    resized = cv2.resize(img, (w, h), interpolation=resample)\n    return resized\n\n\ndef square_image(img):\n    h, w = img.shape[:2]\n    size = max(h, w)\n    t = 0\n    b = size - h\n    l = 0\n    r = size - w\n    return cv2.copyMakeBorder(img, t, b, l, r, cv2.BORDER_CONSTANT, value=0)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:04:01.702593Z","iopub.execute_input":"2021-05-29T12:04:01.702881Z","iopub.status.idle":"2021-05-29T12:04:01.710591Z","shell.execute_reply.started":"2021-05-29T12:04:01.702832Z","shell.execute_reply":"2021-05-29T12:04:01.709856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torchvision.models as models\n\nclass MyResNeXt(models.resnet.ResNet):\n    def __init__(self, training=True):\n        super(MyResNeXt, self).__init__(block=models.resnet.Bottleneck,\n                                        layers=[3, 4, 6, 3], \n                                        groups=32, \n                                        width_per_group=4)\n        self.fc = nn.Linear(2048, 1)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:04:02.330804Z","iopub.execute_input":"2021-05-29T12:04:02.331117Z","iopub.status.idle":"2021-05-29T12:04:02.339089Z","shell.execute_reply.started":"2021-05-29T12:04:02.331062Z","shell.execute_reply":"2021-05-29T12:04:02.337739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = torch.load(\"/kaggle/input/deepfakes-inference-demo/resnext.pth\", map_location=gpu)\n\nmodel = MyResNeXt().to(gpu)\nmodel.load_state_dict(checkpoint)\n_ = model.eval()\n\ndel checkpoint","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:04:03.049519Z","iopub.execute_input":"2021-05-29T12:04:03.049809Z","iopub.status.idle":"2021-05-29T12:04:03.455038Z","shell.execute_reply.started":"2021-05-29T12:04:03.04976Z","shell.execute_reply":"2021-05-29T12:04:03.454323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction(video_path, batch_size):\n    try:\n        \n        faces = face_extractor.process_video(video_path)\n        face_extractor.keep_only_best_face(faces)\n        \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 = resize_image(face, input_size)\n                    resized_face = square_image(resized_face)\n\n                    if n < batch_size:\n                        x[n] = resized_face\n                        n += 1\n                    else:\n                        print(\"WARNING: have %d faces but batch size is %d\" % (n, batch_size))\n\n            if n > 0:\n                x = torch.tensor(x, device=gpu).float()\n\n                x = x.permute((0, 3, 1, 2))\n\n                for i in range(len(x)):\n                    x[i] = normalize(x[i] / 255.)\n                \n                with torch.no_grad():\n                    y_pred = model(x)\n                    y_pred = torch.sigmoid(y_pred.squeeze())\n                    return y_pred[:n].mean().item()\n\n    except Exception as e:\n        print(\"Prediction error on video %s: %s\" % (video_path, str(e)))\n\n    return 0.5","metadata":{"execution":{"iopub.status.busy":"2021-05-29T13:02:33.219462Z","iopub.execute_input":"2021-05-29T13:02:33.219746Z","iopub.status.idle":"2021-05-29T13:02:33.231633Z","shell.execute_reply.started":"2021-05-29T13:02:33.219701Z","shell.execute_reply":"2021-05-29T13:02:33.230888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from concurrent.futures import ThreadPoolExecutor\n\ndef prediction_set(videos, num_workers):\n    def process_file(i):\n        filename = videos[i]\n        y_pred = prediction(os.path.join(test_dir, filename), batch_size=frames_per_video)\n        return y_pred\n\n    with ThreadPoolExecutor(max_workers=num_workers) as ex:\n        predictions = ex.map(process_file, range(len(videos)))\n\n    return list(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:04:06.505187Z","iopub.execute_input":"2021-05-29T12:04:06.505473Z","iopub.status.idle":"2021-05-29T12:04:06.512577Z","shell.execute_reply.started":"2021-05-29T12:04:06.505426Z","shell.execute_reply":"2021-05-29T12:04:06.511473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = prediction_set(test_videos, num_workers=4)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:04:07.583661Z","iopub.execute_input":"2021-05-29T12:04:07.58395Z","iopub.status.idle":"2021-05-29T12:29:00.083887Z","shell.execute_reply.started":"2021-05-29T12:04:07.583901Z","shell.execute_reply":"2021-05-29T12:29:00.082786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_value = []\nfor value in predictions:\n    if value > .60:\n        prediction_value.append('FAKE')\n    else:\n        prediction_value.append('REAL')","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:35:30.59621Z","iopub.execute_input":"2021-05-29T12:35:30.596486Z","iopub.status.idle":"2021-05-29T12:35:30.603481Z","shell.execute_reply.started":"2021-05-29T12:35:30.596439Z","shell.execute_reply":"2021-05-29T12:35:30.602845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df_resnext = pd.DataFrame({\"filename\": test_videos, \"label\": predictions,\"result\":prediction_value})\nsubmission_df_resnext.to_csv(\"submission_resnext.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:35:33.994881Z","iopub.execute_input":"2021-05-29T12:35:33.995186Z","iopub.status.idle":"2021-05-29T12:35:34.005156Z","shell.execute_reply.started":"2021-05-29T12:35:33.995128Z","shell.execute_reply":"2021-05-29T12:35:34.004339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df_resnext.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T12:35:36.886793Z","iopub.execute_input":"2021-05-29T12:35:36.887105Z","iopub.status.idle":"2021-05-29T12:35:36.896647Z","shell.execute_reply.started":"2021-05-29T12:35:36.887042Z","shell.execute_reply":"2021-05-29T12:35:36.895934Z"},"trusted":true},"execution_count":null,"outputs":[]}]}