{"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":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","scrolled":true,"execution":{"iopub.status.busy":"2024-08-11T22:18:45.087277Z","iopub.execute_input":"2024-08-11T22:18:45.087608Z","iopub.status.idle":"2024-08-11T22:19:10.193437Z","shell.execute_reply.started":"2024-08-11T22:18:45.087583Z","shell.execute_reply":"2024-08-11T22:19:10.192464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing libraries","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.optim as optim\nimport torch.nn as nn\nfrom torchvision import datasets, transforms\nimport cv2\nfrom PIL import Image\nimport pandas as pd\nimport torchvision.models as models\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom torch.utils.data import DataLoader,Dataset\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:10.195016Z","iopub.execute_input":"2024-08-11T22:19:10.195496Z","iopub.status.idle":"2024-08-11T22:19:16.946470Z","shell.execute_reply.started":"2024-08-11T22:19:10.195469Z","shell.execute_reply":"2024-08-11T22:19:16.945640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using GPU for faster training\ndevice=torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nuse_cuda=torch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:16.947566Z","iopub.execute_input":"2024-08-11T22:19:16.947998Z","iopub.status.idle":"2024-08-11T22:19:17.000472Z","shell.execute_reply.started":"2024-08-11T22:19:16.947972Z","shell.execute_reply":"2024-08-11T22:19:16.999347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading Data","metadata":{}},{"cell_type":"code","source":"#Resizing images to fit Resnet18 architecture\n#applying horizontal flip and noramlization\ntrain_trasforms=transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.RandomHorizontalFlip(p=0.2),\n    transforms.RandomVerticalFlip(p=0.2),\n    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),  # Apply color jitter\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.003275Z","iopub.execute_input":"2024-08-11T22:19:17.004042Z","iopub.status.idle":"2024-08-11T22:19:17.010334Z","shell.execute_reply.started":"2024-08-11T22:19:17.004015Z","shell.execute_reply":"2024-08-11T22:19:17.009377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Images labels\nlabels=pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\n\n#encoding labels to numbers\nencoder=LabelEncoder()\nlabels['breed']=encoder.fit_transform(labels['breed'])","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.011518Z","iopub.execute_input":"2024-08-11T22:19:17.012052Z","iopub.status.idle":"2024-08-11T22:19:17.051304Z","shell.execute_reply.started":"2024-08-11T22:19:17.012027Z","shell.execute_reply":"2024-08-11T22:19:17.050530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels['breed'].unique())","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.052666Z","iopub.execute_input":"2024-08-11T22:19:17.053011Z","iopub.status.idle":"2024-08-11T22:19:17.062540Z","shell.execute_reply.started":"2024-08-11T22:19:17.052979Z","shell.execute_reply":"2024-08-11T22:19:17.061450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#taking a sratified random sample of data(to speed up training)\nX_train,X_test,y_train,y_test=train_test_split(labels,labels,test_size=0.2,random_state=42,stratify=labels['breed'])","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.063698Z","iopub.execute_input":"2024-08-11T22:19:17.063973Z","iopub.status.idle":"2024-08-11T22:19:17.087337Z","shell.execute_reply.started":"2024-08-11T22:19:17.063951Z","shell.execute_reply":"2024-08-11T22:19:17.086307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=pd.DataFrame(X_train)\ny_train=pd.DataFrame(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.088896Z","iopub.execute_input":"2024-08-11T22:19:17.089288Z","iopub.status.idle":"2024-08-11T22:19:17.094376Z","shell.execute_reply.started":"2024-08-11T22:19:17.089252Z","shell.execute_reply":"2024-08-11T22:19:17.093317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#splitting data into train and test\n#X_train,X_test,y_train,y_test=train_test_split(X_train,y_train,test_size=0.2,random_state=42,stratify=y_train['breed'])","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.095667Z","iopub.execute_input":"2024-08-11T22:19:17.095932Z","iopub.status.idle":"2024-08-11T22:19:17.102114Z","shell.execute_reply.started":"2024-08-11T22:19:17.095911Z","shell.execute_reply":"2024-08-11T22:19:17.101069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#custom class to read and transform the data\nclass Dog_Breed_Dataset(Dataset):\n    def __init__ (self, root_dir,transform=None,labels=None):\n        self.root_dir=root_dir\n        self.transforms=transform\n        self.labels=labels\n    def __len__(self):\n        return len(self.labels)\n    def __getitem__(self,idx):\n        img_name=self.labels[idx]['id'] #read images names from csv\n        img_path=os.path.join(self.root_dir,img_name+'.jpg') # construct image path from the csv and directory\n        image=Image.open(img_path)\n        label=self.labels[idx]['breed']\n        if self.transforms: # if there is a transformation , apply it to data and then return image and label\n            image=self.transforms(image)\n        return image,label","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.106465Z","iopub.execute_input":"2024-08-11T22:19:17.107227Z","iopub.status.idle":"2024-08-11T22:19:17.115163Z","shell.execute_reply.started":"2024-08-11T22:19:17.107190Z","shell.execute_reply":"2024-08-11T22:19:17.114265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset=Dog_Breed_Dataset('/kaggle/input/dog-breed-identification/train',\n                               transform=train_trasforms,\n                               labels=X_train.to_dict('records'))\n\n\n#load data to batches and shuffle\ntrain_dataloader=DataLoader(train_dataset, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.116280Z","iopub.execute_input":"2024-08-11T22:19:17.116534Z","iopub.status.idle":"2024-08-11T22:19:17.146508Z","shell.execute_reply.started":"2024-08-11T22:19:17.116506Z","shell.execute_reply":"2024-08-11T22:19:17.145585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test transoformation\ntest_trasforms=transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.147654Z","iopub.execute_input":"2024-08-11T22:19:17.147985Z","iopub.status.idle":"2024-08-11T22:19:17.153637Z","shell.execute_reply.started":"2024-08-11T22:19:17.147955Z","shell.execute_reply":"2024-08-11T22:19:17.152505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset=Dog_Breed_Dataset('/kaggle/input/dog-breed-identification/train',\n                               transform=test_trasforms,\n                               labels=X_test.to_dict('records'))\n\n\n\n#load data to batches and shuffle\ntest_dataloader=DataLoader(test_dataset, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.154831Z","iopub.execute_input":"2024-08-11T22:19:17.155255Z","iopub.status.idle":"2024-08-11T22:19:17.166405Z","shell.execute_reply.started":"2024-08-11T22:19:17.155232Z","shell.execute_reply":"2024-08-11T22:19:17.165660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#number of images in train and test (internal validation)\nprint(f\"Number of images in train = {len(train_dataset)}\\n\")\nprint(f\"Number of images in test = {len(test_dataset)}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.167442Z","iopub.execute_input":"2024-08-11T22:19:17.167736Z","iopub.status.idle":"2024-08-11T22:19:17.177074Z","shell.execute_reply.started":"2024-08-11T22:19:17.167712Z","shell.execute_reply":"2024-08-11T22:19:17.176097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning","metadata":{}},{"cell_type":"code","source":"#using resenet18\nnet = models.resnet18(pretrained=True)\n#number of classes\nnum_classes=len(labels['breed'].unique())\nnet.fc = nn.Sequential(\n    nn.Dropout(0.4),  # Dropout with a probability of 0.4\n    nn.Linear(net.fc.in_features, 512),\n    nn.BatchNorm1d(512),\n    nn.LeakyReLU(negative_slope=0.01),\n    nn.Linear(512,num_classes))\nnet = net.cuda() if use_cuda else net","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:17.178314Z","iopub.execute_input":"2024-08-11T22:19:17.178597Z","iopub.status.idle":"2024-08-11T22:19:18.114106Z","shell.execute_reply.started":"2024-08-11T22:19:17.178573Z","shell.execute_reply":"2024-08-11T22:19:18.113326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optimizer and loss\ncriterion=nn.CrossEntropyLoss()\nlearning_rate=0.0001\noptimizer = torch.optim.AdamW(net.parameters(), lr=learning_rate)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:18.115219Z","iopub.execute_input":"2024-08-11T22:19:18.115475Z","iopub.status.idle":"2024-08-11T22:19:18.121196Z","shell.execute_reply.started":"2024-08-11T22:19:18.115452Z","shell.execute_reply":"2024-08-11T22:19:18.120013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.optim.lr_scheduler import StepLR\n\nscheduler = StepLR(optimizer, step_size=7, gamma=0.1)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:18.122419Z","iopub.execute_input":"2024-08-11T22:19:18.122758Z","iopub.status.idle":"2024-08-11T22:19:18.136680Z","shell.execute_reply.started":"2024-08-11T22:19:18.122725Z","shell.execute_reply":"2024-08-11T22:19:18.135759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntorch.manual_seed(42)# fixing the seed\n\ntrain_losses = [] #to save train losses\ntest_losses = [] #to save test losses\n\nepochs = 20\nfor epoch in range(epochs):\n    # Training\n    net.train() # in training mode\n    train_loss = 0.0\n    for features, target in train_dataloader: #iterate over the train dataloader\n        optimizer.zero_grad() #to set gradient to zero at the begnining of each epoch\n        features = features.to(device).double()\n        target = target.to(device)\n        outputs = net(features.float()) #calculating outputs from the features\n        loss = criterion(outputs, target) #calculate loss\n        loss.backward() \n        optimizer.step()\n        train_loss += loss.item() * features.size(0)\n    train_loss /= len(train_dataloader.dataset)\n    train_losses.append(train_loss)\n\n    # Evaluation\n    net.eval()\n    test_loss = 0.0\n    correct = 0\n    total = 0\n    with torch.no_grad():\n        for features_t, target_t in test_dataloader:\n            features_t = features_t.to(device).double()\n            target_t = target_t.to(device)\n            outputs_t = net(features_t.float())\n            loss_t = criterion(outputs_t, target_t)\n            test_loss += loss_t.item() * features_t.size(0)  # Use features_t.size(0) for test_loss accumulation\n            _, pred_t = torch.max(outputs_t, 1)\n            total += target_t.size(0)  # Use target_t.size(0) to update total\n            correct += (pred_t == target_t).sum().item()\n    test_loss /= len(test_dataloader.dataset)\n    test_losses.append(test_loss)\n    scheduler.step()\n    \n    if epoch % 1 == 0:\n        print(f'Epoch {epoch+1}/{epochs}, Train Loss: {train_loss:.4f}, Test Loss: {test_loss:.4f}, Test Accuracy: {(100 * correct / total):.2f}%')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T22:19:24.938269Z","iopub.execute_input":"2024-08-11T22:19:24.939121Z","iopub.status.idle":"2024-08-11T23:00:34.811652Z","shell.execute_reply.started":"2024-08-11T22:19:24.939091Z","shell.execute_reply":"2024-08-11T23:00:34.810660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#custom class to read and transform the test data \nclass Dog_Breed_Dataset_test(Dataset):\n    def __init__ (self, root_dir,transform=None):\n        self.root_dir=root_dir # directory of images\n        self.transforms=transform #transformation\n        self.image_names=[f.split('.')[0] for f in os.listdir(root_dir)] #image name is the first element after splitting on \".\"\n        #self.labels=labels\n    def __len__(self):\n        return len(self.image_names)\n    def __getitem__(self,idx):\n        img_name=self.image_names[idx] #read images names from csv\n        img_path=os.path.join(self.root_dir,img_name+'.jpg') # construct image path from the csv and directory\n        image=Image.open(img_path)\n    \n        if self.transforms: # if there is a transformation , apply it to data and then return image and its name\n            image=self.transforms(image)\n        return image,img_name","metadata":{"execution":{"iopub.status.busy":"2024-08-11T23:00:53.671198Z","iopub.execute_input":"2024-08-11T23:00:53.671948Z","iopub.status.idle":"2024-08-11T23:00:53.679280Z","shell.execute_reply.started":"2024-08-11T23:00:53.671918Z","shell.execute_reply":"2024-08-11T23:00:53.678195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kaggle_test=Dog_Breed_Dataset_test(root_dir='/kaggle/input/dog-breed-identification/test',\n                                  transform=test_trasforms)\n\n#kaggla test data loader\nkaggle_test_dataloader=DataLoader(kaggle_test,batch_size=32,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T23:00:55.685060Z","iopub.execute_input":"2024-08-11T23:00:55.685877Z","iopub.status.idle":"2024-08-11T23:00:55.699395Z","shell.execute_reply.started":"2024-08-11T23:00:55.685848Z","shell.execute_reply":"2024-08-11T23:00:55.698514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make sure the model is in evaluation mode\nnet.eval()\n\n# List to store all predictions\nall_predictions = []\n\n# Disable gradient calculations\nwith torch.no_grad():\n    for features_t, target_t in test_dataloader:\n        features_t = features_t.to(device).double()  # Move features to the appropriate device and ensure they are double type\n        target_t = target_t.to(device)  # Move targets to the appropriate device\n\n        # Get model predictions\n        outputs_t = net(features_t.float())  # Get the outputs from the model\n\n        # Get the predicted classes\n        _, pred_t = torch.max(outputs_t, 1)\n\n        # Store predictions\n        all_predictions.extend(pred_t.cpu().numpy())  # Move predictions to CPU and convert to numpy array\n\n# Now all_predictions contains the predicted class labels for the entire test dataset\nprint(all_predictions)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T23:01:10.745449Z","iopub.execute_input":"2024-08-11T23:01:10.746148Z","iopub.status.idle":"2024-08-11T23:01:22.234217Z","shell.execute_reply.started":"2024-08-11T23:01:10.746109Z","shell.execute_reply":"2024-08-11T23:01:22.233183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_breeds = [\n    \"affenpinscher\", \"afghan_hound\", \"african_hunting_dog\", \"airedale\", \"american_staffordshire_terrier\",\n    \"appenzeller\", \"australian_terrier\", \"basenji\", \"basset\", \"beagle\", \"bedlington_terrier\", \"bernese_mountain_dog\",\n    \"black-and-tan_coonhound\", \"blenheim_spaniel\", \"bloodhound\", \"bluetick\", \"border_collie\", \"border_terrier\",\n    \"borzoi\", \"boston_bull\", \"bouvier_des_flandres\", \"boxer\", \"brabancon_griffon\", \"briard\", \"brittany_spaniel\",\n    \"bull_mastiff\", \"cairn\", \"cardigan\", \"chesapeake_bay_retriever\", \"chihuahua\", \"chow\", \"clumber\", \"cocker_spaniel\",\n    \"collie\", \"curly-coated_retriever\", \"dandie_dinmont\", \"dhole\", \"dingo\", \"doberman\", \"english_foxhound\",\n    \"english_setter\", \"english_springer\", \"entlebucher\", \"eskimo_dog\", \"flat-coated_retriever\", \"french_bulldog\",\n    \"german_shepherd\", \"german_short-haired_pointer\", \"giant_schnauzer\", \"golden_retriever\", \"gordon_setter\",\n    \"great_dane\", \"great_pyrenees\", \"greater_swiss_mountain_dog\", \"groenendael\", \"ibizan_hound\", \"irish_setter\",\n    \"irish_terrier\", \"irish_water_spaniel\", \"irish_wolfhound\", \"italian_greyhound\", \"japanese_spaniel\", \"keeshond\",\n    \"kelpie\", \"kerry_blue_terrier\", \"komondor\", \"kuvasz\", \"labrador_retriever\", \"lakeland_terrier\", \"leonberg\",\n    \"lhasa\", \"malamute\", \"malinois\", \"maltese_dog\", \"mexican_hairless\", \"miniature_pinscher\", \"miniature_poodle\",\n    \"miniature_schnauzer\", \"newfoundland\", \"norfolk_terrier\", \"norwegian_elkhound\", \"norwich_terrier\",\n    \"old_english_sheepdog\", \"otterhound\", \"papillon\", \"pekinese\", \"pembroke\", \"pomeranian\", \"pug\", \"redbone\",\n    \"rhodesian_ridgeback\", \"rottweiler\", \"saint_bernard\", \"saluki\", \"samoyed\", \"schipperke\", \"scotch_terrier\",\n    \"scottish_deerhound\", \"sealyham_terrier\", \"shetland_sheepdog\", \"shih-tzu\", \"siberian_husky\", \"silky_terrier\",\n    \"soft-coated_wheaten_terrier\", \"staffordshire_bullterrier\", \"standard_poodle\", \"standard_schnauzer\",\n    \"sussex_spaniel\", \"tibetan_mastiff\", \"tibetan_terrier\", \"toy_poodle\", \"toy_terrier\", \"vizsla\", \"walker_hound\",\n    \"weimaraner\", \"welsh_springer_spaniel\", \"west_highland_white_terrier\", \"whippet\", \"wire-haired_fox_terrier\",\n    \"yorkshire_terrier\"\n]\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T23:01:39.590561Z","iopub.execute_input":"2024-08-11T23:01:39.590933Z","iopub.status.idle":"2024-08-11T23:01:39.600430Z","shell.execute_reply.started":"2024-08-11T23:01:39.590905Z","shell.execute_reply":"2024-08-11T23:01:39.599365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submitting Predictions","metadata":{}},{"cell_type":"code","source":"# import torch\nimport torch.nn.functional as F\n\n# Make sure the model is in evaluation mode\nnet.eval()\n\n# List to store all predictions\nall_predictions = []\n\n# Disable gradient calculations\nwith torch.no_grad():\n    for features_t , img_names in kaggle_test_dataloader:\n        features_t = features_t.to(device).double()  # Move features to the appropriate device and ensure they are double type\n\n        # Get model predictions\n        outputs_t = net(features_t.float())  # Get the raw outputs from the model\n        \n        # Convert raw outputs to probabilities using softmax\n        probabilities = F.softmax(outputs_t, dim=1)\n        \n        # Append probabilities to the list of all predictions\n       # Extract probabilities for each class for each image\n        for i in range(len(img_names)):\n            probabilities_for_image = probabilities[i].cpu().numpy()\n            for class_idx, prob in enumerate(probabilities_for_image):\n                class_name = dog_breeds[class_idx]  # Assuming class_names is a list containing the class names\n                all_predictions.append({'class_name': class_name, 'probability': prob, 'image_name': img_names[i]})\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T23:01:44.516769Z","iopub.execute_input":"2024-08-11T23:01:44.517379Z","iopub.status.idle":"2024-08-11T23:04:04.100483Z","shell.execute_reply.started":"2024-08-11T23:01:44.517349Z","shell.execute_reply":"2024-08-11T23:04:04.099671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_predictions","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-08-11T23:04:04.102021Z","iopub.execute_input":"2024-08-11T23:04:04.102319Z","iopub.status.idle":"2024-08-11T23:04:04.237687Z","shell.execute_reply.started":"2024-08-11T23:04:04.102295Z","shell.execute_reply":"2024-08-11T23:04:04.236748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preparing submissions file\nall_predictions_df = pd.DataFrame.from_records(all_predictions)\nsubmissions = all_predictions_df.pivot(index='image_name', columns='class_name', values='probability')\n\n# Reset index to make 'image_name' a regular column instead of index\nsubmissions.reset_index(inplace=True)\n\n#writing the csv file for predictions\nsubmissions=submissions.rename(columns={'image_name':'id'})\nsubmissions.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T23:04:04.238705Z","iopub.execute_input":"2024-08-11T23:04:04.238962Z","iopub.status.idle":"2024-08-11T23:04:08.150068Z","shell.execute_reply.started":"2024-08-11T23:04:04.238940Z","shell.execute_reply":"2024-08-11T23:04:08.149187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}