{"cells":[{"metadata":{"id":"YjwlB710mIH_","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom tqdm import tqdm,trange\nfrom sklearn.model_selection import train_test_split\nimport sklearn.metrics\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"id":"-xvk_DhD1iUn","colab_type":"text"},"cell_type":"markdown","source":"# Model"},{"metadata":{"id":"dcIJUiPx1DgP","colab_type":"code","outputId":"d8024ba7-ca65-4aed-c2c8-042a5274fdf4","colab":{"base_uri":"https://localhost:8080/","height":52},"trusted":true},"cell_type":"code","source":"!pip install pytorchcv --quiet\nfrom pytorchcv.model_provider import get_model\nmodel = get_model(\"xception\", pretrained=True)\n# model = get_model(\"resnet18\", pretrained=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = nn.Sequential(*list(model.children())[:-1]) # Remove original output layer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model\n","execution_count":null,"outputs":[]},{"metadata":{"id":"jGr9EuSX1ZYI","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))\n# model[0].final_pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model","execution_count":null,"outputs":[]},{"metadata":{"id":"EEVBeVoW1cJX","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"class Head(torch.nn.Module):\n  def __init__(self, in_f, out_f):\n    super(Head, self).__init__()\n    \n    self.f = nn.Flatten()\n    self.l = nn.Linear(in_f, 512)\n    self.d = nn.Dropout(0.75)\n    self.o = nn.Linear(512, out_f)\n    self.b1 = nn.BatchNorm1d(in_f)\n    self.b2 = nn.BatchNorm1d(512)\n    self.r = nn.ReLU()\n\n  def forward(self, x):\n    x = self.f(x)\n    x = self.b1(x)\n    x = self.d(x)\n\n    x = self.l(x)\n    x = self.r(x)\n    x = self.b2(x)\n    x = self.d(x)\n\n    out = self.o(x)\n    return out","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model","execution_count":null,"outputs":[]},{"metadata":{"id":"FRyOSBXy1wim","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"class FCN(torch.nn.Module):\n  def __init__(self, base, in_f):\n    super(FCN, self).__init__()\n    self.base = base\n    self.h1 = Head(in_f, 1)\n  \n  def forward(self, x):\n    x = self.base(x)\n    return self.h1(x)\n\nmodel = FCN(model, 2048)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model","execution_count":null,"outputs":[]},{"metadata":{"id":"OPA6IyUJ1yxU","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"# !pip install torchtoolbox --quiet\n# from torchtoolbox.tools import summary\n\n# model.cuda()\n# summary(model, torch.rand((1, 3, 150, 150)).cuda())","execution_count":null,"outputs":[]},{"metadata":{"id":"pZv7D2KQ2YBk","colab_type":"text"},"cell_type":"markdown","source":"# Train Functions"},{"metadata":{},"cell_type":"markdown","source":"## View this kernal for inference and submission: https://www.kaggle.com/greatgamedota/xception-binary-classifier-inference"}],"metadata":{"colab":{"name":"Deepfake_Detection.ipynb","provenance":[],"collapsed_sections":["AaU3SeKMz_qm","XsefoEdR1gHt"],"machine_shape":"hm"},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":4}