{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"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-10-19T19:01:40.504519Z","iopub.execute_input":"2024-10-19T19:01:40.504973Z","iopub.status.idle":"2024-10-19T19:01:40.512382Z","shell.execute_reply.started":"2024-10-19T19:01:40.504934Z","shell.execute_reply":"2024-10-19T19:01:40.511444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport pandas as pd\nimport numpy as np\nimport random\n\nrandom.seed(0)\nnp.random.seed(0)\ntorch.cuda.manual_seed(0)\ntorch.backends.cudnn.deterministic = True","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:40.513774Z","iopub.execute_input":"2024-10-19T19:01:40.514084Z","iopub.status.idle":"2024-10-19T19:01:40.525256Z","shell.execute_reply.started":"2024-10-19T19:01:40.514042Z","shell.execute_reply":"2024-10-19T19:01:40.524229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.datasets\n\nCIFAR10_train=torchvision.datasets.CIFAR10(root='./', train=True, download=True)\nCIFAR10_test=torchvision.datasets.CIFAR10(root='./', train=False, download=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:40.526745Z","iopub.execute_input":"2024-10-19T19:01:40.527037Z","iopub.status.idle":"2024-10-19T19:01:47.605181Z","shell.execute_reply.started":"2024-10-19T19:01:40.527007Z","shell.execute_reply":"2024-10-19T19:01:47.604206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = CIFAR10_train.data\ny_train = CIFAR10_train.targets\nX_test = CIFAR10_test.data\ny_test = CIFAR10_test.targets","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:47.606342Z","iopub.execute_input":"2024-10-19T19:01:47.606629Z","iopub.status.idle":"2024-10-19T19:01:47.611180Z","shell.execute_reply.started":"2024-10-19T19:01:47.606598Z","shell.execute_reply":"2024-10-19T19:01:47.610223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.moveaxis(X_train, 3, 1)\nX_test = np.moveaxis(X_test, 3, 1)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:47.613354Z","iopub.execute_input":"2024-10-19T19:01:47.613682Z","iopub.status.idle":"2024-10-19T19:01:47.621026Z","shell.execute_reply.started":"2024-10-19T19:01:47.613651Z","shell.execute_reply":"2024-10-19T19:01:47.620200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:47.621890Z","iopub.execute_input":"2024-10-19T19:01:47.622194Z","iopub.status.idle":"2024-10-19T19:01:47.633491Z","shell.execute_reply.started":"2024-10-19T19:01:47.622164Z","shell.execute_reply":"2024-10-19T19:01:47.632670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_images(x):\n    x_norm = x.astype(np.float32)\n    return x_norm/255","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:47.634571Z","iopub.execute_input":"2024-10-19T19:01:47.634914Z","iopub.status.idle":"2024-10-19T19:01:47.642307Z","shell.execute_reply.started":"2024-10-19T19:01:47.634853Z","shell.execute_reply":"2024-10-19T19:01:47.641435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = normalize_images(X_train)\nX_test = normalize_images(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:47.643389Z","iopub.execute_input":"2024-10-19T19:01:47.643712Z","iopub.status.idle":"2024-10-19T19:01:48.049470Z","shell.execute_reply.started":"2024-10-19T19:01:47.643669Z","shell.execute_reply":"2024-10-19T19:01:48.048668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = torch.from_numpy(X_train)\ny_train = torch.as_tensor(y_train)\nX_test = torch.from_numpy(X_test)\ny_test = torch.as_tensor(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:48.050715Z","iopub.execute_input":"2024-10-19T19:01:48.051152Z","iopub.status.idle":"2024-10-19T19:01:48.089697Z","shell.execute_reply.started":"2024-10-19T19:01:48.051118Z","shell.execute_reply":"2024-10-19T19:01:48.088817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_train), len(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:48.090732Z","iopub.execute_input":"2024-10-19T19:01:48.091039Z","iopub.status.idle":"2024-10-19T19:01:48.097693Z","shell.execute_reply.started":"2024-10-19T19:01:48.091008Z","shell.execute_reply":"2024-10-19T19:01:48.096759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(np.transpose(X_train[5,:,:,:], (1,2,0)))\nplt.show()\nprint(y_train[0])","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:48.102012Z","iopub.execute_input":"2024-10-19T19:01:48.102336Z","iopub.status.idle":"2024-10-19T19:01:48.392318Z","shell.execute_reply.started":"2024-10-19T19:01:48.102288Z","shell.execute_reply":"2024-10-19T19:01:48.391400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LeNet","metadata":{}},{"cell_type":"code","source":"class LeNet5(torch.nn.Module):\n  def __init__(self):\n    super (LeNet5, self).__init__()\n\n    self.conv1 = torch.nn.Conv2d(\n        in_channels = 3, out_channels = 6, kernel_size = 5, padding = 2)\n    self.act1 = torch.nn.Tanh()\n    self.pool1 = torch.nn.AvgPool2d(kernel_size = 2, stride = 2)\n    self.conv2 = torch.nn.Conv2d(\n        in_channels = 6, out_channels = 16, kernel_size = 5, padding = 0)\n    self.act2 = torch.nn.Tanh()\n    self.pool2 = torch.nn.AvgPool2d(kernel_size = 2, stride = 2)\n\n    self.fc1 = torch.nn.Linear(6*6*16,120)\n    self.act3 = torch.nn.Tanh()\n\n    self.fc2 = torch.nn.Linear(120,84)\n    self.act4 = torch.nn.Tanh()\n\n    self.fc3 = torch.nn.Linear(84,10)\n\n  def forward(self,x):\n    x = self.conv1(x)\n    x = self.act1(x)\n    x = self.pool1(x)\n\n    x = self.conv2(x)\n    x = self.act2(x)\n    x = self.pool2(x)\n\n    x = x.view(x.size(0), x.size(1)* x.size(2) * x.size(3))\n\n    x = self.fc1(x)\n    x = self.act3(x)\n    x = self.fc2(x)\n    x = self.act4(x)\n    x = self.fc3(x)\n\n    return x\n\nnet_LeNet = LeNet5()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:48.393575Z","iopub.execute_input":"2024-10-19T19:01:48.394304Z","iopub.status.idle":"2024-10-19T19:01:48.412315Z","shell.execute_reply.started":"2024-10-19T19:01:48.394259Z","shell.execute_reply":"2024-10-19T19:01:48.411455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def trainLeNet(net):\n  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n  net = net.to(device)\n\n  loss = torch.nn.CrossEntropyLoss()\n  optimizer = torch.optim.Adam(net.parameters(), lr=0.001)\n\n  batch_size = 30\n\n  test_acc = []\n  test_loss = []\n\n  dev_X_test = X_test.to(device)\n  dev_y_test = y_test.to(device)\n\n  for e in range(20):\n    order = np.random.permutation(len(X_train))\n    net.train()\n    for i in range(0, len(X_train), batch_size):\n      optimizer.zero_grad()\n\n      batch_ind = order[i:i+batch_size]\n\n      X_batch = X_train[batch_ind].to(device)\n      y_batch = y_train[batch_ind].to(device)\n      preds = net.forward(X_batch)\n      loss_val = loss(preds, y_batch)\n      loss_val.backward()\n\n      optimizer.step()\n\n    net.eval()\n    with torch.no_grad():\n      test_preds = net.forward(dev_X_test)\n      test_loss.append(loss(test_preds, dev_y_test).data.cpu())\n\n      acc = (test_preds.argmax(dim = 1) == dev_y_test).float().mean().data.cpu()\n      test_acc.append(acc)\n\n    print(acc, \"   \", test_loss[-1])\n  return test_loss","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:48.413513Z","iopub.execute_input":"2024-10-19T19:01:48.414121Z","iopub.status.idle":"2024-10-19T19:01:48.424044Z","shell.execute_reply.started":"2024-10-19T19:01:48.414077Z","shell.execute_reply":"2024-10-19T19:01:48.423088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss = trainLeNet(net_LeNet)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:01:48.425166Z","iopub.execute_input":"2024-10-19T19:01:48.425448Z","iopub.status.idle":"2024-10-19T19:03:05.386626Z","shell.execute_reply.started":"2024-10-19T19:01:48.425416Z","shell.execute_reply":"2024-10-19T19:03:05.385559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ReLU","metadata":{}},{"cell_type":"code","source":"class LeNet5ReLU(torch.nn.Module):\n  def __init__(self):\n    super (LeNet5ReLU, self).__init__()\n\n    self.conv1 = torch.nn.Conv2d(\n        in_channels = 3, out_channels=6, kernel_size = 5, padding = 2)\n    self.act1 = torch.nn.ReLU()\n    self.pool1 = torch.nn.AvgPool2d(kernel_size= 2, stride = 2)\n    self.conv2 = torch.nn.Conv2d(\n        in_channels = 6, out_channels=16, kernel_size = 5, padding = 0)\n    self.act2 = torch.nn.ReLU()\n    self.pool2 = torch.nn.AvgPool2d(kernel_size= 2, stride = 2)\n\n    self.fc1 = torch.nn.Linear(6*6*16,120)\n    self.act3 = torch.nn.ReLU()\n\n    self.fc2 = torch.nn.Linear(120,84)\n    self.act4 = torch.nn.ReLU()\n\n    self.fc3 = torch.nn.Linear(84,10)\n\n  def forward(self,x):\n    x = self.conv1(x)\n    x = self.act1(x)\n    x = self.pool1(x)\n\n    x = self.conv2(x)\n    x = self.act2(x)\n    x = self.pool2(x)\n\n    x = x.view(x.size(0), x.size(1)* x.size(2) * x.size(3))\n\n    x = self.fc1(x)\n    x = self.act3(x)\n    x = self.fc2(x)\n    x = self.act4(x)\n    x = self.fc3(x)\n\n    return x\n\nnet_ReLU = LeNet5ReLU()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:03:05.388297Z","iopub.execute_input":"2024-10-19T19:03:05.388735Z","iopub.status.idle":"2024-10-19T19:03:05.402986Z","shell.execute_reply.started":"2024-10-19T19:03:05.388689Z","shell.execute_reply":"2024-10-19T19:03:05.402034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss_ReLU = trainLeNet(net_ReLU)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:03:05.403931Z","iopub.execute_input":"2024-10-19T19:03:05.404228Z","iopub.status.idle":"2024-10-19T19:04:20.689126Z","shell.execute_reply.started":"2024-10-19T19:03:05.404197Z","shell.execute_reply":"2024-10-19T19:04:20.688199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MaxPooling","metadata":{}},{"cell_type":"code","source":"class LeNet5MaxPool(torch.nn.Module):\n  def __init__(self):\n    super (LeNet5MaxPool, self).__init__()\n\n    self.conv1 = torch.nn.Conv2d(\n        in_channels = 3, out_channels=6, kernel_size = 5, padding = 2)\n    self.act1 = torch.nn.ReLU()\n    self.pool1 = torch.nn.MaxPool2d(kernel_size= 2, stride = 2)\n    self.conv2 = torch.nn.Conv2d(\n        in_channels = 6, out_channels=16, kernel_size = 5, padding = 0)\n    self.act2 = torch.nn.ReLU()\n    self.pool2 = torch.nn.MaxPool2d(kernel_size= 2, stride = 2)\n\n    self.fc1 = torch.nn.Linear(6*6*16,120)\n    self.act3 = torch.nn.ReLU()\n\n    self.fc2 = torch.nn.Linear(120,84)\n    self.act4 = torch.nn.ReLU()\n\n    self.fc3 = torch.nn.Linear(84,10)\n\n  def forward(self,x):\n    x = self.conv1(x)\n    x = self.act1(x)\n    x = self.pool1(x)\n\n    x = self.conv2(x)\n    x = self.act2(x)\n    x = self.pool2(x)\n\n    x = x.view(x.size(0), x.size(1)* x.size(2) * x.size(3))\n\n    x = self.fc1(x)\n    x = self.act3(x)\n    x = self.fc2(x)\n    x = self.act4(x)\n    x = self.fc3(x)\n\n    return x\n\nnet_MaxPool = LeNet5MaxPool()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:04:20.690446Z","iopub.execute_input":"2024-10-19T19:04:20.690934Z","iopub.status.idle":"2024-10-19T19:04:20.704573Z","shell.execute_reply.started":"2024-10-19T19:04:20.690887Z","shell.execute_reply":"2024-10-19T19:04:20.703667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss_MaxPool = trainLeNet(net_MaxPool)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:04:20.705838Z","iopub.execute_input":"2024-10-19T19:04:20.706208Z","iopub.status.idle":"2024-10-19T19:05:37.369388Z","shell.execute_reply.started":"2024-10-19T19:04:20.706149Z","shell.execute_reply":"2024-10-19T19:05:37.368372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Double","metadata":{}},{"cell_type":"code","source":"class LeNet5Double(torch.nn.Module):\n  def __init__(self):\n    super (LeNet5Double, self).__init__()\n\n    self.conv1_1 = torch.nn.Conv2d(\n                in_channels=3, out_channels=6, kernel_size=3, padding=0)\n    self.conv1_2 = torch.nn.Conv2d(\n                in_channels=6, out_channels=6, kernel_size=3, padding=0)\n\n    self.conv2_1 = torch.nn.Conv2d(\n                in_channels=6, out_channels=16, kernel_size=3, padding=0)\n    self.conv2_2 = torch.nn.Conv2d(\n                in_channels=16, out_channels=16, kernel_size=3, padding=0)\n\n    self.act1 = torch.nn.ReLU()\n    self.pool1 = torch.nn.MaxPool2d(kernel_size= 2, stride = 2)\n\n    self.act2 = torch.nn.ReLU()\n    self.pool2 = torch.nn.MaxPool2d(kernel_size= 2, stride = 2)\n\n    self.fc1 = torch.nn.Linear(5*5*16,120)\n    self.act3 = torch.nn.ReLU()\n\n    self.fc2 = torch.nn.Linear(120,84)\n    self.act4 = torch.nn.ReLU()\n\n    self.fc3 = torch.nn.Linear(84,10)\n\n  def forward(self,x):\n    x = self.conv1_2(self.conv1_1(x))\n    x = self.act1(x)\n    x = self.pool1(x)\n\n    x = self.conv2_2(self.conv2_1(x))\n    x = self.act2(x)\n    x = self.pool2(x)\n\n    x = x.view(x.size(0), x.size(1)* x.size(2) * x.size(3))\n\n    x = self.fc1(x)\n    x = self.act3(x)\n    x = self.fc2(x)\n    x = self.act4(x)\n    x = self.fc3(x)\n\n    return x\n\nnet_Double = LeNet5Double()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:05:37.370643Z","iopub.execute_input":"2024-10-19T19:05:37.370995Z","iopub.status.idle":"2024-10-19T19:05:37.385699Z","shell.execute_reply.started":"2024-10-19T19:05:37.370961Z","shell.execute_reply":"2024-10-19T19:05:37.384709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss_Double = trainLeNet(net_Double)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:05:37.386890Z","iopub.execute_input":"2024-10-19T19:05:37.387189Z","iopub.status.idle":"2024-10-19T19:07:10.214220Z","shell.execute_reply.started":"2024-10-19T19:05:37.387156Z","shell.execute_reply":"2024-10-19T19:07:10.213233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BatchNorm","metadata":{}},{"cell_type":"code","source":"class LeNet5BatchNorm(torch.nn.Module):\n  def __init__(self):\n    super (LeNet5BatchNorm, self).__init__()\n\n    self.conv1_1 = torch.nn.Conv2d(\n                in_channels=3, out_channels=6, kernel_size=3, padding=0)\n    self.bn1_1 = torch.nn.BatchNorm2d(6)\n\n    self.conv1_2 = torch.nn.Conv2d(\n                in_channels=6, out_channels=6, kernel_size=3, padding=0)\n    self.bn1_2 = torch.nn.BatchNorm2d(6)\n\n    self.conv2_1 = torch.nn.Conv2d(\n                in_channels=6, out_channels=16, kernel_size=3, padding=0)\n    self.bn2_1 = torch.nn.BatchNorm2d(16)\n\n    self.conv2_2 = torch.nn.Conv2d(\n                in_channels=16, out_channels=16, kernel_size=3, padding=0)\n    self.bn2_2 = torch.nn.BatchNorm2d(16)\n\n    self.act1 = torch.nn.ReLU()\n    self.pool1 = torch.nn.MaxPool2d(kernel_size= 2, stride = 2)\n\n    self.act2 = torch.nn.ReLU()\n    self.pool2 = torch.nn.MaxPool2d(kernel_size= 2, stride = 2)\n\n    self.fc1 = torch.nn.Linear(5*5*16,120)\n    self.bn3 = torch.nn.BatchNorm1d(120)\n    self.act3 = torch.nn.ReLU()\n\n    self.fc2 = torch.nn.Linear(120,84)\n    self.bn4 = torch.nn.BatchNorm1d(84)\n    self.act4 = torch.nn.ReLU()\n\n    self.fc3 = torch.nn.Linear(84,10)\n\n  def forward(self,x):\n    x = self.conv1_2(self.bn1_1(self.conv1_1(x)))\n    x = self.bn1_2(x)\n    x = self.act1(x)\n    x = self.pool1(x)\n\n    x = self.conv2_2(self.bn2_1(self.conv2_1(x)))\n    x = self.bn2_2(x)\n    x = self.act2(x)\n    x = self.pool2(x)\n\n    x = x.view(x.size(0), x.size(1)* x.size(2) * x.size(3))\n\n    x = self.fc1(x)\n    x = self.bn3(x)\n    x = self.act3(x)\n\n    x = self.fc2(x)\n    x = self.bn4(x)\n    x = self.act4(x)\n\n    x = self.fc3(x)\n\n    return x\n\nnet_BatchNorm = LeNet5BatchNorm()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:07:10.215568Z","iopub.execute_input":"2024-10-19T19:07:10.215985Z","iopub.status.idle":"2024-10-19T19:07:10.237526Z","shell.execute_reply.started":"2024-10-19T19:07:10.215939Z","shell.execute_reply":"2024-10-19T19:07:10.236560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss_BatchNorm = trainLeNet(net_BatchNorm)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:07:10.238708Z","iopub.execute_input":"2024-10-19T19:07:10.239008Z","iopub.status.idle":"2024-10-19T19:09:19.877234Z","shell.execute_reply.started":"2024-10-19T19:07:10.238978Z","shell.execute_reply":"2024-10-19T19:09:19.876254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion","metadata":{}},{"cell_type":"code","source":"plt.plot(test_loss, label='LeNet')\nplt.plot(test_loss_ReLU, label='ReLU')\nplt.plot(test_loss_MaxPool, label='MaxPool')\nplt.plot(test_loss_Double, label='Double')\nplt.plot(test_loss_BatchNorm, label='BatchNorm')\n\nplt.title('Loss train')\nplt.legend()\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:09:19.878620Z","iopub.execute_input":"2024-10-19T19:09:19.879047Z","iopub.status.idle":"2024-10-19T19:09:20.221752Z","shell.execute_reply.started":"2024-10-19T19:09:19.879000Z","shell.execute_reply":"2024-10-19T19:09:20.220819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install  py7zr\n\nimport py7zr\nimport os\n\nfolder_path = '/kaggle/working/test_comp'\n\nif not os.path.exists(folder_path):\n    os.makedirs(folder_path)\n    print(f\"Directory {folder_path} created successfully.\")\nelse:\n    print(f\"Directory {folder_path} already exists.\")\n\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n    z.extractall(folder_path)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T19:43:36.743870Z","iopub.execute_input":"2024-10-19T19:43:36.744589Z","iopub.status.idle":"2024-10-19T19:46:23.908693Z","shell.execute_reply.started":"2024-10-19T19:43:36.744551Z","shell.execute_reply":"2024-10-19T19:46:23.907696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\n\ntest_filenames = [os.path.join('/kaggle/working/test_comp/test', f) for f in os.listdir('/kaggle/working/test_comp/test')]\ntest_filenames.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))\n\nclass CIFAR10(Dataset):\n    def __init__(self, filenames, transforms):\n        self.filenames = filenames\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.filenames)\n    \n    def __getitem__(self, idx):\n        img = np.asarray(Image.open(self.filenames[idx]))\n        img = np.moveaxis(img, 2, 0)\n        transformed_img = self.transforms(img)\n        return transformed_img\n\ntest_dataset = CIFAR10(test_filenames, normalize_images)\n\ntest_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)\n\nclasses = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\nresult = []\nwith torch.no_grad():\n    net_BatchNorm.eval()\n    for batch_in in test_loader:\n        batch_in = batch_in.to(device)\n        \n        y_pred = net_BatchNorm(batch_in)\n        preds = torch.argmax(y_pred, 1)\n        preds = preds.cpu().numpy()\n        result.extend([classes[i] for i in preds])\n        \nsample = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv')\nsample['label'] = result\nsample.to_csv('./submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-19T20:41:25.840535Z","iopub.execute_input":"2024-10-19T20:41:25.841231Z"},"trusted":true},"execution_count":null,"outputs":[]}]}