{"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":"import os\nimport torch\nimport torch.nn as nn\nfrom tqdm import tqdm\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:24:27.101405Z","iopub.execute_input":"2022-04-02T07:24:27.101818Z","iopub.status.idle":"2022-04-02T07:24:28.673509Z","shell.execute_reply.started":"2022-04-02T07:24:27.101714Z","shell.execute_reply":"2022-04-02T07:24:28.672558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parameters\nbatch_size = 64\nnum_epochs = 10\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:24:33.472982Z","iopub.execute_input":"2022-04-02T07:24:33.473786Z","iopub.status.idle":"2022-04-02T07:24:33.478313Z","shell.execute_reply.started":"2022-04-02T07:24:33.473742Z","shell.execute_reply":"2022-04-02T07:24:33.477398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data preparation\ntransform = transforms.Compose([\n    transforms.ToTensor(), \n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\ntrainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,\n                                          shuffle=True)\n\ntestset = datasets.CIFAR10(root='./data', train=False,\n                                       download=True, transform=transform)\ntestloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,\n                                         shuffle=False)\n\nclasses = ('plane', 'car', 'bird', 'cat',\n           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\nNUM_CLASS = len(classes)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:24:53.081054Z","iopub.execute_input":"2022-04-02T07:24:53.081342Z","iopub.status.idle":"2022-04-02T07:25:09.636007Z","shell.execute_reply.started":"2022-04-02T07:24:53.081312Z","shell.execute_reply":"2022-04-02T07:25:09.635068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# building model\nclass CIFAR_CNN(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","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:25:24.179767Z","iopub.execute_input":"2022-04-02T07:25:24.180088Z","iopub.status.idle":"2022-04-02T07:25:24.190175Z","shell.execute_reply.started":"2022-04-02T07:25:24.180054Z","shell.execute_reply":"2022-04-02T07:25:24.189461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# init model\nmodel = CIFAR_CNN().to(device)\n# optimizer \noptimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)\n# loss\ncriterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:25:34.568319Z","iopub.execute_input":"2022-04-02T07:25:34.568741Z","iopub.status.idle":"2022-04-02T07:25:34.612202Z","shell.execute_reply.started":"2022-04-02T07:25:34.568703Z","shell.execute_reply":"2022-04-02T07:25:34.611411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training function\ndef train():\n    print(\"Training...\")\n    current_loss = float('inf')\n    for epoch in range(num_epochs):\n        for idx,(images, labels) in enumerate(trainloader):\n            images = images.to(device)\n            labels = labels.to(device)\n            # zero the parameter gradients\n            optimizer.zero_grad()\n            # forward + backward + optimize\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n        print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'\n                .format(epoch+1, num_epochs, idx+1, len(trainloader), loss.item()))\n        if loss.item() < current_loss:\n            current_loss = loss.item()\n            if not os.path.isdir(\"model\") :\n                os.mkdir(\"model\")\n            torch.save(model.state_dict(), './model/cifar_cnn.pt')\n            print('Model saved')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:25:42.433140Z","iopub.execute_input":"2022-04-02T07:25:42.433833Z","iopub.status.idle":"2022-04-02T07:25:42.442731Z","shell.execute_reply.started":"2022-04-02T07:25:42.433789Z","shell.execute_reply":"2022-04-02T07:25:42.441772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test():\n    print(\"Testing...\")\n    correct = 0\n    total = 0\n    # load model\n    model.load_state_dict(torch.load('./model/cifar_cnn.pt'))\n    with torch.no_grad():\n        for images, labels in testloader:\n            images = images.to(device)\n            labels = labels.to(device)\n            outputs = model(images)\n            _, predicted = torch.max(outputs.data, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n    print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:25:49.063942Z","iopub.execute_input":"2022-04-02T07:25:49.064819Z","iopub.status.idle":"2022-04-02T07:25:49.071904Z","shell.execute_reply.started":"2022-04-02T07:25:49.064754Z","shell.execute_reply":"2022-04-02T07:25:49.071203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if __name__ == \"__main__\":\n    train()\n    test()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T07:25:55.401227Z","iopub.execute_input":"2022-04-02T07:25:55.402047Z","iopub.status.idle":"2022-04-02T07:29:00.754841Z","shell.execute_reply.started":"2022-04-02T07:25:55.402006Z","shell.execute_reply":"2022-04-02T07:29:00.752657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}