{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":70924,"databundleVersionId":7751103,"sourceType":"competition"}],"dockerImageVersionId":30302,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Package","metadata":{"papermill":{"duration":0.010385,"end_time":"2022-11-15T22:55:44.567825","exception":false,"start_time":"2022-11-15T22:55:44.55744","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# model\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torchvision import models\nfrom tqdm import tqdm\n\n# dataset\nimport os\nimport math\nimport glob\nimport pandas as pd\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom torchvision import transforms\nfrom torch.utils.data import Dataset, Subset, DataLoader\nimport matplotlib.pyplot as plt\n\n# save result\nimport pickle","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":2.497408,"end_time":"2022-11-15T22:55:47.073945","exception":false,"start_time":"2022-11-15T22:55:44.576537","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:41:41.222095Z","iopub.execute_input":"2024-03-10T05:41:41.223084Z","iopub.status.idle":"2024-03-10T05:41:43.566915Z","shell.execute_reply.started":"2024-03-10T05:41:41.222987Z","shell.execute_reply":"2024-03-10T05:41:43.566078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.manual_seed(2022)\ntry:\n    device = torch.device(\"mps\")\nexcept:\n    device = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\n\nprint(f'Current Device : {device}')","metadata":{"papermill":{"duration":0.07903,"end_time":"2022-11-15T22:55:47.159925","exception":false,"start_time":"2022-11-15T22:55:47.080895","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:41:53.173665Z","iopub.execute_input":"2024-03-10T05:41:53.174679Z","iopub.status.idle":"2024-03-10T05:41:53.244792Z","shell.execute_reply.started":"2024-03-10T05:41:53.174644Z","shell.execute_reply":"2024-03-10T05:41:53.243787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare Dataset","metadata":{"papermill":{"duration":0.006487,"end_time":"2022-11-15T22:55:47.173752","exception":false,"start_time":"2022-11-15T22:55:47.167265","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Get All Training Image","metadata":{"papermill":{"duration":0.006627,"end_time":"2022-11-15T22:55:47.187704","exception":false,"start_time":"2022-11-15T22:55:47.181077","status":"completed"},"tags":[]}},{"cell_type":"code","source":"img_names = glob.glob(\"../input/msba70282024hw2/train/train/*.jpg\")\n\nprint(f'Total images in Dataset : {len(img_names)}')","metadata":{"execution":{"iopub.status.busy":"2024-03-10T05:42:25.6606Z","iopub.execute_input":"2024-03-10T05:42:25.661011Z","iopub.status.idle":"2024-03-10T05:42:25.699507Z","shell.execute_reply.started":"2024-03-10T05:42:25.660976Z","shell.execute_reply":"2024-03-10T05:42:25.698553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Custom Dataset","metadata":{"papermill":{"duration":0.006884,"end_time":"2022-11-15T22:55:47.486148","exception":false,"start_time":"2022-11-15T22:55:47.479264","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class DogDataset(Dataset):\n\n    def __init__(self, img_path, csv_path):\n        self.csv_path = csv_path\n        self.transform = None\n\n        self.img_names = glob.glob(f\"{img_path}/*.jpg\")\n\n        if csv_path:\n            label_df = pd.read_csv(csv_path)\n            self.label_idx2name = label_df['breed'].unique()\n            self.label_name2idx = {}\n            for i in range(len(self.label_idx2name)):\n                self.label_name2idx[self.label_idx2name[i]] = i\n            self.img2label = {}\n            for _, row in label_df.iterrows():\n                self.img2label[f\"{img_path}/{row['id']}.jpg\"] = self.label_name2idx[row['breed']]\n    \n    def __len__(self):\n        return len(self.img_names)\n    \n    def __getitem__(self, index):\n        img = self.img_names[index]\n\n        if self.csv_path:\n            label = self.img2label[img]\n            label = torch.tensor(label)\n        else:\n            label = -1\n        \n        img = Image.open(img).convert(\"RGB\")\n        img = self.transform(img)\n        return (img, label)","metadata":{"papermill":{"duration":0.019302,"end_time":"2022-11-15T22:55:47.512314","exception":false,"start_time":"2022-11-15T22:55:47.493012","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:42:47.849769Z","iopub.execute_input":"2024-03-10T05:42:47.850166Z","iopub.status.idle":"2024-03-10T05:42:47.860719Z","shell.execute_reply.started":"2024-03-10T05:42:47.850134Z","shell.execute_reply":"2024-03-10T05:42:47.859755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform_fn for pretrained ViT\nchannel_mean = torch.Tensor([0.485, 0.456, 0.406])\nchannel_std = torch.Tensor([0.229, 0.224, 0.225])\n\nvit_train_transform_fn = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n\n    transforms.RandomHorizontalFlip(p=0.6),\n    transforms.RandomRotation(degrees=(30)),\n\n    transforms.ToTensor(),\n    transforms.Normalize(mean=channel_mean, std=channel_std),\n])\n\nvit_valid_transform_fn = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=channel_mean, std=channel_std),\n])","metadata":{"papermill":{"duration":0.017116,"end_time":"2022-11-15T22:55:47.53622","exception":false,"start_time":"2022-11-15T22:55:47.519104","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:42:51.356762Z","iopub.execute_input":"2024-03-10T05:42:51.357504Z","iopub.status.idle":"2024-03-10T05:42:51.365186Z","shell.execute_reply.started":"2024-03-10T05:42:51.357469Z","shell.execute_reply":"2024-03-10T05:42:51.364186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = DogDataset(\n    img_path=\"../input/msba70282024hw2/train/train\",\n    csv_path=\"../input/msba70282024hw2/labels.csv\",\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-10T05:42:53.609832Z","iopub.execute_input":"2024-03-10T05:42:53.6107Z","iopub.status.idle":"2024-03-10T05:42:54.159333Z","shell.execute_reply.started":"2024-03-10T05:42:53.610666Z","shell.execute_reply":"2024-03-10T05:42:54.158487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split `dataset` into `train_dataset` and `valid_dataset`","metadata":{"papermill":{"duration":0.007335,"end_time":"2022-11-15T22:55:48.211811","exception":false,"start_time":"2022-11-15T22:55:48.204476","status":"completed"},"tags":[]}},{"cell_type":"code","source":"indexes = list(range(len(dataset)))\ntrain_indexes, valid_indexes = train_test_split(indexes, test_size=0.1)\ntrain_dataset = Subset(dataset, train_indexes)\nvalid_dataset = Subset(dataset, valid_indexes)\n\nprint(f\"Number of samples in train_dataset: {len(train_dataset)}\")\nprint(f\"Number of samples in valid_dataset: {len(valid_dataset)}\")","metadata":{"papermill":{"duration":0.022493,"end_time":"2022-11-15T22:55:48.241938","exception":false,"start_time":"2022-11-15T22:55:48.219445","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:42:59.919842Z","iopub.execute_input":"2024-03-10T05:42:59.920808Z","iopub.status.idle":"2024-03-10T05:42:59.929635Z","shell.execute_reply.started":"2024-03-10T05:42:59.920769Z","shell.execute_reply":"2024-03-10T05:42:59.928661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.transform = vit_train_transform_fn\ntrain_dataset.transform = vit_train_transform_fn\nvalid_dataset.transform = vit_valid_transform_fn","metadata":{"papermill":{"duration":0.016016,"end_time":"2022-11-15T22:55:48.266526","exception":false,"start_time":"2022-11-15T22:55:48.25051","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:43:02.061737Z","iopub.execute_input":"2024-03-10T05:43:02.062692Z","iopub.status.idle":"2024-03-10T05:43:02.067478Z","shell.execute_reply.started":"2024-03-10T05:43:02.062651Z","shell.execute_reply":"2024-03-10T05:43:02.066338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create DataLoader","metadata":{"papermill":{"duration":0.007251,"end_time":"2022-11-15T22:55:48.281179","exception":false,"start_time":"2022-11-15T22:55:48.273928","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_valid_dataloader = DataLoader(\n    dataset,\n    batch_size=8,\n    shuffle=True\n)\n\ntrain_dataloader = DataLoader(\n    train_dataset,\n    batch_size=8,\n    shuffle=True\n)\n\nvalid_dataloader = DataLoader(\n    valid_dataset,\n    batch_size=32,\n    shuffle=True\n)","metadata":{"papermill":{"duration":0.016948,"end_time":"2022-11-15T22:55:48.305311","exception":false,"start_time":"2022-11-15T22:55:48.288363","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:43:04.223362Z","iopub.execute_input":"2024-03-10T05:43:04.223732Z","iopub.status.idle":"2024-03-10T05:43:04.229527Z","shell.execute_reply.started":"2024-03-10T05:43:04.223703Z","shell.execute_reply":"2024-03-10T05:43:04.228475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show Some Samples in Batch","metadata":{"papermill":{"duration":0.007309,"end_time":"2022-11-15T22:55:48.319988","exception":false,"start_time":"2022-11-15T22:55:48.312679","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def show_samples(batch_img, batch_label=None, num_samples=16):\n\n    sample_idx = 0\n    total_col = 4\n    total_row = math.ceil(num_samples / 4)\n    col_idx = 0\n    row_idx = 0\n\n    fig, axs = plt.subplots(total_row, total_col, figsize=(15, 15))\n\n    while sample_idx < num_samples:\n        img = batch_img[sample_idx]\n        img = img.view(3, -1) * channel_std.view(3, -1) + channel_mean.view(3, -1)\n        img = img.view(3, 224, 224)\n        img = img.permute(1, 2, 0)\n        axs[row_idx, col_idx].imshow(img)\n\n        if batch_label != None:\n            axs[row_idx, col_idx].set_title(dataset.label_idx2name[(batch_label[sample_idx])])\n\n        sample_idx += 1\n        col_idx += 1\n        if col_idx == 4:\n            col_idx = 0\n            row_idx += 1","metadata":{"papermill":{"duration":0.017951,"end_time":"2022-11-15T22:55:48.345185","exception":false,"start_time":"2022-11-15T22:55:48.327234","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:43:07.128397Z","iopub.execute_input":"2024-03-10T05:43:07.129055Z","iopub.status.idle":"2024-03-10T05:43:07.139688Z","shell.execute_reply.started":"2024-03-10T05:43:07.129016Z","shell.execute_reply":"2024-03-10T05:43:07.138757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_img, batch_label = next(iter(train_dataloader))","metadata":{"papermill":{"duration":0.127918,"end_time":"2022-11-15T22:55:48.48036","exception":false,"start_time":"2022-11-15T22:55:48.352442","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:43:11.633694Z","iopub.execute_input":"2024-03-10T05:43:11.634085Z","iopub.status.idle":"2024-03-10T05:43:11.730174Z","shell.execute_reply.started":"2024-03-10T05:43:11.634051Z","shell.execute_reply":"2024-03-10T05:43:11.729317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_samples(batch_img, batch_label, 8)","metadata":{"papermill":{"duration":1.562077,"end_time":"2022-11-15T22:55:50.049861","exception":false,"start_time":"2022-11-15T22:55:48.487784","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T05:43:13.446717Z","iopub.execute_input":"2024-03-10T05:43:13.447109Z","iopub.status.idle":"2024-03-10T05:43:14.813334Z","shell.execute_reply.started":"2024-03-10T05:43:13.447076Z","shell.execute_reply":"2024-03-10T05:43:14.812362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build Model","metadata":{"papermill":{"duration":0.014054,"end_time":"2022-11-15T22:55:50.078273","exception":false,"start_time":"2022-11-15T22:55:50.064219","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class PretrainViT(nn.Module):\n\n    def __init__(self):\n        super(PretrainViT, self).__init__()\n        model = models.vit_l_16(pretrained=True)\n        num_classifier_feature = model.heads.head.in_features\n        model.heads.head = nn.Sequential(\n            nn.Linear(num_classifier_feature, 70)\n        )\n        self.model = model\n\n        for param in self.model.named_parameters():\n            if \"heads\" not in param[0]:\n                param[1].requires_grad = False\n\n    def forward(self, x):\n        return self.model(x)","metadata":{"papermill":{"duration":0.023169,"end_time":"2022-11-15T22:55:50.11476","exception":false,"start_time":"2022-11-15T22:55:50.091591","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T07:48:40.811067Z","iopub.execute_input":"2024-03-10T07:48:40.811999Z","iopub.status.idle":"2024-03-10T07:48:40.819536Z","shell.execute_reply.started":"2024-03-10T07:48:40.811959Z","shell.execute_reply":"2024-03-10T07:48:40.818613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = PretrainViT()\nnet.to(device)\nprint(f\"number of paramaters: {sum([param.numel() for param in net.parameters() if param.requires_grad])}\")","metadata":{"papermill":{"duration":118.283643,"end_time":"2022-11-15T22:57:48.411541","exception":false,"start_time":"2022-11-15T22:55:50.127898","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T07:48:45.769378Z","iopub.execute_input":"2024-03-10T07:48:45.770284Z","iopub.status.idle":"2024-03-10T07:48:50.966211Z","shell.execute_reply.started":"2024-03-10T07:48:45.770238Z","shell.execute_reply":"2024-03-10T07:48:50.96521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Model","metadata":{"papermill":{"duration":0.013534,"end_time":"2022-11-15T22:57:48.440131","exception":false,"start_time":"2022-11-15T22:57:48.426597","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Loss Function and Optimizer","metadata":{"papermill":{"duration":0.013399,"end_time":"2022-11-15T22:57:48.466994","exception":false,"start_time":"2022-11-15T22:57:48.453595","status":"completed"},"tags":[]}},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(net.parameters(), lr=0.009, momentum=0.9)","metadata":{"papermill":{"duration":0.024091,"end_time":"2022-11-15T22:57:48.50458","exception":false,"start_time":"2022-11-15T22:57:48.480489","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T07:48:55.299817Z","iopub.execute_input":"2024-03-10T07:48:55.300278Z","iopub.status.idle":"2024-03-10T07:48:55.307791Z","shell.execute_reply.started":"2024-03-10T07:48:55.300242Z","shell.execute_reply":"2024-03-10T07:48:55.3067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training Loop ","metadata":{"papermill":{"duration":0.013627,"end_time":"2022-11-15T22:57:48.531721","exception":false,"start_time":"2022-11-15T22:57:48.518094","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_accuracy(output, label):\n    output = output.to(\"cpu\")\n    label = label.to(\"cpu\")\n\n    sm = F.softmax(output, dim=1)\n    _, index = torch.max(sm, dim=1)\n    return torch.sum((label == index)) / label.size()[0]","metadata":{"papermill":{"duration":0.022633,"end_time":"2022-11-15T22:57:48.567843","exception":false,"start_time":"2022-11-15T22:57:48.54521","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T07:48:56.918383Z","iopub.execute_input":"2024-03-10T07:48:56.918788Z","iopub.status.idle":"2024-03-10T07:48:56.92623Z","shell.execute_reply.started":"2024-03-10T07:48:56.918746Z","shell.execute_reply":"2024-03-10T07:48:56.925086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(model, dataloader):\n    model.train()\n    running_loss = 0.0\n    total_loss = 0.0\n    running_acc = 0.0\n    total_acc = 0.0\n\n    for batch_idx, (batch_img, batch_label) in enumerate(dataloader):\n\n        batch_img = batch_img.to(device)\n        batch_label = batch_label.to(device)\n\n        optimizer.zero_grad()\n        output = net(batch_img)\n        loss = criterion(output, batch_label)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        total_loss += loss.item()\n\n        acc = get_accuracy(output, batch_label)\n        running_acc += acc\n        total_acc += acc\n\n        if batch_idx % 100 == 0 and batch_idx != 0:\n            print(f\"[step: {batch_idx:4d}/{len(dataloader)}] loss: {running_loss / 100:.3f}\")\n            running_loss = 0.0\n            running_acc = 0.0\n    \n    return total_loss / len(dataloader), total_acc / len(dataloader)","metadata":{"papermill":{"duration":0.024755,"end_time":"2022-11-15T22:57:48.605902","exception":false,"start_time":"2022-11-15T22:57:48.581147","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T07:48:58.66679Z","iopub.execute_input":"2024-03-10T07:48:58.667206Z","iopub.status.idle":"2024-03-10T07:48:58.676961Z","shell.execute_reply.started":"2024-03-10T07:48:58.667171Z","shell.execute_reply":"2024-03-10T07:48:58.676036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate(model, dataloader):\n    model.eval()\n    total_loss = 0.0\n    total_acc = 0.0\n\n    for batch_idx, (batch_img, batch_label) in enumerate(dataloader):\n\n        batch_img = batch_img.to(device)\n        batch_label = batch_label.to(device)\n\n        # optimizer.zero_grad()\n        output = net(batch_img)\n        loss = criterion(output, batch_label)\n        # loss.backward()\n        # optimizer.step()\n\n        total_loss += loss.item()\n        acc = get_accuracy(output, batch_label)\n        total_acc += acc\n    \n    return total_loss / len(dataloader), total_acc / len(dataloader)","metadata":{"papermill":{"duration":0.023686,"end_time":"2022-11-15T22:57:48.643002","exception":false,"start_time":"2022-11-15T22:57:48.619316","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T07:49:01.777175Z","iopub.execute_input":"2024-03-10T07:49:01.777541Z","iopub.status.idle":"2024-03-10T07:49:01.784953Z","shell.execute_reply.started":"2024-03-10T07:49:01.777512Z","shell.execute_reply":"2024-03-10T07:49:01.783916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 14\ntrain_loss_history = []\nvalid_loss_history = []\n\ntrain_acc_history = []\nvalid_acc_history = []\n\nfor epoch in range(EPOCHS):\n    train_loss, train_acc = train(net, train_dataloader)\n    valid_loss, valid_acc = validate(net, valid_dataloader)\n    print(f\"Epoch: {epoch:2d}, training loss: {train_loss:.3f}, training acc: {train_acc:.3f} validation loss: {valid_loss:.3f}, validation acc: {valid_acc:.3f}\")\n\n    train_loss_history.append(train_loss)\n    valid_loss_history.append(valid_loss)\n\n    train_acc_history.append(train_acc)\n    valid_acc_history.append(valid_acc)\n\n    if valid_loss <= min(valid_loss_history):\n        torch.save(net.state_dict(), \"net.pt\")","metadata":{"papermill":{"duration":1448.32153,"end_time":"2022-11-15T23:21:56.977884","exception":false,"start_time":"2022-11-15T22:57:48.656354","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T08:37:55.165899Z","iopub.execute_input":"2024-03-10T08:37:55.1667Z","iopub.status.idle":"2024-03-10T09:26:06.193983Z","shell.execute_reply.started":"2024-03-10T08:37:55.166666Z","shell.execute_reply":"2024-03-10T09:26:06.192921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict on Test Dataset and Submit to Kaggle","metadata":{"papermill":{"duration":0.016543,"end_time":"2022-11-15T23:21:57.011515","exception":false,"start_time":"2022-11-15T23:21:56.994972","status":"completed"},"tags":[]}},{"cell_type":"code","source":"net = PretrainViT()\nnet.load_state_dict(torch.load(\"./net.pt\", map_location=\"cpu\"))\nnet.to(device)\nnet.eval()","metadata":{"papermill":{"duration":11.896357,"end_time":"2022-11-15T23:22:08.924785","exception":false,"start_time":"2022-11-15T23:21:57.028428","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T09:26:06.196153Z","iopub.execute_input":"2024-03-10T09:26:06.19669Z","iopub.status.idle":"2024-03-10T09:26:12.16594Z","shell.execute_reply.started":"2024-03-10T09:26:06.196644Z","shell.execute_reply":"2024-03-10T09:26:12.164968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n\n    def __init__(self, test_names, transform_fn):\n        self.test_names = test_names\n        self.transform = transform_fn\n    \n    def __len__(self):\n        return len(self.test_names)\n\n    def __getitem__(self, idx):\n        name = self.test_names[idx]\n        path = os.path.join(\"../input/msba70282024hw2/test/test\", str(name) + \".jpg\")\n        img = Image.open(path)\n        img = self.transform(img)\n        return (img, name)\n    \ndataset = TestDataset(test_names = test_names,transform_fn = vit_valid_transform_fn)\ntest_dataloader = DataLoader(\n    dataset=dataset,\n    batch_size=64,\n    shuffle=False)","metadata":{"papermill":{"duration":0.027525,"end_time":"2022-11-15T23:22:09.509813","exception":false,"start_time":"2022-11-15T23:22:09.482288","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T09:26:12.167161Z","iopub.execute_input":"2024-03-10T09:26:12.167471Z","iopub.status.idle":"2024-03-10T09:26:12.175621Z","shell.execute_reply.started":"2024-03-10T09:26:12.167442Z","shell.execute_reply":"2024-03-10T09:26:12.174546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    dfs = []\n    for batch_idx, (batch_img, batch_name) in tqdm(enumerate(test_dataloader)):\n        df = pd.DataFrame(columns=[\"id\"] + columns)\n        df[\"id\"] = batch_name\n        sm = F.softmax(net(batch_img.to(device)), dim=1)\n        df[columns] = sm.cpu().detach().numpy()\n        dfs.append(df)","metadata":{"papermill":{"duration":317.835451,"end_time":"2022-11-15T23:27:27.488702","exception":false,"start_time":"2022-11-15T23:22:09.653251","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T09:26:12.177644Z","iopub.execute_input":"2024-03-10T09:26:12.177974Z","iopub.status.idle":"2024-03-10T09:26:28.416532Z","shell.execute_reply.started":"2024-03-10T09:26:12.177946Z","shell.execute_reply":"2024-03-10T09:26:28.415609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submit = pd.concat(dfs)","metadata":{"papermill":{"duration":0.536313,"end_time":"2022-11-15T23:27:28.051182","exception":false,"start_time":"2022-11-15T23:27:27.514869","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T09:26:28.417651Z","iopub.execute_input":"2024-03-10T09:26:28.417954Z","iopub.status.idle":"2024-03-10T09:26:28.445765Z","shell.execute_reply.started":"2024-03-10T09:26:28.417926Z","shell.execute_reply":"2024-03-10T09:26:28.444925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submit","metadata":{"execution":{"iopub.status.busy":"2024-03-10T09:26:28.447057Z","iopub.execute_input":"2024-03-10T09:26:28.447326Z","iopub.status.idle":"2024-03-10T09:26:28.496301Z","shell.execute_reply.started":"2024-03-10T09:26:28.447301Z","shell.execute_reply":"2024-03-10T09:26:28.495311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submit.to_csv(\"submit.csv\", index=False)","metadata":{"papermill":{"duration":1.088596,"end_time":"2022-11-15T23:27:29.165487","exception":false,"start_time":"2022-11-15T23:27:28.076891","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-03-10T09:26:28.497679Z","iopub.execute_input":"2024-03-10T09:26:28.498012Z","iopub.status.idle":"2024-03-10T09:26:28.586867Z","shell.execute_reply.started":"2024-03-10T09:26:28.497983Z","shell.execute_reply":"2024-03-10T09:26:28.586087Z"},"trusted":true},"execution_count":null,"outputs":[]}]}