{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-29T14:57:15.243837Z","iopub.execute_input":"2022-09-29T14:57:15.244552Z","iopub.status.idle":"2022-09-29T14:57:15.252985Z","shell.execute_reply.started":"2022-09-29T14:57:15.244514Z","shell.execute_reply":"2022-09-29T14:57:15.251834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Library & Set env","metadata":{}},{"cell_type":"code","source":"!pip install torchsummary\n!pip install efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:57:17.682287Z","iopub.execute_input":"2022-09-29T14:57:17.682654Z","iopub.status.idle":"2022-09-29T14:57:36.722962Z","shell.execute_reply.started":"2022-09-29T14:57:17.682624Z","shell.execute_reply":"2022-09-29T14:57:36.721721Z"},"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\n\nfrom torchvision import transforms, models\nfrom torchsummary import summary\nfrom efficientnet_pytorch import EfficientNet\n\n\nimport matplotlib.pyplot as plt\n\nimport zipfile\nimport glob\nimport cv2\nimport gc","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:30.246387Z","iopub.execute_input":"2022-09-29T14:32:30.246760Z","iopub.status.idle":"2022-09-29T14:32:31.029209Z","shell.execute_reply.started":"2022-09-29T14:32:30.246729Z","shell.execute_reply":"2022-09-29T14:32:31.028246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 20\nEPOCHS = 12\nLEARNING_RATE = 0.001\nVALIDATION_RATIO = 0.1\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\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:34.417502Z","iopub.execute_input":"2022-09-29T14:32:34.418109Z","iopub.status.idle":"2022-09-29T14:32:34.471725Z","shell.execute_reply.started":"2022-09-29T14:32:34.418070Z","shell.execute_reply":"2022-09-29T14:32:34.470582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:35.241196Z","iopub.execute_input":"2022-09-29T14:32:35.241981Z","iopub.status.idle":"2022-09-29T14:32:36.188839Z","shell.execute_reply.started":"2022-09-29T14:32:35.241943Z","shell.execute_reply":"2022-09-29T14:32:36.187652Z"},"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":"2022-09-29T14:32:36.192625Z","iopub.execute_input":"2022-09-29T14:32:36.192961Z","iopub.status.idle":"2022-09-29T14:32:36.202739Z","shell.execute_reply.started":"2022-09-29T14:32:36.192930Z","shell.execute_reply":"2022-09-29T14:32:36.201790Z"},"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":"2022-09-29T14:32:37.296560Z","iopub.execute_input":"2022-09-29T14:32:37.297507Z","iopub.status.idle":"2022-09-29T14:32:56.529817Z","shell.execute_reply.started":"2022-09-29T14:32:37.297469Z","shell.execute_reply":"2022-09-29T14:32:56.528830Z"},"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":"2022-09-29T14:32:56.531954Z","iopub.execute_input":"2022-09-29T14:32:56.532326Z","iopub.status.idle":"2022-09-29T14:32:56.656084Z","shell.execute_reply.started":"2022-09-29T14:32:56.532290Z","shell.execute_reply":"2022-09-29T14:32:56.655057Z"},"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(test_img_path_list))\nos.path.basename(train_img_path_list[3])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:56.657581Z","iopub.execute_input":"2022-09-29T14:32:56.657960Z","iopub.status.idle":"2022-09-29T14:32:56.665988Z","shell.execute_reply.started":"2022-09-29T14:32:56.657924Z","shell.execute_reply":"2022-09-29T14:32:56.665052Z"},"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":"2022-09-29T14:32:56.669598Z","iopub.execute_input":"2022-09-29T14:32:56.670246Z","iopub.status.idle":"2022-09-29T14:32:56.988610Z","shell.execute_reply.started":"2022-09-29T14:32:56.670202Z","shell.execute_reply":"2022-09-29T14:32:56.987655Z"},"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 (?, ?) -> (256, 256)\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=(256, 256))\n    return res_img\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:56.989621Z","iopub.execute_input":"2022-09-29T14:32:56.990701Z","iopub.status.idle":"2022-09-29T14:32:56.996530Z","shell.execute_reply.started":"2022-09-29T14:32:56.990663Z","shell.execute_reply":"2022-09-29T14:32:56.995417Z"},"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    return category\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:56.998256Z","iopub.execute_input":"2022-09-29T14:32:56.999112Z","iopub.status.idle":"2022-09-29T14:32:57.005694Z","shell.execute_reply.started":"2022-09-29T14:32:56.999075Z","shell.execute_reply":"2022-09-29T14:32:57.004610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate More Data\n\n1. Flip Horizontal\n2. give more noi","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":"2022-09-29T14:32:57.007258Z","iopub.execute_input":"2022-09-29T14:32:57.007903Z","iopub.status.idle":"2022-09-29T14:32:57.243732Z","shell.execute_reply.started":"2022-09-29T14:32:57.007861Z","shell.execute_reply":"2022-09-29T14:32:57.242821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_noise_img(img_data):\n    noise = ( (256/6) * np.random.random((256, 256, 3)) ).astype(int)\n    img_data = img_data + noise\n    img_data[img_data>255]=255\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":"2022-09-29T14:32:57.245080Z","iopub.execute_input":"2022-09-29T14:32:57.245524Z","iopub.status.idle":"2022-09-29T14:32:57.472633Z","shell.execute_reply.started":"2022-09-29T14:32:57.245488Z","shell.execute_reply":"2022-09-29T14:32:57.471714Z"},"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\n4. concat org + transformed","metadata":{}},{"cell_type":"code","source":"class Org_train_dataset(Dataset):\n    def __init__(self,img_path_list):\n        self.img_path_list = img_path_list\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        category = get_train_category(img_path)\n        \n        img_data = img_data/255.\n        x_data = torch.cuda.FloatTensor(img_data).reshape((3, 256, 256))\n        y_data = torch.cuda.LongTensor([category])    \n        return x_data, y_data\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:57.474069Z","iopub.execute_input":"2022-09-29T14:32:57.475140Z","iopub.status.idle":"2022-09-29T14:32:57.482848Z","shell.execute_reply.started":"2022-09-29T14:32:57.475102Z","shell.execute_reply":"2022-09-29T14:32:57.481925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Trans_train_dataset(Dataset):\n    def __init__(self,img_path_list):\n        self.img_path_list = img_path_list\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        category = get_train_category(img_path)\n        \n        flipped_img_data = flip_img(img_data)\n        trans_img_data = add_noise_img(flipped_img_data)\n        \n        trans_img_data = trans_img_data/255.\n        x_data = torch.cuda.FloatTensor(trans_img_data).reshape((3, 256, 256))\n        y_data = torch.cuda.LongTensor([category])    \n        return x_data, y_data","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:57.487860Z","iopub.execute_input":"2022-09-29T14:32:57.488211Z","iopub.status.idle":"2022-09-29T14:32:57.496479Z","shell.execute_reply.started":"2022-09-29T14:32:57.488178Z","shell.execute_reply":"2022-09-29T14:32:57.495315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This method can't be used bcoz RAM Limit\n'''\ndef make_train_data_frame():\n    train_df = pd.DataFrame({'category': [], 'img_data': []})\n    for file_path in train_img_path_list:\n        \n        # handle org image data\n        category = get_train_category(file_path)\n        img_data = resize_img(file_path)\n        \n        temp_df = pd.DataFrame({'category': [category], 'img_data': [img_data]})\n        train_df = train_df.append(temp_df)\n        \n        # handle transformed image data\n        \n        flipped_img_data = flip_img(img_data)\n        transformed_img_data = add_noise_img(flipped_img_data)\n        \n        temp_df2 = pd.DataFrame({'category': [category], 'img_data': [transformed_img_data]})\n        train_df = train_df.append(temp_df2)\n        \n        \n        train_df['category'] = train_df['category'].apply(np.byte)\n        train_df['img_data'] = train_df['img_data'].apply(np.uint8)\n        \n    \n    return train_df\n'''\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:57.497970Z","iopub.execute_input":"2022-09-29T14:32:57.498523Z","iopub.status.idle":"2022-09-29T14:32:57.507841Z","shell.execute_reply.started":"2022-09-29T14:32:57.498486Z","shell.execute_reply":"2022-09-29T14:32:57.506564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"org_train_dataset = Org_train_dataset(train_img_path_list)\ntrans_train_dataset = Trans_train_dataset(train_img_path_list)\n\nwhole_train_dataset = ConcatDataset([org_train_dataset, trans_train_dataset])\n\nlen_whole_train_dataset = len(whole_train_dataset)\nlen_validation_dataset = int(VALIDATION_RATIO*len_whole_train_dataset)\nlen_train_dataset = len_whole_train_dataset - len_validation_dataset\n\ntrain_dataset, validation_dataset = random_split(whole_train_dataset, [len_train_dataset, len_validation_dataset])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:57.509794Z","iopub.execute_input":"2022-09-29T14:32:57.510385Z","iopub.status.idle":"2022-09-29T14:32:57.523629Z","shell.execute_reply.started":"2022-09-29T14:32:57.510350Z","shell.execute_reply":"2022-09-29T14:32:57.522811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)\nvalidation_dataloader = DataLoader(validation_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:32:57.525006Z","iopub.execute_input":"2022-09-29T14:32:57.525358Z","iopub.status.idle":"2022-09-29T14:32:57.530749Z","shell.execute_reply.started":"2022-09-29T14:32:57.525324Z","shell.execute_reply":"2022-09-29T14:32:57.529699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling and Learning","metadata":{}},{"cell_type":"code","source":"# Replace Model EfficientNet\n\n'''\nclass CNN(nn.Module):\n    def __init__(self):\n        super(CNN,self).__init__()\n        \n        self.layer1 = nn.Sequential(\n            nn.Conv2d(3,16,kernel_size=3, stride=2, padding=0),\n            nn.BatchNorm2d(16),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)\n        )\n        \n        self.layer2 = nn.Sequential(\n            nn.Conv2d(16,32, kernel_size=3, stride=2, padding=0),\n            nn.BatchNorm2d(32),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)\n            )\n        \n        self.layer3 = nn.Sequential(\n            nn.Conv2d(32,64, kernel_size=3, stride=2, padding=0),\n            nn.BatchNorm2d(64),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)\n        )\n        \n        # 3x3x64 inputs -> 2 outputs\n        \n        self.fc1 = nn.Linear(3*3*64,20)\n        self.dropout = nn.Dropout(0.5)\n        self.fc2 = nn.Linear(20,40)\n        self.fc3 = nn.Linear(40,2)\n        self.relu = nn.ReLU()\n        \n        \n    def forward(self,x):\n        out = self.layer1(x)\n        out = self.layer2(out)\n        out = self.layer3(out)\n        out = out.view(out.size(0),-1)\n        out = self.fc1(out)\n        out = self.relu(out)\n        out = self.dropout(out)\n        out = self.fc2(out)\n        out = self.relu(out)\n        out = self.fc3(out)\n        \n        return out\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Replace Model EfficientNet\n'''\nmodel1 = CNN().cuda()\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Replace Model EfficientNet\ngc.collect()\ntorch.cuda.empty_cache()\n\nmodel2 = EfficientNet.from_pretrained('efficientnet-b3', num_classes=2)\nmodel2.cuda()\nsummary(model2, input_size=(3,256,256), device='cuda')","metadata":{"execution":{"iopub.status.busy":"2022-09-29T08:54:22.655150Z","iopub.execute_input":"2022-09-29T08:54:22.656630Z","iopub.status.idle":"2022-09-29T08:54:27.210621Z","shell.execute_reply.started":"2022-09-29T08:54:22.656598Z","shell.execute_reply":"2022-09-29T08:54:27.209338Z"},"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.cuda()\n            y_batch = y_batch.cuda()\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":"2022-09-29T08:54:38.432562Z","iopub.execute_input":"2022-09-29T08:54:38.433050Z","iopub.status.idle":"2022-09-29T08:54:38.449326Z","shell.execute_reply.started":"2022-09-29T08:54:38.432989Z","shell.execute_reply":"2022-09-29T08:54:38.447707Z"},"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":"2022-09-29T08:54:39.891311Z","iopub.execute_input":"2022-09-29T08:54:39.892196Z","iopub.status.idle":"2022-09-29T09:22:10.909531Z","shell.execute_reply.started":"2022-09-29T08:54:39.892163Z","shell.execute_reply":"2022-09-29T09:22:10.908089Z"},"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,256,256))\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":"2022-09-29T14:34:32.513793Z","iopub.execute_input":"2022-09-29T14:34:32.514826Z","iopub.status.idle":"2022-09-29T14:34:32.523342Z","shell.execute_reply.started":"2022-09-29T14:34:32.514754Z","shell.execute_reply":"2022-09-29T14:34:32.522280Z"},"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/255.\n        x_data = torch.cuda.FloatTensor(img_data).reshape((3, 256, 256))\n        iid_data = torch.cuda.LongTensor([iid])    \n        return x_data, iid_data","metadata":{"execution":{"iopub.status.busy":"2022-09-29T14:34:34.393595Z","iopub.execute_input":"2022-09-29T14:34:34.393974Z","iopub.status.idle":"2022-09-29T14:34:34.401470Z","shell.execute_reply.started":"2022-09-29T14:34:34.393942Z","shell.execute_reply":"2022-09-29T14:34:34.400495Z"},"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\nbest_model2 = EfficientNet.from_name('efficientnet-b3', num_classes=2)\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":"2022-09-29T14:40:28.104964Z","iopub.execute_input":"2022-09-29T14:40:28.105372Z","iopub.status.idle":"2022-09-29T14:45:08.347944Z","shell.execute_reply.started":"2022-09-29T14:40:28.105336Z","shell.execute_reply":"2022-09-29T14:45:08.347017Z"},"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.237730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}