{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/working/test1","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\npaths = glob.glob('/kaggle/working/train/*.jpg')\npaths[:5]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T10:11:57.659648Z","iopub.execute_input":"2024-05-09T10:11:57.660027Z","iopub.status.idle":"2024-05-09T10:11:57.776424Z","shell.execute_reply.started":"2024-05-09T10:11:57.659995Z","shell.execute_reply":"2024-05-09T10:11:57.77519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_images = list(filter(lambda x : x.split('/')[-1].split('.')[0] == 'cat', paths))\ndog_images = list(filter(lambda x : x.split('/')[-1].split('.')[0] == 'dog', paths))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:13:07.493894Z","iopub.execute_input":"2024-05-09T10:13:07.494361Z","iopub.status.idle":"2024-05-09T10:13:07.550515Z","shell.execute_reply.started":"2024-05-09T10:13:07.494324Z","shell.execute_reply":"2024-05-09T10:13:07.549339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size_cat = int(len(cat_images) * 0.8)\nprint(f'Train Cat Size: {train_size_cat}')\ntrain_size_dog = int(len(dog_images) * 0.8)\nprint(f'Train Dog Size: {train_size_dog}')","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:14:44.334947Z","iopub.execute_input":"2024-05-09T10:14:44.335386Z","iopub.status.idle":"2024-05-09T10:14:44.342556Z","shell.execute_reply.started":"2024-05-09T10:14:44.335353Z","shell.execute_reply":"2024-05-09T10:14:44.341216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader\nfrom torchvision import datasets, transforms\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:16:01.337752Z","iopub.execute_input":"2024-05-09T10:16:01.338166Z","iopub.status.idle":"2024-05-09T10:16:09.176727Z","shell.execute_reply.started":"2024-05-09T10:16:01.338137Z","shell.execute_reply":"2024-05-09T10:16:09.175176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\nfrom IPython.display import display\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:16:57.646769Z","iopub.execute_input":"2024-05-09T10:16:57.647363Z","iopub.status.idle":"2024-05-09T10:16:57.653647Z","shell.execute_reply.started":"2024-05-09T10:16:57.647327Z","shell.execute_reply":"2024-05-09T10:16:57.652361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog = Image.open('/kaggle/working/train/dog.1164.jpg')\nprint(dog.size)\ndisplay(dog)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:17:30.729217Z","iopub.execute_input":"2024-05-09T10:17:30.730396Z","iopub.status.idle":"2024-05-09T10:17:30.859614Z","shell.execute_reply.started":"2024-05-09T10:17:30.730355Z","shell.execute_reply":"2024-05-09T10:17:30.858578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r, g, b = dog.getpixel((0,0))\nprint(r, g, b)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:18:27.816791Z","iopub.execute_input":"2024-05-09T10:18:27.817901Z","iopub.status.idle":"2024-05-09T10:18:27.824468Z","shell.execute_reply.started":"2024-05-09T10:18:27.817784Z","shell.execute_reply":"2024-05-09T10:18:27.823013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToTensor()\n])\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:19:56.390923Z","iopub.execute_input":"2024-05-09T10:19:56.39138Z","iopub.status.idle":"2024-05-09T10:19:56.946634Z","shell.execute_reply.started":"2024-05-09T10:19:56.39135Z","shell.execute_reply":"2024-05-09T10:19:56.945585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im[:, 0, 0]","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:20:43.839673Z","iopub.execute_input":"2024-05-09T10:20:43.841248Z","iopub.status.idle":"2024-05-09T10:20:43.918257Z","shell.execute_reply.started":"2024-05-09T10:20:43.841193Z","shell.execute_reply":"2024-05-09T10:20:43.916834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"71 / 255","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:21:36.657948Z","iopub.execute_input":"2024-05-09T10:21:36.658415Z","iopub.status.idle":"2024-05-09T10:21:36.667899Z","shell.execute_reply.started":"2024-05-09T10:21:36.658379Z","shell.execute_reply":"2024-05-09T10:21:36.666297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize(224),\n    transforms.ToTensor()\n])\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:23:37.306161Z","iopub.execute_input":"2024-05-09T10:23:37.306622Z","iopub.status.idle":"2024-05-09T10:23:37.718468Z","shell.execute_reply.started":"2024-05-09T10:23:37.306591Z","shell.execute_reply":"2024-05-09T10:23:37.717098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.CenterCrop(224),\n    transforms.ToTensor()\n])\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:33:39.816894Z","iopub.execute_input":"2024-05-09T10:33:39.817363Z","iopub.status.idle":"2024-05-09T10:33:40.178834Z","shell.execute_reply.started":"2024-05-09T10:33:39.817331Z","shell.execute_reply":"2024-05-09T10:33:40.177454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize(224),\n    transforms.CenterCrop(180),\n    transforms.ToTensor()\n])\n\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:37:07.586751Z","iopub.execute_input":"2024-05-09T10:37:07.587178Z","iopub.status.idle":"2024-05-09T10:37:07.991282Z","shell.execute_reply.started":"2024-05-09T10:37:07.587146Z","shell.execute_reply":"2024-05-09T10:37:07.989903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.RandomHorizontalFlip(p=0.5),\n    transforms.ToTensor()\n])\n\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:39:46.129041Z","iopub.execute_input":"2024-05-09T10:39:46.129543Z","iopub.status.idle":"2024-05-09T10:39:46.507261Z","shell.execute_reply.started":"2024-05-09T10:39:46.129481Z","shell.execute_reply":"2024-05-09T10:39:46.50623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize(224),\n    transforms.RandomRotation(90),\n    transforms.CenterCrop(180),\n    transforms.ToTensor()\n])\n\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)));","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:43:57.106688Z","iopub.execute_input":"2024-05-09T10:43:57.107091Z","iopub.status.idle":"2024-05-09T10:43:57.564738Z","shell.execute_reply.started":"2024-05-09T10:43:57.107061Z","shell.execute_reply":"2024-05-09T10:43:57.563196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((1000, 1000)),\n    transforms.ToTensor()\n])\n\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)));","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:46:43.045127Z","iopub.execute_input":"2024-05-09T10:46:43.045536Z","iopub.status.idle":"2024-05-09T10:46:43.771281Z","shell.execute_reply.started":"2024-05-09T10:46:43.045503Z","shell.execute_reply":"2024-05-09T10:46:43.769601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.RandomHorizontalFlip(p=1),\n    transforms.RandomRotation(30),\n    transforms.Resize(224),\n    transforms.CenterCrop(224),\n    transforms.ToTensor()\n])\n\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)));","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:48:56.11699Z","iopub.execute_input":"2024-05-09T10:48:56.117377Z","iopub.status.idle":"2024-05-09T10:48:56.850324Z","shell.execute_reply.started":"2024-05-09T10:48:56.117349Z","shell.execute_reply":"2024-05-09T10:48:56.848898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize([0.485, 0.456, 0.406],\n                         [0.229, 0.224, 0.225])\n])\n\nim = transform(dog)\nprint(im.shape)\nplt.imshow(np.transpose(im.numpy(), (1, 2, 0)));","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:51:48.534044Z","iopub.execute_input":"2024-05-09T10:51:48.534546Z","iopub.status.idle":"2024-05-09T10:51:48.945716Z","shell.execute_reply.started":"2024-05-09T10:51:48.534484Z","shell.execute_reply":"2024-05-09T10:51:48.944283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im[:, 0, 0]","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:52:52.493847Z","iopub.execute_input":"2024-05-09T10:52:52.494263Z","iopub.status.idle":"2024-05-09T10:52:52.507034Z","shell.execute_reply.started":"2024-05-09T10:52:52.494233Z","shell.execute_reply":"2024-05-09T10:52:52.505454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inv_normalize = transforms.Normalize(\n    mean=[-0.485/0.229, -0.456/0.224, -0.406/0.225],\n    std=[1/0.229, 1/0.224, 1/0.225]\n)\n\nim_inv = inv_normalize(im)\nplt.imshow(np.transpose(im_inv.numpy(), (1,2,0)));","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:56:00.154636Z","iopub.execute_input":"2024-05-09T10:56:00.155111Z","iopub.status.idle":"2024-05-09T10:56:00.565252Z","shell.execute_reply.started":"2024-05-09T10:56:00.155077Z","shell.execute_reply":"2024-05-09T10:56:00.564049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}