diff --git a/dl/assignment2/NN_in_pytorch_avsh.ipynb b/dl/assignment2/NN_in_pytorch_avsh.ipynb index 72f0c3f..cd4b495 100644 --- a/dl/assignment2/NN_in_pytorch_avsh.ipynb +++ b/dl/assignment2/NN_in_pytorch_avsh.ipynb @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2020-03-12T03:22:05.577442Z", @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 183, + "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2020-03-12T03:22:07.447520Z", @@ -2194,54 +2194,6 @@ "print('Loss, Accuracy of the network on the test data: {}'.format(netNoBN.evaluate(testloader_resized)))" ] }, - { - "cell_type": "code", - "execution_count": 110, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "JqkAqQjqd1Vs" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "Dataset CIFAR10\n", - " Number of datapoints: 50000\n", - " Root location: ./data\n", - " Split: Train\n", - " StandardTransform\n", - "Transform: Compose(\n", - " ToTensor()\n", - " Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))\n", - " )" - ] - }, - "execution_count": 110, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# torchvision.transforms.functional.ten_crop\n", - "transform = transforms.Compose(\n", - " [transforms.ToTensor(),\n", - " transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n", - "\n", - "trainset = torchvision.datasets.CIFAR10(root='./data', train=True,\n", - " download=True, transform=transform)\n", - "trainloader = torch.utils.data.DataLoader(trainset, batch_size=32,\n", - " shuffle=True, num_workers=2)\n", - "\n", - "testset = torchvision.datasets.CIFAR10(root='./data', train=False,\n", - " download=True, transform=transform)\n", - "testloader = torch.utils.data.DataLoader(testset, batch_size=4,\n", - " shuffle=False, num_workers=2)\n", - "\n", - "classes = ('plane', 'car', 'bird', 'cat',\n", - " 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')" - ] - }, { "cell_type": "markdown", "metadata": { @@ -2253,15 +2205,18 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "colab": {}, "colab_type": "code", "id": "Fv2nQBPnd1Vw" }, - "outputs": [], - "source": [] + "source": [ + "We applied random rotation (-15 to 15 degrees), random horizontal flip, and random erasing.\n", + "\n", + "\n", + "Since the data is heavily augmented, we trained for 333 epochs and got a significant improvement: 0.739 accuracy which is 0.0746 improvement on our best result so far." + ] }, { "cell_type": "markdown", @@ -2302,14 +2257,204 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "colab": {}, "colab_type": "code", "id": "34aeYQTxd1V2" }, "outputs": [], - "source": [] + "source": [ + "from torchvision import models\n", + "model = models.vgg16(pretrained=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model.classifier\n", + "for param in model.parameters():\n", + " param.requires_grad = False" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Sequential(\n", + " (0): Linear(in_features=25088, out_features=4096, bias=True)\n", + " (1): ReLU(inplace=True)\n", + " (2): Dropout(p=0.5, inplace=False)\n", + " (3): Linear(in_features=4096, out_features=4096, bias=True)\n", + " (4): ReLU(inplace=True)\n", + " (5): Dropout(p=0.5, inplace=False)\n", + " (6): Sequential(\n", + " (0): Linear(in_features=4096, out_features=256, bias=True)\n", + " (1): ReLU()\n", + " (2): Dropout(p=0.4, inplace=False)\n", + " (3): Linear(in_features=256, out_features=10, bias=True)\n", + " )\n", + ")" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model.classifier[6] = torch.nn.Sequential(\n", + " torch.nn.Linear(4096, 256), \n", + " torch.nn.ReLU(), \n", + " torch.nn.Dropout(0.4),\n", + " torch.nn.Linear(256, 10))\n", + "model.classifier\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 200] loss: 1.441\n", + "[1, 400] loss: 1.351\n", + "[1, 600] loss: 1.362\n", + "[1, 800] loss: 1.706\n", + "[1, 1000] loss: 1.507\n", + "[1, 1200] loss: 1.276\n", + "[1, 1400] loss: 1.309\n", + "[2, 200] loss: 1.317\n", + "[2, 400] loss: 1.287\n", + "[2, 600] loss: 1.240\n", + "[2, 800] loss: 1.295\n", + "[2, 1000] loss: 1.454\n", + "[2, 1200] loss: 1.168\n", + "[2, 1400] loss: 1.118\n", + "[3, 200] loss: 1.017\n", + "[3, 400] loss: 1.409\n", + "[3, 600] loss: 1.149\n", + "[3, 800] loss: 0.860\n", + "[3, 1000] loss: 1.212\n", + "[3, 1200] loss: 1.356\n", + "[3, 1400] loss: 1.086\n" + ] + } + ], + "source": [ + "\n", + "criterion = torch.nn.CrossEntropyLoss()\n", + "optimizer = torch.optim.Adam(model.parameters())\n", + "n_epochs = 3\n", + "\n", + "for epoch in range(n_epochs):\n", + " for i, data in enumerate(trainloader, 0):\n", + " optimizer.zero_grad()\n", + " X, y_true = data\n", + " # Generate predictions\n", + " y_pred = model(X)\n", + " # Calculate loss\n", + " loss = criterion(y_pred, y_true)\n", + " # Backpropagation\n", + " loss.backward()\n", + " # Update model parameters\n", + " optimizer.step()\n", + " if i % 200 == 0 and i != 0:\n", + " print('[%d, %5d] loss: %.3f' % (epoch + 1, i, loss.item()))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.6054\n" + ] + } + ], + "source": [ + "model.eval()\n", + "total = 0\n", + "correct = 0\n", + "with torch.no_grad():\n", + " for data, y_true in testloader:\n", + " log_ps = model(data)\n", + " _, y_pred = torch.max(log_ps, dim=1)\n", + " equals = y_pred == y_true\n", + " total += y_true.size(0)\n", + " correct += equals.sum().item()\n", + " \n", + "print(correct / total)" + ] }, { "cell_type": "markdown", @@ -2325,14 +2470,146 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": { "colab": {}, "colab_type": "code", "id": "JpjqLTVCd1V6" }, "outputs": [], - "source": [] + "source": [ + "model_unfrozen = models.vgg16(pretrained=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "False\n", + "True\n", + "True\n", + "[1, 200] loss: 1.807\n", + "[1, 400] loss: 1.790\n", + "[1, 600] loss: 1.774\n", + "[1, 800] loss: 1.700\n", + "[1, 1000] loss: 1.381\n", + "[1, 1200] loss: 1.712\n", + "[1, 1400] loss: 1.231\n", + "[2, 200] loss: 1.181\n", + "[2, 400] loss: 1.126\n", + "[2, 600] loss: 1.120\n", + "[2, 800] loss: 1.357\n", + "[2, 1000] loss: 1.034\n", + "[2, 1200] loss: 0.986\n", + "[2, 1400] loss: 0.884\n", + "[3, 200] loss: 0.952\n", + "[3, 400] loss: 1.050\n", + "[3, 600] loss: 1.022\n", + "[3, 800] loss: 0.743\n", + "[3, 1000] loss: 1.311\n", + "[3, 1200] loss: 1.388\n", + "[3, 1400] loss: 0.699\n" + ] + } + ], + "source": [ + "\n", + "# for param in model.parameters():\n", + "# print(param)\n", + "i = 0\n", + "for param in model_unfrozen.parameters():\n", + " i += 1\n", + " if (i % 3) == 0:\n", + " param.requires_grad = False\n", + " print(param.requires_grad)\n", + "\n", + "model_unfrozen.classifier[6] = torch.nn.Sequential(\n", + " torch.nn.Linear(4096, 256), \n", + " torch.nn.ReLU(), \n", + " torch.nn.Dropout(0.4),\n", + " torch.nn.Linear(256, 10))\n", + "\n", + "criterion_unfrozen = torch.nn.CrossEntropyLoss()\n", + "optimizer_unfrozen = torch.optim.Adam(model_unfrozen.parameters())\n", + "n_epochs = 3\n", + "\n", + "for epoch in range(n_epochs):\n", + " for i, data in enumerate(trainloader, 0):\n", + " optimizer_unfrozen.zero_grad()\n", + " X, y_true = data\n", + " # Generate predictions\n", + " y_pred = model_unfrozen(X)\n", + " # Calculate loss\n", + " loss = criterion_unfrozen(y_pred, y_true)\n", + " # Backpropagation\n", + " loss.backward()\n", + " # Update model parameters\n", + " optimizer_unfrozen.step()\n", + " if i % 200 == 0 and i != 0:\n", + " print('[%d, %5d] loss: %.3f' % (epoch + 1, i, loss.item()))" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.7181\n" + ] + } + ], + "source": [ + "model_unfrozen.eval()\n", + "total = 0\n", + "correct = 0\n", + "with torch.no_grad():\n", + " for data, y_true in testloader:\n", + " log_ps = model_unfrozen(data)\n", + " _, y_pred = torch.max(log_ps, dim=1)\n", + " equals = y_pred == y_true\n", + " total += y_true.size(0)\n", + " correct += equals.sum().item()\n", + " \n", + "print(correct / total)" + ] }, { "cell_type": "markdown",