{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-07-04T09:36:11.176328Z","iopub.execute_input":"2024-07-04T09:36:11.177111Z","iopub.status.idle":"2024-07-04T09:36:11.544309Z","shell.execute_reply.started":"2024-07-04T09:36:11.177078Z","shell.execute_reply":"2024-07-04T09:36:11.543416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import load_dataset, Features, ClassLabel, Image, Array3D\nfrom transformers import AutoImageProcessor, SwinForImageClassification, get_scheduler, AdamW\nimport numpy as np\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\nfrom tqdm import tqdm\nfrom torchinfo import summary","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:36:13.469684Z","iopub.execute_input":"2024-07-04T09:36:13.470576Z","iopub.status.idle":"2024-07-04T09:36:29.389667Z","shell.execute_reply.started":"2024-07-04T09:36:13.470541Z","shell.execute_reply":"2024-07-04T09:36:29.388868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load dataset and split\ntrain_ds, test_ds = load_dataset('cifar10', split=['train[:10000]', 'test[:4000]'])\nsplits = train_ds.train_test_split(test_size=0.2)\ntrain_ds = splits['train']\nval_ds = splits['test']","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:36:35.323986Z","iopub.execute_input":"2024-07-04T09:36:35.325236Z","iopub.status.idle":"2024-07-04T09:36:39.592397Z","shell.execute_reply.started":"2024-07-04T09:36:35.325199Z","shell.execute_reply":"2024-07-04T09:36:39.591537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Swin feature extractor\nfeature_extractor = AutoImageProcessor.from_pretrained(\"microsoft/swin-large-patch4-window7-224\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:36:45.401054Z","iopub.execute_input":"2024-07-04T09:36:45.401714Z","iopub.status.idle":"2024-07-04T09:36:45.538465Z","shell.execute_reply.started":"2024-07-04T09:36:45.401673Z","shell.execute_reply":"2024-07-04T09:36:45.537604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess images\ndef preprocess_images(examples):\n    images = examples['img']\n    images = [np.array(image, dtype=np.uint8) for image in images]\n    inputs = feature_extractor(images=images, return_tensors=\"np\")\n    examples['pixel_values'] = inputs['pixel_values']\n    return examples\n\nfeatures = Features({\n    'label': ClassLabel(names=['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']),\n    'img': Image(decode=True, id=None),\n    'pixel_values': Array3D(dtype=\"float32\", shape=(3, 224, 224)), \n})","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:36:46.464543Z","iopub.execute_input":"2024-07-04T09:36:46.465473Z","iopub.status.idle":"2024-07-04T09:36:46.474102Z","shell.execute_reply.started":"2024-07-04T09:36:46.465436Z","shell.execute_reply":"2024-07-04T09:36:46.473016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply preprocessing to datasets\npreprocessed_train_ds = train_ds.map(preprocess_images, batched=True, features=features)\npreprocessed_val_ds = val_ds.map(preprocess_images, batched=True, features=features)\npreprocessed_test_ds = test_ds.map(preprocess_images, batched=True, features=features)\n\npreprocessed_train_ds.set_format('torch', columns=['pixel_values', 'label'])\npreprocessed_val_ds.set_format('torch', columns=['pixel_values', 'label'])\npreprocessed_test_ds.set_format('torch', columns=['pixel_values', 'label'])","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:36:47.410509Z","iopub.execute_input":"2024-07-04T09:36:47.410894Z","iopub.status.idle":"2024-07-04T09:38:13.711799Z","shell.execute_reply.started":"2024-07-04T09:36:47.410861Z","shell.execute_reply":"2024-07-04T09:38:13.710772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# Create dataloaders\ntrain_batch_size = 256\neval_batch_size = 256\ntrain_dataloader = DataLoader(preprocessed_train_ds, batch_size=train_batch_size, shuffle=True, num_workers=2)\nval_dataloader = DataLoader(preprocessed_val_ds, batch_size=eval_batch_size, num_workers=2)\ntest_dataloader = DataLoader(preprocessed_test_ds, batch_size=eval_batch_size, num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:39:01.682283Z","iopub.execute_input":"2024-07-04T09:39:01.683148Z","iopub.status.idle":"2024-07-04T09:39:01.740645Z","shell.execute_reply.started":"2024-07-04T09:39:01.683114Z","shell.execute_reply":"2024-07-04T09:39:01.739473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Swin model\nmodel = SwinForImageClassification.from_pretrained(\"microsoft/swin-large-patch4-window7-224\")\nfor param in model.parameters():\n    param.requires_grad = False\nfor param in model.classifier.parameters():\n    param.requires_grad = True\n\nmodel = model.to(device)\nsummary(model)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:39:05.096500Z","iopub.execute_input":"2024-07-04T09:39:05.096849Z","iopub.status.idle":"2024-07-04T09:39:10.572665Z","shell.execute_reply.started":"2024-07-04T09:39:05.096822Z","shell.execute_reply":"2024-07-04T09:39:10.571676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-2, weight_decay=1e-4)\nepochs = 100\n\nscheduler = get_scheduler(\n    \"linear\", optimizer=optimizer, num_warmup_steps=0, \n    num_training_steps=len(train_dataloader) * epochs\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:39:20.088608Z","iopub.execute_input":"2024-07-04T09:39:20.089340Z","iopub.status.idle":"2024-07-04T09:39:20.102501Z","shell.execute_reply.started":"2024-07-04T09:39:20.089307Z","shell.execute_reply":"2024-07-04T09:39:20.101525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train():\n    model.train()\n    total_loss = 0\n    total_preds = []\n    \n    for step, batch in tqdm(enumerate(train_dataloader), total=len(train_dataloader)):\n        lbl, pix = batch['label'].to(device), batch['pixel_values'].to(device)\n        \n        preds = model(pix).logits\n        loss = torch.nn.functional.cross_entropy(preds, lbl)\n        total_loss += loss.item()\n        loss.backward()\n        \n        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)\n        scheduler.step()\n        optimizer.step()\n        optimizer.zero_grad()\n        \n        preds = preds.detach().cpu().numpy()\n        total_preds.append(preds)\n        \n    avg_loss = total_loss / len(train_dataloader)\n    total_preds = np.concatenate(total_preds, axis=0)\n    return avg_loss, total_preds","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:39:31.322346Z","iopub.execute_input":"2024-07-04T09:39:31.323040Z","iopub.status.idle":"2024-07-04T09:39:31.331314Z","shell.execute_reply.started":"2024-07-04T09:39:31.323006Z","shell.execute_reply":"2024-07-04T09:39:31.330249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval():\n    total_loss = 0\n    model.eval()\n    with torch.no_grad():\n        for step, batch in tqdm(enumerate(val_dataloader), total=len(val_dataloader)):\n            lbl, pix = batch['label'].to(device), batch['pixel_values'].to(device)\n            \n            preds = model(pix).logits\n            loss = torch.nn.functional.cross_entropy(preds, lbl)\n            total_loss += loss.item()\n    \n    return total_loss / len(val_dataloader)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:39:37.296567Z","iopub.execute_input":"2024-07-04T09:39:37.296941Z","iopub.status.idle":"2024-07-04T09:39:37.303467Z","shell.execute_reply.started":"2024-07-04T09:39:37.296911Z","shell.execute_reply":"2024-07-04T09:39:37.302543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_loss = float('inf')\nes = 0\nfor epoch in range(epochs):\n    print(f'\\n Epoch {epoch+1} / {epochs}')\n    train_loss, _ = train()\n    val_loss = eval()\n    \n    if val_loss < min_loss:\n        min_loss = val_loss\n        es = 0\n    else:\n        es += 1\n        if es > 4:\n            print(\"Early stopping with train_loss: \", train_loss, \"and val_loss for this epoch: \", val_loss, \"...\")\n            break\n    \n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    print(f'\\n Training Loss: {train_loss}')\n    print(f'\\n Validation Loss: {val_loss}')","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:39:39.530773Z","iopub.execute_input":"2024-07-04T09:39:39.531139Z","iopub.status.idle":"2024-07-04T10:16:04.579513Z","shell.execute_reply.started":"2024-07-04T09:39:39.531110Z","shell.execute_reply":"2024-07-04T10:16:04.578306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval():\n    model.eval()\n    y_pred = []\n    y_true = []\n    with torch.no_grad():\n        for step, batch in tqdm(enumerate(test_dataloader), total=len(test_dataloader)):\n            lbl, pix = batch['label'].to(device), batch['pixel_values'].to(device)\n\n            outputs = model(pix).logits\n            outputs = torch.argmax(outputs, axis=1)\n            y_pred.extend(outputs.cpu().detach().numpy())\n            y_true.extend(lbl.cpu().detach().numpy())\n            \n    return y_pred, y_true\n\ny_pred, y_true = eval()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:16:08.581769Z","iopub.execute_input":"2024-07-04T10:16:08.582151Z","iopub.status.idle":"2024-07-04T10:17:45.306204Z","shell.execute_reply.started":"2024-07-04T10:16:08.582118Z","shell.execute_reply":"2024-07-04T10:17:45.305088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct = np.array(y_pred) == np.array(y_true)\naccuracy = correct.sum() / len(correct)\nprint(\"Accuracy of the model\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:17:52.482905Z","iopub.execute_input":"2024-07-04T10:17:52.483297Z","iopub.status.idle":"2024-07-04T10:17:52.490220Z","shell.execute_reply.started":"2024-07-04T10:17:52.483261Z","shell.execute_reply":"2024-07-04T10:17:52.489148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\nlabels = train_ds.features['label'].names\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)\ndisp.plot(xticks_rotation=45)\n\nprint(\"Swin-tiny, Acc: 0.9135\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T05:57:00.190458Z","iopub.execute_input":"2024-07-04T05:57:00.191467Z","iopub.status.idle":"2024-07-04T05:57:00.790751Z","shell.execute_reply.started":"2024-07-04T05:57:00.191412Z","shell.execute_reply":"2024-07-04T05:57:00.789621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\nlabels = train_ds.features['label'].names\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)\ndisp.plot(xticks_rotation=45)\n\nprint(\"Swin-small, Acc: 0.9352\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T06:22:04.407497Z","iopub.execute_input":"2024-07-04T06:22:04.407878Z","iopub.status.idle":"2024-07-04T06:22:04.970753Z","shell.execute_reply.started":"2024-07-04T06:22:04.407847Z","shell.execute_reply":"2024-07-04T06:22:04.969851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\nlabels = train_ds.features['label'].names\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)\ndisp.plot(xticks_rotation=45)\n\nprint(\"Swin-tiny+small, Acc: 0.914\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T06:22:57.953834Z","iopub.execute_input":"2024-07-04T06:22:57.954514Z","iopub.status.idle":"2024-07-04T06:22:58.559847Z","shell.execute_reply.started":"2024-07-04T06:22:57.954474Z","shell.execute_reply":"2024-07-04T06:22:58.558737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\nlabels = train_ds.features['label'].names\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)\ndisp.plot(xticks_rotation=45)\n\nprint(\"Swin-small+base, Acc: 0.9705\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T06:42:22.268673Z","iopub.execute_input":"2024-07-04T06:42:22.269034Z","iopub.status.idle":"2024-07-04T06:42:22.880819Z","shell.execute_reply.started":"2024-07-04T06:42:22.269005Z","shell.execute_reply":"2024-07-04T06:42:22.879934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\nlabels = train_ds.features['label'].names\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)\ndisp.plot(xticks_rotation=45)\n\nprint(\"Swin-base, Acc: 0.9725\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T07:00:01.687392Z","iopub.execute_input":"2024-07-04T07:00:01.687758Z","iopub.status.idle":"2024-07-04T07:00:02.288636Z","shell.execute_reply.started":"2024-07-04T07:00:01.687726Z","shell.execute_reply":"2024-07-04T07:00:02.287710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\nlabels = train_ds.features['label'].names\ncm = confusion_matrix(y_true, y_pred)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)\ndisp.plot(xticks_rotation=45)\n\nprint(\"Swin-large, Acc: 0.97925\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:15:56.012015Z","iopub.execute_input":"2024-07-04T08:15:56.012416Z","iopub.status.idle":"2024-07-04T08:15:56.619860Z","shell.execute_reply.started":"2024-07-04T08:15:56.012383Z","shell.execute_reply":"2024-07-04T08:15:56.618865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:18:10.410742Z","iopub.execute_input":"2024-07-04T10:18:10.411557Z","iopub.status.idle":"2024-07-04T10:18:26.802006Z","shell.execute_reply.started":"2024-07-04T10:18:10.411521Z","shell.execute_reply":"2024-07-04T10:18:26.800779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\nfrom PIL import Image\nimport os\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n    z.extractall('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:18:26.803915Z","iopub.execute_input":"2024-07-04T10:18:26.804255Z","iopub.status.idle":"2024-07-04T10:20:54.620576Z","shell.execute_reply.started":"2024-07-04T10:18:26.804223Z","shell.execute_reply":"2024-07-04T10:20:54.619739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = [os.path.join('/kaggle/working/test', f) for f in os.listdir('/kaggle/working/test')]\ntest_filenames.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:21:20.798078Z","iopub.execute_input":"2024-07-04T10:21:20.798731Z","iopub.status.idle":"2024-07-04T10:21:21.819660Z","shell.execute_reply.started":"2024-07-04T10:21:20.798696Z","shell.execute_reply":"2024-07-04T10:21:21.818829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize the feature extractor\nfeature_extractor = AutoImageProcessor.from_pretrained(\"microsoft/swin-large-patch4-window7-224\")\n\n# Directory containing external test images\nimage_dir = \"/kaggle/working/test\"\ntest_filenames = [os.path.join(image_dir, filename) for filename in os.listdir(image_dir) if filename.endswith(('png', 'jpg', 'jpeg'))]","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:21:25.898033Z","iopub.execute_input":"2024-07-04T10:21:25.898391Z","iopub.status.idle":"2024-07-04T10:21:26.841318Z","shell.execute_reply.started":"2024-07-04T10:21:25.898363Z","shell.execute_reply":"2024-07-04T10:21:26.840424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\n\nclass ExternalTestDataset(Dataset):\n    def __init__(self, filenames, preprocess_fn):\n        self.filenames = filenames\n        self.preprocess_fn = preprocess_fn\n\n    def __len__(self):\n        return len(self.filenames)\n    \n    def __getitem__(self, idx):\n        img = Image.open(self.filenames[idx]).convert(\"RGB\")\n        example = {'img': img}\n        processed_example = self.preprocess_fn(example)\n        return processed_example['pixel_values']\n\n# Preprocess function for single images\ndef preprocess_images_single(example):\n    image = np.array(example['img'], dtype=np.uint8)\n    image = np.moveaxis(image, source=-1, destination=0)\n    inputs = feature_extractor(images=[image])\n    example['pixel_values'] = inputs['pixel_values'][0]\n    return example","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:22:41.873125Z","iopub.execute_input":"2024-07-04T10:22:41.873529Z","iopub.status.idle":"2024-07-04T10:22:41.882294Z","shell.execute_reply.started":"2024-07-04T10:22:41.873497Z","shell.execute_reply":"2024-07-04T10:22:41.881194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the dataset\nexternal_test_dataset = ExternalTestDataset(test_filenames, preprocess_images_single)\n\n# Create DataLoader\neval_batch_size = 256\nexternal_test_dataloader = DataLoader(external_test_dataset, batch_size=eval_batch_size, shuffle=False, num_workers=2)\n\n# Verify the DataLoader\nbatch = next(iter(external_test_dataloader))\n# print(batch)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:22:43.401550Z","iopub.execute_input":"2024-07-04T10:22:43.402498Z","iopub.status.idle":"2024-07-04T10:22:45.457150Z","shell.execute_reply.started":"2024-07-04T10:22:43.402459Z","shell.execute_reply":"2024-07-04T10:22:45.455686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom tqdm import tqdm\n\ndef eval_sub():\n    model.eval()\n    y_pred = []\n    \n    with torch.no_grad():\n        for step, batch in tqdm(enumerate(external_test_dataloader), total=len(external_test_dataloader)):\n            batch = batch.to(device)\n            \n            outputs = model(batch).logits\n            outputs = torch.argmax(outputs, axis=1)\n            y_pred.extend(outputs.cpu().detach().numpy())\n            \n    return y_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:22:46.623470Z","iopub.execute_input":"2024-07-04T10:22:46.623944Z","iopub.status.idle":"2024-07-04T10:22:46.632037Z","shell.execute_reply.started":"2024-07-04T10:22:46.623904Z","shell.execute_reply":"2024-07-04T10:22:46.630771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = eval_sub()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T10:22:48.590289Z","iopub.execute_input":"2024-07-04T10:22:48.590680Z","iopub.status.idle":"2024-07-04T12:22:52.145853Z","shell.execute_reply.started":"2024-07-04T10:22:48.590649Z","shell.execute_reply":"2024-07-04T12:22:52.144651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = train_ds.features['label'].names","metadata":{"execution":{"iopub.status.busy":"2024-07-04T12:23:55.053483Z","iopub.execute_input":"2024-07-04T12:23:55.054394Z","iopub.status.idle":"2024-07-04T12:23:55.059387Z","shell.execute_reply.started":"2024-07-04T12:23:55.054352Z","shell.execute_reply":"2024-07-04T12:23:55.058434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_cat = [labels[i] for i in predictions]\ny_pred_cat[:10]","metadata":{"execution":{"iopub.status.busy":"2024-07-04T12:24:13.585137Z","iopub.execute_input":"2024-07-04T12:24:13.585550Z","iopub.status.idle":"2024-07-04T12:24:13.625271Z","shell.execute_reply.started":"2024-07-04T12:24:13.585516Z","shell.execute_reply":"2024-07-04T12:24:13.624415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id = [int(i[21:-4]) for i in test_filenames]\nimg_id[0]","metadata":{"execution":{"iopub.status.busy":"2024-07-04T12:24:24.001389Z","iopub.execute_input":"2024-07-04T12:24:24.002007Z","iopub.status.idle":"2024-07-04T12:24:24.101968Z","shell.execute_reply.started":"2024-07-04T12:24:24.001974Z","shell.execute_reply":"2024-07-04T12:24:24.101096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf_sub = pd.DataFrame(list(zip(img_id, y_pred_cat)), columns =['id', 'label'])\ndf_sub.to_csv('submission_final.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T12:24:34.342464Z","iopub.execute_input":"2024-07-04T12:24:34.343095Z","iopub.status.idle":"2024-07-04T12:24:34.948661Z","shell.execute_reply.started":"2024-07-04T12:24:34.343062Z","shell.execute_reply":"2024-07-04T12:24:34.947844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/working/submission_final.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2024-07-04T12:24:49.420058Z","iopub.execute_input":"2024-07-04T12:24:49.420701Z","iopub.status.idle":"2024-07-04T12:24:49.533690Z","shell.execute_reply.started":"2024-07-04T12:24:49.420659Z","shell.execute_reply":"2024-07-04T12:24:49.532675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}