{"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":9509273,"sourceType":"datasetVersion","datasetId":5788103}],"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-09-29T15:20:45.712377Z","iopub.execute_input":"2024-09-29T15:20:45.713313Z","iopub.status.idle":"2024-09-29T15:20:45.720193Z","shell.execute_reply.started":"2024-09-29T15:20:45.713271Z","shell.execute_reply":"2024-09-29T15:20:45.719144Z"},"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-09-29T15:20:45.721731Z","iopub.execute_input":"2024-09-29T15:20:45.722062Z","iopub.status.idle":"2024-09-29T15:20:45.73511Z","shell.execute_reply.started":"2024-09-29T15:20:45.722034Z","shell.execute_reply":"2024-09-29T15:20:45.734205Z"},"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(\"/kaggle/input/dog-breed/train/train/*.jpg\")\n\n# print(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":"2024-09-29T15:20:45.742343Z","iopub.execute_input":"2024-09-29T15:20:45.743041Z","iopub.status.idle":"2024-09-29T15:20:45.747119Z","shell.execute_reply.started":"2024-09-29T15:20:45.743012Z","shell.execute_reply":"2024-09-29T15:20:45.74598Z"},"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 = None\n\n        if csv_path:\n            label_df = pd.read_csv(csv_path)\n            self.img_names = img_path + \"/\" +label_df['filepaths']\n            self.label_idx2name = label_df['class'].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['filepaths']}\"] = self.label_name2idx[row['class']]\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-09-29T17:46:30.856594Z","iopub.execute_input":"2024-09-29T17:46:30.857494Z","iopub.status.idle":"2024-09-29T17:46:30.86815Z","shell.execute_reply.started":"2024-09-29T17:46:30.857454Z","shell.execute_reply":"2024-09-29T17:46:30.867125Z"},"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=(20)),\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-09-29T17:46:40.938466Z","iopub.execute_input":"2024-09-29T17:46:40.938846Z","iopub.status.idle":"2024-09-29T17:46:40.947178Z","shell.execute_reply.started":"2024-09-29T17:46:40.938813Z","shell.execute_reply":"2024-09-29T17:46:40.946204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = DogDataset(\n    img_path=\"/kaggle/input/dog-breed/train\",\n    csv_path=\"/kaggle/input/dog-breed/train.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":"2024-09-29T17:46:41.699994Z","iopub.execute_input":"2024-09-29T17:46:41.700404Z","iopub.status.idle":"2024-09-29T17:46:42.175467Z","shell.execute_reply.started":"2024-09-29T17:46:41.70037Z","shell.execute_reply":"2024-09-29T17:46:42.174363Z"},"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-09-29T17:46:45.68631Z","iopub.execute_input":"2024-09-29T17:46:45.686971Z","iopub.status.idle":"2024-09-29T17:46:45.697441Z","shell.execute_reply.started":"2024-09-29T17:46:45.686934Z","shell.execute_reply":"2024-09-29T17:46:45.696336Z"},"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-09-29T17:46:46.143489Z","iopub.execute_input":"2024-09-29T17:46:46.144399Z","iopub.status.idle":"2024-09-29T17:46:46.149234Z","shell.execute_reply.started":"2024-09-29T17:46:46.14436Z","shell.execute_reply":"2024-09-29T17:46:46.14802Z"},"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-09-29T17:46:54.692137Z","iopub.execute_input":"2024-09-29T17:46:54.692815Z","iopub.status.idle":"2024-09-29T17:46:54.698811Z","shell.execute_reply.started":"2024-09-29T17:46:54.692777Z","shell.execute_reply":"2024-09-29T17:46:54.697688Z"},"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-09-29T17:46:57.51231Z","iopub.execute_input":"2024-09-29T17:46:57.513208Z","iopub.status.idle":"2024-09-29T17:46:57.523182Z","shell.execute_reply.started":"2024-09-29T17:46:57.513172Z","shell.execute_reply":"2024-09-29T17:46:57.522076Z"},"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-09-29T17:47:00.484162Z","iopub.execute_input":"2024-09-29T17:47:00.484836Z","iopub.status.idle":"2024-09-29T17:47:00.527793Z","shell.execute_reply.started":"2024-09-29T17:47:00.484797Z","shell.execute_reply":"2024-09-29T17:47:00.526803Z"},"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-09-29T17:47:01.069443Z","iopub.execute_input":"2024-09-29T17:47:01.070139Z","iopub.status.idle":"2024-09-29T17:47:02.419048Z","shell.execute_reply.started":"2024-09-29T17:47:01.070105Z","shell.execute_reply":"2024-09-29T17:47:02.418047Z"},"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-09-29T17:47:09.244981Z","iopub.execute_input":"2024-09-29T17:47:09.245386Z","iopub.status.idle":"2024-09-29T17:47:09.253462Z","shell.execute_reply.started":"2024-09-29T17:47:09.245351Z","shell.execute_reply":"2024-09-29T17:47:09.25239Z"},"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-09-29T17:48:38.102052Z","iopub.execute_input":"2024-09-29T17:48:38.102459Z","iopub.status.idle":"2024-09-29T17:48:43.342348Z","shell.execute_reply.started":"2024-09-29T17:48:38.102423Z","shell.execute_reply":"2024-09-29T17:48:43.341421Z"},"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)\n\n# # Khởi tạo trình tối ưu hóa AdamW\n# optimizer = optim.AdamW(net.parameters(), lr=0.003)  # learning rate là 0.001\n\n# # Khởi tạo scheduler (ví dụ: giảm learning rate sau mỗi 5 epochs)\n# scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.05)  # Giảm LR xuống 10% sau mỗi 5 epochs","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-09-29T17:48:44.255898Z","iopub.execute_input":"2024-09-29T17:48:44.256595Z","iopub.status.idle":"2024-09-29T17:48:44.263941Z","shell.execute_reply.started":"2024-09-29T17:48:44.256557Z","shell.execute_reply":"2024-09-29T17:48:44.262923Z"},"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-09-29T17:48:44.882955Z","iopub.execute_input":"2024-09-29T17:48:44.883325Z","iopub.status.idle":"2024-09-29T17:48:44.889533Z","shell.execute_reply.started":"2024-09-29T17:48:44.883295Z","shell.execute_reply":"2024-09-29T17:48:44.888495Z"},"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#     scheduler.step()\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-09-29T17:48:45.071145Z","iopub.execute_input":"2024-09-29T17:48:45.071487Z","iopub.status.idle":"2024-09-29T17:48:45.081087Z","shell.execute_reply.started":"2024-09-29T17:48:45.071456Z","shell.execute_reply":"2024-09-29T17:48:45.079958Z"},"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-09-29T17:48:45.256609Z","iopub.execute_input":"2024-09-29T17:48:45.256994Z","iopub.status.idle":"2024-09-29T17:48:45.264534Z","shell.execute_reply.started":"2024-09-29T17:48:45.256958Z","shell.execute_reply":"2024-09-29T17:48:45.263483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 20\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-09-29T17:48:45.481146Z","iopub.execute_input":"2024-09-29T17:48:45.481511Z"},"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-09-29T16:48:31.044113Z","iopub.execute_input":"2024-09-29T16:48:31.045016Z","iopub.status.idle":"2024-09-29T16:48:36.681261Z","shell.execute_reply.started":"2024-09-29T16:48:31.044974Z","shell.execute_reply":"2024-09-29T16:48:36.680262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.read_csv(\"/kaggle/input/dog-breed/test_.csv\")\ntest_names = submit_df[\"filepaths\"].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":[],"execution":{"iopub.status.busy":"2024-09-29T16:50:59.606649Z","iopub.execute_input":"2024-09-29T16:50:59.607027Z","iopub.status.idle":"2024-09-29T16:50:59.616489Z","shell.execute_reply.started":"2024-09-29T16:50:59.606993Z","shell.execute_reply":"2024-09-29T16:50:59.615606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df","metadata":{"execution":{"iopub.status.busy":"2024-09-29T16:52:39.695266Z","iopub.execute_input":"2024-09-29T16:52:39.695967Z","iopub.status.idle":"2024-09-29T16:52:39.707919Z","shell.execute_reply.started":"2024-09-29T16:52:39.695928Z","shell.execute_reply":"2024-09-29T16:52:39.706951Z"},"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(\"/kaggle/input/dog-breed/test/\", name)\n        img = Image.open(path)\n#         if not self.transform:\n#             return (img, name)\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":[],"execution":{"iopub.status.busy":"2024-09-29T16:51:37.28882Z","iopub.execute_input":"2024-09-29T16:51:37.289712Z","iopub.status.idle":"2024-09-29T16:51:37.296945Z","shell.execute_reply.started":"2024-09-29T16:51:37.289672Z","shell.execute_reply":"2024-09-29T16:51:37.295926Z"},"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":[],"execution":{"iopub.status.busy":"2024-09-29T16:51:45.343897Z","iopub.execute_input":"2024-09-29T16:51:45.344908Z","iopub.status.idle":"2024-09-29T16:51:45.349403Z","shell.execute_reply.started":"2024-09-29T16:51:45.344867Z","shell.execute_reply":"2024-09-29T16:51:45.348389Z"},"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":[],"execution":{"iopub.status.busy":"2024-09-29T16:51:45.940902Z","iopub.execute_input":"2024-09-29T16:51:45.941759Z","iopub.status.idle":"2024-09-29T16:51:45.946284Z","shell.execute_reply.started":"2024-09-29T16:51:45.94172Z","shell.execute_reply":"2024-09-29T16:51:45.945256Z"},"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":[],"execution":{"iopub.status.busy":"2024-09-29T16:51:46.802988Z","iopub.execute_input":"2024-09-29T16:51:46.803938Z","iopub.status.idle":"2024-09-29T16:51:46.81028Z","shell.execute_reply.started":"2024-09-29T16:51:46.803896Z","shell.execute_reply":"2024-09-29T16:51:46.809337Z"},"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=[\"filepaths\"] + columns)\n        df[\"filepaths\"] = batch_name\n\n        batch_img = batch_img.to(device)\n        output = net(batch_img)\n        print(get_accuracy(output, label))\n        sm = F.softmax(output, dim=1)\n        predicted_labels = torch.argmax(sm, dim=1)\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":[],"execution":{"iopub.status.busy":"2024-09-29T16:51:47.735048Z","iopub.execute_input":"2024-09-29T16:51:47.735961Z","iopub.status.idle":"2024-09-29T16:51:48.05874Z","shell.execute_reply.started":"2024-09-29T16:51:47.735922Z","shell.execute_reply":"2024-09-29T16:51:48.057584Z"},"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":[],"execution":{"iopub.status.busy":"2024-09-29T16:50:15.465591Z","iopub.execute_input":"2024-09-29T16:50:15.466413Z","iopub.status.idle":"2024-09-29T16:50:15.488571Z","shell.execute_reply.started":"2024-09-29T16:50:15.466376Z","shell.execute_reply":"2024-09-29T16:50:15.487106Z"},"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-09-29T15:54:41.94615Z","iopub.execute_input":"2024-09-29T15:54:41.946497Z","iopub.status.idle":"2024-09-29T15:54:42.09305Z","shell.execute_reply.started":"2024-09-29T15:54:41.946467Z","shell.execute_reply":"2024-09-29T15:54:42.092223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submit","metadata":{"execution":{"iopub.status.busy":"2024-09-29T15:55:02.931298Z","iopub.execute_input":"2024-09-29T15:55:02.93166Z","iopub.status.idle":"2024-09-29T15:55:02.995497Z","shell.execute_reply.started":"2024-09-29T15:55:02.931631Z","shell.execute_reply":"2024-09-29T15:55:02.994364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}