{"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":"# 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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn import preprocessing\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms\nimport time\nimport math\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-15T03:41:53.815798Z","iopub.execute_input":"2021-08-15T03:41:53.81622Z","iopub.status.idle":"2021-08-15T03:41:56.23544Z","shell.execute_reply.started":"2021-08-15T03:41:53.816153Z","shell.execute_reply":"2021-08-15T03:41:56.234434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l ../input/train/ | grep '^d'","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2021-08-15T03:41:56.238345Z","iopub.execute_input":"2021-08-15T03:41:56.23871Z","iopub.status.idle":"2021-08-15T03:41:56.97893Z","shell.execute_reply.started":"2021-08-15T03:41:56.238652Z","shell.execute_reply":"2021-08-15T03:41:56.977793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, sys\n","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:48:24.772727Z","iopub.execute_input":"2021-08-15T03:48:24.773227Z","iopub.status.idle":"2021-08-15T03:48:24.777658Z","shell.execute_reply.started":"2021-08-15T03:48:24.773155Z","shell.execute_reply":"2021-08-15T03:48:24.776803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.applications import vgg16\nimport zipfile\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')\n    \nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:51:20.887771Z","iopub.execute_input":"2021-08-15T03:51:20.888205Z","iopub.status.idle":"2021-08-15T03:51:23.332226Z","shell.execute_reply.started":"2021-08-15T03:51:20.888163Z","shell.execute_reply":"2021-08-15T03:51:23.330778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_files = os.listdir('path')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:46:02.278469Z","iopub.execute_input":"2021-08-15T03:46:02.278957Z","iopub.status.idle":"2021-08-15T03:46:02.294195Z","shell.execute_reply.started":"2021-08-15T03:46:02.278902Z","shell.execute_reply":"2021-08-15T03:46:02.293254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlen(img_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:45:09.994938Z","iopub.execute_input":"2021-08-15T03:45:09.995293Z","iopub.status.idle":"2021-08-15T03:45:10.012509Z","shell.execute_reply.started":"2021-08-15T03:45:09.995243Z","shell.execute_reply":"2021-08-15T03:45:10.011071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_files = list(filter(lambda x: x != 'train', img_files))\nlen(img_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.157599Z","iopub.status.idle":"2021-08-15T03:41:57.158215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_path(p): return f\"../input/train/{p}\"\nimg_files = list(map(train_path, img_files))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.159251Z","iopub.status.idle":"2021-08-15T03:41:57.159743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CatDogDataset(Dataset):\n    def __init__(self, image_paths, transform):\n        super().__init__()\n        self.paths = image_paths\n        self.len = len(self.paths)\n        self.transform = transform\n        \n    def __len__(self): return self.len\n    \n    def __getitem__(self, index): \n        path = self.paths[index]\n        image = Image.open(path).convert('RGB')\n        image = self.transform(image)\n        label = 0 if 'cat' in path else 1\n        return (image, label)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.160635Z","iopub.status.idle":"2021-08-15T03:41:57.161154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\n","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.162042Z","iopub.status.idle":"2021-08-15T03:41:57.162522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.163421Z","iopub.status.idle":"2021-08-15T03:41:57.163933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(img_files)\ntrain_files = img_files[:20000]\nvalid = img_files[20000:]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.164824Z","iopub.status.idle":"2021-08-15T03:41:57.165328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = CatDogDataset(train_files, transform)\ntrain_dl = DataLoader(train_ds, batch_size=100)\nlen(train_ds), len(train_dl)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.166223Z","iopub.status.idle":"2021-08-15T03:41:57.1667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_ds = CatDogDataset(valid, transform)\nvalid_dl = DataLoader(valid_ds, batch_size=100)\nlen(valid_ds), len(valid_dl)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.167587Z","iopub.status.idle":"2021-08-15T03:41:57.168086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class CatAndDogNet(nn.Module):\n#     def __init__(self):\n#         super(CatAndDogNet, self).__init__()\n#         self.conv1 = nn.Conv2d(3, 32, 7, padding=1)\n#         self.conv2_bn = nn.BatchNorm2d(32)\n#         self.pool = nn.MaxPool2d(2, 2)\n        \n#         self.conv2 = nn.Conv2d(32, 64, 3, padding=1)        \n#         self.pool2 = nn.AvgPool2d(3, 3)\n        \n#         self.fc1 = nn.Linear(64 * 6 * 6, 32)\n#         self.fc2 = nn.Linear(32, 2)\n\n#         self.dropout = nn.Dropout(0.5)        \n\n#     def forward(self, x):\n#         x = self.pool(F.relu(self.conv2_bn(self.conv1(x))))\n#         x = self.pool2(F.relu(self.conv2(x)))\n#         x = x.view(-1, 64 * 6 * 6)\n#         x = F.relu(self.fc1(x))\n#         x = self.dropout(x)\n#         x = self.fc2(x)\n\n#         return x  \n    \nclass CatAndDogNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(in_channels = 3, out_channels = 16, kernel_size=(5, 5), stride=2, padding=1)\n        self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 32, kernel_size=(5, 5), stride=2, padding=1)\n        self.conv3 = nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size=(3, 3), padding=1)\n        self.conv3_bn = nn.BatchNorm2d(64)\n\n        self.fc1 = nn.Linear(in_features= 64 * 6 * 6, out_features=500)\n        self.dropout = nn.Dropout(0.5)        \n        self.fc2 = nn.Linear(in_features=500, out_features=50)\n        self.fc3 = nn.Linear(in_features=50, out_features=2)\n\n        \n    def forward(self, X):\n        X = F.relu(self.conv1(X))\n        X = F.max_pool2d(X, 2)\n        \n        X = F.relu(self.conv2(X))\n        X = F.max_pool2d(X, 2)\n        \n        X = F.relu(self.conv3_bn(self.conv3(X)))\n        X = F.max_pool2d(X, 2)\n        \n#         print(X.shape)\n        X = X.view(X.shape[0], -1)\n        X = F.relu(self.fc1(X))\n        X = self.dropout(X)\n        X = F.relu(self.fc2(X))\n        X = self.fc3(X)\n        \n#         X = torch.sigmoid(X)\n        return X","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.169014Z","iopub.status.idle":"2021-08-15T03:41:57.169488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CatAndDogNet().cuda()\nmodel","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.170356Z","iopub.status.idle":"2021-08-15T03:41:57.170837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses = []\naccuracies = []\nepoches = 5\nstart = time.time()\nloss_fn = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr = 0.001)\nfor epoch in range(epoches):\n    epoch_loss = 0\n    epoch_accuracy = 0\n    for X, y in train_dl:\n        X = X.cuda()\n        y = y.cuda()\n        preds = model(X)\n        loss = loss_fn(preds, y)\n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        accuracy = ((preds.argmax(dim=1) == y).float().mean())\n        epoch_accuracy += accuracy\n        epoch_loss += loss\n        print('.', end='', flush=True)\n        \n    epoch_accuracy = epoch_accuracy/len(train_dl)\n    accuracies.append(epoch_accuracy)\n    epoch_loss = epoch_loss / len(train_dl)\n    losses.append(epoch_loss)\n    print(\"Epoch: {}, train loss: {:.4f}, train accracy: {:.4f}, time: {}\".format(epoch, epoch_loss, epoch_accuracy, time.time() - start))\n\n\n    with torch.no_grad():\n        val_epoch_loss = 0\n        val_epoch_accuracy = 0\n        for val_X, val_y in valid_dl:\n            val_X = val_X.cuda()\n            val_y = val_y.cuda()\n            val_preds = model(val_X)\n            val_loss = loss_fn(val_preds, val_y)\n\n            val_epoch_loss += val_loss            \n            val_accuracy = ((val_preds.argmax(dim=1) == val_y).float().mean())\n            val_epoch_accuracy += val_accuracy\n        val_epoch_accuracy = val_epoch_accuracy/len(valid_dl)\n        val_epoch_loss = val_epoch_loss / len(valid_dl)\n        print(\"Epoch: {}, valid loss: {:.4f}, valid accracy: {:.4f}, time: {}\\n\".format(epoch, val_epoch_loss, val_epoch_accuracy, time.time() - start))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.171714Z","iopub.status.idle":"2021-08-15T03:41:57.172219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = os.listdir('../input/test/')\nlen(test_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.173077Z","iopub.status.idle":"2021-08-15T03:41:57.17355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l ../input/test/ | grep '^d'","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.174569Z","iopub.status.idle":"2021-08-15T03:41:57.175069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = list(filter(lambda x: x != 'test', test_files))\nlen(test_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.175897Z","iopub.status.idle":"2021-08-15T03:41:57.176386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_path(p): return f\"../input/test/{p}\"\ntest_files = list(map(test_path, test_files))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.177292Z","iopub.status.idle":"2021-08-15T03:41:57.177746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files[:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.178618Z","iopub.status.idle":"2021-08-15T03:41:57.179117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestCatDogDataset(Dataset):\n    def __init__(self, image_paths, transform):\n        super().__init__()\n        self.paths = image_paths\n        self.len = len(self.paths)\n        self.transform = transform\n        \n    def __len__(self): return self.len\n    \n    def __getitem__(self, index): \n        path = self.paths[index]\n        image = Image.open(path).convert('RGB')\n        image = self.transform(image)\n        fileid = path.split('/')[-1].split('.')[0]\n        return (image, fileid)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.179972Z","iopub.status.idle":"2021-08-15T03:41:57.180449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = TestCatDogDataset(test_files, transform)\ntest_dl = DataLoader(test_ds, batch_size=100)\nlen(test_ds), len(test_dl)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.181321Z","iopub.status.idle":"2021-08-15T03:41:57.181801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_probs = []\nwith torch.no_grad():\n    for X, fileid in test_dl:\n        X = X.cuda()\n        preds = model(X)\n        preds_list = F.softmax(preds, dim=1)[:, 1].tolist()\n        dog_probs += list(zip(list(fileid), preds_list))\n#         print(dog_probs)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.182662Z","iopub.status.idle":"2021-08-15T03:41:57.183159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dog_probs)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.184007Z","iopub.status.idle":"2021-08-15T03:41:57.184475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_probs.sort(key = lambda d: int(d[0]))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.185329Z","iopub.status.idle":"2021-08-15T03:41:57.185808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_probs[:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.186658Z","iopub.status.idle":"2021-08-15T03:41:57.187164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = list(map(lambda x: x[0], dog_probs))\nprobs = list(map(lambda x: x[1], dog_probs))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.188035Z","iopub.status.idle":"2021-08-15T03:41:57.188519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids[:10], probs[:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.189381Z","iopub.status.idle":"2021-08-15T03:41:57.189861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df = pd.DataFrame({'id':ids,'label':probs})\noutput_df.to_csv('output.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.190722Z","iopub.status.idle":"2021-08-15T03:41:57.191213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.192078Z","iopub.status.idle":"2021-08-15T03:41:57.19254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# version4: Add validation set","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:41:57.193392Z","iopub.status.idle":"2021-08-15T03:41:57.193875Z"},"trusted":true},"execution_count":null,"outputs":[]}]}