{"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":"\nimport numpy as np \nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nimport torch\nfrom torch.utils.data import Dataset\nfrom PIL import Image\n\n# from skimage import io\n# from torchvision.io import read_image\n\n\nimport os\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-30T06:10:55.059973Z","iopub.execute_input":"2021-11-30T06:10:55.060403Z","iopub.status.idle":"2021-11-30T06:10:55.073158Z","shell.execute_reply.started":"2021-11-30T06:10:55.060348Z","shell.execute_reply":"2021-11-30T06:10:55.071953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.getcwd())  # Current Working Directory","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:10:55.075391Z","iopub.execute_input":"2021-11-30T06:10:55.075888Z","iopub.status.idle":"2021-11-30T06:10:55.084232Z","shell.execute_reply.started":"2021-11-30T06:10:55.075691Z","shell.execute_reply":"2021-11-30T06:10:55.083355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\", \"r\") as zip_ref:\n    zip_ref.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:10:55.085708Z","iopub.execute_input":"2021-11-30T06:10:55.086051Z","iopub.status.idle":"2021-11-30T06:11:04.247409Z","shell.execute_reply.started":"2021-11-30T06:10:55.086011Z","shell.execute_reply":"2021-11-30T06:11:04.246690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", \"r\") as zip_ref:\n    zip_ref.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:11:04.249617Z","iopub.execute_input":"2021-11-30T06:11:04.249875Z","iopub.status.idle":"2021-11-30T06:11:09.293885Z","shell.execute_reply.started":"2021-11-30T06:11:04.249837Z","shell.execute_reply":"2021-11-30T06:11:09.293136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = './train'\ntest_dir = './test1'\n\ntrain_files = os.listdir(train_dir)\ntest_files = os.listdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:11:09.295133Z","iopub.execute_input":"2021-11-30T06:11:09.295407Z","iopub.status.idle":"2021-11-30T06:11:09.324017Z","shell.execute_reply.started":"2021-11-30T06:11:09.295370Z","shell.execute_reply":"2021-11-30T06:11:09.323336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img = Image.open(os.path.join(train_dir, train_files[0]))\nsample_img.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:11:09.325326Z","iopub.execute_input":"2021-11-30T06:11:09.325698Z","iopub.status.idle":"2021-11-30T06:11:09.403690Z","shell.execute_reply.started":"2021-11-30T06:11:09.325656Z","shell.execute_reply":"2021-11-30T06:11:09.402372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:11:09.405600Z","iopub.execute_input":"2021-11-30T06:11:09.406141Z","iopub.status.idle":"2021-11-30T06:11:09.412691Z","shell.execute_reply.started":"2021-11-30T06:11:09.406091Z","shell.execute_reply":"2021-11-30T06:11:09.411920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CatsAndDogsDataset(Dataset):\n    \n    def __init__(self, img_dir, mode = 'train', transform = None):\n        \n        self.img_dir = img_dir\n        self.transform = transform\n        self.mode = mode\n        \n        self.img_names = os.listdir(img_dir)\n        \n    def __len__(self):\n        \n        return len(self.img_names)\n    \n    def __getitem__(self, index):\n        \n        filename = self.img_names[index]\n        img = Image.open(os.path.join(self.img_dir, filename))\n                         \n        if self.transform:\n            img = self.transform(img)\n            \n        img = img.numpy()\n        \n        if filename.split('.')[0] == 'cat':\n            label = torch.tensor(0)\n        else :\n            label = torch.tensor(1)\n            \n        if self.mode == 'test' :\n            \n            label = filename\n        \n        return img.astype('float32'), label\n                         \n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.127808Z","iopub.execute_input":"2021-11-30T06:12:18.128189Z","iopub.status.idle":"2021-11-30T06:12:18.136927Z","shell.execute_reply.started":"2021-11-30T06:12:18.128160Z","shell.execute_reply":"2021-11-30T06:12:18.136250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resize and rescale..\n\nimport torchvision.transforms as transforms\n\ntransform = transforms.Compose([\n    \n    transforms.Resize((224, 224)),\n    transforms.CenterCrop((224, 224)),\n    transforms.ToTensor(),\n    \n])","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.138750Z","iopub.execute_input":"2021-11-30T06:12:18.139407Z","iopub.status.idle":"2021-11-30T06:12:18.148642Z","shell.execute_reply.started":"2021-11-30T06:12:18.139372Z","shell.execute_reply":"2021-11-30T06:12:18.147797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = CatsAndDogsDataset(train_dir, transform = transform)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.149770Z","iopub.execute_input":"2021-11-30T06:12:18.150096Z","iopub.status.idle":"2021-11-30T06:12:18.172208Z","shell.execute_reply.started":"2021-11-30T06:12:18.150060Z","shell.execute_reply":"2021-11-30T06:12:18.171593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_sample = dataset[0]\nfirst_sample[0].shape","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.173297Z","iopub.execute_input":"2021-11-30T06:12:18.173545Z","iopub.status.idle":"2021-11-30T06:12:18.187658Z","shell.execute_reply.started":"2021-11-30T06:12:18.173513Z","shell.execute_reply":"2021-11-30T06:12:18.186821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_sample[1]","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.189590Z","iopub.execute_input":"2021-11-30T06:12:18.189931Z","iopub.status.idle":"2021-11-30T06:12:18.195817Z","shell.execute_reply.started":"2021-11-30T06:12:18.189866Z","shell.execute_reply":"2021-11-30T06:12:18.195019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_sample[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.197282Z","iopub.execute_input":"2021-11-30T06:12:18.197546Z","iopub.status.idle":"2021-11-30T06:12:18.208986Z","shell.execute_reply.started":"2021-11-30T06:12:18.197511Z","shell.execute_reply":"2021-11-30T06:12:18.207955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.models as models\n\nmodel = models.resnet18(pretrained = True)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.210760Z","iopub.execute_input":"2021-11-30T06:12:18.211059Z","iopub.status.idle":"2021-11-30T06:12:18.434741Z","shell.execute_reply.started":"2021-11-30T06:12:18.211024Z","shell.execute_reply":"2021-11-30T06:12:18.433945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for param in model.parameters():\n    param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.437143Z","iopub.execute_input":"2021-11-30T06:12:18.437693Z","iopub.status.idle":"2021-11-30T06:12:18.442402Z","shell.execute_reply.started":"2021-11-30T06:12:18.437649Z","shell.execute_reply":"2021-11-30T06:12:18.441333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\n\nnum_ftrs = model.fc.in_features\n\nmodel.fc = nn.Sequential(\n    nn.Linear(num_ftrs, 50),\n    nn.Linear(50, 2)\n)\n\nmodel = model.to(device)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.443954Z","iopub.execute_input":"2021-11-30T06:12:18.444229Z","iopub.status.idle":"2021-11-30T06:12:18.471042Z","shell.execute_reply.started":"2021-11-30T06:12:18.444189Z","shell.execute_reply":"2021-11-30T06:12:18.470381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import random_split\n\ntrain_size = int(0.8 * len(dataset))\nval_size = len(dataset) - train_size\ntrain_dataset, val_datset = random_split(dataset, [train_size, val_size])","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.473700Z","iopub.execute_input":"2021-11-30T06:12:18.474011Z","iopub.status.idle":"2021-11-30T06:12:18.480930Z","shell.execute_reply.started":"2021-11-30T06:12:18.473973Z","shell.execute_reply":"2021-11-30T06:12:18.479951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ntrain_dataloader = DataLoader(train_dataset, batch_size = 128, shuffle = True, num_workers = 4)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.482640Z","iopub.execute_input":"2021-11-30T06:12:18.483022Z","iopub.status.idle":"2021-11-30T06:12:18.490760Z","shell.execute_reply.started":"2021-11-30T06:12:18.482929Z","shell.execute_reply":"2021-11-30T06:12:18.489069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr = 0.01)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.492612Z","iopub.execute_input":"2021-11-30T06:12:18.492817Z","iopub.status.idle":"2021-11-30T06:12:18.500495Z","shell.execute_reply.started":"2021-11-30T06:12:18.492795Z","shell.execute_reply":"2021-11-30T06:12:18.499729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 5\n\nfor epoch in range(num_epochs):\n    \n    running_loss = 0.0\n    \n    for i, data in enumerate(train_dataloader, 0):\n        \n        inputs, labels = data\n        \n        inputs, labels = inputs.to(device), labels.to(device)\n        \n        outputs = model(inputs)\n        \n        loss = criterion(outputs, labels)\n        \n        loss.backward()\n        \n        optimizer.step()\n        \n        optimizer.zero_grad()\n        \n        running_loss += loss.item()\n        \n    print(f'Epoch {epoch} : Loss = {running_loss:.3f}')\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:12:18.502072Z","iopub.execute_input":"2021-11-30T06:12:18.502429Z","iopub.status.idle":"2021-11-30T06:18:53.042903Z","shell.execute_reply.started":"2021-11-30T06:12:18.502391Z","shell.execute_reply":"2021-11-30T06:18:53.042050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transform = transforms.Compose([\n    \n    transforms.Resize((224, 224)),\n    transforms.ToTensor()\n])","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:18:53.046285Z","iopub.execute_input":"2021-11-30T06:18:53.049224Z","iopub.status.idle":"2021-11-30T06:18:53.055920Z","shell.execute_reply.started":"2021-11-30T06:18:53.049182Z","shell.execute_reply":"2021-11-30T06:18:53.055241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_files[:5])","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:18:53.057274Z","iopub.execute_input":"2021-11-30T06:18:53.058611Z","iopub.status.idle":"2021-11-30T06:18:53.067861Z","shell.execute_reply.started":"2021-11-30T06:18:53.058573Z","shell.execute_reply":"2021-11-30T06:18:53.067153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = CatsAndDogsDataset(test_dir, mode = 'test', transform = test_transform)\ntest_loader = DataLoader(test_dataset, batch_size = 128, shuffle = False, num_workers = 4)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:19:55.653757Z","iopub.execute_input":"2021-11-30T06:19:55.654652Z","iopub.status.idle":"2021-11-30T06:19:55.667798Z","shell.execute_reply.started":"2021-11-30T06:19:55.654604Z","shell.execute_reply":"2021-11-30T06:19:55.666967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_list = []\nind_list = []\n\nfor x, fn in test_loader:\n    \n    with torch.no_grad():\n        \n        x = x.to(device)\n        output = model(x)\n        pred = torch.argmax(output, dim = 1)\n        ind_list += [n[:-4] for n in fn]\n        pred_list += [p.item() for p in pred]","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:19:55.669713Z","iopub.execute_input":"2021-11-30T06:19:55.670048Z","iopub.status.idle":"2021-11-30T06:20:44.622303Z","shell.execute_reply.started":"2021-11-30T06:19:55.670010Z","shell.execute_reply":"2021-11-30T06:20:44.621293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"id\": ind_list, \"label\": pred_list})\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:23:12.467382Z","iopub.execute_input":"2021-11-30T06:23:12.467868Z","iopub.status.idle":"2021-11-30T06:23:12.494309Z","shell.execute_reply.started":"2021-11-30T06:23:12.467831Z","shell.execute_reply":"2021-11-30T06:23:12.493537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('preds_resnet18v1.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:23:52.720727Z","iopub.execute_input":"2021-11-30T06:23:52.721000Z","iopub.status.idle":"2021-11-30T06:23:52.750224Z","shell.execute_reply.started":"2021-11-30T06:23:52.720970Z","shell.execute_reply":"2021-11-30T06:23:52.749580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}