{"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":"#importing the packages required\ntry:\n    import py7zr\n    import torchmetrics, mlxtend\n    from torchsummary import summary\nexcept:\n    !pip install py7zr\n    !pip install -q torchmetrics -U mlxtend # <- Note: If you're using Google Colab, this may require restarting the runtime\n    !pip install torchsummary\n    \n    import torchmetrics, mlxtend\n    import torchsummary\n    import py7zr","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:36:31.270285Z","iopub.execute_input":"2023-09-28T13:36:31.270662Z","iopub.status.idle":"2023-09-28T13:37:26.862762Z","shell.execute_reply.started":"2023-09-28T13:36:31.270636Z","shell.execute_reply":"2023-09-28T13:37:26.861724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip uninstall protobuf -y","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:37:26.864785Z","iopub.execute_input":"2023-09-28T13:37:26.865134Z","iopub.status.idle":"2023-09-28T13:37:28.970180Z","shell.execute_reply.started":"2023-09-28T13:37:26.865099Z","shell.execute_reply":"2023-09-28T13:37:28.969053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install --force-reinstall -v \"protobuf==3.19.0\"","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:37:28.971943Z","iopub.execute_input":"2023-09-28T13:37:28.974548Z","iopub.status.idle":"2023-09-28T13:37:48.503459Z","shell.execute_reply.started":"2023-09-28T13:37:28.974507Z","shell.execute_reply":"2023-09-28T13:37:48.502236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pytorch_lightning ","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:37:48.506787Z","iopub.execute_input":"2023-09-28T13:37:48.507167Z","iopub.status.idle":"2023-09-28T13:38:00.115461Z","shell.execute_reply.started":"2023-09-28T13:37:48.507120Z","shell.execute_reply":"2023-09-28T13:38:00.114328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pytorch_lightning as pl","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:38:00.118217Z","iopub.execute_input":"2023-09-28T13:38:00.119002Z","iopub.status.idle":"2023-09-28T13:38:00.870652Z","shell.execute_reply.started":"2023-09-28T13:38:00.118962Z","shell.execute_reply":"2023-09-28T13:38:00.869689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# imports the torch_xla package\n# import torch_xla\n# import torch_xla.core.xla_model as xm","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:38:00.872104Z","iopub.execute_input":"2023-09-28T13:38:00.872449Z","iopub.status.idle":"2023-09-28T13:38:01.825476Z","shell.execute_reply.started":"2023-09-28T13:38:00.872416Z","shell.execute_reply":"2023-09-28T13:38:01.822182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\n\nfrom tqdm.auto import tqdm\n\n# Import the visualization library\nimport matplotlib.pyplot as plt\nfrom matplotlib import image\n\n# Import PyTorch\nimport torch\nfrom torch import nn\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader\n\n# Torchvision\nfrom torchvision import datasets, transforms\n\n# Import matplotlib for visualization\nimport matplotlib.pyplot as plt\n\nfrom sklearn.preprocessing import LabelEncoder\n\nprint(f\"PyTorch version: {torch.__version__}\")\ndevice = \"cuda\" if torch.cuda.is_available() else None\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:44:34.126135Z","iopub.execute_input":"2023-09-28T13:44:34.126760Z","iopub.status.idle":"2023-09-28T13:44:34.411082Z","shell.execute_reply.started":"2023-09-28T13:44:34.126726Z","shell.execute_reply":"2023-09-28T13:44:34.410148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:44:34.413067Z","iopub.execute_input":"2023-09-28T13:44:34.413652Z","iopub.status.idle":"2023-09-28T13:44:34.421832Z","shell.execute_reply.started":"2023-09-28T13:44:34.413619Z","shell.execute_reply":"2023-09-28T13:44:34.420923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_dir = os.path.join(\"/kaggle\", \"input\", \"cifar-10/\")\noutput_dir = os.path.join(\"/kaggle\", \"output\", \"cifar-10/\")\n\n# Create the output directory if it doesn't exist\nos.makedirs(output_dir, exist_ok=True)\n\ninput_dir, output_dir","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:44:34.423113Z","iopub.execute_input":"2023-09-28T13:44:34.424429Z","iopub.status.idle":"2023-09-28T13:44:34.433861Z","shell.execute_reply.started":"2023-09-28T13:44:34.424398Z","shell.execute_reply":"2023-09-28T13:44:34.432685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for extracting the files\nzip_files = [file for file in os.listdir(input_dir) if file[-3:] == \".7z\"]\n\nfor file in zip_files:\n    print(f\"Extracting the file {file}\")\n    \n    with py7zr.SevenZipFile(os.path.join(input_dir, file), mode='r') as z:\n        z.extractall(path=output_dir)\n    \n    print(f\"Extraction Complete\")","metadata":{"execution":{"iopub.status.busy":"2023-09-28T13:44:34.437060Z","iopub.execute_input":"2023-09-28T13:44:34.437524Z","iopub.status.idle":"2023-09-28T14:04:37.740401Z","shell.execute_reply.started":"2023-09-28T13:44:34.437494Z","shell.execute_reply":"2023-09-28T14:04:37.739306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(input_dir + \"trainLabels.csv\", index_col=[\"id\"]).squeeze()\nlabel_encoder = LabelEncoder().fit(train_labels)\n\nclasses = label_encoder.classes_","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:04:37.741779Z","iopub.execute_input":"2023-09-28T14:04:37.742387Z","iopub.status.idle":"2023-09-28T14:04:37.810936Z","shell.execute_reply.started":"2023-09-28T14:04:37.742353Z","shell.execute_reply":"2023-09-28T14:04:37.809980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = []\nfor i in range(1, len(train_labels) + 1):\n    img = image.imread('/kaggle/output/cifar-10/train/' + str(i) + \".png\")\n    train_data.append([img, label_encoder.transform([train_labels[i]])[0]])\n    \nlen(train_data)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:04:37.812506Z","iopub.execute_input":"2023-09-28T14:04:37.812830Z","iopub.status.idle":"2023-09-28T14:04:57.811204Z","shell.execute_reply.started":"2023-09-28T14:04:37.812799Z","shell.execute_reply":"2023-09-28T14:04:57.810182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder.inverse_transform([train_data[0][1]]), train_data[0][0].shape","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:04:57.812560Z","iopub.execute_input":"2023-09-28T14:04:57.813002Z","iopub.status.idle":"2023-09-28T14:04:57.827838Z","shell.execute_reply.started":"2023-09-28T14:04:57.812967Z","shell.execute_reply":"2023-09-28T14:04:57.826848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Configs:\n    batch_size = 512,\n    shuffle    = True,\n    epochs     = 15\n    \nconfigs = Configs()","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:05:32.964884Z","iopub.execute_input":"2023-09-28T14:05:32.965259Z","iopub.status.idle":"2023-09-28T14:05:32.971936Z","shell.execute_reply.started":"2023-09-28T14:05:32.965229Z","shell.execute_reply":"2023-09-28T14:05:32.970875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #CNN model\n# class CNN_Model(pl.LightningModule):\n#     def __init__(self, input_shape: int = 3, output_shape: int = 10):\n#         super().__init__()\n\n#         self.conv1 = nn.Conv2d(3, 32, 5)\n#         self.conv2 = nn.Conv2d(32, 64, 5)\n#         self.pool = nn.MaxPool2d(2, 2)\n\n#         self.fc1 = nn.Linear(64 * 5 * 5, 120)\n#         self.fc2 = nn.Linear(120, 84)\n#         self.fc3 = nn.Linear(84, 10)\n\n#     def forward(self, x):\n#         x = self.pool(F.relu(self.conv1(x)))\n#         x = self.pool(F.relu(self.conv2(x)))\n#         x = torch.flatten(x, 1) # flatten all dimensions except batch\n#         x = F.relu(self.fc1(x))\n#         x = F.relu(self.fc2(x))\n#         x = self.fc3(x)\n#         return x\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:05:32.978051Z","iopub.execute_input":"2023-09-28T14:05:32.978336Z","iopub.status.idle":"2023-09-28T14:05:33.001553Z","shell.execute_reply.started":"2023-09-28T14:05:32.978312Z","shell.execute_reply":"2023-09-28T14:05:33.000580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.models as models\nimport torch.nn as nn\nfrom torchinfo import summary\nmodel = models.resnet18(pretrained=True)\nnum_features = model.fc.in_features\nmodel.fc = nn.Linear(num_features, 10) \n#device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = model.to(device)\nmodel","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:05:33.003799Z","iopub.execute_input":"2023-09-28T14:05:33.004409Z","iopub.status.idle":"2023-09-28T14:05:33.268893Z","shell.execute_reply.started":"2023-09-28T14:05:33.004379Z","shell.execute_reply":"2023-09-28T14:05:33.267972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = CNN_Model()\n\n# Setup loss and optimizer\nloss_fn = nn.CrossEntropyLoss().to(device)\n# optimizer = torch.optim.SGD(params=model.parameters(), lr=0.001, momentum=0.9)\noptimizer = torch.optim.AdamW(params=model.parameters(), lr=0.001, weight_decay=0.005)\naccuracy = torchmetrics.Accuracy(task=\"multiclass\", num_classes=10).to(device)\n\n# device = xm.xla_device()\n\n# model.to(device)\n#summary(model, (3, 32, 32))","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:05:33.270406Z","iopub.execute_input":"2023-09-28T14:05:33.271016Z","iopub.status.idle":"2023-09-28T14:05:33.284113Z","shell.execute_reply.started":"2023-09-28T14:05:33.270981Z","shell.execute_reply":"2023-09-28T14:05:33.283146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(\n    train_data, \n    batch_size   = configs.batch_size[0],\n    shuffle      = configs.shuffle,\n)\n\nprint(f\"Length of train dataloader: {len(train_dataloader)} batches of {configs.batch_size[0]}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:05:33.287443Z","iopub.execute_input":"2023-09-28T14:05:33.287708Z","iopub.status.idle":"2023-09-28T14:05:33.293108Z","shell.execute_reply.started":"2023-09-28T14:05:33.287685Z","shell.execute_reply":"2023-09-28T14:05:33.292217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training process\nmodel.train()\nmodel.to(device)\n\nfor epoch in tqdm(range(configs.epochs)):\n    print(f\"Epoch: {epoch+1}\\n---------\")\n\n    train_loss, train_acc = 0, 0\n    \n    for batch, (X, y) in enumerate(train_dataloader):\n        # Send data to GPU\n        X, y = X.reshape(-1, 3, 32, 32).to(device), y.to(device)\n        \n        # Optimizer zero grad\n        optimizer.zero_grad()\n        \n        #  Forward pass\n        y_logits = model(X)\n        y_preds  = torch.softmax(y_logits, dim=1)\n        #  Calculate loss\n        loss = loss_fn(y_logits, y) \n        #  Loss backward\n        loss.backward()\n        optimizer.step()\n        \n        train_loss += loss\n        train_acc += accuracy(y, y_preds.argmax(dim=1)) # Go from logits -> pred labels\n        \n    # Calculate loss and accuracy per epoch and print out what's happening\n    train_loss /= len(train_dataloader)\n    train_acc /= len(train_dataloader)\n    print(f\"Train loss: {train_loss:.5f} | Train accuracy: {train_acc:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:05:33.296729Z","iopub.execute_input":"2023-09-28T14:05:33.298205Z","iopub.status.idle":"2023-09-28T14:07:12.750962Z","shell.execute_reply.started":"2023-09-28T14:05:33.298179Z","shell.execute_reply":"2023-09-28T14:07:12.749996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.eval()\n# test_labels = model(torch.tensor(test_data.reshape(-1, 3, 32, 32)).to(device)).argmax(dim=1)\n# test_labels = label_encoder.inverse_transform(np.array(test_labels.cpu()))\n# test_labels","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:07:12.752635Z","iopub.execute_input":"2023-09-28T14:07:12.753317Z","iopub.status.idle":"2023-09-28T14:07:12.757245Z","shell.execute_reply.started":"2023-09-28T14:07:12.753279Z","shell.execute_reply":"2023-09-28T14:07:12.756332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# idx = np.random.randint(18)\n\n# plt.imshow(test_data[idx])\n# plt.title(test_labels[idx])\n# plt.axis(False)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:07:12.758751Z","iopub.execute_input":"2023-09-28T14:07:12.759393Z","iopub.status.idle":"2023-09-28T14:07:12.771062Z","shell.execute_reply.started":"2023-09-28T14:07:12.759362Z","shell.execute_reply":"2023-09-28T14:07:12.770140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(15, 5))\n \n# num_rows = 3\n# num_cols = 8\n\n# for idx in range(num_rows * num_cols):\n#     img         = test_data[idx]\n#     label       = test_labels[idx]\n    \n#     ax = plt.subplot(num_rows, num_cols, idx + 1)\n#     plt.imshow(img)\n#     plt.title(label)\n#     plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:07:12.772234Z","iopub.execute_input":"2023-09-28T14:07:12.772920Z","iopub.status.idle":"2023-09-28T14:07:12.783511Z","shell.execute_reply.started":"2023-09-28T14:07:12.772888Z","shell.execute_reply":"2023-09-28T14:07:12.782241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# putting the model in evaluation mode\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:07:12.785406Z","iopub.execute_input":"2023-09-28T14:07:12.786230Z","iopub.status.idle":"2023-09-28T14:07:12.797878Z","shell.execute_reply.started":"2023-09-28T14:07:12.786192Z","shell.execute_reply":"2023-09-28T14:07:12.796816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_test_images = len(os.listdir(\"/kaggle/output/cifar-10/test\"))\nmodel.eval()\ntest_data = []\n\nfor i in range(1, no_of_test_images + 1):\n    img = image.imread('/kaggle/output/cifar-10/test/' + str(i) + \".png\")\n    test_data.append([img, i]) ","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:07:12.799455Z","iopub.execute_input":"2023-09-28T14:07:12.799912Z","iopub.status.idle":"2023-09-28T14:08:54.570782Z","shell.execute_reply.started":"2023-09-28T14:07:12.799873Z","shell.execute_reply":"2023-09-28T14:08:54.569621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataloader = DataLoader(\n    test_data, \n    batch_size   = configs.batch_size[0],\n    shuffle      = False,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:08:54.572935Z","iopub.execute_input":"2023-09-28T14:08:54.573756Z","iopub.status.idle":"2023-09-28T14:08:54.580501Z","shell.execute_reply.started":"2023-09-28T14:08:54.573719Z","shell.execute_reply":"2023-09-28T14:08:54.579333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:08:54.583478Z","iopub.execute_input":"2023-09-28T14:08:54.585323Z","iopub.status.idle":"2023-09-28T14:08:54.608499Z","shell.execute_reply.started":"2023-09-28T14:08:54.585289Z","shell.execute_reply":"2023-09-28T14:08:54.607595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nlabels = []\n\nfor (X, y) in tqdm(test_dataloader):\n    X = X.reshape(-1, 3, 32, 32)\n    X = X.to(device)\n    y_logits = model(X)\n    y_preds  = torch.softmax(y_logits, dim=1).argmax(dim=1).cpu()\n    labels += label_encoder.inverse_transform(y_preds).tolist()\n    ids += y.squeeze().tolist()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:31:55.574326Z","iopub.execute_input":"2023-09-28T14:31:55.574700Z","iopub.status.idle":"2023-09-28T14:32:10.837900Z","shell.execute_reply.started":"2023-09-28T14:31:55.574671Z","shell.execute_reply":"2023-09-28T14:32:10.836950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a DataFrame for the submission\nsubmission_df = pd.DataFrame({\n    'id': ids,\n    'label': labels\n})\n\n# Save the submission file with class labels\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:32:14.681860Z","iopub.execute_input":"2023-09-28T14:32:14.682336Z","iopub.status.idle":"2023-09-28T14:32:15.824113Z","shell.execute_reply.started":"2023-09-28T14:32:14.682295Z","shell.execute_reply":"2023-09-28T14:32:15.823126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head","metadata":{"execution":{"iopub.status.busy":"2023-09-28T14:32:18.801770Z","iopub.execute_input":"2023-09-28T14:32:18.802742Z","iopub.status.idle":"2023-09-28T14:32:18.825043Z","shell.execute_reply.started":"2023-09-28T14:32:18.802701Z","shell.execute_reply":"2023-09-28T14:32:18.823943Z"},"trusted":true},"execution_count":null,"outputs":[]}]}