{"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":"markdown","source":"## Import Libaries","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import os\nimport torch\nimport torchvision\nimport tarfile\nfrom torchvision.datasets.utils import download_url\nfrom torch.utils.data import random_split\nfrom torchvision.datasets import ImageFolder\nfrom torchvision.transforms import ToTensor, ToPILImage\nimport matplotlib.pyplot as plt\nfrom torchvision.utils import make_grid\nfrom torch.utils.data.dataloader import DataLoader\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:39.492586Z","iopub.execute_input":"2022-02-19T12:27:39.493198Z","iopub.status.idle":"2022-02-19T12:27:41.278782Z","shell.execute_reply.started":"2022-02-19T12:27:39.493095Z","shell.execute_reply":"2022-02-19T12:27:41.278060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring the Data","metadata":{}},{"cell_type":"code","source":"with tarfile.open('../input/cifar10/cifar10.tgz', 'r:gz') as tar:\n    tar.extractall(path = './input')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:41.280838Z","iopub.execute_input":"2022-02-19T12:27:41.281109Z","iopub.status.idle":"2022-02-19T12:27:55.546198Z","shell.execute_reply.started":"2022-02-19T12:27:41.281074Z","shell.execute_reply":"2022-02-19T12:27:55.545465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('./input/cifar10'))\nclasses = os.listdir('./input/cifar10/train')\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.547360Z","iopub.execute_input":"2022-02-19T12:27:55.547633Z","iopub.status.idle":"2022-02-19T12:27:55.554867Z","shell.execute_reply.started":"2022-02-19T12:27:55.547598Z","shell.execute_reply":"2022-02-19T12:27:55.553948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_files = os.listdir('./input/cifar10/train/cat')\nprint('No of training example for cats : ', len(cat_files))\nprint(cat_files[:5])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.556265Z","iopub.execute_input":"2022-02-19T12:27:55.556984Z","iopub.status.idle":"2022-02-19T12:27:55.566176Z","shell.execute_reply.started":"2022-02-19T12:27:55.556950Z","shell.execute_reply":"2022-02-19T12:27:55.565338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automobile_files = os.listdir('./input/cifar10/train/automobile')\nprint('No of training example for automobiles : ', len(automobile_files))\nprint(automobile_files[:5])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.568956Z","iopub.execute_input":"2022-02-19T12:27:55.569363Z","iopub.status.idle":"2022-02-19T12:27:55.578407Z","shell.execute_reply.started":"2022-02-19T12:27:55.569325Z","shell.execute_reply":"2022-02-19T12:27:55.577476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_files = os.listdir('./input/cifar10/test/cat')\nprint('No of test example for cats : ', len(cat_files))\nprint(cat_files[:5])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.579938Z","iopub.execute_input":"2022-02-19T12:27:55.580199Z","iopub.status.idle":"2022-02-19T12:27:55.586505Z","shell.execute_reply.started":"2022-02-19T12:27:55.580166Z","shell.execute_reply":"2022-02-19T12:27:55.585721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = './input/cifar10'\ndataset = ImageFolder(data_dir + '/train', transform = ToTensor())","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.587775Z","iopub.execute_input":"2022-02-19T12:27:55.588437Z","iopub.status.idle":"2022-02-19T12:27:55.858162Z","shell.execute_reply.started":"2022-02-19T12:27:55.588402Z","shell.execute_reply":"2022-02-19T12:27:55.857444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = dataset[0]\nprint(img.shape, label)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.859796Z","iopub.execute_input":"2022-02-19T12:27:55.860211Z","iopub.status.idle":"2022-02-19T12:27:55.938892Z","shell.execute_reply.started":"2022-02-19T12:27:55.860176Z","shell.execute_reply":"2022-02-19T12:27:55.938212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.940200Z","iopub.execute_input":"2022-02-19T12:27:55.940938Z","iopub.status.idle":"2022-02-19T12:27:55.947009Z","shell.execute_reply.started":"2022-02-19T12:27:55.940898Z","shell.execute_reply":"2022-02-19T12:27:55.946303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dataset.classes)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.948274Z","iopub.execute_input":"2022-02-19T12:27:55.949677Z","iopub.status.idle":"2022-02-19T12:27:55.957222Z","shell.execute_reply.started":"2022-02-19T12:27:55.949641Z","shell.execute_reply":"2022-02-19T12:27:55.956464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(img, label):\n    print('Label : ', dataset.classes[label], \"(\"+str(label)+\")\")\n    plt.imshow(img.permute(1, 2, 0))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.958558Z","iopub.execute_input":"2022-02-19T12:27:55.958994Z","iopub.status.idle":"2022-02-19T12:27:55.964393Z","shell.execute_reply.started":"2022-02-19T12:27:55.958958Z","shell.execute_reply":"2022-02-19T12:27:55.963375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image(dataset[222][0], dataset[222][1])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:55.965752Z","iopub.execute_input":"2022-02-19T12:27:55.965995Z","iopub.status.idle":"2022-02-19T12:27:56.167566Z","shell.execute_reply.started":"2022-02-19T12:27:55.965965Z","shell.execute_reply":"2022-02-19T12:27:56.166881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image(*dataset[1099])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.168817Z","iopub.execute_input":"2022-02-19T12:27:56.169216Z","iopub.status.idle":"2022-02-19T12:27:56.360675Z","shell.execute_reply.started":"2022-02-19T12:27:56.169182Z","shell.execute_reply":"2022-02-19T12:27:56.359898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training and Validation Datasets","metadata":{}},{"cell_type":"code","source":"random_seed = 42\ntorch.manual_seed(random_seed)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.364204Z","iopub.execute_input":"2022-02-19T12:27:56.364815Z","iopub.status.idle":"2022-02-19T12:27:56.375990Z","shell.execute_reply.started":"2022-02-19T12:27:56.364781Z","shell.execute_reply":"2022-02-19T12:27:56.375003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_size = 5000\ntrain_size = len(dataset) - val_size\ntrain_ds, val_ds = random_split(dataset, [train_size, val_size], generator = torch.manual_seed(random_seed))\nlen(train_ds), len(val_ds)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.378782Z","iopub.execute_input":"2022-02-19T12:27:56.378990Z","iopub.status.idle":"2022-02-19T12:27:56.394186Z","shell.execute_reply.started":"2022-02-19T12:27:56.378965Z","shell.execute_reply":"2022-02-19T12:27:56.393518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\ntrain_dl = DataLoader(train_ds, batch_size, shuffle = True, num_workers = 4, pin_memory = True)\nval_dl = DataLoader(val_ds, batch_size, shuffle = True, num_workers = 4, pin_memory = True)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.395394Z","iopub.execute_input":"2022-02-19T12:27:56.395799Z","iopub.status.idle":"2022-02-19T12:27:56.404082Z","shell.execute_reply.started":"2022-02-19T12:27:56.395763Z","shell.execute_reply":"2022-02-19T12:27:56.402520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.405564Z","iopub.execute_input":"2022-02-19T12:27:56.406094Z","iopub.status.idle":"2022-02-19T12:27:56.411983Z","shell.execute_reply.started":"2022-02-19T12:27:56.406047Z","shell.execute_reply":"2022-02-19T12:27:56.411214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images_batch(d1):\n    for images, labels in d1:\n        fig, ax = plt.subplots(figsize = (16, 8))\n        ax.set_xticks([])\n        ax.set_yticks([])\n        ax.imshow(make_grid(images, nrow = 16).permute(1, 2, 0))\n        break","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.413583Z","iopub.execute_input":"2022-02-19T12:27:56.414206Z","iopub.status.idle":"2022-02-19T12:27:56.420892Z","shell.execute_reply.started":"2022-02-19T12:27:56.414025Z","shell.execute_reply":"2022-02-19T12:27:56.420098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images_batch(train_dl)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:27:56.423261Z","iopub.execute_input":"2022-02-19T12:27:56.424738Z","iopub.status.idle":"2022-02-19T12:28:00.281319Z","shell.execute_reply.started":"2022-02-19T12:27:56.424701Z","shell.execute_reply":"2022-02-19T12:28:00.278206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images_batch(val_dl)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:00.282471Z","iopub.execute_input":"2022-02-19T12:28:00.283153Z","iopub.status.idle":"2022-02-19T12:28:01.133618Z","shell.execute_reply.started":"2022-02-19T12:28:00.283101Z","shell.execute_reply":"2022-02-19T12:28:01.132927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convolutional Neural Network","metadata":{}},{"cell_type":"code","source":"def apply_kernel(image, kernel):\n    ri, ci = image.shape\n    rk, ck = kernel.shape\n    ro, co = ri-rk+1, ci-ck+1\n    output = torch.zeros([ro, co])\n    for i in range(ro):\n        for j in range(co):\n            output[i, j] = torch.sum(image[i:i+rk, j:j+ck] * kernel)\n    return output","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:01.135154Z","iopub.execute_input":"2022-02-19T12:28:01.135597Z","iopub.status.idle":"2022-02-19T12:28:01.142524Z","shell.execute_reply.started":"2022-02-19T12:28:01.135558Z","shell.execute_reply":"2022-02-19T12:28:01.141505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image = torch.tensor([\n    [4, 4, 3, 1, 0],\n    [0, 0, 1, 4, 1],\n    [4, 1, 3, 3, 4],\n    [2, 0, 0, 3, 3],\n    [3, 0, 0, 0, 1]\n], dtype = torch.float32)\n\nsample_kernel = torch.tensor([\n    [1, 1, 1],\n    [1, 1, 1],\n    [1, 1, 1]\n], dtype = torch.float32)\n\napply_kernel(sample_image, sample_kernel)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:01.144385Z","iopub.execute_input":"2022-02-19T12:28:01.144950Z","iopub.status.idle":"2022-02-19T12:28:01.160032Z","shell.execute_reply.started":"2022-02-19T12:28:01.144900Z","shell.execute_reply":"2022-02-19T12:28:01.159090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def accuracy(outputs, labels):\n    _, preds = torch.max(outputs, dim = 1)\n    return torch.tensor(torch.sum(preds == labels).item() / len(preds))\n\nclass ImageClassificationBase(nn.Module):\n    def training_step(self, batch):\n        images, labels = batch\n        out = self(images)\n        loss = F.cross_entropy(out, labels)\n        return loss\n    \n    def validation_step(self, batch):\n        images, labels = batch\n        out = self(images)\n        loss = F.cross_entropy(out, labels)\n        acc = accuracy(out, labels)\n        return {'val_loss' : loss.detach(), 'val_acc' : acc}\n    \n    def validation_epoch_end(self, outputs):\n        batch_losses = [x['val_loss'] for x in outputs]\n        epoch_loss = torch.stack(batch_losses).mean()\n        batch_accs = [x['val_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()\n        return {'val_loss' : epoch_loss.item(), 'val_acc' : epoch_acc.item()}\n    \n    def epoch_end(self, epoch, result):\n        print('Epoch [{}], train_loss {:.4f}, val_loss : {:.4f}, val_acc : {:.4f}'.format(\n            epoch, result['train_loss'], result['val_loss'], result['val_acc']))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:01.161381Z","iopub.execute_input":"2022-02-19T12:28:01.161839Z","iopub.status.idle":"2022-02-19T12:28:01.174108Z","shell.execute_reply.started":"2022-02-19T12:28:01.161795Z","shell.execute_reply":"2022-02-19T12:28:01.173179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Cifar10CnnModel(ImageClassificationBase):\n    def __init__(self):\n        super().__init__()\n        self.network = nn.Sequential(\n            nn.Conv2d(3, 32, kernel_size = 3, padding = 1),\n            nn.ReLU(),\n            nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding = 1),\n            nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n        \n            nn.Conv2d(64, 128, kernel_size = 3, stride = 1, padding = 1),\n            nn.ReLU(),\n            nn.Conv2d(128, 128, kernel_size = 3, stride = 1, padding = 1),\n            nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n        \n            nn.Conv2d(128, 256, kernel_size = 3, stride = 1, padding = 1),\n            nn.ReLU(),\n            nn.Conv2d(256, 256, kernel_size = 3, stride = 1, padding = 1),\n            nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n        \n            nn.Flatten(),\n            nn.Linear(256*4*4, 1024),\n            nn.ReLU(),\n            nn.Linear(1024, 512),\n            nn.ReLU(),\n            nn.Linear(512, 10))\n        \n    def forward(self, xb):\n        return self.network(xb)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:01.175636Z","iopub.execute_input":"2022-02-19T12:28:01.176139Z","iopub.status.idle":"2022-02-19T12:28:01.188199Z","shell.execute_reply.started":"2022-02-19T12:28:01.176106Z","shell.execute_reply":"2022-02-19T12:28:01.187348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Cifar10CnnModel()\nmodel","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:01.190948Z","iopub.execute_input":"2022-02-19T12:28:01.191175Z","iopub.status.idle":"2022-02-19T12:28:01.247953Z","shell.execute_reply.started":"2022-02-19T12:28:01.191147Z","shell.execute_reply":"2022-02-19T12:28:01.247316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images, labels in train_dl:\n    print('images.shape : ', images.shape)\n    out = model(images)\n    print('out.shape : ', out.shape)\n    print('out[0]', out[0])\n    break","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:01.248992Z","iopub.execute_input":"2022-02-19T12:28:01.249240Z","iopub.status.idle":"2022-02-19T12:28:02.309856Z","shell.execute_reply.started":"2022-02-19T12:28:01.249206Z","shell.execute_reply":"2022-02-19T12:28:02.309089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:02.311284Z","iopub.execute_input":"2022-02-19T12:28:02.311835Z","iopub.status.idle":"2022-02-19T12:28:02.318349Z","shell.execute_reply.started":"2022-02-19T12:28:02.311799Z","shell.execute_reply":"2022-02-19T12:28:02.317570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_default_device():\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    if isinstance(data, (list, tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking = True)\n\nclass DeviceDataLoader():\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        for b in self.dl:\n            yield to_device(b, self.device)\n            \n    def __len__(self):\n        return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:02.319579Z","iopub.execute_input":"2022-02-19T12:28:02.320233Z","iopub.status.idle":"2022-02-19T12:28:02.330574Z","shell.execute_reply.started":"2022-02-19T12:28:02.320197Z","shell.execute_reply":"2022-02-19T12:28:02.329863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_default_device()\ndevice","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:02.331761Z","iopub.execute_input":"2022-02-19T12:28:02.332101Z","iopub.status.idle":"2022-02-19T12:28:02.341444Z","shell.execute_reply.started":"2022-02-19T12:28:02.332046Z","shell.execute_reply":"2022-02-19T12:28:02.340713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = DeviceDataLoader(train_dl, device)\nval_dl = DeviceDataLoader(val_dl, device)\nto_device(model, device)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:28:37.557759Z","iopub.execute_input":"2022-02-19T12:28:37.558020Z","iopub.status.idle":"2022-02-19T12:28:37.579379Z","shell.execute_reply.started":"2022-02-19T12:28:37.557989Z","shell.execute_reply":"2022-02-19T12:28:37.578706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training the Model","metadata":{}},{"cell_type":"code","source":"@torch.no_grad()\n\ndef evaluate(model, val_loader):\n    model.eval()\n    outputs = [model.validation_step(batch) for batch in val_loader]\n    return model.validation_epoch_end(outputs)\n\ndef fit(epochs, lr, model, train_loader, val_loader, opt_func = torch.optim.SGD):\n    history = []\n    optimizer = opt_func(model.parameters(), lr)\n    \n    for epoch in range(epochs):\n        model.train()\n        train_losses = []\n        for batch in train_loader:\n            loss = model.training_step(batch)\n            train_losses.append(loss)\n            loss.backward()\n            optimizer.step()\n            optimizer.zero_grad()\n        \n        result = evaluate(model, val_loader)\n        result['train_loss'] = torch.stack(train_losses).mean().item()\n        model.epoch_end(epoch, result)\n        history.append(result)\n    return history","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:32:38.249779Z","iopub.execute_input":"2022-02-19T12:32:38.250331Z","iopub.status.idle":"2022-02-19T12:32:38.258828Z","shell.execute_reply.started":"2022-02-19T12:32:38.250291Z","shell.execute_reply":"2022-02-19T12:32:38.257900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:32:40.626569Z","iopub.execute_input":"2022-02-19T12:32:40.626828Z","iopub.status.idle":"2022-02-19T12:32:40.632300Z","shell.execute_reply.started":"2022-02-19T12:32:40.626798Z","shell.execute_reply":"2022-02-19T12:32:40.631561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluate(model, val_dl)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:32:50.460521Z","iopub.execute_input":"2022-02-19T12:32:50.460784Z","iopub.status.idle":"2022-02-19T12:32:57.426562Z","shell.execute_reply.started":"2022-02-19T12:32:50.460756Z","shell.execute_reply":"2022-02-19T12:32:57.425846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 15\nopt_func = torch.optim.Adam\nlr = 0.001","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:33:51.932232Z","iopub.execute_input":"2022-02-19T12:33:51.932973Z","iopub.status.idle":"2022-02-19T12:33:51.937579Z","shell.execute_reply.started":"2022-02-19T12:33:51.932930Z","shell.execute_reply":"2022-02-19T12:33:51.936850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(num_epochs, lr, model, train_dl, val_dl, opt_func)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:34:15.227666Z","iopub.execute_input":"2022-02-19T12:34:15.228381Z","iopub.status.idle":"2022-02-19T12:37:53.622996Z","shell.execute_reply.started":"2022-02-19T12:34:15.228341Z","shell.execute_reply":"2022-02-19T12:37:53.622159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_accuracies(history):\n    accuracies = [x['val_acc'] for x in history]\n    plt.plot(accuracies, '-x')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.title('Accuracy cs No of epochs')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:37:53.628024Z","iopub.execute_input":"2022-02-19T12:37:53.628624Z","iopub.status.idle":"2022-02-19T12:37:53.635509Z","shell.execute_reply.started":"2022-02-19T12:37:53.628592Z","shell.execute_reply":"2022-02-19T12:37:53.634798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 6))\nplot_accuracies(history)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:38:09.092666Z","iopub.execute_input":"2022-02-19T12:38:09.092994Z","iopub.status.idle":"2022-02-19T12:38:09.342570Z","shell.execute_reply.started":"2022-02-19T12:38:09.092956Z","shell.execute_reply":"2022-02-19T12:38:09.341888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_losses(history):\n    train_losses = [x.get('train_loss') for x in history]\n    val_losses = [x['val_loss'] for x in history]\n    plt.plot(train_losses, '-bx')\n    plt.plot(val_losses, '-rx')\n    plt.xlabel('epoch')\n    plt.ylabel('loss')\n    plt.legend(['Training', 'Validation'])\n    plt.title('Loss Vs. No. of epochs')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:40:09.818900Z","iopub.execute_input":"2022-02-19T12:40:09.819271Z","iopub.status.idle":"2022-02-19T12:40:09.827647Z","shell.execute_reply.started":"2022-02-19T12:40:09.819229Z","shell.execute_reply":"2022-02-19T12:40:09.826705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 6))\nplot_losses(history)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:40:10.080932Z","iopub.execute_input":"2022-02-19T12:40:10.081252Z","iopub.status.idle":"2022-02-19T12:40:10.356509Z","shell.execute_reply.started":"2022-02-19T12:40:10.081216Z","shell.execute_reply":"2022-02-19T12:40:10.355863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing with individual images","metadata":{}},{"cell_type":"code","source":"test_dataset = ImageFolder(data_dir+'/test', transform = ToTensor())\ntest_dataset","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:40:45.787181Z","iopub.execute_input":"2022-02-19T12:40:45.787952Z","iopub.status.idle":"2022-02-19T12:40:45.849693Z","shell.execute_reply.started":"2022-02-19T12:40:45.787903Z","shell.execute_reply":"2022-02-19T12:40:45.848976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_image(img, model):\n    xb = to_device(img.unsqueeze(0), device)\n    yb = model(xb)\n    _, preds = torch.max(yb, dim = 1)\n    return dataset.classes[preds[0].item()]","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:41:27.596021Z","iopub.execute_input":"2022-02-19T12:41:27.596788Z","iopub.status.idle":"2022-02-19T12:41:27.602802Z","shell.execute_reply.started":"2022-02-19T12:41:27.596742Z","shell.execute_reply":"2022-02-19T12:41:27.602099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[8]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:42:06.999795Z","iopub.execute_input":"2022-02-19T12:42:07.000329Z","iopub.status.idle":"2022-02-19T12:42:07.196702Z","shell.execute_reply.started":"2022-02-19T12:42:07.000289Z","shell.execute_reply":"2022-02-19T12:42:07.195884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[1220]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:42:47.852391Z","iopub.execute_input":"2022-02-19T12:42:47.852933Z","iopub.status.idle":"2022-02-19T12:42:48.040512Z","shell.execute_reply.started":"2022-02-19T12:42:47.852895Z","shell.execute_reply":"2022-02-19T12:42:48.039294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[345]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:43:24.334829Z","iopub.execute_input":"2022-02-19T12:43:24.335565Z","iopub.status.idle":"2022-02-19T12:43:24.526660Z","shell.execute_reply.started":"2022-02-19T12:43:24.335512Z","shell.execute_reply":"2022-02-19T12:43:24.525980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[6153]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:44:14.332611Z","iopub.execute_input":"2022-02-19T12:44:14.332868Z","iopub.status.idle":"2022-02-19T12:44:14.521553Z","shell.execute_reply.started":"2022-02-19T12:44:14.332839Z","shell.execute_reply":"2022-02-19T12:44:14.520883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[456]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:44:26.000037Z","iopub.execute_input":"2022-02-19T12:44:26.001025Z","iopub.status.idle":"2022-02-19T12:44:26.192577Z","shell.execute_reply.started":"2022-02-19T12:44:26.000975Z","shell.execute_reply":"2022-02-19T12:44:26.191755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[10]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:44:30.565725Z","iopub.execute_input":"2022-02-19T12:44:30.566417Z","iopub.status.idle":"2022-02-19T12:44:30.749983Z","shell.execute_reply.started":"2022-02-19T12:44:30.566379Z","shell.execute_reply":"2022-02-19T12:44:30.749329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = test_dataset[1432]\nplt.imshow(img.permute(1, 2, 0))\nprint('Label : ', dataset.classes[label], ', Predicted : ', predict_image(img, model))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:44:37.911366Z","iopub.execute_input":"2022-02-19T12:44:37.911674Z","iopub.status.idle":"2022-02-19T12:44:38.336344Z","shell.execute_reply.started":"2022-02-19T12:44:37.911641Z","shell.execute_reply":"2022-02-19T12:44:38.335616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DeviceDataLoader(DataLoader(test_dataset, batch_size), device)\ntest_result = evaluate(model, test_loader)\ntest_result","metadata":{"execution":{"iopub.status.busy":"2022-02-19T12:45:12.253433Z","iopub.execute_input":"2022-02-19T12:45:12.254157Z","iopub.status.idle":"2022-02-19T12:45:15.768658Z","shell.execute_reply.started":"2022-02-19T12:45:12.254119Z","shell.execute_reply":"2022-02-19T12:45:15.767968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}