{"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":"## Import Package","metadata":{"papermill":{"duration":0.010385,"end_time":"2022-11-15T22:55:44.567825","exception":false,"start_time":"2022-11-15T22:55:44.557440","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":"2022-11-17T09:26:08.687079Z","iopub.execute_input":"2022-11-17T09:26:08.687788Z","iopub.status.idle":"2022-11-17T09:26:11.355039Z","shell.execute_reply.started":"2022-11-17T09:26:08.687713Z","shell.execute_reply":"2022-11-17T09:26:11.353863Z"},"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":"2022-11-17T09:26:11.362063Z","iopub.execute_input":"2022-11-17T09:26:11.364996Z","iopub.status.idle":"2022-11-17T09:26:11.453481Z","shell.execute_reply.started":"2022-11-17T09:26:11.364954Z","shell.execute_reply":"2022-11-17T09:26:11.452439Z"},"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/dog-breed-identification/train/*.jpg\")\n\nprint(f'Total images in Dataset : {len(img_names)}')","metadata":{"papermill":{"duration":0.276925,"end_time":"2022-11-15T22:55:47.471441","exception":false,"start_time":"2022-11-15T22:55:47.194516","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:11.455162Z","iopub.execute_input":"2022-11-17T09:26:11.455559Z","iopub.status.idle":"2022-11-17T09:26:11.645770Z","shell.execute_reply.started":"2022-11-17T09:26:11.455522Z","shell.execute_reply":"2022-11-17T09:26:11.645009Z"},"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":"2022-11-17T09:26:11.648529Z","iopub.execute_input":"2022-11-17T09:26:11.649189Z","iopub.status.idle":"2022-11-17T09:26:11.660691Z","shell.execute_reply.started":"2022-11-17T09:26:11.649151Z","shell.execute_reply":"2022-11-17T09:26:11.659426Z"},"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.536220","exception":false,"start_time":"2022-11-15T22:55:47.519104","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:11.662220Z","iopub.execute_input":"2022-11-17T09:26:11.662764Z","iopub.status.idle":"2022-11-17T09:26:11.672604Z","shell.execute_reply.started":"2022-11-17T09:26:11.662730Z","shell.execute_reply":"2022-11-17T09:26:11.671597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = DogDataset(\n    img_path=\"../input/dog-breed-identification/train\",\n    csv_path=\"../input/dog-breed-identification/labels.csv\",\n)","metadata":{"papermill":{"duration":0.653669,"end_time":"2022-11-15T22:55:48.196691","exception":false,"start_time":"2022-11-15T22:55:47.543022","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:11.674029Z","iopub.execute_input":"2022-11-17T09:26:11.674689Z","iopub.status.idle":"2022-11-17T09:26:12.671718Z","shell.execute_reply.started":"2022-11-17T09:26:11.674655Z","shell.execute_reply":"2022-11-17T09:26:12.670629Z"},"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":"2022-11-17T09:26:12.673436Z","iopub.execute_input":"2022-11-17T09:26:12.674071Z","iopub.status.idle":"2022-11-17T09:26:12.686190Z","shell.execute_reply.started":"2022-11-17T09:26:12.674031Z","shell.execute_reply":"2022-11-17T09:26:12.684983Z"},"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.250510","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:12.688042Z","iopub.execute_input":"2022-11-17T09:26:12.690519Z","iopub.status.idle":"2022-11-17T09:26:12.696846Z","shell.execute_reply.started":"2022-11-17T09:26:12.690467Z","shell.execute_reply":"2022-11-17T09:26:12.694737Z"},"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":"2022-11-17T09:26:12.699329Z","iopub.execute_input":"2022-11-17T09:26:12.700164Z","iopub.status.idle":"2022-11-17T09:26:12.709616Z","shell.execute_reply.started":"2022-11-17T09:26:12.700121Z","shell.execute_reply":"2022-11-17T09:26:12.708442Z"},"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":"2022-11-17T09:26:12.716857Z","iopub.execute_input":"2022-11-17T09:26:12.717199Z","iopub.status.idle":"2022-11-17T09:26:12.727949Z","shell.execute_reply.started":"2022-11-17T09:26:12.717163Z","shell.execute_reply":"2022-11-17T09:26:12.726332Z"},"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.480360","exception":false,"start_time":"2022-11-15T22:55:48.352442","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:12.729800Z","iopub.execute_input":"2022-11-17T09:26:12.731173Z","iopub.status.idle":"2022-11-17T09:26:12.900843Z","shell.execute_reply.started":"2022-11-17T09:26:12.731128Z","shell.execute_reply":"2022-11-17T09:26:12.899822Z"},"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":"2022-11-17T09:26:12.902395Z","iopub.execute_input":"2022-11-17T09:26:12.902999Z","iopub.status.idle":"2022-11-17T09:26:14.874704Z","shell.execute_reply.started":"2022-11-17T09:26:12.902961Z","shell.execute_reply":"2022-11-17T09:26:14.873433Z"},"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, 120)\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.114760","exception":false,"start_time":"2022-11-15T22:55:50.091591","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:14.875686Z","iopub.execute_input":"2022-11-17T09:26:14.876016Z","iopub.status.idle":"2022-11-17T09:26:14.884597Z","shell.execute_reply.started":"2022-11-17T09:26:14.875983Z","shell.execute_reply":"2022-11-17T09:26:14.883846Z"},"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":"2022-11-17T09:26:14.886611Z","iopub.execute_input":"2022-11-17T09:26:14.887244Z","iopub.status.idle":"2022-11-17T09:26:39.478348Z","shell.execute_reply.started":"2022-11-17T09:26:14.887201Z","shell.execute_reply":"2022-11-17T09:26:39.477298Z"},"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.504580","exception":false,"start_time":"2022-11-15T22:57:48.480489","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:39.480301Z","iopub.execute_input":"2022-11-17T09:26:39.480992Z","iopub.status.idle":"2022-11-17T09:26:39.488130Z","shell.execute_reply.started":"2022-11-17T09:26:39.480952Z","shell.execute_reply":"2022-11-17T09:26:39.486661Z"},"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.545210","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-17T09:26:39.491440Z","iopub.execute_input":"2022-11-17T09:26:39.491796Z","iopub.status.idle":"2022-11-17T09:26:39.499000Z","shell.execute_reply.started":"2022-11-17T09:26:39.491768Z","shell.execute_reply":"2022-11-17T09:26:39.497634Z"},"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":"2022-11-17T09:26:39.500766Z","iopub.execute_input":"2022-11-17T09:26:39.501154Z","iopub.status.idle":"2022-11-17T09:26:39.510581Z","shell.execute_reply.started":"2022-11-17T09:26:39.501102Z","shell.execute_reply":"2022-11-17T09:26:39.509387Z"},"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":"2022-11-17T09:26:39.512386Z","iopub.execute_input":"2022-11-17T09:26:39.512914Z","iopub.status.idle":"2022-11-17T09:26:39.522199Z","shell.execute_reply.started":"2022-11-17T09:26:39.512879Z","shell.execute_reply":"2022-11-17T09:26:39.521297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 7\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":"2022-11-17T09:26:39.523528Z","iopub.execute_input":"2022-11-17T09:26:39.524038Z"},"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":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.read_csv(\"../input/dog-breed-identification/sample_submission.csv\")\ntest_names = submit_df[\"id\"].values\ncolumns = list(dataset.label_idx2name)","metadata":{"papermill":{"duration":0.521486,"end_time":"2022-11-15T23:22:09.464548","exception":false,"start_time":"2022-11-15T23:22:08.943062","status":"completed"},"tags":[],"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/dog-breed-identification/test\", name + \".jpg\")\n        img = Image.open(path)\n        img = self.transform(img)\n        return (img, name)","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":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = TestDataset(\n    test_names = test_names,\n    transform_fn = vit_valid_transform_fn\n)","metadata":{"papermill":{"duration":0.025243,"end_time":"2022-11-15T23:22:09.551875","exception":false,"start_time":"2022-11-15T23:22:09.526632","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataloader = DataLoader(\n    dataset=dataset,\n    batch_size=64,\n    shuffle=False\n)","metadata":{"papermill":{"duration":0.024975,"end_time":"2022-11-15T23:22:09.594139","exception":false,"start_time":"2022-11-15T23:22:09.569164","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(columns)","metadata":{"papermill":{"duration":0.025396,"end_time":"2022-11-15T23:22:09.636485","exception":false,"start_time":"2022-11-15T23:22:09.611089","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n\n    dfs = []\n\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\n        batch_img = batch_img.to(device)\n        output = net(batch_img)\n        sm = F.softmax(output, dim=1)\n        df[columns] = sm.cpu().numpy()\n        dfs.append(df)\n","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":[],"trusted":true},"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":[],"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":[],"trusted":true},"execution_count":null,"outputs":[]}]}