{"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-04-26T05:04:08.622814Z","iopub.execute_input":"2022-04-26T05:04:08.623324Z","iopub.status.idle":"2022-04-26T05:04:08.631268Z","shell.execute_reply.started":"2022-04-26T05:04:08.623288Z","shell.execute_reply":"2022-04-26T05:04:08.630428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:04:10.032636Z","iopub.execute_input":"2022-04-26T05:04:10.033181Z","iopub.status.idle":"2022-04-26T05:04:20.387632Z","shell.execute_reply.started":"2022-04-26T05:04:10.033142Z","shell.execute_reply":"2022-04-26T05:04:20.386861Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = os.listdir(\"./train\")","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:04:37.684572Z","iopub.execute_input":"2022-04-26T05:04:37.684859Z","iopub.status.idle":"2022-04-26T05:04:37.705506Z","shell.execute_reply.started":"2022-04-26T05:04:37.684831Z","shell.execute_reply":"2022-04-26T05:04:37.704798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset , DataLoader\nfrom torchvision import transforms\nfrom torch.optim import Adam\nfrom torch.autograd import Variable","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:04:55.266213Z","iopub.execute_input":"2022-04-26T05:04:55.266520Z","iopub.status.idle":"2022-04-26T05:04:56.974317Z","shell.execute_reply.started":"2022-04-26T05:04:55.266490Z","shell.execute_reply":"2022-04-26T05:04:56.973575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = np.random.choice(imgs , 20000 , replace = False)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:27.599587Z","iopub.execute_input":"2022-04-26T05:05:27.599872Z","iopub.status.idle":"2022-04-26T05:05:27.615244Z","shell.execute_reply.started":"2022-04-26T05:05:27.599838Z","shell.execute_reply":"2022-04-26T05:05:27.614608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = np.setdiff1d(imgs , train_imgs)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:29.068731Z","iopub.execute_input":"2022-04-26T05:05:29.069015Z","iopub.status.idle":"2022-04-26T05:05:29.102388Z","shell.execute_reply.started":"2022-04-26T05:05:29.068986Z","shell.execute_reply":"2022-04-26T05:05:29.101686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_to_int = {\"cat\" : 0 , \"dog\" : 1}\ntrain_transforms = transforms.Compose([\n    transforms.Resize((360 , 360)) , \n    transforms.RandomHorizontalFlip(p = 0.5) , \n    transforms.ToTensor() , \n    transforms.Normalize((0 , 0 , 0) , (1 , 1 , 1))\n])\n\ntest_pred_transforms = transforms.Compose([\n    transforms.Resize((360 , 360)) , \n    transforms.ToTensor() , \n    transforms.Normalize((0 , 0 , 0) , (1 , 1 , 1))\n])","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:33.428387Z","iopub.execute_input":"2022-04-26T05:05:33.428667Z","iopub.status.idle":"2022-04-26T05:05:33.435973Z","shell.execute_reply.started":"2022-04-26T05:05:33.428637Z","shell.execute_reply":"2022-04-26T05:05:33.435150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustDat(Dataset):\n    def __init__(self ,imgs , class_to_int , path , mode , transforms):\n        self.imgs = imgs\n        self.class_to_int = class_to_int\n        self.mode = mode\n        self.transforms = transforms\n        self.path = path\n        \n    def __len__(self):\n        return len(self.imgs)\n    \n    def __getitem__(self , idx):\n        img = Image.open(self.path + self.imgs[idx])\n        if self.mode  in [\"train\" , \"test\"]:\n            label = self.class_to_int[self.imgs[idx].split(\".\")[0]]\n            img = self.transforms(img)\n            return img , label\n        elif self.mode == \"pred\":\n            img = self.transforms(img)\n            return img","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:35.453414Z","iopub.execute_input":"2022-04-26T05:05:35.453658Z","iopub.status.idle":"2022-04-26T05:05:35.461269Z","shell.execute_reply.started":"2022-04-26T05:05:35.453631Z","shell.execute_reply":"2022-04-26T05:05:35.460309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = DataLoader(CustDat(train_imgs , class_to_int , \"./train/\" , \"train\" , train_transforms) , batch_size = 20 , shuffle = True)\ntest = DataLoader(CustDat(test_imgs , class_to_int , \"./train/\" , \"test\" , test_pred_transforms) , batch_size = 20 , shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:37.454351Z","iopub.execute_input":"2022-04-26T05:05:37.454600Z","iopub.status.idle":"2022-04-26T05:05:37.459575Z","shell.execute_reply.started":"2022-04-26T05:05:37.454573Z","shell.execute_reply":"2022-04-26T05:05:37.458466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:39.058712Z","iopub.execute_input":"2022-04-26T05:05:39.058981Z","iopub.status.idle":"2022-04-26T05:05:41.890734Z","shell.execute_reply.started":"2022-04-26T05:05:39.058952Z","shell.execute_reply":"2022-04-26T05:05:41.890007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_last_features = model.fc.in_features","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:45.628322Z","iopub.execute_input":"2022-04-26T05:05:45.628790Z","iopub.status.idle":"2022-04-26T05:05:45.632401Z","shell.execute_reply.started":"2022-04-26T05:05:45.628734Z","shell.execute_reply":"2022-04-26T05:05:45.631613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_last_yr = nn.Sequential(\nnn.Linear(num_last_features , 1) , \n    nn.Sigmoid()\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:47.855400Z","iopub.execute_input":"2022-04-26T05:05:47.855882Z","iopub.status.idle":"2022-04-26T05:05:47.861277Z","shell.execute_reply.started":"2022-04-26T05:05:47.855830Z","shell.execute_reply":"2022-04-26T05:05:47.860579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc = new_last_yr","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:48.974172Z","iopub.execute_input":"2022-04-26T05:05:48.974868Z","iopub.status.idle":"2022-04-26T05:05:48.978523Z","shell.execute_reply.started":"2022-04-26T05:05:48.974815Z","shell.execute_reply":"2022-04-26T05:05:48.977820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:53.274098Z","iopub.execute_input":"2022-04-26T05:05:53.274345Z","iopub.status.idle":"2022-04-26T05:05:53.347423Z","shell.execute_reply.started":"2022-04-26T05:05:53.274317Z","shell.execute_reply":"2022-04-26T05:05:53.346695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_fin = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:05:54.764285Z","iopub.execute_input":"2022-04-26T05:05:54.764844Z","iopub.status.idle":"2022-04-26T05:05:57.464237Z","shell.execute_reply.started":"2022-04-26T05:05:54.764806Z","shell.execute_reply":"2022-04-26T05:05:57.463477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.BCELoss()\noptimizer = Adam(model_fin.fc.parameters() , lr = 0.001)","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:06:00.773714Z","iopub.execute_input":"2022-04-26T05:06:00.774304Z","iopub.status.idle":"2022-04-26T05:06:00.780827Z","shell.execute_reply.started":"2022-04-26T05:06:00.774266Z","shell.execute_reply":"2022-04-26T05:06:00.778948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\nfor epoch in range(num_epochs):\n    model_fin.train()\n    su_tr = 0\n    tot_tr = 0\n    loss_list = []\n    for images , labels in train:\n        if torch.cuda.is_available():\n            images = Variable(images.cuda())\n            labels = Variable(labels.cuda())\n        optimizer.zero_grad()\n        output = model_fin(images)\n        loss = criterion(torch.squeeze(output) , labels.float())\n        loss_list.append(loss.data)\n        su_tr += torch.sum(output.reshape(labels.shape).round() == labels)\n        tot_tr += labels.shape[0]\n        loss.backward()\n        optimizer.step()\n    model_fin.eval()\n    su_te = 0\n    tot_te = 0\n    with torch.no_grad():\n        for images , labels in test:\n            output = model_fin(images.cuda())\n            su_te += torch.sum(output.reshape(labels.shape).round().cpu() == labels)\n            tot_te += labels.shape[0]\n    print(\"loss is \"+str(torch.mean(torch.tensor(loss_list)))+\" train accu \"+str(su_tr/tot_tr)+\" test accu \"+str(su_te/tot_te))","metadata":{"execution":{"iopub.status.busy":"2022-04-26T05:17:27.358161Z","iopub.execute_input":"2022-04-26T05:17:27.358408Z","iopub.status.idle":"2022-04-26T05:46:17.875779Z","shell.execute_reply.started":"2022-04-26T05:17:27.358382Z","shell.execute_reply":"2022-04-26T05:46:17.875040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}