{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Transfer_Learning.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"cells":[{"cell_type":"markdown","metadata":{"id":"SM7EIpCLfaT9","colab_type":"text"},"source":["# Transfer Learning\n","- 이미지넷으로 이미 학습된 모델의 앞부분을 사용합니다 (Pretrained ResNet-50)\n","- 또한 해당 모델을 다른 데이터셋에 적용합니다. \n","- 다른 데이터셋에 적용하기 위해 모델의 뒷단을 새롭게 만듭니다. (Add fully connected layer )"]},{"cell_type":"code","metadata":{"id":"gXhMNS8dfgYi","colab_type":"code","outputId":"0696284d-58ed-4342-d533-261390d74103","executionInfo":{"status":"ok","timestamp":1559564043920,"user_tz":-540,"elapsed":7118,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":119}},"source":["!pip install torch torchvision"],"execution_count":1,"outputs":[{"output_type":"stream","text":["Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (1.1.0)\n","Requirement already satisfied: torchvision in /usr/local/lib/python3.6/dist-packages (0.3.0)\n","Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch) (1.16.4)\n","Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision) (4.3.0)\n","Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from torchvision) (1.12.0)\n","Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from pillow>=4.1.1->torchvision) (0.46)\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"QS07cyCVfl2V","colab_type":"code","outputId":"a9f6c0a5-e925-4fb7-d59a-d60555f9255e","executionInfo":{"status":"ok","timestamp":1559564058988,"user_tz":-540,"elapsed":22175,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":782}},"source":["!rm -r images\n","import os \n","\n","try:\n"," os.mkdir(\"images\")\n"," os.mkdir(\"images/dogs\")\n"," os.mkdir(\"images/cats\")\n","except:\n"," pass\n","\n","!wget https://i.kinja-img.com/gawker-media/image/upload/s--WFkXeene--/c_scale,f_auto,fl_progressive,q_80,w_800/ol9ceoqxidudap8owlwn.jpg -P images/dogs\n","!wget https://www.rspcansw.org.au/wp-content/uploads/2017/08/50_a-feature_dogs-and-puppies_mobile.jpg -P images/dogs\n"," \n","!wget https://www.catster.com/wp-content/uploads/2018/05/A-gray-cat-crying-looking-upset.jpg -P images/cats\n","!wget https://www.scarymommy.com/wp-content/uploads/2018/01/c1.jpg?w=700 -P images/cats"],"execution_count":2,"outputs":[{"output_type":"stream","text":["rm: cannot remove 'images': No such file or directory\n","--2019-06-03 12:14:10-- https://i.kinja-img.com/gawker-media/image/upload/s--WFkXeene--/c_scale,f_auto,fl_progressive,q_80,w_800/ol9ceoqxidudap8owlwn.jpg\n","Resolving i.kinja-img.com (i.kinja-img.com)... 151.101.130.166, 151.101.194.166, 151.101.2.166, ...\n","Connecting to i.kinja-img.com (i.kinja-img.com)|151.101.130.166|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 32099 (31K) [image/jpeg]\n","Saving to: ‘images/dogs/ol9ceoqxidudap8owlwn.jpg’\n","\n","ol9ceoqxidudap8owlw 100%[===================>] 31.35K --.-KB/s in 0.001s \n","\n","2019-06-03 12:14:10 (30.1 MB/s) - ‘images/dogs/ol9ceoqxidudap8owlwn.jpg’ saved [32099/32099]\n","\n","--2019-06-03 12:14:12-- https://www.rspcansw.org.au/wp-content/uploads/2017/08/50_a-feature_dogs-and-puppies_mobile.jpg\n","Resolving www.rspcansw.org.au (www.rspcansw.org.au)... 101.0.77.122\n","Connecting to www.rspcansw.org.au (www.rspcansw.org.au)|101.0.77.122|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 130940 (128K) [image/jpeg]\n","Saving to: ‘images/dogs/50_a-feature_dogs-and-puppies_mobile.jpg’\n","\n","50_a-feature_dogs-a 100%[===================>] 127.87K 460KB/s in 0.3s \n","\n","2019-06-03 12:14:12 (460 KB/s) - ‘images/dogs/50_a-feature_dogs-and-puppies_mobile.jpg’ saved [130940/130940]\n","\n","--2019-06-03 12:14:14-- https://www.catster.com/wp-content/uploads/2018/05/A-gray-cat-crying-looking-upset.jpg\n","Resolving www.catster.com (www.catster.com)... 192.124.249.102\n","Connecting to www.catster.com (www.catster.com)|192.124.249.102|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 165145 (161K) [image/jpeg]\n","Saving to: ‘images/cats/A-gray-cat-crying-looking-upset.jpg’\n","\n","A-gray-cat-crying-l 100%[===================>] 161.27K --.-KB/s in 0.006s \n","\n","2019-06-03 12:14:14 (25.5 MB/s) - ‘images/cats/A-gray-cat-crying-looking-upset.jpg’ saved [165145/165145]\n","\n","--2019-06-03 12:14:17-- https://www.scarymommy.com/wp-content/uploads/2018/01/c1.jpg?w=700\n","Resolving www.scarymommy.com (www.scarymommy.com)... 192.0.66.2\n","Connecting to www.scarymommy.com (www.scarymommy.com)|192.0.66.2|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 96891 (95K) [image/jpeg]\n","Saving to: ‘images/cats/c1.jpg?w=700’\n","\n","c1.jpg?w=700 100%[===================>] 94.62K --.-KB/s in 0.003s \n","\n","2019-06-03 12:14:17 (36.2 MB/s) - ‘images/cats/c1.jpg?w=700’ saved [96891/96891]\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"QBSZspaLfaT_","colab_type":"text"},"source":["## 1. Settings\n","### 1) Import required libraries"]},{"cell_type":"code","metadata":{"id":"rJdLPZ3AfaUA","colab_type":"code","colab":{}},"source":["import torch\n","import torch.nn as nn\n","import torch.optim as optim\n","import torchvision.models as models\n","import torchvision.datasets as dset\n","import torchvision.transforms as transforms\n","from torch.utils.data import DataLoader\n","import matplotlib.pyplot as plt"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"yLbzpMQTfaUF","colab_type":"text"},"source":["### 2) Hyperparameters"]},{"cell_type":"code","metadata":{"id":"lT0oUbUGfaUG","colab_type":"code","colab":{}},"source":["batch_size = 2\n","learning_rate = 0.001\n","num_epoch = 10\n","num_category = 2"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"EVhqf7ISfaUK","colab_type":"text"},"source":["## 2. Data\n","### 1) Load images from folder"]},{"cell_type":"code","metadata":{"id":"_4W0gZ3efaUL","colab_type":"code","outputId":"7bb4b85d-8229-4c06-e4f3-70a470ffde2a","executionInfo":{"status":"ok","timestamp":1559564058994,"user_tz":-540,"elapsed":22118,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":122}},"source":["# Input pipeline from a folder containing multiple folders of images\n","# we can check the classes, class_to_idx, and filename with idx\n","\n","img_dir = \"./images\"\n","img_data = dset.ImageFolder(img_dir, transforms.Compose([\n"," transforms.RandomSizedCrop(224),\n"," transforms.RandomHorizontalFlip(),\n"," transforms.ToTensor(),\n"," ]))\n","\n","print(img_data.classes)\n","print(img_data.class_to_idx)\n","print(img_data.imgs)"],"execution_count":5,"outputs":[{"output_type":"stream","text":["['cats', 'dogs']\n","{'cats': 0, 'dogs': 1}\n","[('./images/cats/A-gray-cat-crying-looking-upset.jpg', 0), ('./images/dogs/50_a-feature_dogs-and-puppies_mobile.jpg', 1), ('./images/dogs/ol9ceoqxidudap8owlwn.jpg', 1)]\n"],"name":"stdout"},{"output_type":"stream","text":["/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py:692: UserWarning: The use of the transforms.RandomSizedCrop transform is deprecated, please use transforms.RandomResizedCrop instead.\n"," \"please use transforms.RandomResizedCrop instead.\")\n"],"name":"stderr"}]},{"cell_type":"markdown","metadata":{"id":"5HrW7x2ofaUQ","colab_type":"text"},"source":["### 2) Set data loader"]},{"cell_type":"code","metadata":{"id":"SKIsG9BwfaUR","colab_type":"code","outputId":"893cd3fd-3e69-4d2b-973a-816a3d98ffdb","executionInfo":{"status":"ok","timestamp":1559564058994,"user_tz":-540,"elapsed":22099,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":51}},"source":["# After we get the list of images, we can turn the list into batches of images\n","# with torch.utils.data.DataLoader()\n","\n","train_loader = DataLoader(img_data, batch_size=batch_size,\n"," shuffle=True, num_workers=2,drop_last=True)\n","\n","for img,label in train_loader:\n"," print(img.size())\n"," print(label)"],"execution_count":6,"outputs":[{"output_type":"stream","text":["torch.Size([2, 3, 224, 224])\n","tensor([1, 0])\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"SJQOm3mzfaUV","colab_type":"text"},"source":["## 3. Model & Optimizer\n","### 1) ResNet"]},{"cell_type":"code","metadata":{"id":"7Z6CInjifaUW","colab_type":"code","outputId":"60d1bd76-f8f8-4d76-dafb-015dc2bc617d","executionInfo":{"status":"ok","timestamp":1559564059377,"user_tz":-540,"elapsed":22461,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":187}},"source":["# https://discuss.pytorch.org/t/module-children-vs-module-modules/4551\n","# children() -> immediate children modules \n","# modules() -> iterate all modules\n","\n","resnet = models.resnet50(pretrained=True)\n","\n","for name,module in resnet.named_children():\n"," print(name)"],"execution_count":7,"outputs":[{"output_type":"stream","text":["conv1\n","bn1\n","relu\n","maxpool\n","layer1\n","layer2\n","layer3\n","layer4\n","avgpool\n","fc\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"vhkMFE1BfaUa","colab_type":"text"},"source":["### 2) Fully Connected Model"]},{"cell_type":"code","metadata":{"id":"H5RokTb5faUb","colab_type":"code","colab":{}},"source":["# 커스텀 레즈넷을 새로 정의하되 layer0는 이미 학습된 모델의 파라미터를 가져오고\n","# layer1는 새롭게 만들어서 이 부분을 학습합니다.\n","\n","class Resnet(nn.Module):\n"," def __init__(self):\n"," super(Resnet,self).__init__()\n"," self.layer0 = nn.Sequential(*list(resnet.children())[0:-1])\n"," self.layer1 = nn.Sequential(\n"," nn.Linear(2048,500),\n"," nn.BatchNorm1d(500),\n"," nn.ReLU(),\n"," nn.Linear(500,num_category),\n"," nn.ReLU()\n"," )\n"," \n"," def forward(self,x):\n"," out = self.layer0(x)\n"," out = out.view(batch_size,-1)\n"," out= self.layer1(out)\n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"iA4Lj0BYfaUd","colab_type":"text"},"source":["### 3) Model on GPU"]},{"cell_type":"code","metadata":{"id":"ibIiT0UcfaUe","colab_type":"code","outputId":"53a9d8eb-17c4-4c12-9247-ac29dce40c36","executionInfo":{"status":"ok","timestamp":1559564065116,"user_tz":-540,"elapsed":28169,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n","print(device)\n","\n","model = Resnet().to(device)\n","\n","# 모델의 layer0의 파라미터들은 학습이 되지 않도록 기울기 계산을 꺼둡니다.\n","for params in model.layer0.parameters():\n"," params.require_grad = False\n"," \n","# layer1의 파라미터들은 학습되도록 기울기 계산을 켜둡니다.\n","for params in model.layer1.parameters():\n"," params.requires_grad = True"],"execution_count":9,"outputs":[{"output_type":"stream","text":["cuda:0\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"mxfbhezlfaUg","colab_type":"code","outputId":"471aca02-ebcc-4bbb-8f6e-854d821f72e2","executionInfo":{"status":"ok","timestamp":1559564065119,"user_tz":-540,"elapsed":28156,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":3111}},"source":["# 모델을 한번 확인합니다\n","for m in model.children():\n"," print(m)"],"execution_count":10,"outputs":[{"output_type":"stream","text":["Sequential(\n"," (0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n"," (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (2): ReLU(inplace)\n"," (3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n"," (4): Sequential(\n"," (0): Bottleneck(\n"," (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," (downsample): Sequential(\n"," (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," )\n"," )\n"," (1): Bottleneck(\n"," (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (2): Bottleneck(\n"," (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," )\n"," (5): Sequential(\n"," (0): Bottleneck(\n"," (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," (downsample): Sequential(\n"," (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)\n"," (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," )\n"," )\n"," (1): Bottleneck(\n"," (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (2): Bottleneck(\n"," (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (3): Bottleneck(\n"," (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," )\n"," (6): Sequential(\n"," (0): Bottleneck(\n"," (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," (downsample): Sequential(\n"," (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)\n"," (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," )\n"," )\n"," (1): Bottleneck(\n"," (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (2): Bottleneck(\n"," (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (3): Bottleneck(\n"," (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (4): Bottleneck(\n"," (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (5): Bottleneck(\n"," (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," )\n"," (7): Sequential(\n"," (0): Bottleneck(\n"," (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," (downsample): Sequential(\n"," (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)\n"," (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," )\n"," )\n"," (1): Bottleneck(\n"," (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," (2): Bottleneck(\n"," (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n"," (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n"," (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (relu): ReLU(inplace)\n"," )\n"," )\n"," (8): AdaptiveAvgPool2d(output_size=(1, 1))\n",")\n","Sequential(\n"," (0): Linear(in_features=2048, out_features=500, bias=True)\n"," (1): BatchNorm1d(500, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n"," (2): ReLU()\n"," (3): Linear(in_features=500, out_features=2, bias=True)\n"," (4): ReLU()\n",")\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"bZAUISCEfaUk","colab_type":"text"},"source":["### 4) Loss & Optimizer"]},{"cell_type":"code","metadata":{"id":"39ADaIO2faUl","colab_type":"code","colab":{}},"source":["# define loss func & optimizer\n","# model.parameters() also works because of the cell right above\n","\n","loss_func = nn.CrossEntropyLoss()\n","optimizer = optim.Adam(model.layer1.parameters(),lr=learning_rate) "],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"JkdNNSfAfaUn","colab_type":"text"},"source":["## 4. Train"]},{"cell_type":"code","metadata":{"id":"R1xFggNhfaUo","colab_type":"code","outputId":"500d56cb-7abf-41fc-dd61-a76ff759581e","executionInfo":{"status":"ok","timestamp":1559564066913,"user_tz":-540,"elapsed":29913,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["for i in range(num_epoch):\n"," for j,[image,label] in enumerate(train_loader):\n"," x = image.to(device)\n"," y_= label.to(device)\n"," \n"," optimizer.zero_grad()\n"," output = model.forward(x)\n"," loss = loss_func(output,y_)\n"," loss.backward()\n"," optimizer.step()\n","\n"," if i % 10 ==0:\n"," print(loss)"],"execution_count":12,"outputs":[{"output_type":"stream","text":["tensor(0.6931, device='cuda:0', grad_fn=)\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"KFcTecW7faUs","colab_type":"text"},"source":["## 6. Test"]},{"cell_type":"code","metadata":{"id":"29C1xbBdfaUu","colab_type":"code","outputId":"92c9379e-8208-4f96-fa71-4e0cac671176","executionInfo":{"status":"ok","timestamp":1559564066915,"user_tz":-540,"elapsed":29900,"user":{"displayName":"Choi Gunho","photoUrl":"","userId":"04388737836176863066"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["model.eval()\n","correct = 0\n","total = 0\n","\n","with torch.no_grad():\n"," for image,label in train_loader:\n"," x = image.to(device)\n"," y_= label.to(device)\n"," \n"," output = model.forward(x)\n"," _,output_index = torch.max(output,1)\n"," \n"," total += label.size(0)\n"," correct += (output_index == y_).sum().float()\n","\n"," print(\"Accuracy of Train Data: {}\".format(100*correct/total))"],"execution_count":13,"outputs":[{"output_type":"stream","text":["Accuracy of Train Data: 100.0\n"],"name":"stdout"}]}]}