{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.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":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30635,"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2024-01-22T19:16:56.100212Z","iopub.execute_input":"2024-01-22T19:16:56.100876Z","iopub.status.idle":"2024-01-22T19:16:57.097671Z","shell.execute_reply.started":"2024-01-22T19:16:56.100843Z","shell.execute_reply":"2024-01-22T19:16:57.096600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import everything needed","metadata":{}},{"cell_type":"code","source":"import glob\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport collections\nimport math\nimport os\nimport shutil\nfrom sklearn.model_selection import train_test_split\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision\nfrom torchvision import datasets, transforms\nnp.random.seed(0)\ntorch.manual_seed(0)\ntorch.cuda.manual_seed(0)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:16:57.099347Z","iopub.execute_input":"2024-01-22T19:16:57.099790Z","iopub.status.idle":"2024-01-22T19:17:01.744348Z","shell.execute_reply.started":"2024-01-22T19:16:57.099762Z","shell.execute_reply":"2024-01-22T19:17:01.743537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unzip datasets","metadata":{}},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:17:05.609292Z","iopub.execute_input":"2024-01-22T19:17:05.609950Z","iopub.status.idle":"2024-01-22T19:17:23.166109Z","shell.execute_reply.started":"2024-01-22T19:17:05.609911Z","shell.execute_reply":"2024-01-22T19:17:23.164858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WARNING: It can take a lot of time to uncompress!","metadata":{}},{"cell_type":"code","source":"!python -m py7zr x /kaggle/input/cifar-10/train.7z","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:17:23.168316Z","iopub.execute_input":"2024-01-22T19:17:23.168706Z","iopub.status.idle":"2024-01-22T19:18:20.809504Z","shell.execute_reply.started":"2024-01-22T19:17:23.168676Z","shell.execute_reply":"2024-01-22T19:18:20.808102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m py7zr x /kaggle/input/cifar-10/test.7z","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:18:20.811247Z","iopub.execute_input":"2024-01-22T19:18:20.811695Z","iopub.status.idle":"2024-01-22T19:38:07.260604Z","shell.execute_reply.started":"2024-01-22T19:18:20.811652Z","shell.execute_reply":"2024-01-22T19:38:07.259427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '/kaggle/working/'","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:38:22.980794Z","iopub.execute_input":"2024-01-22T19:38:22.981503Z","iopub.status.idle":"2024-01-22T19:38:22.985616Z","shell.execute_reply.started":"2024-01-22T19:38:22.981470Z","shell.execute_reply":"2024-01-22T19:38:22.984547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nconfig = {\n    \"epochs\": 11,\n    \"lr\": 0.003,\n    \"momentum\": 0.9,\n    \"optimizer\": \"SGD\"\n}\n\nrun = wandb.init(project=\"MLDL_CIFAR-10\",\n                 name=f\"ResNet-18-Test-4\",\n                 notes=\"Tryr\",\n                 mode=\"disabled\",\n                 config=config,\n                 save_code=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T20:58:58.240500Z","iopub.execute_input":"2024-01-22T20:58:58.241476Z","iopub.status.idle":"2024-01-22T20:59:31.041579Z","shell.execute_reply.started":"2024-01-22T20:58:58.241442Z","shell.execute_reply":"2024-01-22T20:59:31.040620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_csv_labels(fname):\n    \"\"\"Read `fname` to return a filename to label dictionary.\"\"\"\n    with open(fname, 'r') as f:\n        # Skip the file header line (column name)\n        lines = f.readlines()[1:]\n    tokens = [l.rstrip().split(',') for l in lines]\n    return dict(((name, label) for name, label in tokens))\n\nlabels = read_csv_labels(os.path.join(data_dir, '/kaggle/input/cifar-10/trainLabels.csv'))\nprint(f'Number training examples: {len(labels)}')\nprint(f'Number classes: {len(set(labels.values()))}')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T20:59:34.274416Z","iopub.execute_input":"2024-01-22T20:59:34.275309Z","iopub.status.idle":"2024-01-22T20:59:35.817252Z","shell.execute_reply.started":"2024-01-22T20:59:34.275274Z","shell.execute_reply":"2024-01-22T20:59:35.816354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_classes = set(labels.values())\n#Print which classes the dataset contains.\nprint(\"Unique Classes:\")\nfor class_label in unique_classes:\n    print(class_label)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T20:59:38.538410Z","iopub.execute_input":"2024-01-22T20:59:38.539229Z","iopub.status.idle":"2024-01-22T20:59:39.628832Z","shell.execute_reply.started":"2024-01-22T20:59:38.539186Z","shell.execute_reply":"2024-01-22T20:59:39.627860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts = {}\nfor label in labels.values():\n    if label in class_counts:\n        class_counts[label] += 1\n    else:\n        class_counts[label] = 1\n\nplt.figure(figsize=(10, 6))\nplt.bar(class_counts.keys(), class_counts.values(), color='purple')\nplt.xlabel('Class Label')\nplt.ylabel('Count')\nplt.title('Class Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-22T20:59:42.730882Z","iopub.execute_input":"2024-01-22T20:59:42.731265Z","iopub.status.idle":"2024-01-22T20:59:44.595232Z","shell.execute_reply.started":"2024-01-22T20:59:42.731234Z","shell.execute_reply":"2024-01-22T20:59:44.594296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def copyfile(filename, target_dir):\n    \"\"\"Copy a file into a target directory.\"\"\"\n    os.makedirs(target_dir, exist_ok=True)\n    shutil.copy(filename, target_dir)\n\ndef reorg_train_valid(data_dir, labels, valid_ratio):\n    \"\"\"Split the validation set out of the original training set.\"\"\"\n    # The number of examples of the class that has the fewest examples in the\n    # training dataset\n    n = collections.Counter(labels.values()).most_common()[-1][1]\n    # The number of examples per class for the validation set\n    n_valid_per_label = max(1, math.floor(n * valid_ratio))\n    label_count = {}\n    for train_file in os.listdir(os.path.join(data_dir, 'train')):\n        label = labels[train_file.split('.')[0]]\n        fname = os.path.join(data_dir, 'train', train_file)\n        copyfile(fname, os.path.join(data_dir, 'train_valid_test',\n                                     'train_valid', label))\n        if label not in label_count or label_count[label] < n_valid_per_label:\n            copyfile(fname, os.path.join(data_dir, 'train_valid_test',\n                                         'valid', label))\n            label_count[label] = label_count.get(label, 0) + 1\n        else:\n            copyfile(fname, os.path.join(data_dir, 'train_valid_test',\n                                         'train', label))\n    return n_valid_per_label\n\ndef reorg_test(data_dir):\n    \"\"\"Organize the testing set for data loading during prediction.\"\"\"\n    for test_file in os.listdir(os.path.join(data_dir, 'test')):\n        copyfile(os.path.join(data_dir, 'test', test_file),\n                 os.path.join(data_dir, 'train_valid_test', 'test',\n                              'unknown'))\n        \ndef reorg_cifar10_data(data_dir, valid_ratio):\n    labels = read_csv_labels('/kaggle/input/cifar-10/trainLabels.csv')\n    reorg_train_valid(data_dir, labels, valid_ratio)\n    reorg_test(data_dir)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:39:58.799693Z","iopub.execute_input":"2024-01-22T19:39:58.800382Z","iopub.status.idle":"2024-01-22T19:40:00.471149Z","shell.execute_reply.started":"2024-01-22T19:39:58.800334Z","shell.execute_reply":"2024-01-22T19:40:00.470143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nvalid_ratio = 0.1\nreorg_cifar10_data(data_dir, valid_ratio)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:40:04.467610Z","iopub.execute_input":"2024-01-22T19:40:04.468323Z","iopub.status.idle":"2024-01-22T19:41:06.429735Z","shell.execute_reply.started":"2024-01-22T19:40:04.468293Z","shell.execute_reply":"2024-01-22T19:41:06.428805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Transformations\n- Pre-trained networks usually apply some transformations to their inputs\n- TODOs:\n    - Check the transformations in the pre-trained ResNet and sync with yours! \n    - Consider adding more transformations, like HorizontalFlip, mnRandomSizeCrop","metadata":{}},{"cell_type":"markdown","source":"... image or its horizontal flip, with the per-pixel mean subtracted [21]. The standard color augmentation in [21] is used ...","metadata":{}},{"cell_type":"code","source":"\nfrom torchvision.transforms import RandomApply, GaussianBlur, ToTensor, Normalize\n\ntransform_train = torchvision.transforms.Compose([\n    transforms.RandomRotation(degrees=20),\n    RandomApply([transforms.GaussianBlur(kernel_size=3)], p=0.3),\n    transforms.ColorJitter(brightness=0.1,contrast = 0.1 ,saturation =0.1),\n    transforms.RandomAdjustSharpness(sharpness_factor = 2, p = 0.1),\n    transforms.RandomHorizontalFlip(),\n    torchvision.transforms.ToTensor(),\n    torchvision.transforms.Normalize([0.4914, 0.4822, 0.4465],\n                                     [0.2023, 0.1994, 0.2010])\n])\n\ntransform_test = torchvision.transforms.Compose([\n    torchvision.transforms.ToTensor(),\n    torchvision.transforms.Normalize([0.4914, 0.4822, 0.4465],\n                                     [0.2023, 0.1994, 0.2010])])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-22T20:59:49.578398Z","iopub.execute_input":"2024-01-22T20:59:49.578752Z","iopub.status.idle":"2024-01-22T20:59:50.749866Z","shell.execute_reply.started":"2024-01-22T20:59:49.578722Z","shell.execute_reply":"2024-01-22T20:59:50.748835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds, train_valid_ds = [torchvision.datasets.ImageFolder(\n    os.path.join(data_dir, 'train_valid_test', folder),\n    transform=transform_train) for folder in ['train', 'train_valid']]\n\nvalid_ds, test_ds = [torchvision.datasets.ImageFolder(\n    os.path.join(data_dir, 'train_valid_test', folder),\n    transform=transform_test) for folder in ['valid', 'test']]\n\ntrain_iter, train_valid_iter = [torch.utils.data.DataLoader(\n    dataset, batch_size, shuffle=True, drop_last=True)\n    for dataset in (train_ds, train_valid_ds)]\n\nvalid_iter = torch.utils.data.DataLoader(valid_ds, batch_size, shuffle=False,\n                                         drop_last=True)\n\ntest_iter = torch.utils.data.DataLoader(test_ds, batch_size, shuffle=False,\n                                        drop_last=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T20:59:55.052567Z","iopub.execute_input":"2024-01-22T20:59:55.052932Z","iopub.status.idle":"2024-01-22T20:59:58.272314Z","shell.execute_reply.started":"2024-01-22T20:59:55.052901Z","shell.execute_reply":"2024-01-22T20:59:58.271349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_ds), len(train_valid_ds), len(valid_ds), len(train_valid_iter), len(test_ds)\n#THe test set contains of 300000 images, whereas only 10.000 are from the real dataset. 290.000 images where added as \"junk\"\n#in order to prevent cheating. Still we should predict on all 300000","metadata":{"execution":{"iopub.status.busy":"2024-01-22T19:41:37.302733Z","iopub.execute_input":"2024-01-22T19:41:37.303462Z","iopub.status.idle":"2024-01-22T19:41:38.756801Z","shell.execute_reply.started":"2024-01-22T19:41:37.303429Z","shell.execute_reply":"2024-01-22T19:41:38.755836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"VISUALIZE SOME IMAGES","metadata":{}},{"cell_type":"code","source":"label_mapping = {\n    0: \"airplane\",\n    1: \"automobile\",\n    2: \"bird\",\n    3: \"cat\",\n    4: \"deer\",\n    5: \"dog\",\n    6: \"frog\",\n    7: \"horse\",\n    8: \"ship\",\n    9: \"truck\"\n}\n    \ndef show_images_from_loader(data_loader, num_images=9):\n    data_iter = iter(data_loader)\n    images, labels = next(data_iter)\n\n    # Denormalize images to display them correctly\n    denormalize = transforms.Normalize(mean=[-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225],\n                                       std=[1 / 0.229, 1 / 0.224, 1 / 0.225])\n    images = denormalize(images)\n\n    plt.figure(figsize=(10, 8))\n    for i in range(min(num_images, len(images))):\n        plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].permute(1, 2, 0))\n        plt.title(f\"Label: {label_mapping[int(labels[i])]}\")\n        plt.axis(\"off\")\n    plt.tight_layout()\n    plt.show()\n\nshow_images_from_loader(train_iter, num_images=9)\nshow_images_from_loader(valid_iter, num_images=9)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:00:01.755062Z","iopub.execute_input":"2024-01-22T21:00:01.755736Z","iopub.status.idle":"2024-01-22T21:00:05.104577Z","shell.execute_reply.started":"2024-01-22T21:00:01.755705Z","shell.execute_reply":"2024-01-22T21:00:05.103620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TODO: \n* Define the data augmentation technique \n* Fine-tune a pre-trained ResNet-18 on CIFAR-10\n* Submit your submission file to Kaggle (as described in the competition rules) and get your accuracy on the test set","metadata":{}},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T20:42:27.278728Z","iopub.execute_input":"2024-01-22T20:42:27.279196Z","iopub.status.idle":"2024-01-22T20:42:28.795842Z","shell.execute_reply.started":"2024-01-22T20:42:27.279159Z","shell.execute_reply":"2024-01-22T20:42:28.794888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision.models as models\n!pip install torchsummary\nfrom torchsummary import summary\n\nnet = models.resnet18(weights='ResNet18_Weights.DEFAULT')\n#The output layer shows having 1000 outputs.\n#Our porblem only has 10 classes, so changing that below.\nprint(net.fc)\n\nnum_classes = 10\n#Changing to output 10 values.\n\nnet.fc = nn.Sequential(\n    nn.Linear(net.fc.in_features, 256),\n    nn.BatchNorm1d(256),\n    nn.ReLU(),\n    nn.Linear(256, num_classes)\n)\n\nnn.init.xavier_normal_(net.fc[0].weight)\nnn.init.xavier_normal_(net.fc[3].weight)\n\nnn.init.constant_(net.fc[0].bias, 0)\nnn.init.constant_(net.fc[3].bias, 0)\n\nprint(net.fc)\nnet = net.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:00:17.332125Z","iopub.execute_input":"2024-01-22T21:00:17.332550Z","iopub.status.idle":"2024-01-22T21:00:31.023482Z","shell.execute_reply.started":"2024-01-22T21:00:17.332517Z","shell.execute_reply":"2024-01-22T21:00:31.022442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for param in net.fc.parameters():\n        param.requires_grad = True","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:00:31.026066Z","iopub.execute_input":"2024-01-22T21:00:31.026496Z","iopub.status.idle":"2024-01-22T21:00:32.249246Z","shell.execute_reply.started":"2024-01-22T21:00:31.026456Z","shell.execute_reply":"2024-01-22T21:00:32.247989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nepochs = wandb.config[\"epochs\"]\nlr = wandb.config[\"lr\"]\nmomentum = wandb.config[\"momentum\"]\n\nloss_f = torch.nn.CrossEntropyLoss()\nif(wandb.config[\"optimizer\"] == \"SGD\"):\n    opt = torch.optim.SGD(params=net.parameters(), lr=lr, momentum=momentum)\nelse:\n    opt = torch.optim.Adam(params=net.parameters(),lr=lr)\n    \n\n\nstart_time = time.time()\nfor epoch in range(epochs):\n    net.train()\n    epoch_train_loss = 0\n    epoch_train_correct = 0\n    for images, labels in train_iter:\n        images = images.to(device)\n        labels = labels.to(device)\n       \n        opt.zero_grad()\n        outputs = net(images)\n        loss = loss_f(outputs, labels)\n        \n        loss.backward()\n        opt.step()\n        \n        epoch_train_loss += loss.item()\n        _, predicted = torch.max(outputs, 1)\n        epoch_train_correct += torch.sum(predicted == labels.data)\n        \n    train_loss = epoch_train_loss / len(train_ds)\n    train_acc = epoch_train_correct / len(train_ds)\n    \n    net.eval()\n    \n    epoch_val_loss = 0\n    epoch_val_correct = 0\n    total_samples = 0\n    with torch.no_grad():\n        for images, labels in valid_iter:\n            images = images.to(device)\n            labels = labels.to(device)\n            outputs = net(images)\n            loss = loss_f(outputs, labels)\n            \n            epoch_val_loss += loss.item()\n\n            _, predicted = torch.max(outputs, 1)\n            epoch_val_correct += (predicted == labels).sum().item()\n            total_samples += labels.size(0)\n    \n    val_loss = epoch_val_loss / total_samples\n    val_acc = epoch_val_correct / total_samples\n    print(total_samples)\n    print(len(valid_ds))\n    print(f\"Epoch {epoch + 1}/{epochs}: \"\n          f\"Train Loss: {train_loss:.4f}, \"\n          f\"Train Accuracy: {train_acc * 100:.2f}, \"\n          f\"Valid Loss: {val_loss:.4f}, \"\n          f\"Val Accuracy: {val_acc * 100:.2f}%\")\n    wandb.log({\"training_loss\": train_loss, \"training_accuracy\": train_acc, \"valid_loss\":val_loss, \"val_accuracy\": val_acc})\n        \nwandb.log({\"total_train_val_time\": time.time()-start_time})\n    ","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:00:40.728698Z","iopub.execute_input":"2024-01-22T21:00:40.729423Z","iopub.status.idle":"2024-01-22T21:18:47.103296Z","shell.execute_reply.started":"2024-01-22T21:00:40.729393Z","shell.execute_reply":"2024-01-22T21:18:47.102414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain one batch of test images\ndataiter = iter(valid_iter)\nimages, labels = next(dataiter)\nfor i in range(78):\n    images, labels = next(dataiter)\n    if(i == 70):\n        break\nimages, labels = images.to(device), labels.to(device)\n\n# get sample outputs\noutput = net(images)\n\n# convert output probabilities to predicted class\n_, preds = torch.max(output, 1)\n\n# move the images to CPU before converting to NumPy\nimages = images.cpu().numpy()\n\n# plot the images in the batch, along with predicted and true labels\nfig = plt.figure(figsize=(25, 4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])\n\n    # use images[idx][0] to get the 2D array\n    ax.imshow(images[idx][0], cmap='gray')\n\n    ax.set_title(\"{} ({})\".format(str(preds[idx].item()), str(labels[idx].item())),\n                 color=(\"green\" if preds[idx] == labels[idx] else \"red\"))\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:19:32.544651Z","iopub.execute_input":"2024-01-22T21:19:32.545019Z","iopub.status.idle":"2024-01-22T21:19:37.156321Z","shell.execute_reply.started":"2024-01-22T21:19:32.544991Z","shell.execute_reply":"2024-01-22T21:19:37.155394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SAVE THE MODEL\nfrom tqdm import tqdm\ntorch.save(net.state_dict(), 'kjelle_cifar10.pt')\npreds = []\n\nnet.eval()\nwith torch.no_grad():\n    for X, y in tqdm(test_iter):\n        X = X.to(device) \n        preds.extend(net(X).argmax(dim=1).type(torch.int32).cpu().numpy()) \nids = list(range(1, len(test_ds)+1)) # Get IDs from the dataset\nids.sort(key=lambda x: str(x)) # Sort the IDs\ndf = pd.DataFrame({'id': ids, 'label': preds}) # Dataframe creation\ndf['label'] = df['label'].apply(lambda x: train_ds.classes[x]) # Set the class predicted in the prediction column,i.e.cat  \n\n#df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:19:43.153901Z","iopub.execute_input":"2024-01-22T21:19:43.154285Z","iopub.status.idle":"2024-01-22T21:22:27.443385Z","shell.execute_reply.started":"2024-01-22T21:19:43.154253Z","shell.execute_reply":"2024-01-22T21:22:27.442413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:22:27.445040Z","iopub.execute_input":"2024-01-22T21:22:27.445347Z","iopub.status.idle":"2024-01-22T21:22:28.881329Z","shell.execute_reply.started":"2024-01-22T21:22:27.445310Z","shell.execute_reply":"2024-01-22T21:22:28.880411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:22:29.382061Z","iopub.execute_input":"2024-01-22T21:22:29.382926Z","iopub.status.idle":"2024-01-22T21:22:31.476069Z","shell.execute_reply.started":"2024-01-22T21:22:29.382893Z","shell.execute_reply":"2024-01-22T21:22:31.475096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.finish()","metadata":{"execution":{"iopub.status.busy":"2024-01-22T21:22:58.226455Z","iopub.execute_input":"2024-01-22T21:22:58.227111Z","iopub.status.idle":"2024-01-22T21:23:07.495070Z","shell.execute_reply.started":"2024-01-22T21:22:58.227079Z","shell.execute_reply":"2024-01-22T21:23:07.494106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}