{"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-05-07T07:12:47.746366Z","iopub.execute_input":"2022-05-07T07:12:47.746837Z","iopub.status.idle":"2022-05-07T07:12:47.771531Z","shell.execute_reply.started":"2022-05-07T07:12:47.746746Z","shell.execute_reply":"2022-05-07T07:12:47.770723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handling Data","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport cv2\nimport zipfile\nimport os\nimport shutil\nimport copy\nimport time","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:12:47.773238Z","iopub.execute_input":"2022-05-07T07:12:47.773991Z","iopub.status.idle":"2022-05-07T07:12:47.919388Z","shell.execute_reply.started":"2022-05-07T07:12:47.773952Z","shell.execute_reply":"2022-05-07T07:12:47.918561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z= 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-05-07T07:12:47.920866Z","iopub.execute_input":"2022-05-07T07:12:47.921134Z","iopub.status.idle":"2022-05-07T07:13:04.715255Z","shell.execute_reply.started":"2022-05-07T07:12:47.921096Z","shell.execute_reply":"2022-05-07T07:13:04.714482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"./train\"\ntest_path = \"./test1\"","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.717492Z","iopub.execute_input":"2022-05-07T07:13:04.717788Z","iopub.status.idle":"2022-05-07T07:13:04.722767Z","shell.execute_reply.started":"2022-05-07T07:13:04.717745Z","shell.execute_reply":"2022-05-07T07:13:04.721725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath_train = os.listdir(train_path)\nfilepath_test1 = os.listdir(test_path)\n#shutil.rmtree(\"./data\")","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.724153Z","iopub.execute_input":"2022-05-07T07:13:04.725009Z","iopub.status.idle":"2022-05-07T07:13:04.752416Z","shell.execute_reply.started":"2022-05-07T07:13:04.724966Z","shell.execute_reply":"2022-05-07T07:13:04.751697Z"},"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-05-07T07:13:04.755773Z","iopub.execute_input":"2022-05-07T07:13:04.756124Z","iopub.status.idle":"2022-05-07T07:13:04.800012Z","shell.execute_reply.started":"2022-05-07T07:13:04.756095Z","shell.execute_reply":"2022-05-07T07:13:04.799322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./data\")","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.801210Z","iopub.execute_input":"2022-05-07T07:13:04.802015Z","iopub.status.idle":"2022-05-07T07:13:04.805787Z","shell.execute_reply.started":"2022-05-07T07:13:04.801978Z","shell.execute_reply":"2022-05-07T07:13:04.805100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#try:\nos.mkdir(\"./data/train_data\")\nos.mkdir(\"./data/train_data/cats\")\nos.mkdir(\"./data/train_data/dogs\")\n    \n#except Exception as e:\n#print(e)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.807166Z","iopub.execute_input":"2022-05-07T07:13:04.807889Z","iopub.status.idle":"2022-05-07T07:13:04.815213Z","shell.execute_reply.started":"2022-05-07T07:13:04.807853Z","shell.execute_reply":"2022-05-07T07:13:04.814517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./data/val_data\")\nos.mkdir(\"./data/val_data/cats\")\nos.mkdir(\"./data/val_data/dogs\")","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.818581Z","iopub.execute_input":"2022-05-07T07:13:04.819535Z","iopub.status.idle":"2022-05-07T07:13:04.824376Z","shell.execute_reply.started":"2022-05-07T07:13:04.819494Z","shell.execute_reply":"2022-05-07T07:13:04.823580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.remove(\"./train_data/dogs\")\nos.listdir(\"./data/train_data\")","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.825692Z","iopub.execute_input":"2022-05-07T07:13:04.825967Z","iopub.status.idle":"2022-05-07T07:13:04.836419Z","shell.execute_reply.started":"2022-05-07T07:13:04.825935Z","shell.execute_reply":"2022-05-07T07:13:04.835736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_root = \"./data/train_data\"\ncats_train_folder = \"./data/train_data/cats\"\ndogs_train_folder = \"./data/train_data/dogs\"\n\nval_data_root = \"./data/val_data\"\ncats_val_folder = \"./data/val_data/cats\"         \ndogs_val_folder = \"./data/val_data/dogs\"","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.837831Z","iopub.execute_input":"2022-05-07T07:13:04.838078Z","iopub.status.idle":"2022-05-07T07:13:04.843832Z","shell.execute_reply.started":"2022-05-07T07:13:04.838047Z","shell.execute_reply":"2022-05-07T07:13:04.842991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(train_data_root)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.844839Z","iopub.execute_input":"2022-05-07T07:13:04.845112Z","iopub.status.idle":"2022-05-07T07:13:04.855479Z","shell.execute_reply.started":"2022-05-07T07:13:04.845073Z","shell.execute_reply":"2022-05-07T07:13:04.854409Z"},"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    src = os.path.join(train_path,i)\n    if i.split(\".\")[0]== \"cat\":\n        cat+=1\n        shutil.copy(src,cats_train_folder)\n    elif i.split(\".\")[0] == \"dog\":\n        dog+=1\n        shutil.copy(src,dogs_train_folder)\n    else:\n        others+=1\n\nprint(f\"Cats: {cat}, Dogs: {dog}, Others: {others}, Length: {cat+dog+others}\")\nprint(\"---------------------<SUCESSFULLY MOVED>---------------------\")","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:04.857254Z","iopub.execute_input":"2022-05-07T07:13:04.857533Z","iopub.status.idle":"2022-05-07T07:13:08.132870Z","shell.execute_reply.started":"2022-05-07T07:13:04.857498Z","shell.execute_reply":"2022-05-07T07:13:08.132031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(cats_train_folder))","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.134307Z","iopub.execute_input":"2022-05-07T07:13:08.134613Z","iopub.status.idle":"2022-05-07T07:13:08.149762Z","shell.execute_reply.started":"2022-05-07T07:13:08.134575Z","shell.execute_reply":"2022-05-07T07:13:08.149085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(dogs_train_folder)[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.151018Z","iopub.execute_input":"2022-05-07T07:13:08.151269Z","iopub.status.idle":"2022-05-07T07:13:08.164610Z","shell.execute_reply.started":"2022-05-07T07:13:08.151229Z","shell.execute_reply":"2022-05-07T07:13:08.163902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_train_images = os.listdir(cats_train_folder)\ndog_train_images = os.listdir(dogs_train_folder)\ncat_train_images[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.165753Z","iopub.execute_input":"2022-05-07T07:13:08.166381Z","iopub.status.idle":"2022-05-07T07:13:08.186173Z","shell.execute_reply.started":"2022-05-07T07:13:08.166345Z","shell.execute_reply":"2022-05-07T07:13:08.185531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(os.path.join(cats_train_folder,cat_train_images[0]))\n#print(img)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.187394Z","iopub.execute_input":"2022-05-07T07:13:08.187638Z","iopub.status.idle":"2022-05-07T07:13:08.489001Z","shell.execute_reply.started":"2022-05-07T07:13:08.187606Z","shell.execute_reply":"2022-05-07T07:13:08.488325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_cat_images = cat_train_images[-int((len(cat_train_images)*0.2)):]\nsrc = cats_train_folder\ndestination = cats_val_folder\nfor cat in val_cat_images:\n    shutil.move(os.path.join(src,cat),destination)\n    \nprint(\"-------COMPLETE-------\")\nprint(len(os.listdir(cats_val_folder)))","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.489997Z","iopub.execute_input":"2022-05-07T07:13:08.490224Z","iopub.status.idle":"2022-05-07T07:13:08.617872Z","shell.execute_reply.started":"2022-05-07T07:13:08.490191Z","shell.execute_reply":"2022-05-07T07:13:08.615686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dog_images = dog_train_images[-int((len(dog_train_images)*0.2)):]\nsrc = dogs_train_folder\ndestination = dogs_val_folder\nfor dog in val_dog_images:\n    shutil.move(os.path.join(src,dog),destination)\n    \nprint(\"-------COMPLETE-------\")\nprint(len(os.listdir(dogs_val_folder)))","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.618997Z","iopub.execute_input":"2022-05-07T07:13:08.619251Z","iopub.status.idle":"2022-05-07T07:13:08.724655Z","shell.execute_reply.started":"2022-05-07T07:13:08.619203Z","shell.execute_reply":"2022-05-07T07:13:08.723309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Working with Data","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchvision\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import models\nimport torch.nn.functional as F\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import DataLoader\nimport torchvision.datasets as datasets\nimport torchvision.transforms as transforms\nfrom torchvision.datasets import ImageFolder","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:08.726139Z","iopub.execute_input":"2022-05-07T07:13:08.726404Z","iopub.status.idle":"2022-05-07T07:13:12.571838Z","shell.execute_reply.started":"2022-05-07T07:13:08.726371Z","shell.execute_reply":"2022-05-07T07:13:12.571102Z"},"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-05-07T07:13:12.573061Z","iopub.execute_input":"2022-05-07T07:13:12.573319Z","iopub.status.idle":"2022-05-07T07:13:12.622867Z","shell.execute_reply.started":"2022-05-07T07:13:12.573286Z","shell.execute_reply":"2022-05-07T07:13:12.622154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"./data\"\nos.listdir(data_dir)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:12.624023Z","iopub.execute_input":"2022-05-07T07:13:12.624669Z","iopub.status.idle":"2022-05-07T07:13:12.639363Z","shell.execute_reply.started":"2022-05-07T07:13:12.624608Z","shell.execute_reply":"2022-05-07T07:13:12.638634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sets = ['train_data', 'val_data']","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:12.640200Z","iopub.execute_input":"2022-05-07T07:13:12.640412Z","iopub.status.idle":"2022-05-07T07:13:12.646121Z","shell.execute_reply.started":"2022-05-07T07:13:12.640388Z","shell.execute_reply":"2022-05-07T07:13:12.645260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = transforms.Compose([transforms.Resize((224,224)),\n                                     transforms.ToTensor(),\n                                     transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225]),\n                                     transforms.RandomHorizontalFlip(0.5)])\n\nval_transform = transforms.Compose([transforms.Resize((224,224)),\n                                     transforms.ToTensor(),\n                                     transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])])\n\ndata_transforms = {\n    \"train_data\": train_transform,\n    \"val_data\": val_transform\n}\n\ndata_transforms[\"val_data\"]","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:12.651831Z","iopub.execute_input":"2022-05-07T07:13:12.652023Z","iopub.status.idle":"2022-05-07T07:13:12.660568Z","shell.execute_reply.started":"2022-05-07T07:13:12.651999Z","shell.execute_reply":"2022-05-07T07:13:12.659866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_dataset = ImageFolder(os.path.join(data_dir,\"train_data\"), transform=train_transform)\n#val_dataset = ImageFolder(os.path.join(data_dir,\"val_data\"), transform=val_transform)\n\nimage_datasets = {x: ImageFolder(os.path.join(data_dir,x), transform=data_transforms[x]) for x in [\"train_data\",\"val_data\"]}","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:12.661931Z","iopub.execute_input":"2022-05-07T07:13:12.662321Z","iopub.status.idle":"2022-05-07T07:13:12.863284Z","shell.execute_reply.started":"2022-05-07T07:13:12.662281Z","shell.execute_reply":"2022-05-07T07:13:12.862477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 19213\nimg_permuted = image_datasets[\"train_data\"][idx][0].permute(1,2,0)\nplt.imshow(img_permuted)\nprint(image_datasets[\"train_data\"].classes[ image_datasets[\"train_data\"][idx][1]])","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:12.864618Z","iopub.execute_input":"2022-05-07T07:13:12.865039Z","iopub.status.idle":"2022-05-07T07:13:13.220975Z","shell.execute_reply.started":"2022-05-07T07:13:12.865003Z","shell.execute_reply":"2022-05-07T07:13:13.220327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_dataloader = DataLoader(train_dataset, batch_size=16, shuffle=True)\n#val_dataloader = DataLoader(val_dataset, batch_size=16, shuffle=True)\n\ndataloaders = {x: DataLoader(image_datasets[x],batch_size=16,shuffle=True) for x in [\"train_data\",\"val_data\"]}","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:13.222003Z","iopub.execute_input":"2022-05-07T07:13:13.222379Z","iopub.status.idle":"2022-05-07T07:13:13.227612Z","shell.execute_reply.started":"2022-05-07T07:13:13.222333Z","shell.execute_reply":"2022-05-07T07:13:13.226904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloaders[\"train_data\"]\n#train_dataloader","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:13.229063Z","iopub.execute_input":"2022-05-07T07:13:13.229541Z","iopub.status.idle":"2022-05-07T07:13:13.238472Z","shell.execute_reply.started":"2022-05-07T07:13:13.229505Z","shell.execute_reply":"2022-05-07T07:13:13.237703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = iter(dataloaders[\"train_data\"])\na.next()[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:13.239886Z","iopub.execute_input":"2022-05-07T07:13:13.240421Z","iopub.status.idle":"2022-05-07T07:13:13.352245Z","shell.execute_reply.started":"2022-05-07T07:13:13.240386Z","shell.execute_reply":"2022-05-07T07:13:13.351479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Function","metadata":{}},{"cell_type":"code","source":"def train_model(model, criterion, optimizer, scheduler, num_epochs=25):\n    since = time.time()\n\n    best_acc = 0.0\n\n    for epoch in range(num_epochs):\n        print('Epoch {}/{}'.format(epoch, num_epochs - 1))\n        print('-' * 10)\n\n        \n        for phase in ['train_data', 'val_data']:\n            if phase == 'train_data':\n                model.train()\n            else:\n                model.eval()\n\n            running_loss = 0.0\n            running_corrects = 0\n\n           \n            for inputs, labels in dataloaders[phase]:\n                inputs = inputs.to(device)\n                labels = labels.to(device)\n\n                \n                with torch.set_grad_enabled(phase == 'train_data'):\n                    outputs = model(inputs)\n                    _, preds = torch.max(outputs, 1)\n                    loss = criterion(outputs, labels)\n\n                    \n                    if phase == 'train_data':\n                        optimizer.zero_grad()\n                        loss.backward()\n                        optimizer.step()\n\n               \n                running_loss += loss.item() * inputs.size(0)\n                running_corrects += torch.sum(preds == labels.data)\n\n            if phase == 'train_data':\n                scheduler.step()\n\n            epoch_loss = running_loss / len(image_datasets[phase])\n            epoch_acc = running_corrects.double() / len(image_datasets[phase])\n\n            print('{} Loss: {:.4f} Acc: {:.4f}'.format(\n                phase, epoch_loss, epoch_acc))\n\n          \n            if phase == 'val_data' and epoch_acc > best_acc:\n                best_acc = epoch_acc\n                \n\n        print()\n\n    time_elapsed = time.time() - since\n    print('Training complete in {:.0f}m {:.0f}s'.format(\n        time_elapsed // 60, time_elapsed % 60))\n    print('Best val Acc: {:4f}'.format(best_acc))\n\n    \n    return model\n            ","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:13.353679Z","iopub.execute_input":"2022-05-07T07:13:13.353918Z","iopub.status.idle":"2022-05-07T07:13:13.366215Z","shell.execute_reply.started":"2022-05-07T07:13:13.353885Z","shell.execute_reply":"2022-05-07T07:13:13.365510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning with Resnet18","metadata":{}},{"cell_type":"code","source":"model = models.resnet18(pretrained=True)\nnum_ftrs = model.fc.in_features","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:13.367734Z","iopub.execute_input":"2022-05-07T07:13:13.368209Z","iopub.status.idle":"2022-05-07T07:13:23.279162Z","shell.execute_reply.started":"2022-05-07T07:13:13.368172Z","shell.execute_reply":"2022-05-07T07:13:23.278361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fc = nn.Linear(num_ftrs, 2)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:23.280255Z","iopub.execute_input":"2022-05-07T07:13:23.280588Z","iopub.status.idle":"2022-05-07T07:13:23.288355Z","shell.execute_reply.started":"2022-05-07T07:13:23.280546Z","shell.execute_reply":"2022-05-07T07:13:23.287679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:23.289515Z","iopub.execute_input":"2022-05-07T07:13:23.290218Z","iopub.status.idle":"2022-05-07T07:13:26.369792Z","shell.execute_reply.started":"2022-05-07T07:13:23.290180Z","shell.execute_reply":"2022-05-07T07:13:26.369050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:26.371161Z","iopub.execute_input":"2022-05-07T07:13:26.371417Z","iopub.status.idle":"2022-05-07T07:13:26.379047Z","shell.execute_reply.started":"2022-05-07T07:13:26.371384Z","shell.execute_reply":"2022-05-07T07:13:26.378375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = optim.SGD(model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:26.380552Z","iopub.execute_input":"2022-05-07T07:13:26.381041Z","iopub.status.idle":"2022-05-07T07:13:26.426309Z","shell.execute_reply.started":"2022-05-07T07:13:26.380994Z","shell.execute_reply":"2022-05-07T07:13:26.425511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"step_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:26.427735Z","iopub.execute_input":"2022-05-07T07:13:26.428166Z","iopub.status.idle":"2022-05-07T07:13:26.435016Z","shell.execute_reply.started":"2022-05-07T07:13:26.428128Z","shell.execute_reply":"2022-05-07T07:13:26.434260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = train_model(model, criterion, optimizer, step_lr_scheduler, num_epochs=3)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:13:26.436557Z","iopub.execute_input":"2022-05-07T07:13:26.436976Z","iopub.status.idle":"2022-05-07T07:21:54.371958Z","shell.execute_reply.started":"2022-05-07T07:13:26.436910Z","shell.execute_reply":"2022-05-07T07:21:54.371166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Model","metadata":{}},{"cell_type":"code","source":"FILE = \"dogs_vs_cats_resnet18.pth\"","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.373099Z","iopub.execute_input":"2022-05-07T07:21:54.373716Z","iopub.status.idle":"2022-05-07T07:21:54.378688Z","shell.execute_reply.started":"2022-05-07T07:21:54.373676Z","shell.execute_reply":"2022-05-07T07:21:54.377692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(),FILE)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.379958Z","iopub.execute_input":"2022-05-07T07:21:54.380703Z","iopub.status.idle":"2022-05-07T07:21:54.462917Z","shell.execute_reply.started":"2022-05-07T07:21:54.380668Z","shell.execute_reply":"2022-05-07T07:21:54.462165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r'./dogs_vs_cats_resnet18.pth')","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.464194Z","iopub.execute_input":"2022-05-07T07:21:54.464442Z","iopub.status.idle":"2022-05-07T07:21:54.470484Z","shell.execute_reply.started":"2022-05-07T07:21:54.464408Z","shell.execute_reply":"2022-05-07T07:21:54.469815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing","metadata":{}},{"cell_type":"code","source":"test_images = os.listdir(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.471829Z","iopub.execute_input":"2022-05-07T07:21:54.472315Z","iopub.status.idle":"2022-05-07T07:21:54.486338Z","shell.execute_reply.started":"2022-05-07T07:21:54.472276Z","shell.execute_reply":"2022-05-07T07:21:54.485688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_dataset = ImageFolder(os.path.join(data_dir,\"val_data\"), transform=val_transform)","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.487672Z","iopub.execute_input":"2022-05-07T07:21:54.487916Z","iopub.status.idle":"2022-05-07T07:21:54.491313Z","shell.execute_reply.started":"2022-05-07T07:21:54.487884Z","shell.execute_reply":"2022-05-07T07:21:54.490580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_transform = transforms.Compose([transforms.ToTensor(),\n                                    transforms.Resize((224,224)),\n                                     transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])])","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.492597Z","iopub.execute_input":"2022-05-07T07:21:54.493015Z","iopub.status.idle":"2022-05-07T07:21:54.499714Z","shell.execute_reply.started":"2022-05-07T07:21:54.492980Z","shell.execute_reply":"2022-05-07T07:21:54.498970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = [\"cat\",\"dog\"]","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.501052Z","iopub.execute_input":"2022-05-07T07:21:54.501319Z","iopub.status.idle":"2022-05-07T07:21:54.507550Z","shell.execute_reply.started":"2022-05-07T07:21:54.501285Z","shell.execute_reply":"2022-05-07T07:21:54.506833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:54.509945Z","iopub.execute_input":"2022-05-07T07:21:54.510310Z","iopub.status.idle":"2022-05-07T07:21:54.518558Z","shell.execute_reply.started":"2022-05-07T07:21:54.510276Z","shell.execute_reply":"2022-05-07T07:21:54.517628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(230,240):\n    img_path = os.path.join(test_path,test_images[i])\n    img = cv2.imread(img_path)\n    tensor_img = test_transform(img)\n    tensor_img = tensor_img.reshape(-1,3,224,224)\n    tensor_img=tensor_img.to(device)\n    prediction = model(tensor_img)\n    _,pred = torch.max(prediction,axis=1)\n    result = classes[pred]\n    print(result)\n    #print(tensor_img)\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:28:03.801267Z","iopub.execute_input":"2022-05-07T07:28:03.801559Z","iopub.status.idle":"2022-05-07T07:28:06.379238Z","shell.execute_reply.started":"2022-05-07T07:28:03.801526Z","shell.execute_reply":"2022-05-07T07:28:06.378508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Summary\n\n## Resnet18 validation accuracy is very good (greater than 98%)\n## Using ImageFolder is an easy method of loading images\n## ImageFolder required a lot of folder organizing for this project\n## With transfer learning, 3 epochs were enough to reach this accuracy","metadata":{}},{"cell_type":"code","source":"import shutil\nfrom IPython.display import FileLink\n\nOUTPUT_NAME = \"test1\"\nDIRECTORY_TO_ZIP = \"./test1\"\nshutil.make_archive(OUTPUT_NAME, 'zip', DIRECTORY_TO_ZIP)\n\nFileLink(r'./test1.zip')","metadata":{"execution":{"iopub.status.busy":"2022-05-07T07:21:59.320346Z","iopub.execute_input":"2022-05-07T07:21:59.320744Z","iopub.status.idle":"2022-05-07T07:22:08.635265Z","shell.execute_reply.started":"2022-05-07T07:21:59.320708Z","shell.execute_reply":"2022-05-07T07:22:08.634551Z"},"trusted":true},"execution_count":null,"outputs":[]}]}