{"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 numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-17T07:08:19.170043Z","iopub.execute_input":"2021-11-17T07:08:19.170507Z","iopub.status.idle":"2021-11-17T07:08:19.191595Z","shell.execute_reply.started":"2021-11-17T07:08:19.170420Z","shell.execute_reply":"2021-11-17T07:08:19.190717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile \nzipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip').extractall('/kaggle/data')\nzipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip').extractall('/kaggle/data')","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:19.193355Z","iopub.execute_input":"2021-11-17T07:08:19.193642Z","iopub.status.idle":"2021-11-17T07:08:37.997415Z","shell.execute_reply.started":"2021-11-17T07:08:19.193603Z","shell.execute_reply":"2021-11-17T07:08:37.996698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls /kaggle/data/train \nimage_names = os.listdir('/kaggle/data/train')\nprint(len(image_names))","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:37.998774Z","iopub.execute_input":"2021-11-17T07:08:37.999054Z","iopub.status.idle":"2021-11-17T07:08:38.027815Z","shell.execute_reply.started":"2021-11-17T07:08:37.999019Z","shell.execute_reply":"2021-11-17T07:08:38.027100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_list = [name for name in image_names if name[0:3] == 'dog']\ncat_list = [name for name in image_names if name[0:3] == 'cat']\nprint(dog_list[0:5])\nprint(cat_list[0:5])\nprint(len(dog_list))\nprint(len(cat_list))","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:38.029317Z","iopub.execute_input":"2021-11-17T07:08:38.029514Z","iopub.status.idle":"2021-11-17T07:08:38.209968Z","shell.execute_reply.started":"2021-11-17T07:08:38.029490Z","shell.execute_reply":"2021-11-17T07:08:38.209161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nrandom.seed(123)\n\nrandom.shuffle(dog_list)\nrandom.shuffle(cat_list)\n\ntrain_list = dog_list[0:11000] + cat_list[0:11000]\nvalid_list = dog_list[11000:] + cat_list[11000:]\n\nrandom.shuffle(train_list)\nrandom.shuffle(valid_list)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:38.212356Z","iopub.execute_input":"2021-11-17T07:08:38.212568Z","iopub.status.idle":"2021-11-17T07:08:38.265963Z","shell.execute_reply.started":"2021-11-17T07:08:38.212544Z","shell.execute_reply":"2021-11-17T07:08:38.265037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torch.utils.data import Dataset\nfrom torchvision.io import read_image","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:38.267214Z","iopub.execute_input":"2021-11-17T07:08:38.267620Z","iopub.status.idle":"2021-11-17T07:08:39.827014Z","shell.execute_reply.started":"2021-11-17T07:08:38.267585Z","shell.execute_reply":"2021-11-17T07:08:39.826208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:39.828468Z","iopub.execute_input":"2021-11-17T07:08:39.828742Z","iopub.status.idle":"2021-11-17T07:08:39.881906Z","shell.execute_reply.started":"2021-11-17T07:08:39.828696Z","shell.execute_reply":"2021-11-17T07:08:39.880726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 128\n\ntransform = torchvision.transforms.Compose([\n    torchvision.transforms.Resize((size,size)),\n#     torchvision.transforms.RandomCrop((size,size))\n])\n\nclass DogsAndCatsDataset(Dataset):\n    def __init__(self, name_list, img_dir=\"/kaggle/data/train\", transform=transform, target_transform=None):\n        self.name_list = name_list\n        self.img_dir = img_dir\n        self.transform = transform\n        self.target_transform = target_transform\n\n    def __len__(self):\n        return len(self.name_list)\n\n    def __getitem__(self, idx):\n        img_path = os.path.join(self.img_dir, self.name_list[idx])\n        image = read_image(img_path)\n        if self.name_list[idx][0:3] == 'dog':\n            label = 0\n        elif self.name_list[idx][0:3] == 'cat':\n            label = 1\n        if self.transform:\n            image = self.transform(image)\n        if self.target_transform:\n            label = self.target_transform(label)\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:39.883599Z","iopub.execute_input":"2021-11-17T07:08:39.883993Z","iopub.status.idle":"2021-11-17T07:08:39.893492Z","shell.execute_reply.started":"2021-11-17T07:08:39.883955Z","shell.execute_reply":"2021-11-17T07:08:39.892717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\nbatch_size = 128\n\ntrain_set = DogsAndCatsDataset(train_list)\nvalid_set = DogsAndCatsDataset(valid_list)\n\ntrain_dataloader = DataLoader(train_set, batch_size=batch_size, shuffle=True)\nvalid_dataloader = DataLoader(valid_set, batch_size=batch_size, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:39.894924Z","iopub.execute_input":"2021-11-17T07:08:39.895202Z","iopub.status.idle":"2021-11-17T07:08:39.905063Z","shell.execute_reply.started":"2021-11-17T07:08:39.895165Z","shell.execute_reply":"2021-11-17T07:08:39.904251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_features, train_labels = next(iter(train_dataloader))\n# print(f\"Feature batch shape: {train_features.size()}\")\n# print(f\"Labels batch shape: {train_labels.size()}\")\n# image = train_features[0].squeeze()\n# displayImage = image.permute(1,2,0)\n# label = train_labels[0]\n# plt.imshow(displayImage, interpolation='nearest')\n# plt.show()\n# print(f\"Label: {label}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\nclass Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3, 32, 3, padding=1)\n        self.conv2 = nn.Conv2d(32, 32, 3, padding=1)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv3 = nn.Conv2d(32, 64, 3, padding=1)\n        self.conv4 = nn.Conv2d(64, 64, 3, padding=1)\n        self.conv5 = nn.Conv2d(64, 128, 3, padding=1)\n        self.conv6 = nn.Conv2d(128, 128, 3, padding=1)\n        self.conv7 = nn.Conv2d(128, 256, 3, padding=1)\n        self.conv8 = nn.Conv2d(256, 256, 3, padding=1)\n        self.dropout2d = nn.Dropout2d(0.25)\n        self.dropout = nn.Dropout(0.6)\n        self.batchnorm_fc1 = nn.BatchNorm1d(256)\n        self.batchnorm_fc2 = nn.BatchNorm1d(128)\n        self.batchnorm1 = nn.BatchNorm2d(32)\n        self.batchnorm2 = nn.BatchNorm2d(64)\n        self.batchnorm3 = nn.BatchNorm2d(128)\n        self.batchnorm4 = nn.BatchNorm2d(256)\n        self.fc1 = nn.Linear(256 * 8 * 8, 256)\n        self.fc2 = nn.Linear(256, 128)\n        self.fc3 = nn.Linear(128, 2)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.batchnorm1(x)\n        x = F.relu(x)\n#         x = self.dropout2d(x)\n        x = self.conv2(x)\n        x = self.batchnorm1(x)\n        x = F.relu(x)\n        x = self.dropout2d(x)\n        \n        x = self.pool(x)\n        \n        x = self.conv3(x)\n        x = self.batchnorm2(x)\n        x = F.relu(x)\n#         x = self.dropout2d(x)\n        x = self.conv4(x)\n        x = self.batchnorm2(x)\n        x = F.relu(x)\n        x = self.dropout2d(x)\n        \n        x = self.pool(x)\n\n        x = self.conv5(x)\n        x = self.batchnorm3(x)\n        x = F.relu(x)\n#         x = self.dropout2d(x)\n        x = self.conv6(x)\n        x = self.batchnorm3(x)\n        x = F.relu(x)\n        x = self.dropout2d(x)\n        \n        x = self.pool(x)\n        \n        x = self.conv7(x)\n        x = self.batchnorm4(x)\n        x = F.relu(x)\n#         x = self.dropout2d(x)\n        x = self.conv8(x)\n        x = self.batchnorm4(x)\n        x = F.relu(x)\n        x = self.dropout2d(x)\n        \n        x = self.pool(x)\n\n        x = torch.flatten(x, 1)\n        \n        x = self.fc1(x)\n        x = self.batchnorm_fc1(x)\n        x = F.relu(x)\n        x = self.dropout(x)\n        x = self.fc2(x)\n        x = self.batchnorm_fc2(x)\n        x = F.relu(x)\n        x = self.fc3(x)\n        return x\n\nnet = Net().to(device)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:39.908595Z","iopub.execute_input":"2021-11-17T07:08:39.908865Z","iopub.status.idle":"2021-11-17T07:08:42.827034Z","shell.execute_reply.started":"2021-11-17T07:08:39.908839Z","shell.execute_reply":"2021-11-17T07:08:42.825863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(net.parameters(), lr=1e-2, momentum=0.9, weight_decay=5e-2)\n# optimizer = optim.Adam(net.parameters(), lr=5e-4, weight_decay=1e-2)\n# scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)\nscheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.1, patience=0, verbose=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:42.828724Z","iopub.execute_input":"2021-11-17T07:08:42.829097Z","iopub.status.idle":"2021-11-17T07:08:42.840626Z","shell.execute_reply.started":"2021-11-17T07:08:42.829061Z","shell.execute_reply":"2021-11-17T07:08:42.839277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate():\n    correct = 0\n    total = 0\n    running_loss = 0\n    with torch.no_grad():\n        for data in valid_dataloader:\n            images, labels = data[0].to(device), data[1].to(device)\n            outputs = net(images.float())\n            loss = criterion(outputs, labels)\n            _, predicted = torch.max(outputs.data, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n            running_loss += loss.item() * batch_size\n        valid_loss = running_loss / len(valid_list)\n        valid_acc = 100 * correct / total\n\n    print(f'valid loss: {valid_loss:.4f}, valid accuracy: {valid_acc:.2f}')\n    return (valid_loss, valid_acc)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:42.841553Z","iopub.execute_input":"2021-11-17T07:08:42.841901Z","iopub.status.idle":"2021-11-17T07:08:43.109849Z","shell.execute_reply.started":"2021-11-17T07:08:42.841869Z","shell.execute_reply":"2021-11-17T07:08:43.109091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time \nstart_time = time.time()\nepochs = 50\n\nprint('Training Started')\ntrain_losses = []\nvalid_losses = []\ntrain_correct = 0\ntrain_total = 0\n\nfor epoch in range(epochs):\n    \n    running_loss = 0.0\n    \n    for i, data in enumerate(train_dataloader, 0):\n        inputs, labels = data[0].to(device), data[1].to(device)\n\n        optimizer.zero_grad()\n\n        outputs = net(inputs.float())\n\n        _, predicted = torch.max(outputs.data, 1)\n        train_total += labels.size(0)\n        train_correct += (predicted == labels).sum().item()\n        \n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item() * batch_size\n        \n    train_losses.append(running_loss / len(train_list))\n    print(f'[{epoch + 1} epoch] loss: {running_loss / len(train_list) :.4f}, accuracy: {100 * train_correct / train_total :.2f}')\n    valid_loss, valid_accu = validate()\n    valid_losses.append(valid_loss)\n    scheduler.step(running_loss)\n    \nend_time = time.time()\nprint(f'Finished Training. {end_time - start_time:.5f} sec')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T07:08:43.111663Z","iopub.execute_input":"2021-11-17T07:08:43.113925Z","iopub.status.idle":"2021-11-17T08:30:33.306514Z","shell.execute_reply.started":"2021-11-17T07:08:43.113887Z","shell.execute_reply":"2021-11-17T08:30:33.305741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(1,epochs+1), train_losses)\nplt.plot(range(1,epochs+1), valid_losses)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T08:30:33.308180Z","iopub.execute_input":"2021-11-17T08:30:33.308604Z","iopub.status.idle":"2021-11-17T08:30:33.524063Z","shell.execute_reply.started":"2021-11-17T08:30:33.308565Z","shell.execute_reply":"2021-11-17T08:30:33.523412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T08:30:33.525292Z","iopub.execute_input":"2021-11-17T08:30:33.525691Z","iopub.status.idle":"2021-11-17T08:30:42.994249Z","shell.execute_reply.started":"2021-11-17T08:30:33.525640Z","shell.execute_reply":"2021-11-17T08:30:42.993541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PATH = '/kaggle/model/baseline.pth'\n# torch.save(net.state_dict(), PATH)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}