{"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-09-28T11:43:58.997628Z","iopub.execute_input":"2023-09-28T11:43:58.998093Z","iopub.status.idle":"2023-09-28T11:43:59.358106Z","shell.execute_reply.started":"2023-09-28T11:43:58.998063Z","shell.execute_reply":"2023-09-28T11:43:59.357023Z"},"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\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T11:43:59.359995Z","iopub.execute_input":"2023-09-28T11:43:59.360518Z","iopub.status.idle":"2023-09-28T11:43:59.370033Z","shell.execute_reply.started":"2023-09-28T11:43:59.360485Z","shell.execute_reply":"2023-09-28T11:43:59.368999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    import py7zr\n    import torchmetrics, mlxtend\n    from torchsummary import summary\nexcept:\n    !pip install py7zr\n    !pip install -q torchmetrics -U mlxtend \n    !pip install torchsummary\n    \n    import torchmetrics, mlxtend\n    import torchsummary\n    import py7zr","metadata":{"execution":{"iopub.status.busy":"2023-09-28T11:43:59.371575Z","iopub.execute_input":"2023-09-28T11:43:59.372698Z","iopub.status.idle":"2023-09-28T11:44:59.720365Z","shell.execute_reply.started":"2023-09-28T11:43:59.372664Z","shell.execute_reply":"2023-09-28T11:44:59.719343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_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-28T11:44:59.723169Z","iopub.execute_input":"2023-09-28T11:44:59.723526Z","iopub.status.idle":"2023-09-28T12:05:45.696326Z","shell.execute_reply.started":"2023-09-28T11:44:59.723491Z","shell.execute_reply":"2023-09-28T12:05:45.695115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision\nimport torchvision.transforms as transforms\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:45.698107Z","iopub.execute_input":"2023-09-28T12:05:45.698540Z","iopub.status.idle":"2023-09-28T12:05:45.704528Z","shell.execute_reply.started":"2023-09-28T12:05:45.698494Z","shell.execute_reply":"2023-09-28T12:05:45.703349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:45.706001Z","iopub.execute_input":"2023-09-28T12:05:45.706421Z","iopub.status.idle":"2023-09-28T12:05:45.853322Z","shell.execute_reply.started":"2023-09-28T12:05:45.706386Z","shell.execute_reply":"2023-09-28T12:05:45.852081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"PyTorch version: {torch.__version__}\")\ndevice = \"cuda\" if torch.cuda.is_available() else None\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:45.854953Z","iopub.execute_input":"2023-09-28T12:05:45.855330Z","iopub.status.idle":"2023-09-28T12:05:45.893011Z","shell.execute_reply.started":"2023-09-28T12:05:45.855294Z","shell.execute_reply":"2023-09-28T12:05:45.889381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyper-parameters \nnum_epochs = 30\nbatch_size = 512\nlearning_rate = 0.01\n\n# dataset has PILImage images of range [0, 1]. \n# We transform them to Tensors of normalized range [-1, 1]\ntransform = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:45.895087Z","iopub.execute_input":"2023-09-28T12:05:45.895896Z","iopub.status.idle":"2023-09-28T12:05:45.940156Z","shell.execute_reply.started":"2023-09-28T12:05:45.895857Z","shell.execute_reply":"2023-09-28T12:05:45.933588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True,\n                                        download=True, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:45.945694Z","iopub.execute_input":"2023-09-28T12:05:45.946107Z","iopub.status.idle":"2023-09-28T12:05:50.785895Z","shell.execute_reply.started":"2023-09-28T12:05:45.946069Z","shell.execute_reply":"2023-09-28T12:05:50.784907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,\n                                          shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:50.790553Z","iopub.execute_input":"2023-09-28T12:05:50.790861Z","iopub.status.idle":"2023-09-28T12:05:50.795791Z","shell.execute_reply.started":"2023-09-28T12:05:50.790834Z","shell.execute_reply":"2023-09-28T12:05:50.794774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ('plane', 'car', 'bird', 'cat',\n           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:50.797146Z","iopub.execute_input":"2023-09-28T12:05:50.797986Z","iopub.status.idle":"2023-09-28T12:05:50.808662Z","shell.execute_reply.started":"2023-09-28T12:05:50.797931Z","shell.execute_reply":"2023-09-28T12:05:50.807446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def imshow(img):\n    img = img / 2 + 0.5  # unnormalize\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg, (1, 2, 0)))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:50.811514Z","iopub.execute_input":"2023-09-28T12:05:50.812253Z","iopub.status.idle":"2023-09-28T12:05:50.822009Z","shell.execute_reply.started":"2023-09-28T12:05:50.812218Z","shell.execute_reply":"2023-09-28T12:05:50.820873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get some random training images\ndataiter = iter(train_loader)\nimages, labels = next(dataiter)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:50.823458Z","iopub.execute_input":"2023-09-28T12:05:50.823826Z","iopub.status.idle":"2023-09-28T12:05:51.304543Z","shell.execute_reply.started":"2023-09-28T12:05:50.823792Z","shell.execute_reply":"2023-09-28T12:05:51.301657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show images\n\nimshow(torchvision.utils.make_grid(images))","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:51.307056Z","iopub.execute_input":"2023-09-28T12:05:51.307491Z","iopub.status.idle":"2023-09-28T12:05:52.224528Z","shell.execute_reply.started":"2023-09-28T12:05:51.307454Z","shell.execute_reply":"2023-09-28T12:05:52.220110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class ConvNet(nn.Module):\n#     def __init__(self):\n#         super(ConvNet, self).__init__()\n#         self.conv1 = nn.Conv2d(3, 6, 5)\n#         self.pool = nn.MaxPool2d(2, 2)\n#         self.conv2 = nn.Conv2d(6, 16, 5)\n#         self.fc1 = nn.Linear(16 * 5 * 5, 120)\n#         self.fc2 = nn.Linear(120, 84)\n#         self.fc3 = nn.Linear(84, 10)\n\n#     def forward(self, x):\n#         # -> n, 3, 32, 32\n#         x = self.pool(F.relu(self.conv1(x)))  # -> n, 6, 14, 14\n#         x = self.pool(F.relu(self.conv2(x)))  # -> n, 16, 5, 5\n#         x = x.view(-1, 16 * 5 * 5)            # -> n, 400\n#         x = F.relu(self.fc1(x))               # -> n, 120\n#         x = F.relu(self.fc2(x))               # -> n, 84\n#         x = self.fc3(x)                       # -> n, 10\n#         return x","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:52.226902Z","iopub.execute_input":"2023-09-28T12:05:52.227896Z","iopub.status.idle":"2023-09-28T12:05:52.262142Z","shell.execute_reply.started":"2023-09-28T12:05:52.227710Z","shell.execute_reply":"2023-09-28T12:05:52.256901Z"},"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-28T12:09:31.638737Z","iopub.execute_input":"2023-09-28T12:09:31.639807Z","iopub.status.idle":"2023-09-28T12:09:38.388048Z","shell.execute_reply.started":"2023-09-28T12:09:31.639762Z","shell.execute_reply":"2023-09-28T12:09:38.386873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = ConvNet()\nmodel = nn.DataParallel(model, device_ids=[0, 1])\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:09:44.147567Z","iopub.execute_input":"2023-09-28T12:09:44.147958Z","iopub.status.idle":"2023-09-28T12:09:44.160476Z","shell.execute_reply.started":"2023-09-28T12:09:44.147912Z","shell.execute_reply":"2023-09-28T12:09:44.159340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:05:53.730172Z","iopub.status.idle":"2023-09-28T12:05:53.731199Z","shell.execute_reply.started":"2023-09-28T12:05:53.730914Z","shell.execute_reply":"2023-09-28T12:05:53.730958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:09:59.787789Z","iopub.execute_input":"2023-09-28T12:09:59.788348Z","iopub.status.idle":"2023-09-28T12:09:59.795968Z","shell.execute_reply.started":"2023-09-28T12:09:59.788311Z","shell.execute_reply":"2023-09-28T12:09:59.794872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training loop\nn_total_steps = len(train_loader)\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_loader):\n        # origin shape: [4, 3, 32, 32] = 4, 3, 1024\n        # input_layer: 3 input channels, 6 output channels, 5 kernel size\n        images = images.to(device)\n        labels = labels.to(device)\n\n        # Forward pass\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        # Backward and optimize\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        if (i+1) % 2000 == 0:\n            print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}')\n\nprint('Finished Training')\nPATH = './cnn.pth'\ntorch.save(model.state_dict(), PATH)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:10:03.620014Z","iopub.execute_input":"2023-09-28T12:10:03.620558Z","iopub.status.idle":"2023-09-28T12:17:58.841067Z","shell.execute_reply.started":"2023-09-28T12:10:03.620524Z","shell.execute_reply":"2023-09-28T12:17:58.839969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:23:26.751706Z","iopub.execute_input":"2023-09-28T12:23:26.752160Z","iopub.status.idle":"2023-09-28T12:23:26.758238Z","shell.execute_reply.started":"2023-09-28T12:23:26.752126Z","shell.execute_reply":"2023-09-28T12:23:26.757363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import image\nno_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-28T12:23:30.402807Z","iopub.execute_input":"2023-09-28T12:23:30.403544Z","iopub.status.idle":"2023-09-28T12:25:28.716277Z","shell.execute_reply.started":"2023-09-28T12:23:30.403511Z","shell.execute_reply":"2023-09-28T12:25:28.713719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:26:12.083196Z","iopub.execute_input":"2023-09-28T12:26:12.083587Z","iopub.status.idle":"2023-09-28T12:26:12.091151Z","shell.execute_reply.started":"2023-09-28T12:26:12.083557Z","shell.execute_reply":"2023-09-28T12:26:12.089963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DataLoader(\n    test_data, \n    batch_size   = batch_size,\n    shuffle      = False,\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:26:15.126578Z","iopub.execute_input":"2023-09-28T12:26:15.126979Z","iopub.status.idle":"2023-09-28T12:26:15.135234Z","shell.execute_reply.started":"2023-09-28T12:26:15.126924Z","shell.execute_reply":"2023-09-28T12:26:15.134009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\nfrom sklearn.preprocessing import LabelEncoder\ntrain_labels = pd.read_csv(input_dir + \"trainLabels.csv\", index_col=[\"id\"]).squeeze()\nlabel_encoder = LabelEncoder().fit(train_labels)\n\nids = []\nlabels = []\n\nfor (X, y) in tqdm(test_loader):\n    X = X.reshape(-1, 3, 32, 32).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()","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:26:19.236797Z","iopub.execute_input":"2023-09-28T12:26:19.237511Z","iopub.status.idle":"2023-09-28T12:26:34.951163Z","shell.execute_reply.started":"2023-09-28T12:26:19.237474Z","shell.execute_reply":"2023-09-28T12:26:34.950093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_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-28T12:26:46.782586Z","iopub.execute_input":"2023-09-28T12:26:46.782981Z","iopub.status.idle":"2023-09-28T12:26:47.568345Z","shell.execute_reply.started":"2023-09-28T12:26:46.782927Z","shell.execute_reply":"2023-09-28T12:26:47.567349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head\n","metadata":{"execution":{"iopub.status.busy":"2023-09-28T12:26:50.858442Z","iopub.execute_input":"2023-09-28T12:26:50.858845Z","iopub.status.idle":"2023-09-28T12:26:50.875462Z","shell.execute_reply.started":"2023-09-28T12:26:50.858802Z","shell.execute_reply":"2023-09-28T12:26:50.874192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}