{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"!ls -l ../input/train/ | grep '^d'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_files = os.listdir('../input/train/')\nlen(img_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_files = list(filter(lambda x: x != 'train', img_files))\nlen(img_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train_path(p): return f\"../input/train/{p}\"\nimg_files = list(map(train_path, img_files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import random","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random.shuffle(img_files)\ntrain_files = img_files[:20000]\nvalid = img_files[20000:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_ds = CatDogDataset(train_files, transform)\ntrain_dl = DataLoader(train_ds, batch_size=100)\nlen(train_ds), len(train_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_ds = CatDogDataset(valid, transform)\nvalid_dl = DataLoader(valid_ds, batch_size=100)\nlen(valid_ds), len(valid_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class 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.fc1 = nn.Linear(in_features= 64 * 6 * 6, out_features=500)\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(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 = F.relu(self.fc2(X))\n        X = self.fc3(X)\n        \n#         X = torch.sigmoid(X)\n        return X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = CatAndDogNet().cuda()\nlosses = []\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))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_files = os.listdir('../input/test/')\nlen(test_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls -l ../input/test/ | grep '^d'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_files = list(filter(lambda x: x != 'test', test_files))\nlen(test_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_path(p): return f\"../input/test/{p}\"\ntest_files = list(map(test_path, test_files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_files[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ds = TestCatDogDataset(test_files, transform)\ntest_dl = DataLoader(test_ds, batch_size=100)\nlen(test_ds), len(test_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(dog_probs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dog_probs.sort(key = lambda d: int(d[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dog_probs[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = list(map(lambda x: x[0], dog_probs))\nprobs = list(map(lambda x: x[1], dog_probs))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids[:10], probs[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_df = pd.DataFrame({'id':ids,'label':probs})\noutput_df.to_csv('output.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# version4: Add validation set","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}