{"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":"## 1. Load and Normalize `CIFAR10`","metadata":{"id":"c8NwpJcH5guh"}},{"cell_type":"code","source":"import torch \nimport torchvision\nimport torchvision.transforms as transforms","metadata":{"id":"t9BWFqEQsfhm","execution":{"iopub.status.busy":"2021-10-28T18:01:33.773167Z","iopub.execute_input":"2021-10-28T18:01:33.773614Z","iopub.status.idle":"2021-10-28T18:01:33.777859Z","shell.execute_reply.started":"2021-10-28T18:01:33.773585Z","shell.execute_reply":"2021-10-28T18:01:33.777328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n\nbatch_size = 4\n\ntrainset = torchvision.datasets.CIFAR10(root='./data', train=True,\n                                        download=True, transform=transform)\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,\n                                          shuffle=True, num_workers=2)\n\ntestset = torchvision.datasets.CIFAR10(root='./data', train=False,\n                                       download=True, transform=transform)\ntestloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,\n                                         shuffle=False, num_workers=2)\n\nclasses = ('plane', 'car', 'bird', 'cat',\n           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')","metadata":{"id":"xY5cgcyouN3H","outputId":"b224e674-2021-4ca7-cebf-a47e85056bee","execution":{"iopub.status.busy":"2021-10-28T18:01:33.778918Z","iopub.execute_input":"2021-10-28T18:01:33.779088Z","iopub.status.idle":"2021-10-28T18:01:35.301248Z","shell.execute_reply.started":"2021-10-28T18:01:33.779061Z","shell.execute_reply":"2021-10-28T18:01:35.299687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# functions to show an image\n\n\ndef imshow(img):\n    img = img / 2 + 0.5     # unnormalize\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg, (1, 2, 0)))\n    plt.show()\n\n\n# get some random training images\ndataiter = iter(trainloader)\nimages, labels = dataiter.next()\n\n# show images\nimshow(torchvision.utils.make_grid(images))\n# print labels\nprint(' '.join('%5s' % classes[labels[j]] for j in range(batch_size)))","metadata":{"id":"OnMX-ChP6Ks5","outputId":"a42f2911-6d3d-4909-c8f4-6e7f33e17c69","execution":{"iopub.status.busy":"2021-10-28T18:01:35.30272Z","iopub.execute_input":"2021-10-28T18:01:35.302899Z","iopub.status.idle":"2021-10-28T18:01:35.576924Z","shell.execute_reply.started":"2021-10-28T18:01:35.302874Z","shell.execute_reply":"2021-10-28T18:01:35.576047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Define a Convolution Neural Network","metadata":{"id":"iPV4Y6Hm9-Jz"}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\n\nclass Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3, 6, 5)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv2 = nn.Conv2d(6, 16, 5)\n        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = torch.flatten(x, 1) # flatten all dimensions except batch\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n\n\nnet = Net() ","metadata":{"id":"jT8F-sSI90Ug","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Define a loss function and optimizer","metadata":{"id":"Lsxxt3L_tdKj"}},{"cell_type":"code","source":"#using classification cross-entropy loss and SGD with momentum\nimport torch.optim as optim\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)","metadata":{"id":"NklYL9ZW_Mvc","execution":{"iopub.status.busy":"2021-10-28T18:01:35.592604Z","iopub.execute_input":"2021-10-28T18:01:35.592843Z","iopub.status.idle":"2021-10-28T18:01:35.60697Z","shell.execute_reply.started":"2021-10-28T18:01:35.59282Z","shell.execute_reply":"2021-10-28T18:01:35.606086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4.Train the Network ","metadata":{"id":"HYBLn8xjt5fv"}},{"cell_type":"code","source":"#looping over our data iterator and feed the inputs to the network and optimize\n\nfor epoch in range(5):  # loop over the dataset multiple times\n\n    running_loss = 0.0\n    for i, data in enumerate(trainloader, 0):\n        # get the inputs; data is a list of [inputs, labels]\n        inputs, labels = data\n\n        # zero the parameter gradients\n        optimizer.zero_grad()\n\n        # forward + backward + optimize\n        outputs = net(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        # print statistics\n        running_loss += loss.item()\n        if i % 2000 == 1999:    # print every 2000 mini-batches\n            print('[%d, %5d] loss: %.3f' %\n                  (epoch + 1, i + 1, running_loss / 2000))\n            running_loss = 0.0\n\nprint('Finished Training')","metadata":{"id":"Yvui0Dk4tytX","outputId":"281bb6f7-7be5-49df-9b28-63c4bf3be40b","execution":{"iopub.status.busy":"2021-10-28T18:01:35.60793Z","iopub.execute_input":"2021-10-28T18:01:35.608175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Saving our `model` ","metadata":{"id":"vN9wY9sayW7e"}},{"cell_type":"code","source":"PATH = './cifar_net.pth'\ntorch.save(net.state_dict(), PATH)  ","metadata":{"id":"KI0ypbV8wmgX","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. Test the Network on the test data","metadata":{"id":"yZfZMhiBzgzT"}},{"cell_type":"markdown","source":"After training the network for 2 passes over the training dataset \nWe will now predict the class label that the neural network outputs to check \nif the network has learnt anything. \nNow, displaying the images from the test data","metadata":{"id":"UCk85dW9zuEP"}},{"cell_type":"code","source":"dataiter = iter(testloader)\nimages, labels = dataiter.next()\n\n# print images\nimshow(torchvision.utils.make_grid(images))\nprint('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))","metadata":{"id":"73TBpjLwzco3","outputId":"d0df8e2e-6481-48de-d8dc-c9fa07d599a2","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = Net()\nnet.load_state_dict(torch.load(PATH))","metadata":{"id":"xSMa0DBy0th8","outputId":"d25f1acb-61b4-46fd-a752-56819d480867","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"What NN thinks of the above images","metadata":{"id":"lyFDdDkp1jIJ"}},{"cell_type":"code","source":"outputs = net(images)","metadata":{"id":"iGVmw_PZ1Ixu","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, predicted = torch.max(outputs, 1)\n\nprint('Predicted: ', ' '.join('%5s' % classes[predicted[j]]\n                              for j in range(4)))","metadata":{"id":"Udz2P0Px1LSX","outputId":"1e123d60-5a69-4b20-f377-aaa21ce019a6","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Performance of the model on the `whole dataset`","metadata":{"id":"gsR1ikKx11q_"}},{"cell_type":"code","source":"correct = 0\ntotal = 0\n# since we're not training, we don't need to calculate the gradients for our outputs\nwith torch.no_grad():\n    for data in testloader:\n        images, labels = data\n        # calculate outputs by running images through the network\n        outputs = net(images)\n        # the class with the highest energy is what we choose as prediction\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\nprint('Accuracy of the network on the 10000 test images: %d %%' % (\n    100 * correct / total))","metadata":{"id":"kkim_7GO1v4a","outputId":"3bcb6cd7-561f-4785-fefb-df0d77e2869c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Checking the particular class performance ","metadata":{"id":"JgyqN88m2TAE"}},{"cell_type":"code","source":"# prepare to count predictions for each class\ncorrect_pred = {classname: 0 for classname in classes}\ntotal_pred = {classname: 0 for classname in classes}\n\n# again no gradients needed\nwith torch.no_grad():\n    for data in testloader:\n        images, labels = data\n        outputs = net(images)\n        _, predictions = torch.max(outputs, 1)\n        # collect the correct predictions for each class\n        for label, prediction in zip(labels, predictions):\n            if label == prediction:\n                correct_pred[classes[label]] += 1\n            total_pred[classes[label]] += 1\n\n\n# print accuracy for each class\nfor classname, correct_count in correct_pred.items():\n    accuracy = 100 * float(correct_count) / total_pred[classname]\n    print(\"Accuracy for class {:5s} is: {:.1f} %\".format(classname,\n                                                   accuracy))","metadata":{"id":"O1te6ogf1_eg","outputId":"6e3cb68d-72a6-4758-fb32-a580d0e3516d","trusted":true},"execution_count":null,"outputs":[]}]}