{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"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":"2023-10-05T14:24:30.862343Z","iopub.execute_input":"2023-10-05T14:24:30.863336Z","iopub.status.idle":"2023-10-05T14:24:31.198869Z","shell.execute_reply.started":"2023-10-05T14:24:30.863298Z","shell.execute_reply":"2023-10-05T14:24:31.197828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:24:39.364306Z","iopub.execute_input":"2023-10-05T14:24:39.364741Z","iopub.status.idle":"2023-10-05T14:24:52.315115Z","shell.execute_reply.started":"2023-10-05T14:24:39.364713Z","shell.execute_reply":"2023-10-05T14:24:52.313809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport copy\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\n\nimport torch\n# neural network modules, nn.Linear, nn.Conv2d, loss functions\nimport torch.nn as nn\n# activation functions\nimport torch.nn.functional as F\n# optimizers\nimport torch.optim as optim\n# transformations \nimport torchvision.transforms as transforms\n\nfrom torchvision import models\nimport torchvision\nfrom torch.utils.data import DataLoader, Dataset, Subset\n\nfrom sklearn.model_selection import train_test_split\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:27:59.064440Z","iopub.execute_input":"2023-10-05T14:27:59.064799Z","iopub.status.idle":"2023-10-05T14:28:03.019327Z","shell.execute_reply.started":"2023-10-05T14:27:59.064769Z","shell.execute_reply":"2023-10-05T14:28:03.018259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from py7zr import unpack_7zarchive\nimport shutil\n\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\n\nshutil.unpack_archive('../input/cifar-10/train.7z')\n\nshutil.unpack_archive('../input/cifar-10/test.7z')\n\nprint('complete')","metadata":{"execution":{"iopub.status.busy":"2023-10-05T14:32:42.868049Z","iopub.execute_input":"2023-10-05T14:32:42.868543Z","iopub.status.idle":"2023-10-05T14:48:22.762790Z","shell.execute_reply.started":"2023-10-05T14:32:42.868514Z","shell.execute_reply":"2023-10-05T14:48:22.761729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hyper parameters\nnum_epochs = 3\nbatch_size = 32\nlr = 0.01","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.630211Z","iopub.execute_input":"2023-10-05T15:23:51.630613Z","iopub.status.idle":"2023-10-05T15:23:51.635806Z","shell.execute_reply.started":"2023-10-05T15:23:51.630587Z","shell.execute_reply":"2023-10-05T15:23:51.634741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# custom dataset class\n\nclass CifarLoader(Dataset):\n  def __init__(self, root_dir, csv_file = None, transform = None):\n\n    # images directory \n    self.root_dir = root_dir\n    # transformations if any\n    self.transform = transform\n\n    if csv_file:\n      # read csv file\n      self.df = pd.read_csv(csv_file)\n\n      # {class: lbl}\n      self.annot_dict = {}\n\n      for i, lbl in enumerate(self.df['label'].unique()):\n        self.annot_dict[lbl] = i\n\n      # {lbl: class}\n      self.annot_dict_reversed = {v:k for k,v in self.annot_dict.items()}\n\n      # add a column with decoded labels\n      self.df['encoded'] = self.df['label'].map(self.annot_dict)\n\n    else: \n      # if no csv file provided -> create a df, fill labels with 0's\n      self.df = pd.DataFrame({\n          'id': list(range(1, len(os.listdir(self.root_dir)) + 1)),\n          'label': np.zeros(len(os.listdir(self.root_dir))),\n          'encoded': np.zeros(len(os.listdir(self.root_dir)))\n      })\n\n\n  def __len__(self):\n    # num of records in the df\n    return len(self.df)\n\n  def __getitem__(self, index):\n    # path to the image\n    img_path = os.path.join(self.root_dir, str(self.df.iloc[index, 0]) + '.png')\n    # read image\n    image = np.array(Image.open(img_path).convert('RGB'))\n    # get label\n    y_label = torch.tensor(int(self.df.iloc[index, 2]))\n\n    # apply transformations\n    if self.transform:\n      image = self.transform(image)\n    \n    return (image, y_label)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.637813Z","iopub.execute_input":"2023-10-05T15:23:51.639011Z","iopub.status.idle":"2023-10-05T15:23:51.650227Z","shell.execute_reply.started":"2023-10-05T15:23:51.638978Z","shell.execute_reply":"2023-10-05T15:23:51.649204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform tensors to normalized range form [0, 1] to [-1, 1]\nmean = np.array([0.5, 0.5, 0.5])\nstd = np.array([0.5, 0.5, 0.5])\n\ntr = transforms.Compose([\n        transforms.ToPILImage(),\n        transforms.Resize((224, 224)),  \n        transforms.ToTensor(),        \n        transforms.Normalize(mean, std)\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.651884Z","iopub.execute_input":"2023-10-05T15:23:51.652730Z","iopub.status.idle":"2023-10-05T15:23:51.668257Z","shell.execute_reply.started":"2023-10-05T15:23:51.652698Z","shell.execute_reply":"2023-10-05T15:23:51.667280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = CifarLoader(csv_file = '../input/cifar-10/trainLabels.csv', \n                      root_dir = './train',\n                      transform = tr)\n\nprint(dataset.df.head())","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.670112Z","iopub.execute_input":"2023-10-05T15:23:51.671014Z","iopub.status.idle":"2023-10-05T15:23:51.703176Z","shell.execute_reply.started":"2023-10-05T15:23:51.670981Z","shell.execute_reply":"2023-10-05T15:23:51.702071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting the data\n\nindexes = list(range(len(dataset)))\n\ntrain_indexes, val_indexes = train_test_split(indexes, test_size=0.2)\n\ndata = {}\ndata['train'] = Subset(dataset, train_indexes)\ndata['val'] = Subset(dataset, val_indexes)\n\ndataloader_train = DataLoader(dataset = data['train'], batch_size = batch_size, shuffle=False)\ndataloader_val = DataLoader(dataset = data['val'], batch_size = batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.704821Z","iopub.execute_input":"2023-10-05T15:23:51.705444Z","iopub.status.idle":"2023-10-05T15:23:51.725426Z","shell.execute_reply.started":"2023-10-05T15:23:51.705408Z","shell.execute_reply":"2023-10-05T15:23:51.724394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_indexes), len(val_indexes))","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.727981Z","iopub.execute_input":"2023-10-05T15:23:51.728343Z","iopub.status.idle":"2023-10-05T15:23:51.741477Z","shell.execute_reply.started":"2023-10-05T15:23:51.728312Z","shell.execute_reply":"2023-10-05T15:23:51.740176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def images_show(images, lbls=None):\n  # show only 4 first images from each batch\n  for i in range(4):\n    plt.subplot(1, 4, i + 1)\n    # convert to numpy\n    img = images[i].numpy().transpose((1, 2, 0))\n    # unnormalize\n    img = img * std + mean \n    img = np.clip(img, a_min=0, a_max=1)\n    plt.imshow(img)\n \n    if lbls != None:\n      # decode labels using reversed annotation dictionary\n      plt.title(dataset.annot_dict_reversed[lbls[i].item()])\n    plt.axis('off')\n  plt.tight_layout(pad=0.5)\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:23:51.742867Z","iopub.execute_input":"2023-10-05T15:23:51.743907Z","iopub.status.idle":"2023-10-05T15:23:51.753033Z","shell.execute_reply.started":"2023-10-05T15:23:51.743874Z","shell.execute_reply":"2023-10-05T15:23:51.752081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataiter = iter(dataloader_train)\n# images, labels = dataiter.next()\n\n# print(f'Shape: {images.shape}')\n# print(f'Max: {torch.max(images[0]):.2f}')\n# print(f'Min: {torch.min(images[0]):.2f}')\n\n# for i in range(3):\n#   images, labels = dataiter.next()\n#   images_show(images, labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:28:38.393074Z","iopub.execute_input":"2023-10-05T15:28:38.394146Z","iopub.status.idle":"2023-10-05T15:28:38.399424Z","shell.execute_reply.started":"2023-10-05T15:28:38.394100Z","shell.execute_reply":"2023-10-05T15:28:38.398303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for images, labels in dataloader_train:\n#     print(f'Shape: {images.shape}')\n#     print(f'Max: {torch.max(images[0]):.2f}')\n#     print(f'Min: {torch.min(images[0]):.2f}')\n    \n#     for i in range(3):\n#         images_show(images, labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:28:06.305795Z","iopub.execute_input":"2023-10-05T15:28:06.306126Z","iopub.status.idle":"2023-10-05T15:28:06.314244Z","shell.execute_reply.started":"2023-10-05T15:28:06.306100Z","shell.execute_reply":"2023-10-05T15:28:06.312989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Displaying images from the first 3 batches\n\n# Create an iterator from the data loader\ndataiter = iter(dataloader_train)\n\n# Iterate through the first three batches\nfor i in range(3):\n    images, labels = next(dataiter)\n    print(f'Batch {i+1}:')\n    print(f'Shape: {images.shape}')\n    print(f'Max: {torch.max(images[0]):.2f}')\n    print(f'Min: {torch.min(images[0]):.2f}')\n    \n    # Display images and labels\n    images_show(images, labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:28:09.364969Z","iopub.execute_input":"2023-10-05T15:28:09.365342Z","iopub.status.idle":"2023-10-05T15:28:10.809991Z","shell.execute_reply.started":"2023-10-05T15:28:09.365314Z","shell.execute_reply":"2023-10-05T15:28:10.809057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# weights = torchvision.models.ResNet152_Weights.DEFAULT\n# model = torchvision.models.resNet152(weights=weights)\n\nmodel = models.resnet152(pretrained=True)\n#model","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:46:21.943764Z","iopub.execute_input":"2023-10-05T15:46:21.944118Z","iopub.status.idle":"2023-10-05T15:46:24.319706Z","shell.execute_reply.started":"2023-10-05T15:46:21.944091Z","shell.execute_reply":"2023-10-05T15:46:24.318759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#modify the fully connected classifier layer\nnum_ftrs = model.fc.in_features\n\nmodel.fc = nn.Sequential(\n                        nn.Linear(num_ftrs, 256),  \n                        nn.ReLU(), \n                        nn.Dropout(0.3),\n                        nn.Linear(256, 10))\n\n# copy weights for futher retraining on full train dataset\nmodel_wts = copy.deepcopy(model.state_dict())\n\n# move model to a device\nmodel = model.to(device)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:48:30.082491Z","iopub.execute_input":"2023-10-05T15:48:30.082893Z","iopub.status.idle":"2023-10-05T15:48:33.720579Z","shell.execute_reply.started":"2023-10-05T15:48:30.082863Z","shell.execute_reply":"2023-10-05T15:48:33.719535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:48:41.825050Z","iopub.execute_input":"2023-10-05T15:48:41.826188Z","iopub.status.idle":"2023-10-05T15:48:41.834429Z","shell.execute_reply.started":"2023-10-05T15:48:41.826143Z","shell.execute_reply":"2023-10-05T15:48:41.833245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loss\ncriterion = nn.CrossEntropyLoss()\n\n# all parameters are being optimized\noptimizer = optim.SGD(model.parameters(), lr=lr)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:49:17.196363Z","iopub.execute_input":"2023-10-05T15:49:17.196686Z","iopub.status.idle":"2023-10-05T15:49:17.203965Z","shell.execute_reply.started":"2023-10-05T15:49:17.196661Z","shell.execute_reply":"2023-10-05T15:49:17.202745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_wts","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:50:18.627585Z","iopub.execute_input":"2023-10-05T15:50:18.627933Z","iopub.status.idle":"2023-10-05T15:50:18.632666Z","shell.execute_reply.started":"2023-10-05T15:50:18.627907Z","shell.execute_reply":"2023-10-05T15:50:18.631745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# num_ftrs","metadata":{"execution":{"iopub.status.busy":"2023-10-05T15:51:30.842683Z","iopub.execute_input":"2023-10-05T15:51:30.843037Z","iopub.status.idle":"2023-10-05T15:51:30.847100Z","shell.execute_reply.started":"2023-10-05T15:51:30.843010Z","shell.execute_reply":"2023-10-05T15:51:30.846084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training and evaluation\n\ndef train(model, criterion, optimizer, num_epochs, \n          dataloader_train, dataloader_val = None):\n\n  history = {\n        'loss': [],\n        'accuracy': [],\n        'val_loss': [],\n        'val_accuracy': [],\n        'best train acc': (0, 0)\n        }\n\n  best_model_wts = copy.deepcopy(model.state_dict())\n  best_acc = 0.0\n\n\n  for epoch in range(num_epochs):\n    print('Epoch {}/{}'.format(epoch + 1, num_epochs))\n\n    # set model to the training mode\n    model.train()\n\n    n_samples = 0\n    correct_train = 0\n    epoch_loss_train = 0\n    epoch_loss_val = 0\n\n\n    for i, (images, labels) in enumerate(dataloader_train):\n      # origin shape: [4, 3, 32, 32] = 4, 3, 1024\n    \n      images = images.to(device)\n      labels = labels.to(device)\n\n      # forward pass (pred)\n      pred = model(images)\n\n      # loss\n      loss = criterion(pred, labels)\n\n      # mean loss * num samples in batch\n      epoch_loss_train += labels.shape[0] * loss.item()\n\n      # empty gradients\n      optimizer.zero_grad()\n\n      # gradient (backpropagation)\n      loss.backward()\n\n      # update weights\n      optimizer.step()\n\n      # values, indexes\n      value, predicted = torch.max(pred, 1)\n\n      # += batch_size\n      n_samples += labels.shape[0]\n      # num of correctly predicted in this batch\n      correct_train += (predicted==labels).sum().item()\n\n    # train acc per epoch\n    train_acc = 100 * correct_train / n_samples\n    # train loss per epoch\n    epoch_loss_train = epoch_loss_train / n_samples\n\n    print(f'Train accuracy: {train_acc:.2f}, loss: {epoch_loss_train:.2f}')\n    #print(f'Best accuracy: {best_acc:.2f}')\n\n    history['accuracy'].append(train_acc)\n    history['loss'].append(epoch_loss_train)\n\n    # find best accuracy on training data\n    if train_acc > best_acc:\n\n      #print('*New best accuracy*')\n      best_acc = train_acc\n\n      # copy current model weights\n      best_model_wts = copy.deepcopy(model.state_dict())\n      history['best train acc'] = (epoch, best_acc)\n\n\n\n    if dataloader_val: # this section is required to print validation loss and accuracy at the end of each epoch\n\n      # evaluation mode\n      model.eval()\n\n      with torch.no_grad():\n        correct_val = 0\n        n_samples = 0\n\n        for images, labels in dataloader_val: \n          images = images.to(device)\n          labels = labels.to(device)\n\n          outputs = model(images)\n\n          loss = criterion(outputs, labels)\n          epoch_loss_val += labels.shape[0] * loss.item()\n\n          # value, index\n          _, pred = torch.max(outputs, 1)\n          n_samples += labels.shape[0]\n          correct_val += (pred==labels).sum().item()\n\n        # calculate validation accuracy and loss for each epoch\n        val_acc = 100 * correct_val / len(data['val'])\n        epoch_loss_val = epoch_loss_val / len(data['val'])\n\n        print(f'Val accuracy: {val_acc:.2f}, loss: {epoch_loss_val:.2f}')\n\n        history['val_accuracy'].append(val_acc)\n        history['val_loss'].append(epoch_loss_val)\n      \n    print('=' * 80)\n\n\n  print('Best train acc: {:2f}'.format(best_acc))\n\n  # load best weights\n  model.load_state_dict(best_model_wts)\n\n  return model, history\n\n# For an image classification task, pred might contain a probability distribution over classes. Each element of the distribution corresponds to the likelihood of the input image belonging to a particular class. Typically, you would use the class with the highest probability as the predicted class label.\n# Training:\n# During training, you use a labeled dataset to optimize your model's parameters (e.g., weights and biases) to minimize a loss function.\n# The loss function measures the error between the model's predictions and the ground truth labels.\n# An optimizer, such as stochastic gradient descent (SGD) or Adam, is used to adjust the model's parameters based on the gradients of the loss function with respect to those parameters.\n# The goal of training is to make the model learn from the data and improve its performance over time.\n# Evaluation:\n# During evaluation (inference), you use a trained model to make predictions on new, unseen data.\n# You don't need to compute or optimize any parameters during evaluation because the model's parameters are fixed after training.\n# Instead, you assess the model's performance by comparing its predictions to the true labels or ground truth.\n# Common evaluation metrics include accuracy, precision, recall, F1-score, and others, depending on the specific task.","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:04:48.537383Z","iopub.execute_input":"2023-10-05T16:04:48.537780Z","iopub.status.idle":"2023-10-05T16:04:48.550055Z","shell.execute_reply.started":"2023-10-05T16:04:48.537744Z","shell.execute_reply":"2023-10-05T16:04:48.549165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, history = train(model, criterion, optimizer, num_epochs, \n                       dataloader_train, dataloader_val)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:05:03.752836Z","iopub.execute_input":"2023-10-05T16:05:03.753195Z","iopub.status.idle":"2023-10-05T16:35:01.121021Z","shell.execute_reply.started":"2023-10-05T16:05:03.753168Z","shell.execute_reply":"2023-10-05T16:35:01.119991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history):\n   \n    loss = history['loss']\n    accuracy = history['accuracy']\n    val_loss = history['val_loss']\n    val_accuracy = history['val_accuracy']\n    x = range(len(loss))\n\n    plt.figure(figsize=(12, 5))\n    plt.subplot(1, 2, 1)\n    plt.plot(x, accuracy, label='Training acc', color='#03045e', linewidth=2)\n    if len(val_loss) != 0:\n      plt.plot(x, val_accuracy, label='Validation acc', color='#48cae4', linewidth=2)\n    plt.plot(history['best train acc'][0], \n             history['best train acc'][1], \n             'bo', label='Best train acc', markersize=7, color='black')\n    plt.title('Accuracy')\n    plt.grid(True)\n    plt.legend()\n    \n    \n    plt.subplot(1, 2, 2)\n    plt.plot(x, loss, label='Training loss', color='#03045e', linewidth=2)\n    if len(val_loss) != 0:\n      plt.plot(x, val_loss, label='Validation loss', color='#48cae4', linewidth=2)\n    plt.title('Loss')\n    plt.grid(True)\n    plt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:40:03.705562Z","iopub.execute_input":"2023-10-05T16:40:03.705926Z","iopub.status.idle":"2023-10-05T16:40:03.714737Z","shell.execute_reply.started":"2023-10-05T16:40:03.705897Z","shell.execute_reply":"2023-10-05T16:40:03.713480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:40:07.173820Z","iopub.execute_input":"2023-10-05T16:40:07.174205Z","iopub.status.idle":"2023-10-05T16:40:07.821256Z","shell.execute_reply.started":"2023-10-05T16:40:07.174162Z","shell.execute_reply":"2023-10-05T16:40:07.820288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We use evaluation for inference. \ndef evaluate(model, dataloader): # we are defining a separate evaluate function so as to print the accuracy of any dataset in the future\n\n  with torch.no_grad():\n    model.eval()\n    n_correct = 0\n    n_samples = 0\n\n    for images, labels in dataloader: \n      images = images.to(device)\n      labels = labels.to(device)\n\n      outputs = model(images)\n\n      # value, index\n      v, pred = torch.max(outputs, 1)\n\n      n_samples += labels.shape[0]\n      n_correct += (pred==labels).sum().item()\n\n    acc = 100.0 * n_correct / n_samples\n    print(acc)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:40:16.063433Z","iopub.execute_input":"2023-10-05T16:40:16.063771Z","iopub.status.idle":"2023-10-05T16:40:16.070121Z","shell.execute_reply.started":"2023-10-05T16:40:16.063744Z","shell.execute_reply":"2023-10-05T16:40:16.069122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluate(model, dataloader_val)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:40:32.166083Z","iopub.execute_input":"2023-10-05T16:40:32.166715Z","iopub.status.idle":"2023-10-05T16:41:27.756398Z","shell.execute_reply.started":"2023-10-05T16:40:32.166668Z","shell.execute_reply":"2023-10-05T16:41:27.755304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we have trained the model after splitting it into train and val dataset\n# now the model is trained without splitting\n# ie on the complete dataset\n\n\ndataloader = DataLoader(dataset = dataset, batch_size = batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T16:57:32.574473Z","iopub.execute_input":"2023-10-05T16:57:32.574828Z","iopub.status.idle":"2023-10-05T16:57:32.580274Z","shell.execute_reply.started":"2023-10-05T16:57:32.574802Z","shell.execute_reply":"2023-10-05T16:57:32.579181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#defining test data and dataloader for test data\ndataset_test = CifarLoader(root_dir = './test',\n                      transform = tr)\n\ndataloader_test = DataLoader(dataset = dataset_test, \n                             batch_size = batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T17:06:20.502770Z","iopub.execute_input":"2023-10-05T17:06:20.503139Z","iopub.status.idle":"2023-10-05T17:06:21.044891Z","shell.execute_reply.started":"2023-10-05T17:06:20.503113Z","shell.execute_reply":"2023-10-05T17:06:21.043809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The load_state_dict method is used to transfer the values from model_wts to the corresponding components of the model. \n# This operation effectively initializes the model with the pretrained or saved weights.\n\nmodel.load_state_dict(model_wts)\n\n# loss\ncriterion = nn.CrossEntropyLoss()\n\n# all parameters are being optimized\noptimizer = optim.SGD(model.parameters(), lr=lr)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T17:06:23.337043Z","iopub.execute_input":"2023-10-05T17:06:23.337395Z","iopub.status.idle":"2023-10-05T17:06:23.445876Z","shell.execute_reply.started":"2023-10-05T17:06:23.337368Z","shell.execute_reply":"2023-10-05T17:06:23.444801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, history = train(model, criterion, optimizer, 3, dataloader)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T17:06:24.862841Z","iopub.execute_input":"2023-10-05T17:06:24.863185Z","iopub.status.idle":"2023-10-05T17:40:14.529676Z","shell.execute_reply.started":"2023-10-05T17:06:24.863158Z","shell.execute_reply":"2023-10-05T17:40:14.528598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(dataset_test))","metadata":{"execution":{"iopub.status.busy":"2023-10-05T17:41:48.179850Z","iopub.execute_input":"2023-10-05T17:41:48.180302Z","iopub.status.idle":"2023-10-05T17:41:48.186334Z","shell.execute_reply.started":"2023-10-05T17:41:48.180269Z","shell.execute_reply":"2023-10-05T17:41:48.185040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prediction.\n\npredictions = np.array([])\n\nwith torch.no_grad():\n    model.eval()\n\n    for images, _ in dataloader_test:\n      images = images.to(device)\n\n      outputs = model(images)\n\n      # value, index\n      v, pred = torch.max(outputs, 1)\n\n      pred = pred.cpu().numpy()\n\n      predictions = np.concatenate((predictions, pred), axis = None)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T17:41:50.995532Z","iopub.execute_input":"2023-10-05T17:41:50.995886Z","iopub.status.idle":"2023-10-05T18:09:35.636179Z","shell.execute_reply.started":"2023-10-05T17:41:50.995859Z","shell.execute_reply":"2023-10-05T18:09:35.635127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test.df['encoded'] = predictions.astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T18:13:18.867987Z","iopub.execute_input":"2023-10-05T18:13:18.868359Z","iopub.status.idle":"2023-10-05T18:13:18.908396Z","shell.execute_reply.started":"2023-10-05T18:13:18.868332Z","shell.execute_reply":"2023-10-05T18:13:18.907103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test.df['label'] = dataset_test.df['encoded'].map(dataset.annot_dict_reversed)\n# self.annot_dict_reversed: This dictionary is essentially the reverse of self.annot_dict. It maps the numerical values back to the original class labels. \n# This reverse mapping can be useful when you want to interpret model predictions or convert numerical outputs back to their corresponding class labels.","metadata":{"execution":{"iopub.status.busy":"2023-10-05T18:13:21.445668Z","iopub.execute_input":"2023-10-05T18:13:21.446044Z","iopub.status.idle":"2023-10-05T18:13:21.458581Z","shell.execute_reply.started":"2023-10-05T18:13:21.446015Z","shell.execute_reply":"2023-10-05T18:13:21.457597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test.df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-05T18:13:24.115524Z","iopub.execute_input":"2023-10-05T18:13:24.115892Z","iopub.status.idle":"2023-10-05T18:13:24.129374Z","shell.execute_reply.started":"2023-10-05T18:13:24.115863Z","shell.execute_reply":"2023-10-05T18:13:24.128282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create an iterator from the data loader\ndataiter = iter(dataloader_test)\n\n# Iterate through the first three batches\nfor i in range(3):\n    images, labels = next(dataiter)\n    images_show(images, labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-05T18:13:29.125685Z","iopub.execute_input":"2023-10-05T18:13:29.126012Z","iopub.status.idle":"2023-10-05T18:13:30.609138Z","shell.execute_reply.started":"2023-10-05T18:13:29.125986Z","shell.execute_reply":"2023-10-05T18:13:30.608255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test.df.drop(columns ='encoded', inplace=True)\ndataset_test.df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-05T18:13:36.775722Z","iopub.execute_input":"2023-10-05T18:13:36.776078Z","iopub.status.idle":"2023-10-05T18:13:37.128780Z","shell.execute_reply.started":"2023-10-05T18:13:36.776052Z","shell.execute_reply":"2023-10-05T18:13:37.127615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %rm -rf ./train\n# %rm -rf ./test\n\n#permanently delete the specified directories and all files and subdirectories contained within them.","metadata":{},"execution_count":null,"outputs":[]}]}