{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-02T05:45:09.603435Z","iopub.execute_input":"2022-06-02T05:45:09.603818Z","iopub.status.idle":"2022-06-02T05:45:09.638851Z","shell.execute_reply.started":"2022-06-02T05:45:09.603722Z","shell.execute_reply":"2022-06-02T05:45:09.638135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try making own CNN model ✓\n# try making output layer with one node ✓\n# transforms.HorizontalFlip ✓\n# try momentum ✓\n# try other optimizers (Adam, Adagrad, Adadelta) ✓\n# try other activation fucntions (tanh) ✓","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:09.773051Z","iopub.execute_input":"2022-06-02T05:45:09.773583Z","iopub.status.idle":"2022-06-02T05:45:09.777721Z","shell.execute_reply.started":"2022-06-02T05:45:09.773541Z","shell.execute_reply":"2022-06-02T05:45:09.776507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### {\"cat\":0, \"dog\":1}","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nz= zipfile.ZipFile('../input/dogs-vs-cats/train.zip')\nz.extractall()\n\nz1= zipfile.ZipFile('../input/dogs-vs-cats/test1.zip')\nz1.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:09.941205Z","iopub.execute_input":"2022-06-02T05:45:09.941546Z","iopub.status.idle":"2022-06-02T05:45:29.811462Z","shell.execute_reply.started":"2022-06-02T05:45:09.941512Z","shell.execute_reply":"2022-06-02T05:45:29.810397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:29.813140Z","iopub.execute_input":"2022-06-02T05:45:29.813426Z","iopub.status.idle":"2022-06-02T05:45:29.818359Z","shell.execute_reply.started":"2022-06-02T05:45:29.813387Z","shell.execute_reply":"2022-06-02T05:45:29.817422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"./train\"\ntest_path = \"./test1\"","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:29.819670Z","iopub.execute_input":"2022-06-02T05:45:29.820311Z","iopub.status.idle":"2022-06-02T05:45:29.831621Z","shell.execute_reply.started":"2022-06-02T05:45:29.820272Z","shell.execute_reply":"2022-06-02T05:45:29.830428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfilepath_train = os.listdir(train_path)\nfilepath_test1 = os.listdir(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:29.833975Z","iopub.execute_input":"2022-06-02T05:45:29.834793Z","iopub.status.idle":"2022-06-02T05:45:29.867460Z","shell.execute_reply.started":"2022-06-02T05:45:29.834750Z","shell.execute_reply":"2022-06-02T05:45:29.866577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath_train[1]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:29.868777Z","iopub.execute_input":"2022-06-02T05:45:29.869492Z","iopub.status.idle":"2022-06-02T05:45:29.878495Z","shell.execute_reply.started":"2022-06-02T05:45:29.869455Z","shell.execute_reply":"2022-06-02T05:45:29.877743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat=0\ndog=0\nothers=0\ntotal_len=0\nfor i in tqdm(filepath_train):\n    if i.split(\".\")[0]== \"cat\":\n        cat+=1\n    elif i.split(\".\")[0] == \"dog\":\n        dog+=1\n    else:\n        others+=1\n\nprint(f\"Cats: {cat}, Dogs: {dog}, Others: {others}, Length: {cat+dog+others}\")","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:29.879695Z","iopub.execute_input":"2022-06-02T05:45:29.880388Z","iopub.status.idle":"2022-06-02T05:45:29.937786Z","shell.execute_reply.started":"2022-06-02T05:45:29.880345Z","shell.execute_reply":"2022-06-02T05:45:29.936888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:29.939324Z","iopub.execute_input":"2022-06-02T05:45:29.939894Z","iopub.status.idle":"2022-06-02T05:45:30.153666Z","shell.execute_reply.started":"2022-06-02T05:45:29.939846Z","shell.execute_reply":"2022-06-02T05:45:30.152959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimg_shapes = []\n\nfor i in tqdm(range(len(filepath_train))):\n    img = cv2.imread(os.path.join(train_path,filepath_train[i]))\n    #plt.imshow(img, cmap=\"gray\")\n    #plt.show()\n    img_shapes.append(img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:45:30.154888Z","iopub.execute_input":"2022-06-02T05:45:30.155829Z","iopub.status.idle":"2022-06-02T05:46:26.633867Z","shell.execute_reply.started":"2022-06-02T05:45:30.155786Z","shell.execute_reply":"2022-06-02T05:46:26.632994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.635379Z","iopub.execute_input":"2022-06-02T05:46:26.635776Z","iopub.status.idle":"2022-06-02T05:46:26.640748Z","shell.execute_reply.started":"2022-06-02T05:46:26.635728Z","shell.execute_reply":"2022-06-02T05:46:26.639468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(img_shapes)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.645075Z","iopub.execute_input":"2022-06-02T05:46:26.645309Z","iopub.status.idle":"2022-06-02T05:46:26.656485Z","shell.execute_reply.started":"2022-06-02T05:46:26.645281Z","shell.execute_reply":"2022-06-02T05:46:26.655802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmin(np.array(img_shapes),axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.657991Z","iopub.execute_input":"2022-06-02T05:46:26.658222Z","iopub.status.idle":"2022-06-02T05:46:26.701155Z","shell.execute_reply.started":"2022-06-02T05:46:26.658196Z","shell.execute_reply":"2022-06-02T05:46:26.700518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_shapes[np.argmin(np.array(img_shapes),axis=0)[0]]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.702469Z","iopub.execute_input":"2022-06-02T05:46:26.702903Z","iopub.status.idle":"2022-06-02T05:46:26.740612Z","shell.execute_reply.started":"2022-06-02T05:46:26.702870Z","shell.execute_reply":"2022-06-02T05:46:26.739610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_shapes[np.argmin(np.array(img_shapes),axis=0)[1]]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.741730Z","iopub.execute_input":"2022-06-02T05:46:26.742309Z","iopub.status.idle":"2022-06-02T05:46:26.781355Z","shell.execute_reply.started":"2022-06-02T05:46:26.742275Z","shell.execute_reply":"2022-06-02T05:46:26.780573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(np.array(img_shapes),axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.782721Z","iopub.execute_input":"2022-06-02T05:46:26.783157Z","iopub.status.idle":"2022-06-02T05:46:26.823693Z","shell.execute_reply.started":"2022-06-02T05:46:26.783124Z","shell.execute_reply":"2022-06-02T05:46:26.823013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_shapes[np.argmax(np.array(img_shapes),axis=0)[0]]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.824860Z","iopub.execute_input":"2022-06-02T05:46:26.825377Z","iopub.status.idle":"2022-06-02T05:46:26.862574Z","shell.execute_reply.started":"2022-06-02T05:46:26.825341Z","shell.execute_reply":"2022-06-02T05:46:26.861677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_shapes[np.argmax(np.array(img_shapes),axis=0)[1]]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.863589Z","iopub.execute_input":"2022-06-02T05:46:26.864448Z","iopub.status.idle":"2022-06-02T05:46:26.904675Z","shell.execute_reply.started":"2022-06-02T05:46:26.864395Z","shell.execute_reply":"2022-06-02T05:46:26.903795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"new_reshape = 50#img_shapes[23863][1]\nprint(new_reshape)\n\n#?cv2.resize\n\nsrc_img = cv2.imread(os.path.join(train_path,filepath_train[2032]))\n\nsrc_img.shape\n\nresized_img = cv2.resize(src_img,(new_reshape,new_reshape))\n\nplt.imshow(resized_img,cmap='gray')\n\n\nplt.imshow(src_img)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T08:32:33.056331Z","iopub.execute_input":"2022-04-16T08:32:33.056624Z","iopub.status.idle":"2022-04-16T08:32:33.238671Z","shell.execute_reply.started":"2022-04-16T08:32:33.056595Z","shell.execute_reply":"2022-04-16T08:32:33.238068Z"}}},{"cell_type":"code","source":"#os.mkdir(\"./training\")","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.907179Z","iopub.execute_input":"2022-06-02T05:46:26.907848Z","iopub.status.idle":"2022-06-02T05:46:26.913640Z","shell.execute_reply.started":"2022-06-02T05:46:26.907795Z","shell.execute_reply":"2022-06-02T05:46:26.913036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.mkdir(\"./training/dogs\")","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.914985Z","iopub.execute_input":"2022-06-02T05:46:26.915448Z","iopub.status.idle":"2022-06-02T05:46:26.927785Z","shell.execute_reply.started":"2022-06-02T05:46:26.915414Z","shell.execute_reply":"2022-06-02T05:46:26.926716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1 = cv2.imread(os.path.join(test_path,filepath_test1[1]))","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.928921Z","iopub.execute_input":"2022-06-02T05:46:26.929763Z","iopub.status.idle":"2022-06-02T05:46:26.943180Z","shell.execute_reply.started":"2022-06-02T05:46:26.929724Z","shell.execute_reply":"2022-06-02T05:46:26.942120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test1","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.946154Z","iopub.execute_input":"2022-06-02T05:46:26.946527Z","iopub.status.idle":"2022-06-02T05:46:26.951780Z","shell.execute_reply.started":"2022-06-02T05:46:26.946480Z","shell.execute_reply":"2022-06-02T05:46:26.950724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(test1)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.953153Z","iopub.execute_input":"2022-06-02T05:46:26.953472Z","iopub.status.idle":"2022-06-02T05:46:26.965302Z","shell.execute_reply.started":"2022-06-02T05:46:26.953427Z","shell.execute_reply":"2022-06-02T05:46:26.964495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(filepath_test1[1])\nplt.imshow(test1)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:26.966744Z","iopub.execute_input":"2022-06-02T05:46:26.967377Z","iopub.status.idle":"2022-06-02T05:46:27.280952Z","shell.execute_reply.started":"2022-06-02T05:46:26.967335Z","shell.execute_reply":"2022-06-02T05:46:27.280279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_slice = int(0.8*len(filepath_train))","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:27.282514Z","iopub.execute_input":"2022-06-02T05:46:27.282958Z","iopub.status.idle":"2022-06-02T05:46:27.287526Z","shell.execute_reply.started":"2022-06-02T05:46:27.282924Z","shell.execute_reply":"2022-06-02T05:46:27.286584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_slice","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:27.289051Z","iopub.execute_input":"2022-06-02T05:46:27.289924Z","iopub.status.idle":"2022-06-02T05:46:27.300985Z","shell.execute_reply.started":"2022-06-02T05:46:27.289878Z","shell.execute_reply":"2022-06-02T05:46:27.300149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_split = filepath_train[:train_slice]\nval_split = filepath_train[train_slice:]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:27.302699Z","iopub.execute_input":"2022-06-02T05:46:27.303264Z","iopub.status.idle":"2022-06-02T05:46:27.312864Z","shell.execute_reply.started":"2022-06-02T05:46:27.303216Z","shell.execute_reply":"2022-06-02T05:46:27.312145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(val_split)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:27.316010Z","iopub.execute_input":"2022-06-02T05:46:27.316395Z","iopub.status.idle":"2022-06-02T05:46:27.327165Z","shell.execute_reply.started":"2022-06-02T05:46:27.316347Z","shell.execute_reply":"2022-06-02T05:46:27.326310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nimport torchvision \nfrom torchvision import transforms\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:27.328526Z","iopub.execute_input":"2022-06-02T05:46:27.329193Z","iopub.status.idle":"2022-06-02T05:46:29.129963Z","shell.execute_reply.started":"2022-06-02T05:46:27.329087Z","shell.execute_reply":"2022-06-02T05:46:29.128957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\ndevice","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.134806Z","iopub.execute_input":"2022-06-02T05:46:29.135117Z","iopub.status.idle":"2022-06-02T05:46:29.141886Z","shell.execute_reply.started":"2022-06-02T05:46:29.135077Z","shell.execute_reply":"2022-06-02T05:46:29.141086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DogsVsCatsDataset(Dataset):\n    def __init__(self, root, img_list, transform):\n        self.root = root\n        self.img_list = img_list\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.img_list)\n    \n    def __getitem__(self,index):\n        img = cv2.imread(os.path.join(self.root,self.img_list[index]))\n        if self.transform:\n            img = self.transform(img)\n        #img = torch.from_numpy(img)\n        label = 0 if self.img_list[index].split(\".\")[0] == \"cat\" else 1\n        #label = torch.tensor(label)\n        \n        return img,label\n        ","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.143277Z","iopub.execute_input":"2022-06-02T05:46:29.143858Z","iopub.status.idle":"2022-06-02T05:46:29.153168Z","shell.execute_reply.started":"2022-06-02T05:46:29.143817Z","shell.execute_reply":"2022-06-02T05:46:29.152150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = transforms.Compose([transforms.ToTensor(), transforms.Resize((48,48)), transforms.RandomHorizontalFlip(0.5)])\nval_transform = transforms.Compose([transforms.ToTensor(), transforms.Resize((48,48))])","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.154655Z","iopub.execute_input":"2022-06-02T05:46:29.155078Z","iopub.status.idle":"2022-06-02T05:46:29.164984Z","shell.execute_reply.started":"2022-06-02T05:46:29.155011Z","shell.execute_reply":"2022-06-02T05:46:29.163975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = DogsVsCatsDataset(root = train_path, img_list = train_split, transform=train_transform)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.166544Z","iopub.execute_input":"2022-06-02T05:46:29.166828Z","iopub.status.idle":"2022-06-02T05:46:29.182656Z","shell.execute_reply.started":"2022-06-02T05:46:29.166784Z","shell.execute_reply":"2022-06-02T05:46:29.181706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 420\nimg,label = train_data[idx]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.186154Z","iopub.execute_input":"2022-06-02T05:46:29.186793Z","iopub.status.idle":"2022-06-02T05:46:29.273619Z","shell.execute_reply.started":"2022-06-02T05:46:29.186758Z","shell.execute_reply":"2022-06-02T05:46:29.272870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label)\nplt.imshow(img[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.275080Z","iopub.execute_input":"2022-06-02T05:46:29.275637Z","iopub.status.idle":"2022-06-02T05:46:29.435898Z","shell.execute_reply.started":"2022-06-02T05:46:29.275588Z","shell.execute_reply":"2022-06-02T05:46:29.435019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data = DogsVsCatsDataset(root = train_path, img_list = val_split, transform=val_transform)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.437234Z","iopub.execute_input":"2022-06-02T05:46:29.437741Z","iopub.status.idle":"2022-06-02T05:46:29.446191Z","shell.execute_reply.started":"2022-06-02T05:46:29.437705Z","shell.execute_reply":"2022-06-02T05:46:29.442762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 20\nimg,label = val_data[idx]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.447467Z","iopub.execute_input":"2022-06-02T05:46:29.448175Z","iopub.status.idle":"2022-06-02T05:46:29.456082Z","shell.execute_reply.started":"2022-06-02T05:46:29.448133Z","shell.execute_reply":"2022-06-02T05:46:29.455227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label)\nplt.imshow(img[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.457302Z","iopub.execute_input":"2022-06-02T05:46:29.460272Z","iopub.status.idle":"2022-06-02T05:46:29.661322Z","shell.execute_reply.started":"2022-06-02T05:46:29.460210Z","shell.execute_reply":"2022-06-02T05:46:29.660149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"cat=0\ndog=0\nfor img,label in val_data:\n    if label == \"cat\":\n        cat += 1\n    else:\n        dog+=1\n\nprint(cat)\nprint(dog)\n\ncat=0\ndog=0\nfor img,label in test_data:\n    if label == \"cat\":\n        cat += 1\n    else:\n        dog+=1\n\nprint(cat)\nprint(dog)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T09:59:27.899984Z","iopub.execute_input":"2022-04-16T09:59:27.900206Z","iopub.status.idle":"2022-04-16T09:59:27.905315Z","shell.execute_reply.started":"2022-04-16T09:59:27.90018Z","shell.execute_reply":"2022-04-16T09:59:27.904193Z"}}},{"cell_type":"code","source":"train_dataloader = DataLoader(train_data, batch_size=32, shuffle=True) \nval_dataloader = DataLoader(val_data, batch_size=32, shuffle=True) ","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.663129Z","iopub.execute_input":"2022-06-02T05:46:29.663630Z","iopub.status.idle":"2022-06-02T05:46:29.668245Z","shell.execute_reply.started":"2022-06-02T05:46:29.663595Z","shell.execute_reply":"2022-06-02T05:46:29.667416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#?torch.nn.Conv2d","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.669299Z","iopub.execute_input":"2022-06-02T05:46:29.669688Z","iopub.status.idle":"2022-06-02T05:46:29.682020Z","shell.execute_reply.started":"2022-06-02T05:46:29.669649Z","shell.execute_reply":"2022-06-02T05:46:29.681021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ConvNet(nn.Module):\n    \n    def __init__(self):\n        super(ConvNet,self).__init__()\n        self.conv1 = torch.nn.Conv2d(in_channels=3,out_channels=8, kernel_size=3, stride=1, padding=1)\n        self.conv2 = torch.nn.Conv2d(in_channels=8,out_channels=16, kernel_size=3, stride=1, padding=1)\n        self.conv3 = torch.nn.Conv2d(in_channels=16,out_channels=32, kernel_size=3, stride=1, padding=1)\n        self.pooling = torch.nn.MaxPool2d(2)\n        self.linear1 = torch.nn.Linear(6*6*32, 124)\n        self.linear2 = torch.nn.Linear(124,32)\n        self.linear3 = torch.nn.Linear(32,2)\n        nn.init.xavier_normal_(self.linear1.weight)\n        nn.init.xavier_normal_(self.linear2.weight)\n        nn.init.xavier_normal_(self.linear3.weight)\n        \n        \n    def forward(self,x):\n        x = self.pooling(F.tanh(self.conv1(x)))\n        x = self.pooling(F.tanh(self.conv2(x)))\n        x = self.pooling(F.tanh(self.conv3(x)))\n        x = x.view(-1,6*6*32)\n        x = F.tanh(self.linear1(x))\n        x = F.tanh(self.linear2(x))\n        x = self.linear3(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.683494Z","iopub.execute_input":"2022-06-02T05:46:29.683980Z","iopub.status.idle":"2022-06-02T05:46:29.699668Z","shell.execute_reply.started":"2022-06-02T05:46:29.683914Z","shell.execute_reply":"2022-06-02T05:46:29.698558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ConvNet()\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.701253Z","iopub.execute_input":"2022-06-02T05:46:29.702109Z","iopub.status.idle":"2022-06-02T05:46:29.726050Z","shell.execute_reply.started":"2022-06-02T05:46:29.702039Z","shell.execute_reply":"2022-06-02T05:46:29.725338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = torch.nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.727545Z","iopub.execute_input":"2022-06-02T05:46:29.728323Z","iopub.status.idle":"2022-06-02T05:46:29.733448Z","shell.execute_reply.started":"2022-06-02T05:46:29.728271Z","shell.execute_reply":"2022-06-02T05:46:29.732605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate = 3e-4\n#optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum = 0.9)\noptimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)\n\"\"\"SGD (\nParameter Group 0\n    dampening: 0\n    lr: 0.0003\n    momentum: 0\n    nesterov: False\n    weight_decay: 0\n)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.735096Z","iopub.execute_input":"2022-06-02T05:46:29.735705Z","iopub.status.idle":"2022-06-02T05:46:29.749387Z","shell.execute_reply.started":"2022-06-02T05:46:29.735656Z","shell.execute_reply":"2022-06-02T05:46:29.748468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 7","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.750773Z","iopub.execute_input":"2022-06-02T05:46:29.751564Z","iopub.status.idle":"2022-06-02T05:46:29.762940Z","shell.execute_reply.started":"2022-06-02T05:46:29.751516Z","shell.execute_reply":"2022-06-02T05:46:29.762137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LOSS = []\nACC = []\ncorrect=0\nnum_samples = 0\nfor epoch in tqdm(range(num_epochs)):\n    for img,label in train_dataloader:\n        img = img.to(device)\n        label = label.to(device)\n        outputs = model(img)\n        loss = criterion(outputs,label)\n        optimizer.zero_grad()\n    \n        loss.backward()\n        optimizer.step()\n    \n        res = torch.max(outputs,axis=1).indices\n        #print(outputs)\n        #print(res)\n        #print(label)\n        \n    \n        correct += (res == label).float().sum()\n        num_samples += len(label)\n        #print(correct)\n        \n    acc = correct / num_samples\n    LOSS.append(loss.item())\n    ACC.append(acc.item())\n    print(f\"Epoch: {epoch+1}/{num_epochs}, Loss: {loss.item():.5f}, Accuracy: {acc.item():.2f}\")\n    #print(loss.item())\n    #print(acc.item())\n    \nprint(\"---------------------------<|[RESULTS]|>---------------------------\")    \nprint(f\"LOSS: {LOSS}\")\nprint(f\"ACC: {ACC}\")\nprint(\"--------------------------<|[FINISHED]|>--------------------------\")\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:46:29.764388Z","iopub.execute_input":"2022-06-02T05:46:29.764654Z","iopub.status.idle":"2022-06-02T05:55:31.837690Z","shell.execute_reply.started":"2022-06-02T05:46:29.764623Z","shell.execute_reply":"2022-06-02T05:55:31.836860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax1 = plt.subplots()\ncolor = 'tab:red'\nax1.plot(LOSS, color=color)\nax1.set_xlabel('epoch', color=color)\nax1.set_ylabel('Cost', color=color)\nax1.tick_params(axis='y', color=color)\n    \nax2 = ax1.twinx()  \ncolor = 'tab:blue'\nax2.set_ylabel('accuracy', color=color) \nax2.set_xlabel('epoch', color=color)\nax2.plot( ACC, color=color)\nax2.tick_params(axis='y', color=color)\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:31.839576Z","iopub.execute_input":"2022-06-02T05:55:31.839970Z","iopub.status.idle":"2022-06-02T05:55:32.207761Z","shell.execute_reply.started":"2022-06-02T05:55:31.839927Z","shell.execute_reply":"2022-06-02T05:55:32.207140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    LOSS1 = []\n    ACC1 = []\n    correct=0\n    num_samples = 0\n    for img,label in val_dataloader:\n        img = img.to(device)\n        label = label.to(device)\n        outputs = model(img)\n        res = torch.max(outputs,axis=1).indices\n        #print(outputs)\n        #print(res)\n        #print(label)\n        correct += (res == label).float().sum()\n        num_samples += len(label)\n        #print(correct)\n        \n    acc = correct / num_samples\n    LOSS1.append(loss.item())\n    ACC1.append(acc)\n        \n    print(LOSS1)\n    print(ACC1)\nprint(\"--------------------------FINISHED--------------------------\")","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:32.209149Z","iopub.execute_input":"2022-06-02T05:55:32.209506Z","iopub.status.idle":"2022-06-02T05:55:50.536864Z","shell.execute_reply.started":"2022-06-02T05:55:32.209475Z","shell.execute_reply":"2022-06-02T05:55:50.535781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Relu AdamW Train -> 71.00, Val -> 76.12\n# TanH AdamW Train -> 73.59, Val -> 77.78\n","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:50.538470Z","iopub.execute_input":"2022-06-02T05:55:50.538971Z","iopub.status.idle":"2022-06-02T05:55:50.543876Z","shell.execute_reply.started":"2022-06-02T05:55:50.538912Z","shell.execute_reply":"2022-06-02T05:55:50.542731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing","metadata":{}},{"cell_type":"code","source":"#filepath_test1","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:50.545750Z","iopub.execute_input":"2022-06-02T05:55:50.545980Z","iopub.status.idle":"2022-06-02T05:55:50.555001Z","shell.execute_reply.started":"2022-06-02T05:55:50.545952Z","shell.execute_reply":"2022-06-02T05:55:50.554351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DogsVsCatsDatasetTest(Dataset):\n    def __init__(self, root, img_list, transform):\n        self.root = root\n        self.img_list = img_list\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.img_list)\n    \n    def __getitem__(self,index):\n        img = cv2.imread(os.path.join(self.root,self.img_list[index]))\n        if self.transform:\n            img = self.transform(img)\n        #img = torch.from_numpy(img)\n        #label = 0 if self.img_list[index].split(\".\")[0] == \"cat\" else 1\n        #label = torch.tensor(label)\n        \n        return img","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:50.556082Z","iopub.execute_input":"2022-06-02T05:55:50.556675Z","iopub.status.idle":"2022-06-02T05:55:50.569543Z","shell.execute_reply.started":"2022-06-02T05:55:50.556644Z","shell.execute_reply":"2022-06-02T05:55:50.568599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transform = transforms.Compose([transforms.ToTensor(), transforms.Resize((48,48))])","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:50.571165Z","iopub.execute_input":"2022-06-02T05:55:50.572238Z","iopub.status.idle":"2022-06-02T05:55:50.581490Z","shell.execute_reply.started":"2022-06-02T05:55:50.572186Z","shell.execute_reply":"2022-06-02T05:55:50.580716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = DogsVsCatsDatasetTest(root=test_path, img_list=filepath_test1, transform=test_transform)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:50.582875Z","iopub.execute_input":"2022-06-02T05:55:50.583140Z","iopub.status.idle":"2022-06-02T05:55:50.594437Z","shell.execute_reply.started":"2022-06-02T05:55:50.583110Z","shell.execute_reply":"2022-06-02T05:55:50.593373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUTS = []\nIMGS = []\n\nfor i,img in enumerate(test_dataset):\n    \n    img = img.to(device)\n    img = img.reshape(-1,3,48,48)\n    output = model(img)\n    \n    #OUTPUTS.append(output)\n    #IMGS.append(img)\n    res = torch.argmax(output)\n    if res == 0:\n        print(f\"--------CAT--------\")\n    else: \n        print(f\"--------DOG--------\")\n    result_img = img.cpu()\n    plt.imshow(result_img[0][0],cmap=\"gray\")\n    plt.show()\n    \n    if i >= 10:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-06-02T05:55:50.597840Z","iopub.execute_input":"2022-06-02T05:55:50.598406Z","iopub.status.idle":"2022-06-02T05:55:52.450923Z","shell.execute_reply.started":"2022-06-02T05:55:50.598357Z","shell.execute_reply":"2022-06-02T05:55:52.450156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Summary\n### Xavier initializations works better\n### Learning Rate of 3e-4 works very well\n### Increasing spatial dimension from 48x48 to 96x96 didn't work very well\n### Adam Optimizer seems to work slightly better than SGD with momentum(0.9)\n### Adagrad 61.01 -> 65.00 Accuracy (seems to work similar to Adam)\n### GPU -> 01:00 mins per epoch (approx)\n### CPU -> 01:48 mins per epoch (approx)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}