{"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":"# Introduction\nViT (Vision Transformer) is paradigm shift of transformer.\nTransformer is made for NLP originally. But bcoz of its performance, Many studies for differnet field are in progress.\nNow, ViT is nearly SOTA Model for ImageNet. So Let's try ViT just for fun","metadata":{}},{"cell_type":"markdown","source":"# Load Library & Set env","metadata":{}},{"cell_type":"code","source":"!pip install torchsummary\n!pip install vit-pytorch\n!pip install timm","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:51:47.534842Z","iopub.execute_input":"2023-03-27T11:51:47.535364Z","iopub.status.idle":"2023-03-27T11:52:19.324602Z","shell.execute_reply.started":"2023-03-27T11:51:47.535259Z","shell.execute_reply":"2023-03-27T11:52:19.323413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport torch\nimport torch.nn.functional as F\nfrom torch import nn, optim\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.utils.data import Dataset, DataLoader, random_split, ConcatDataset\nfrom torch.autograd import Variable\nimport torch.nn as nn\nimport torch.nn.init\nfrom vit_pytorch import ViT\nimport timm\n\nfrom torchvision import transforms, models\nfrom torchsummary import summary\n\nimport matplotlib.pyplot as plt\n\nimport zipfile\nimport glob\nimport cv2\nimport gc\nimport os\nimport random\nimport math\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:19.328373Z","iopub.execute_input":"2023-03-27T11:52:19.328677Z","iopub.status.idle":"2023-03-27T11:52:22.702531Z","shell.execute_reply.started":"2023-03-27T11:52:19.328646Z","shell.execute_reply":"2023-03-27T11:52:22.701493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 31\nEPOCHS = 12\nLEARNING_RATE = 0.001\nVALIDATION_RATIO = 0.05\n\nDEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\ntorch.manual_seed(1234)\nif torch.cuda.is_available():\n    torch.cuda.manual_seed_all(1234)\n\nprint(DEVICE)\n\nIMG_SIZE = 224\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:59:32.669134Z","iopub.execute_input":"2023-03-27T11:59:32.670265Z","iopub.status.idle":"2023-03-27T11:59:32.678062Z","shell.execute_reply.started":"2023-03-27T11:59:32.670194Z","shell.execute_reply":"2023-03-27T11:59:32.676602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:22.774917Z","iopub.execute_input":"2023-03-27T11:52:22.775618Z","iopub.status.idle":"2023-03-27T11:52:23.727759Z","shell.execute_reply.started":"2023-03-27T11:52:22.775519Z","shell.execute_reply":"2023-03-27T11:52:23.726427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = '/kaggle/working'\nIMG_ROOT_DIR = os.path.join(BASE_DIR,'imgs')\nTRAIN_DIR = os.path.join(IMG_ROOT_DIR, 'train')\nTEST_DIR = os.path.join(IMG_ROOT_DIR, 'test')\nMODEL_PATH = os.path.join(BASE_DIR,'best_model.pt')\n\nprint(TRAIN_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:23.729549Z","iopub.execute_input":"2023-03-27T11:52:23.730294Z","iopub.status.idle":"2023-03-27T11:52:23.737442Z","shell.execute_reply.started":"2023-03-27T11:52:23.730250Z","shell.execute_reply":"2023-03-27T11:52:23.736271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzip & Scheming Dataset","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip') as train_imgs_zip:\n    train_imgs_zip.extractall(IMG_ROOT_DIR)\n    \nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip') as test_imgs_zip:\n    test_imgs_zip.extractall(IMG_ROOT_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:23.739329Z","iopub.execute_input":"2023-03-27T11:52:23.739738Z","iopub.status.idle":"2023-03-27T11:52:43.786023Z","shell.execute_reply.started":"2023-03-27T11:52:23.739704Z","shell.execute_reply":"2023-03-27T11:52:43.785014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path_list = glob.glob(os.path.join(TRAIN_DIR,'*.jpg'))\ntest_img_path_list = glob.glob(os.path.join(TEST_DIR, '*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:43.787533Z","iopub.execute_input":"2023-03-27T11:52:43.787870Z","iopub.status.idle":"2023-03-27T11:52:43.894368Z","shell.execute_reply.started":"2023-03-27T11:52:43.787833Z","shell.execute_reply":"2023-03-27T11:52:43.893518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path_list, valid_img_path_list = train_test_split(train_img_path_list, train_size=1-VALIDATION_RATIO, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:43.895839Z","iopub.execute_input":"2023-03-27T11:52:43.896223Z","iopub.status.idle":"2023-03-27T11:52:43.908093Z","shell.execute_reply.started":"2023-03-27T11:52:43.896172Z","shell.execute_reply":"2023-03-27T11:52:43.907090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_img_path_list[:3])\nprint(test_img_path_list[:3])\nprint(len(train_img_path_list))\nprint(len(valid_img_path_list))\nprint(len(test_img_path_list))\nos.path.basename(train_img_path_list[3])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:43.909940Z","iopub.execute_input":"2023-03-27T11:52:43.910354Z","iopub.status.idle":"2023-03-27T11:52:43.923101Z","shell.execute_reply.started":"2023-03-27T11:52:43.910319Z","shell.execute_reply":"2023-03-27T11:52:43.922185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ViewImg(img_path):\n    img = cv2.imread(img_path)\n    plt.imshow(img)\n\nViewImg(train_img_path_list[10])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:43.926768Z","iopub.execute_input":"2023-03-27T11:52:43.927022Z","iopub.status.idle":"2023-03-27T11:52:44.232134Z","shell.execute_reply.started":"2023-03-27T11:52:43.926992Z","shell.execute_reply":"2023-03-27T11:52:44.231169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Method for Generate Data\n\n**Have 2 do**\n\n1. method 2 resize image (?, ?) -> (IMG_SIZE, IMG_SIZE)\n2. method 2 get category (1 = dog, 0 = cat)","metadata":{}},{"cell_type":"code","source":"def resize_img(img_path):\n    img = cv2.imread(img_path)\n    res_img = cv2.resize(img, dsize=(IMG_SIZE, IMG_SIZE))\n    return res_img\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:44.233151Z","iopub.execute_input":"2023-03-27T11:52:44.233488Z","iopub.status.idle":"2023-03-27T11:52:44.239647Z","shell.execute_reply.started":"2023-03-27T11:52:44.233459Z","shell.execute_reply":"2023-03-27T11:52:44.238409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_category(file_path):\n    file_name = os.path.basename(file_path)\n    category, iid, _ = file_name.split(\".\")\n    if category == 'dog':\n        return 1\n    else:\n        return 0\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:44.242311Z","iopub.execute_input":"2023-03-27T11:52:44.244261Z","iopub.status.idle":"2023-03-27T11:52:44.262063Z","shell.execute_reply.started":"2023-03-27T11:52:44.244074Z","shell.execute_reply":"2023-03-27T11:52:44.261081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate More Data\n\n1. Flip Horizontal\n2. give more noise\n3. add pepper\n4. rotate img\n5. move img","metadata":{}},{"cell_type":"code","source":"def flip_img(img_data):\n    flipped = cv2.flip(img_data, 1)\n    return flipped\n\n\nres_just = resize_img(train_img_path_list[10])\nplt.imshow(flip_img(res_just))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:44.265514Z","iopub.execute_input":"2023-03-27T11:52:44.266139Z","iopub.status.idle":"2023-03-27T11:52:44.761009Z","shell.execute_reply.started":"2023-03-27T11:52:44.266097Z","shell.execute_reply":"2023-03-27T11:52:44.760097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_noise_img(img_data):\n    noise = ( (IMG_SIZE/6) * np.random.random((IMG_SIZE, IMG_SIZE, 3)) ).astype(int)\n    img_data = img_data + noise\n    img_data[img_data>IMG_SIZE-1]=IMG_SIZE-1\n    return img_data\n\nres_just = resize_img(train_img_path_list[10])\nplt.imshow(add_noise_img(res_just))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:44.762491Z","iopub.execute_input":"2023-03-27T11:52:44.763099Z","iopub.status.idle":"2023-03-27T11:52:45.167992Z","shell.execute_reply.started":"2023-03-27T11:52:44.763054Z","shell.execute_reply":"2023-03-27T11:52:45.167154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_pepper_img(img_data, num_of_pepper=150):\n    # Pick a random x coordinate \n    \n    pepper_img = img_data.copy()\n    for _ in range(num_of_pepper):\n        x_coord=random.randint(0, IMG_SIZE-1)\n        y_coord=random.randint(0, IMG_SIZE-1)\n\n        # Color that pixel to black\n        pepper_img[x_coord][y_coord] = random.randint(0,30)\n        \n    return pepper_img\n\nres_just = resize_img(train_img_path_list[10])\nplt.imshow(add_pepper_img(res_just))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:45.169351Z","iopub.execute_input":"2023-03-27T11:52:45.170244Z","iopub.status.idle":"2023-03-27T11:52:45.561835Z","shell.execute_reply.started":"2023-03-27T11:52:45.170195Z","shell.execute_reply":"2023-03-27T11:52:45.540962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rotate_img(img_data, angle=4):\n\n    (h, w) = img_data.shape[:2]\n    (cX, cY) = (w // 2, h // 2)\n\n    M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)\n    rotated_img = cv2.warpAffine(img_data, M, (w, h))\n\n    return rotated_img\n\nres_just = resize_img(train_img_path_list[10])\nplt.imshow(rotate_img(res_just))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:45.563380Z","iopub.execute_input":"2023-03-27T11:52:45.563813Z","iopub.status.idle":"2023-03-27T11:52:46.061039Z","shell.execute_reply.started":"2023-03-27T11:52:45.563770Z","shell.execute_reply":"2023-03-27T11:52:46.060096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def move_img(img_data, stride=5):\n    \n    moved_img = np.roll(img_data, stride, axis=0)\n    moved_img = np.roll(moved_img, stride, axis=1)\n    return moved_img\n\nres_just = resize_img(train_img_path_list[10])\nplt.imshow(move_img(res_just))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:46.062410Z","iopub.execute_input":"2023-03-27T11:52:46.063407Z","iopub.status.idle":"2023-03-27T11:52:46.306702Z","shell.execute_reply.started":"2023-03-27T11:52:46.063363Z","shell.execute_reply":"2023-03-27T11:52:46.305784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's make Dataset !\n\n**Sequence**\n1. get pixel and resize\n2. get category\n3. generate transformed image data","metadata":{}},{"cell_type":"code","source":"class CatDogDataset(Dataset):\n    def __init__(self,img_path_list):\n        self.img_path_list = img_path_list*4\n        self.type_std = len(img_path_list)\n        \n    def __len__(self):\n        return len(self.img_path_list)\n        \n    def __getitem__(self, idx):\n        \n        distortion_type = math.floor(idx / self.type_std) \n        img_path = self.img_path_list[idx]\n        img_data = resize_img(img_path)\n        category = get_train_category(img_path)\n        \n        res_img_data = None\n        \n        # original\n        if distortion_type == 0 : \n            res_img_data = img_data\n        \n        # flip and move 7 and add pepper\n        elif distortion_type == 1:\n            flipped_img_data = flip_img(img_data)\n            moved_img_data = move_img(flipped_img_data)\n            res_img_data = add_pepper_img(moved_img_data)\n            \n        # flip and move -7 and add noise\n        elif distortion_type == 2:\n            flipped_img_data = flip_img(img_data)\n            moved_img_data = move_img(flipped_img_data, -5)\n            res_img_data = add_noise_img(moved_img_data)\n            \n        # rotate 10 and add noise and pepper\n        elif distortion_type == 3:\n            rotated_img_data = rotate_img(img_data)\n            noised_img_data = add_noise_img(rotated_img_data)\n            res_img_data = add_pepper_img(noised_img_data)\n            \n        else:\n            res_img_data = img_data\n        \n        res_img_data = res_img_data/float(IMG_SIZE-1)\n        x_data = torch.FloatTensor(res_img_data).to(DEVICE).reshape((3, IMG_SIZE, IMG_SIZE))\n        y_data = torch.LongTensor([category]).to(DEVICE)\n        return x_data, y_data","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:46.308284Z","iopub.execute_input":"2023-03-27T11:52:46.308636Z","iopub.status.idle":"2023-03-27T11:52:46.319606Z","shell.execute_reply.started":"2023-03-27T11:52:46.308601Z","shell.execute_reply":"2023-03-27T11:52:46.318401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset= CatDogDataset(train_img_path_list)\n\nstride = len(train_img_path_list)\n\nfor idx in range(0,4):\n    plt.subplot(1,4,idx+1)\n    plt.imshow(train_dataset[10+stride*idx][0].cpu().numpy().reshape((IMG_SIZE,IMG_SIZE,3)))\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:52:46.321222Z","iopub.execute_input":"2023-03-27T11:52:46.321839Z","iopub.status.idle":"2023-03-27T11:52:49.689106Z","shell.execute_reply.started":"2023-03-27T11:52:46.321804Z","shell.execute_reply":"2023-03-27T11:52:49.688134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_dataset= CatDogDataset(valid_img_path_list)\ntrain_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)\nvalidation_dataloader = DataLoader(validation_dataset, batch_size=BATCH_SIZE, shuffle=False, drop_last=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:53:42.796285Z","iopub.execute_input":"2023-03-27T11:53:42.796761Z","iopub.status.idle":"2023-03-27T11:53:42.804024Z","shell.execute_reply.started":"2023-03-27T11:53:42.796722Z","shell.execute_reply":"2023-03-27T11:53:42.802937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling and Learning","metadata":{}},{"cell_type":"code","source":"# Model EfficientNet\n\n'''\ngc.collect()\ntorch.cuda.empty_cache()\n\nmodel2 = EfficientNet.from_pretrained('efficientnet-b3', num_classes=2)\nmodel2.cuda()\nsummary(model2, input_size=(3,IMG_SIZE,IMG_SIZE), device='cuda')\n'''","metadata":{"execution":{"iopub.status.busy":"2023-03-26T09:36:22.342365Z","iopub.execute_input":"2023-03-26T09:36:22.342728Z","iopub.status.idle":"2023-03-26T09:36:22.349805Z","shell.execute_reply.started":"2023-03-26T09:36:22.342696Z","shell.execute_reply":"2023-03-26T09:36:22.348648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=2)\nempty_model = timm.create_model('vit_base_patch16_224', pretrained=False, num_classes=2)\nmodel2 = model2.to(DEVICE)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T12:10:15.475174Z","iopub.execute_input":"2023-03-27T12:10:15.475902Z","iopub.status.idle":"2023-03-27T12:10:19.072604Z","shell.execute_reply.started":"2023-03-27T12:10:15.475865Z","shell.execute_reply":"2023-03-27T12:10:19.071504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(model, train_dataloader, validation_dataloader):\n    \n    optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)\n    error_func = nn.CrossEntropyLoss().cuda()\n    best_val_score = 0\n    \n    for epoch in range(EPOCHS):\n        model.train()\n        correct = 0\n        val_correct = 0\n        \n        # for train\n        for batch_idx, (x_batch, y_batch) in enumerate(train_dataloader):\n                        \n            # for GPU\n            x_batch = x_batch.to(DEVICE)\n            y_batch = y_batch.to(DEVICE)\n                \n            # adjust shape for model input\n            x_batch = Variable(x_batch)\n            y_batch = Variable(y_batch).squeeze(dim=-1)\n            optimizer.zero_grad()\n            output = model(x_batch)\n            \n            loss = error_func(output, y_batch)\n            loss.backward()\n            optimizer.step()\n\n            # Total correct predictions\n            predicted = torch.max(output.data, 1)[1]\n            correct += (predicted == y_batch).sum()\n\n            if batch_idx % 400 == 0:\n                print('Epoch : {} [{}/{}]\\t Train Accuracy:{:.3f}%'.format(\n                    epoch, batch_idx*BATCH_SIZE, len(train_dataloader.dataset), float(correct*100) / float(BATCH_SIZE*(batch_idx+1))\n                        )\n                     )\n                \n        # for validation\n        \n        with torch.no_grad():\n            model.eval()\n            epoch_val_accuracy=0\n            epoch_val_loss = 0\n            for batch_idx, (x_batch, y_batch) in enumerate(validation_dataloader):\n                \n                # for GPU\n                x_batch = x_batch.cuda()\n                y_batch = y_batch.cuda()\n                \n                x_batch = Variable(x_batch)\n                y_batch = Variable(y_batch).squeeze(dim=-1)\n                val_output = model(x_batch)\n                val_loss = error_func(val_output, y_batch)\n            \n                # Total correct predictions\n                val_predicted = torch.max(val_output.data, 1)[1]\n                val_correct += (val_predicted == y_batch).sum()\n                \n                \n                \n                if batch_idx % 100 == 0:\n                    print('Epoch : {} [{}/{}]\\t Validation Accuracy:{:.3f}%'.format(\n                    epoch, batch_idx*BATCH_SIZE, len(validation_dataloader.dataset), float(val_correct*100) / float(BATCH_SIZE*(batch_idx+1))\n                        )\n                     )\n            \n        if val_correct >= best_val_score:\n            print('Best Val Acc: {}%'.format(float(val_correct*100) / len(validation_dataloader.dataset)))\n            best_val_score = val_correct\n            torch.save(model.state_dict(), MODEL_PATH)\n            \n    print(\"=\"*10)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:59:52.465930Z","iopub.execute_input":"2023-03-27T11:59:52.466327Z","iopub.status.idle":"2023-03-27T11:59:52.481348Z","shell.execute_reply.started":"2023-03-27T11:59:52.466293Z","shell.execute_reply":"2023-03-27T11:59:52.480138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\ntorch.cuda.empty_cache()\nfit(model2, train_dataloader, validation_dataloader)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T11:59:58.940119Z","iopub.execute_input":"2023-03-27T11:59:58.940559Z","iopub.status.idle":"2023-03-27T12:08:47.274634Z","shell.execute_reply.started":"2023-03-27T11:59:58.940524Z","shell.execute_reply":"2023-03-27T12:08:47.271878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate","metadata":{}},{"cell_type":"code","source":"def evaluate(model, test_dataset):\n    model.eval()\n    preds = torch.LongTensor().cuda()\n    test_preds = torch.LongTensor().cuda()\n    iid_data = torch.LongTensor().cuda()\n    \n    for batch_idx, (x_data, iid) in enumerate(test_dataset):\n        \n        x_data = x_data.reshape((1,3,IMG_SIZE,IMG_SIZE))\n        output = model(x_data)\n        \n        preds = output.data.max(1, keepdim=True)[1]\n        test_preds = torch.cat((test_preds, preds), dim=0)\n        \n        iid_data = torch.cat((iid_data, iid), dim=0)\n        \n        \n    return iid_data, test_preds","metadata":{"execution":{"iopub.status.busy":"2023-03-26T10:14:26.194470Z","iopub.execute_input":"2023-03-26T10:14:26.194853Z","iopub.status.idle":"2023-03-26T10:14:26.203726Z","shell.execute_reply.started":"2023-03-26T10:14:26.194819Z","shell.execute_reply":"2023-03-26T10:14:26.202695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Test_dataset(Dataset):\n    def __init__(self,img_path_list):\n        self.img_path_list = img_path_list\n        \n        \n    def __len__(self):\n        return len(self.img_path_list)\n\n    def __getitem__(self, idx):\n        \n        img_path = self.img_path_list[idx]\n        \n        img_data = resize_img(img_path)\n        \n        # get iid\n        file_name = os.path.basename(img_path)\n        iid, _ = file_name.split(\".\")\n        iid = int(iid)\n        \n        img_data = img_data/IMG_SIZE-1.\n        x_data = torch.cuda.FloatTensor(img_data).reshape((3, IMG_SIZE, IMG_SIZE))\n        iid_data = torch.cuda.LongTensor([iid])    \n        return x_data, iid_data","metadata":{"execution":{"iopub.status.busy":"2023-03-26T10:14:26.933947Z","iopub.execute_input":"2023-03-26T10:14:26.934346Z","iopub.status.idle":"2023-03-26T10:14:26.941681Z","shell.execute_reply.started":"2023-03-26T10:14:26.934313Z","shell.execute_reply":"2023-03-26T10:14:26.940592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = Test_dataset(test_img_path_list)\ngc.collect()\ntorch.cuda.empty_cache()\n\n\n# Load best model\n\n\nbest_model2 = empty_model\nbest_model2 = best_model2.cuda()\nbest_model2.load_state_dict(torch.load(MODEL_PATH))\nbest_model2.eval()\n\nres = evaluate(best_model2, test_dataset)\nres","metadata":{"execution":{"iopub.status.busy":"2023-03-26T10:14:27.524351Z","iopub.execute_input":"2023-03-26T10:14:27.524717Z","iopub.status.idle":"2023-03-26T10:16:33.142292Z","shell.execute_reply.started":"2023-03-26T10:14:27.524685Z","shell.execute_reply":"2023-03-26T10:16:33.141162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id': [], 'label': []})","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:46:58.404026Z","iopub.execute_input":"2022-09-29T14:46:58.404574Z","iopub.status.idle":"2022-09-29T14:46:58.415717Z","shell.execute_reply.started":"2022-09-29T14:46:58.404535Z","shell.execute_reply":"2022-09-29T14:46:58.414658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['id'] = res[0].cpu().numpy()\nsubmission_df['label'] = res[1].cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:46:59.401634Z","iopub.execute_input":"2022-09-29T14:46:59.402307Z","iopub.status.idle":"2022-09-29T14:46:59.416138Z","shell.execute_reply.started":"2022-09-29T14:46:59.402271Z","shell.execute_reply":"2022-09-29T14:46:59.415259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:47:00.084757Z","iopub.execute_input":"2022-09-29T14:47:00.087094Z","iopub.status.idle":"2022-09-29T14:47:00.099755Z","shell.execute_reply.started":"2022-09-29T14:47:00.087045Z","shell.execute_reply":"2022-09-29T14:47:00.098834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = submission_df.sort_values(by=['id'])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:47:04.392119Z","iopub.execute_input":"2022-09-29T14:47:04.392489Z","iopub.status.idle":"2022-09-29T14:47:04.400196Z","shell.execute_reply.started":"2022-09-29T14:47:04.392455Z","shell.execute_reply":"2022-09-29T14:47:04.399031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:47:09.329979Z","iopub.execute_input":"2022-09-29T14:47:09.330536Z","iopub.status.idle":"2022-09-29T14:47:09.342563Z","shell.execute_reply.started":"2022-09-29T14:47:09.330489Z","shell.execute_reply":"2022-09-29T14:47:09.341447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:47:13.218825Z","iopub.execute_input":"2022-09-29T14:47:13.219175Z","iopub.status.idle":"2022-09-29T14:47:13.238597Z","shell.execute_reply.started":"2022-09-29T14:47:13.219144Z","shell.execute_reply":"2022-09-29T14:47:13.23773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}