{"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":"# 이름 : 김도윤\n# 학번 : 20185214\n","metadata":{"execution":{"iopub.execute_input":"2022-05-02T04:27:09.729446Z","iopub.status.busy":"2022-05-02T04:27:09.729037Z","iopub.status.idle":"2022-05-02T04:27:09.736951Z","shell.execute_reply":"2022-05-02T04:27:09.7359Z","shell.execute_reply.started":"2022-05-02T04:27:09.729396Z"}}},{"cell_type":"markdown","source":"## Step 0: Import library\n\n- 필요하다 생각되는 라이브러리를 미리 import합니다. (참고)\n- 필요 없는 라이브러리를 제거하거나 필요한 라이브러리를 추가하셔도 됩니다.","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tqdm.auto import tqdm, trange\nimport random\nimport pandas as pd\nfrom PIL import Image\nfrom pathlib import Path\nfrom collections import OrderedDict\nfrom time import time, ctime, localtime\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.utils.data import Dataset,DataLoader\n\nimport torchvision\nimport torchvision.transforms as transforms\n\nfrom torchvision import datasets\nfrom torchvision import models\n\n\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import train_test_split,train_test_split\nfrom sklearn.model_selection import StratifiedKFold,  KFold\n","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:48.673283Z","iopub.execute_input":"2022-12-19T05:08:48.673615Z","iopub.status.idle":"2022-12-19T05:08:51.539097Z","shell.execute_reply.started":"2022-12-19T05:08:48.673575Z","shell.execute_reply":"2022-12-19T05:08:51.53836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-----------------------------------","metadata":{}},{"cell_type":"code","source":"# \ntrain_transforms = transforms.Compose([\n                                        transforms.RandomHorizontalFlip(),\n                                        transforms.ToTensor(),\n                                        transforms.Normalize([0.5200497, 0.4652539, 0.39320767],\n                                                            [0.20736203, 0.19569875, 0.1818195])])\n\ntrain_data = datasets.ImageFolder('../input/2022-deep-learning-basics-challenge/painting_resize_256'+'/train')\n\nvalid_size = 0.17\nnum_train = len(train_data)\nnum_train = int(num_train)\nindices = list(range(num_train))\nnp.random.shuffle(indices)\nsplit = int(np.floor(valid_size * num_train))\ntrain_idx, valid_idx = indices[split:], indices[:split]\n\n\n# define samplers for obtaining training and validation batches\ntrain_sampler = SubsetRandomSampler(train_idx)\nvalid_sampler = SubsetRandomSampler(valid_idx)\n\ntrainloader = torch.utils.data.DataLoader(train_data, batch_size=64,\n    sampler=train_sampler)\nvalidloader = torch.utils.data.DataLoader(train_data, batch_size=64, \n    sampler=valid_sampler)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:51.544086Z","iopub.execute_input":"2022-12-19T05:08:51.544691Z","iopub.status.idle":"2022-12-19T05:08:51.994039Z","shell.execute_reply.started":"2022-12-19T05:08:51.544637Z","shell.execute_reply":"2022-12-19T05:08:51.993267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sampler1 = SubsetRandomSampler(list(range(num_train)))\ntrainloader1 = torch.utils.data.DataLoader(train_data, batch_size=64,\n    sampler=train_sampler1)\ntrain_sampler2 = SubsetRandomSampler(list(range(num_train)))\ntrainloader2 = torch.utils.data.DataLoader(train_data, batch_size=64,\n    sampler=train_sampler1)\ntrain_sampler3 = SubsetRandomSampler(list(range(num_train)))\ntrainloader3 = torch.utils.data.DataLoader(train_data, batch_size=64,\n    sampler=train_sampler1)\ntrain_sampler4 = SubsetRandomSampler(list(range(num_train)))\ntrainloader4 = torch.utils.data.DataLoader(train_data, batch_size=64,\n    sampler=train_sampler1)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:51.995463Z","iopub.execute_input":"2022-12-19T05:08:51.995732Z","iopub.status.idle":"2022-12-19T05:08:52.111328Z","shell.execute_reply.started":"2022-12-19T05:08:51.995693Z","shell.execute_reply":"2022-12-19T05:08:52.110361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = train_data.classes","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:52.112824Z","iopub.execute_input":"2022-12-19T05:08:52.113086Z","iopub.status.idle":"2022-12-19T05:08:52.124423Z","shell.execute_reply.started":"2022-12-19T05:08:52.113051Z","shell.execute_reply":"2022-12-19T05:08:52.123727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--------------------------","metadata":{"execution":{"iopub.execute_input":"2022-05-02T07:16:33.271344Z","iopub.status.busy":"2022-05-02T07:16:33.271002Z","iopub.status.idle":"2022-05-02T07:16:33.277614Z","shell.execute_reply":"2022-05-02T07:16:33.276446Z","shell.execute_reply.started":"2022-05-02T07:16:33.271298Z"}}},{"cell_type":"code","source":"train_on_gpu = torch.cuda.is_available()\n#train_on_gpu = False\n\nif not train_on_gpu:\n    device = 'cpu'\n    print('CUDA is not available.  Training on CPU ...')\nelse:\n    device = 'cuda'\n    print('CUDA is available!  Training on GPU ...')","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:52.126701Z","iopub.execute_input":"2022-12-19T05:08:52.127009Z","iopub.status.idle":"2022-12-19T05:08:52.204189Z","shell.execute_reply.started":"2022-12-19T05:08:52.126974Z","shell.execute_reply":"2022-12-19T05:08:52.203225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myModel3(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.seq = nn.Sequential(\n\n            nn.Conv2d(3, 32, 3, padding=1),nn.BatchNorm2d(32),nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n\n            nn.Conv2d(32, 64, 3, padding=1),nn.BatchNorm2d(64),nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n\n            nn.Conv2d(64, 64, 3, padding=1),nn.BatchNorm2d(64),nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n            \n            nn.Conv2d(64, 128, 3, padding=1),nn.BatchNorm2d(128),nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n            \n            nn.Conv2d(128, 128, 3, padding=1),nn.BatchNorm2d(128),nn.ReLU(),\n            nn.MaxPool2d(2, 2),\n            self.initialize_weights()\n        )\n        self.fc1 = nn.Linear(8192,15)\n        self.bn6 = nn.BatchNorm1d(num_features=512)\n        self.fc2 = nn.Linear(512,15)\n        self.bn7 = nn.BatchNorm1d(num_features=512)\n        self.fc3 = nn.Linear(512,15)\n        self.dropout = nn.Dropout(0.7)\n        self.relu = nn.ReLU(True)\n        self.initialize_weights()\n        \n    def initialize_weights(self):\n        # track all layers\n        for m in self.modules():\n            if isinstance(m, nn.Conv2d):\n                nn.init.kaiming_uniform_(m.weight)\n\n                if m.bias is not None:\n                    nn.init.constant_(m.bias, 0)\n            elif isinstance(m, nn.BatchNorm2d):\n                nn.init.constant_(m.weight, 1)\n                nn.init.constant_(m.bias, 0)\n\n            elif isinstance(m, nn.Linear):\n                nn.init.kaiming_uniform_(m.weight)\n                nn.init.constant_(m.bias, 0)\n        \n    def forward(self, x):\n        x = self.seq(x)\n        x = x.reshape(-1,8192)\n        x = self.dropout(x)\n        x = self.fc1(x)\n        return x\n    \nmodel1 = myModel3()\nmodel1.to(device)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:52.208935Z","iopub.execute_input":"2022-12-19T05:08:52.209272Z","iopub.status.idle":"2022-12-19T05:08:55.365799Z","shell.execute_reply.started":"2022-12-19T05:08:52.209214Z","shell.execute_reply":"2022-12-19T05:08:55.365072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"모델 선정 이유 : VGG-16모델을 표방한 모습으로 모델을 만들었고 maxpool2d를 5번 적용해서 원래 256x256의 크기였던 이미지를 8x8까지 축소하였다.\n                이를 통해서 Linear에 들어갈 input수를 6272까지 축소할 수 있었다.","metadata":{}},{"cell_type":"markdown","source":"--------------------------","metadata":{"execution":{"iopub.execute_input":"2022-05-02T07:16:33.271344Z","iopub.status.busy":"2022-05-02T07:16:33.271002Z","iopub.status.idle":"2022-05-02T07:16:33.277614Z","shell.execute_reply":"2022-05-02T07:16:33.276446Z","shell.execute_reply.started":"2022-05-02T07:16:33.271298Z"}}},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.AdamW(model1.parameters(),betas = (0.75,0.999), lr=0.001, weight_decay=1)\nscheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min')","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.367215Z","iopub.execute_input":"2022-12-19T05:08:55.367488Z","iopub.status.idle":"2022-12-19T05:08:55.373031Z","shell.execute_reply.started":"2022-12-19T05:08:55.367453Z","shell.execute_reply":"2022-12-19T05:08:55.371969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer2 = optim.AdamW(model2.parameters(),betas = (0.75,0.999), lr=0.001, weight_decay=1.2)\noptimizer3 = optim.AdamW(model3.parameters(),betas = (0.75,0.999), lr=0.001, weight_decay=1.2)\noptimizer4 = optim.AdamW(model4.parameters(),betas = (0.75,0.999), lr=0.001, weight_decay=1.2)\nscheduler2 = optim.lr_scheduler.ReduceLROnPlateau(optimizer2, 'min')\nscheduler3 = optim.lr_scheduler.ReduceLROnPlateau(optimizer3, 'min')\nscheduler4 = optim.lr_scheduler.ReduceLROnPlateau(optimizer4, 'min')","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.374461Z","iopub.execute_input":"2022-12-19T05:08:55.374758Z","iopub.status.idle":"2022-12-19T05:08:55.458811Z","shell.execute_reply.started":"2022-12-19T05:08:55.374703Z","shell.execute_reply":"2022-12-19T05:08:55.456989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--------------------------","metadata":{"execution":{"iopub.execute_input":"2022-05-02T07:16:33.271344Z","iopub.status.busy":"2022-05-02T07:16:33.271002Z","iopub.status.idle":"2022-05-02T07:16:33.277614Z","shell.execute_reply":"2022-05-02T07:16:33.276446Z","shell.execute_reply.started":"2022-05-02T07:16:33.271298Z"}}},{"cell_type":"code","source":"\nepochs = 300\nvalid_loss_min = np.Inf\ntrain_losses = []\nval_losses = []\nfor epoch in range(epochs):\n    running_loss = 0\n    running_accuracy = 0\n    for inputs, labels in trainloader:\n        \n        # Move input and label tensors to the default device\n        inputs, labels = inputs.to(device), labels.to(device)\n        \n        optimizer.zero_grad()\n        \n        logits = model1.forward(inputs)\n        loss = criterion(logits, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        ps = F.softmax(logits, dim=1)\n        top_p, top_class = ps.topk(1, dim=1)\n        equals = top_class == labels.view(*top_class.shape)\n        running_accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n        \n    valid_loss = 0\n    valid_accuracy = 0\n       \n    model1.eval()    \n    with torch.no_grad():\n        for inputs, labels in validloader:\n            inputs, labels = inputs.to(device), labels.to(device)\n            logits = model1.forward(inputs)\n            batch_loss = criterion(logits, labels)\n            valid_loss += batch_loss.item()\n\n            # Calculate accuracy\n            ps = F.softmax(logits, dim=1)\n            top_p, top_class = ps.topk(1, dim=1)\n            equals = top_class == labels.view(*top_class.shape)\n            valid_accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n    scheduler.step(valid_loss)\n    model1.train()\n    train_losses.append(running_loss/len(trainloader))\n    val_losses.append(valid_loss/len(validloader))\n    \n    print(f\"Epoch {epoch+1}/{epochs}.. \"\n          f\"Train loss: {running_loss/len(trainloader):.3f}.. \"\n          f\"Train accuracy: {running_accuracy/len(trainloader):.3f}..\"\n          f\"Valid loss: {valid_loss/len(validloader):.3f}.. \"\n          f\"Valid accuracy: {valid_accuracy/len(validloader):.3f}\")\n\n    if valid_loss <= valid_loss_min:\n        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        valid_loss_min,\n        valid_loss))\n        torch.save(model1.state_dict(), 'model_cifar.pt')\n        valid_loss_min = valid_loss\n","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.459821Z","iopub.status.idle":"2022-12-19T05:08:55.460132Z","shell.execute_reply.started":"2022-12-19T05:08:55.459973Z","shell.execute_reply":"2022-12-19T05:08:55.459992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer.param_groups[0]['lr'] = 0.001","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.461507Z","iopub.status.idle":"2022-12-19T05:08:55.462197Z","shell.execute_reply.started":"2022-12-19T05:08:55.461948Z","shell.execute_reply":"2022-12-19T05:08:55.461976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 200\ntrain_loss_min = np.Inf\ntrain_losses = []\nfor epoch in range(epochs):\n    running_loss = 0\n    running_accuracy = 0\n    for inputs, labels in trainloader1:\n        \n        # Move input and label tensors to the default device\n        inputs, labels = inputs.to(device), labels.to(device)\n        \n        optimizer.zero_grad()\n        \n        logits = model1.forward(inputs)\n        loss = criterion(logits, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        ps = F.softmax(logits, dim=1)\n        top_p, top_class = ps.topk(1, dim=1)\n        equals = top_class == labels.view(*top_class.shape)\n        running_accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n\n    scheduler.step(running_loss)\n    train_losses.append(running_loss/len(trainloader1))\n    \n    print(f\"Epoch {epoch+1}/{epochs}.. \"\n          f\"Train loss: {running_loss/len(trainloader1):.3f}.. \"\n          f\"Train accuracy: {running_accuracy/len(trainloader1):.3f}..\")\n\n    if running_loss <= train_loss_min:\n        print('running loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        train_loss_min,\n        running_loss))\n        torch.save(model1.state_dict(), 'model1_cifar.pt')\n        train_loss_min = running_loss\n","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.463668Z","iopub.status.idle":"2022-12-19T05:08:55.464069Z","shell.execute_reply.started":"2022-12-19T05:08:55.463852Z","shell.execute_reply":"2022-12-19T05:08:55.463875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.load_state_dict(torch.load('model1_cifar.pt'))","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.465469Z","iopub.status.idle":"2022-12-19T05:08:55.46603Z","shell.execute_reply.started":"2022-12-19T05:08:55.465808Z","shell.execute_reply":"2022-12-19T05:08:55.465832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 200\ntrain_loss_min = np.Inf\ntrain_losses = []\nfor epoch in range(epochs):\n    running_loss = 0\n    running_accuracy = 0\n    for inputs, labels in trainloader2:\n        \n        # Move input and label tensors to the default device\n        inputs, labels = inputs.to(device), labels.to(device)\n        \n        optimizer2.zero_grad()\n        \n        logits = model2.forward(inputs)\n        loss = criterion(logits, labels)\n        loss.backward()\n        optimizer2.step()\n\n        running_loss += loss.item()\n        ps = F.softmax(logits, dim=1)\n        top_p, top_class = ps.topk(1, dim=1)\n        equals = top_class == labels.view(*top_class.shape)\n        running_accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n\n    scheduler2.step(running_loss)\n    train_losses.append(running_loss/len(trainloader1))\n    \n    print(f\"Epoch {epoch+1}/{epochs}.. \"\n          f\"Train loss: {running_loss/len(trainloader1):.3f}.. \"\n          f\"Train accuracy: {running_accuracy/len(trainloader1):.3f}..\")\n\n    if running_loss <= train_loss_min:\n        print('running loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        train_loss_min,\n        running_loss))\n        torch.save(model2.state_dict(), 'model2_cifar.pt')\n        train_loss_min = running_loss\n        \n    if running_loss<=0.035:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.46732Z","iopub.status.idle":"2022-12-19T05:08:55.467894Z","shell.execute_reply.started":"2022-12-19T05:08:55.467648Z","shell.execute_reply":"2022-12-19T05:08:55.467672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.load_state_dict(torch.load('model2_cifar.pt'))","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.469292Z","iopub.status.idle":"2022-12-19T05:08:55.469946Z","shell.execute_reply.started":"2022-12-19T05:08:55.469717Z","shell.execute_reply":"2022-12-19T05:08:55.469741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 200\ntrain_loss_min = np.Inf\ntrain_losses = []\nfor epoch in range(epochs):\n    running_loss = 0\n    running_accuracy = 0\n    for inputs, labels in trainloader3:\n        \n        # Move input and label tensors to the default device\n        inputs, labels = inputs.to(device), labels.to(device)\n        \n        optimizer3.zero_grad()\n        \n        logits = model3.forward(inputs)\n        loss = criterion(logits, labels)\n        loss.backward()\n        optimizer3.step()\n\n        running_loss += loss.item()\n        ps = F.softmax(logits, dim=1)\n        top_p, top_class = ps.topk(1, dim=1)\n        equals = top_class == labels.view(*top_class.shape)\n        running_accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n\n    scheduler3.step(running_loss)\n    train_losses.append(running_loss/len(trainloader1))\n    \n    print(f\"Epoch {epoch+1}/{epochs}.. \"\n          f\"Train loss: {running_loss/len(trainloader1):.3f}.. \"\n          f\"Train accuracy: {running_accuracy/len(trainloader1):.3f}..\")\n\n    if running_loss <= train_loss_min:\n        print('running loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        train_loss_min,\n        running_loss))\n        torch.save(model3.state_dict(), 'model3_cifar.pt')\n        train_loss_min = running_loss\n","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.471159Z","iopub.status.idle":"2022-12-19T05:08:55.471925Z","shell.execute_reply.started":"2022-12-19T05:08:55.471696Z","shell.execute_reply":"2022-12-19T05:08:55.47172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.load_state_dict(torch.load('model3_cifar.pt'))","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.472976Z","iopub.status.idle":"2022-12-19T05:08:55.474938Z","shell.execute_reply.started":"2022-12-19T05:08:55.474702Z","shell.execute_reply":"2022-12-19T05:08:55.474727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 200\ntrain_loss_min = np.Inf\ntrain_losses = []\nfor epoch in range(epochs):\n    running_loss = 0\n    running_accuracy = 0\n    for inputs, labels in trainloader4:\n        \n        # Move input and label tensors to the default device\n        inputs, labels = inputs.to(device), labels.to(device)\n        \n        optimizer4.zero_grad()\n        \n        logits = model4.forward(inputs)\n        loss = criterion(logits, labels)\n        loss.backward()\n        optimizer4.step()\n\n        running_loss += loss.item()\n        ps = F.softmax(logits, dim=1)\n        top_p, top_class = ps.topk(1, dim=1)\n        equals = top_class == labels.view(*top_class.shape)\n        running_accuracy += torch.mean(equals.type(torch.FloatTensor)).item()\n\n    scheduler4.step(running_loss)\n    train_losses.append(running_loss/len(trainloader1))\n    \n    print(f\"Epoch {epoch+1}/{epochs}.. \"\n          f\"Train loss: {running_loss/len(trainloader1):.3f}.. \"\n          f\"Train accuracy: {running_accuracy/len(trainloader1):.3f}..\")\n\n    if running_loss <= train_loss_min:\n        print('running loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n        train_loss_min,\n        running_loss))\n        torch.save(model4.state_dict(), 'model4_cifar.pt')\n        train_loss_min = running_loss\n","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.476012Z","iopub.status.idle":"2022-12-19T05:08:55.47654Z","shell.execute_reply.started":"2022-12-19T05:08:55.476317Z","shell.execute_reply":"2022-12-19T05:08:55.476341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model4.load_state_dict(torch.load('model4_cifar.pt'))","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.478164Z","iopub.status.idle":"2022-12-19T05:08:55.478976Z","shell.execute_reply.started":"2022-12-19T05:08:55.478743Z","shell.execute_reply":"2022-12-19T05:08:55.478768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--------------------------","metadata":{"execution":{"iopub.execute_input":"2022-05-02T07:16:33.271344Z","iopub.status.busy":"2022-05-02T07:16:33.271002Z","iopub.status.idle":"2022-05-02T07:16:33.277614Z","shell.execute_reply":"2022-05-02T07:16:33.276446Z","shell.execute_reply.started":"2022-05-02T07:16:33.271298Z"}}},{"cell_type":"code","source":"%matplotlib inline\n%config InlineBackend.figure_format = 'retina'\n\nimport matplotlib.pyplot as plt\n\nplt.plot(train_losses, label='Training loss')\nplt.plot(val_losses, label='Validation loss')\nplt.legend(frameon=False)\nplt.xlabel('epoch')\nplt.ylabel('loss')","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.480347Z","iopub.status.idle":"2022-12-19T05:08:55.481221Z","shell.execute_reply.started":"2022-12-19T05:08:55.480987Z","shell.execute_reply":"2022-12-19T05:08:55.481012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--------------------------","metadata":{"execution":{"iopub.execute_input":"2022-05-02T07:16:33.271344Z","iopub.status.busy":"2022-05-02T07:16:33.271002Z","iopub.status.idle":"2022-05-02T07:16:33.277614Z","shell.execute_reply":"2022-05-02T07:16:33.276446Z","shell.execute_reply.started":"2022-05-02T07:16:33.271298Z"}}},{"cell_type":"code","source":"'''Custom Dataset을 제작하기 위해서는 3가지를 반드시 포함해야합니다. \n1.def __init__()      :초기화 함수입니다. Dataset을 상속받아 imagepath와 transform을 지정합니다.\n2.def __len__()       :dataset의 총 길이를 return하는 함수입니다. \n3.def __getitem__()   :index(idx)에 해당하는 data sample을 return 하는 함수입니다. \n'''\nclass test_Dataset(Dataset): #test_set을 class로 정의합니다.\n    def __init__(self,imgpath,transform=None):\n        \n        self.imgpath = imgpath\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.imgpath)\n    \n    def __getitem__(self,idx):\n        if self.transform !=None:\n            x = self.transform(Image.open(self.imgpath[idx]).convert('RGB'))\n        else: x = Image.open(self.imgpath[idx]).convert('RGB')\n        return x","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-12-19T05:08:55.482329Z","iopub.status.idle":"2022-12-19T05:08:55.483207Z","shell.execute_reply.started":"2022-12-19T05:08:55.482972Z","shell.execute_reply":"2022-12-19T05:08:55.482998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#예시 코드입니다. 잘 작동하는지 확인해봅시다.\ntest_dir = '../input/2022-deep-learning-basics-challenge/painting_resize_256'\ntest_set  = sorted(glob.glob(test_dir+'/*'))\ntest_data = test_Dataset(test_set)\n\n# 잘 불러오는지 확인해 봅시다. \nprint(\"test_data_len:\", test_data.__len__())\ntest_data.__getitem__(0)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.484383Z","iopub.status.idle":"2022-12-19T05:08:55.485254Z","shell.execute_reply.started":"2022-12-19T05:08:55.485008Z","shell.execute_reply":"2022-12-19T05:08:55.485033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '../input/2022-deep-learning-basics-challenge/painting_resize_256/test'\ntest_transform = torchvision.transforms.Compose([\n                                        transforms.ToTensor(),\n                                        transforms.Normalize([0.5200497, 0.4652539, 0.39320767],\n                                                            [0.20736203, 0.19569875, 0.1818195])]\n)\n#test_transform 정의 \n\ntest_set  = sorted(glob.glob(test_dir+'/*')) #모든 파일을 읽어서 정리합니다. \ntest_data = test_Dataset(test_set,test_transform)\n\n#Data Loader 작성 , (tip: test이므로 Shuffle을 할 필요가 없습니다. )\ntest_DataLoader = torch.utils.data.DataLoader(test_data, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.486364Z","iopub.status.idle":"2022-12-19T05:08:55.487283Z","shell.execute_reply.started":"2022-12-19T05:08:55.486994Z","shell.execute_reply":"2022-12-19T05:08:55.487018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"----------------","metadata":{}},{"cell_type":"code","source":"import gc\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.488367Z","iopub.status.idle":"2022-12-19T05:08:55.489259Z","shell.execute_reply.started":"2022-12-19T05:08:55.489011Z","shell.execute_reply":"2022-12-19T05:08:55.489036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#최적의 모델 로드해서 predict를 진행합니다.\n# ex) : \nmodel1.load_state_dict(torch.load('model_cifar.pt'))","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.490339Z","iopub.status.idle":"2022-12-19T05:08:55.491207Z","shell.execute_reply.started":"2022-12-19T05:08:55.490974Z","shell.execute_reply":"2022-12-19T05:08:55.490998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = []    \n# 최종적으로 예측한 각 sample의 label을 담아 놓을 list입니다.  \n#0~14까지 우리가 예측한 가장 높은 확률의 label이 들어가면 되겠죠? \n\nfor data in test_DataLoader:\n    data = data.to(device)\n    logits1 = model1.forward(data)\n    a,b = logits1.topk(1,dim=1)\n    b = b.tolist()\n    for i in range(len(b)):\n        pred.append(b[i])","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.492333Z","iopub.status.idle":"2022-12-19T05:08:55.493295Z","shell.execute_reply.started":"2022-12-19T05:08:55.493014Z","shell.execute_reply":"2022-12-19T05:08:55.493049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = torch.tensor(pred)\npred.reshape(1,834)\npred = pred.tolist()\npred[2][0]","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.494419Z","iopub.status.idle":"2022-12-19T05:08:55.495303Z","shell.execute_reply.started":"2022-12-19T05:08:55.495052Z","shell.execute_reply":"2022-12-19T05:08:55.495077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sudo apt-get install ia32-libs g++-multilib","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.496451Z","iopub.status.idle":"2022-12-19T05:08:55.497366Z","shell.execute_reply.started":"2022-12-19T05:08:55.497115Z","shell.execute_reply":"2022-12-19T05:08:55.49714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name=[]                                     # 그림의 Id를 담을 list 생성 \ncategory =[]                                # 그림의 Category를 담을 list 생성\nfor i in range(test_data.__len__()):        #\n    name.append(test_set[i].split('/')[-1]) # 파일 이름에서 label만 가지고 옵니다. \n    category.append(classes[pred[i][0]])       # pred에 해당하는 class를 category에 집어 넣습니다. \n    \n    \ntm = localtime()\ncsv_file_name = f\"{tm.tm_mday}day_{tm.tm_hour}h_{tm.tm_min}m_{tm.tm_sec}s\"\n\n#파일의 이름은 마음대로 설정할 수 있습니다. 경로를 쓰지 않으면 현재 디렉토리에 알아서 생성됩니다. \npd.DataFrame({'Id':name,'Category':category}).to_csv(f'predict_{csv_file_name}.csv',index=False)    \n\n#predict 하기 위해서 model 전체를 저장합니다. 체점을 위해서 필요한 과정입니다.                                       ","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.498456Z","iopub.status.idle":"2022-12-19T05:08:55.499374Z","shell.execute_reply.started":"2022-12-19T05:08:55.499133Z","shell.execute_reply":"2022-12-19T05:08:55.499158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model1,'model1.pt')","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.500472Z","iopub.status.idle":"2022-12-19T05:08:55.501217Z","shell.execute_reply.started":"2022-12-19T05:08:55.500967Z","shell.execute_reply":"2022-12-19T05:08:55.500992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(name[:5])\nprint(category[:5])","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.502483Z","iopub.status.idle":"2022-12-19T05:08:55.503169Z","shell.execute_reply.started":"2022-12-19T05:08:55.50294Z","shell.execute_reply":"2022-12-19T05:08:55.502965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 잘 생성됬는지 확인 , 따로 경로를 설정하지 않았다면,현재 디렉토리에 자동으로 생성됩니다. \npd.read_csv(f'./predict_{csv_file_name}.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-19T05:08:55.504515Z","iopub.status.idle":"2022-12-19T05:08:55.505378Z","shell.execute_reply.started":"2022-12-19T05:08:55.505132Z","shell.execute_reply":"2022-12-19T05:08:55.505157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 생성한 csv파일을 kaggle에 등록하시면 됩니다.\n\n","metadata":{}}]}