{"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":"tpu1vmV38","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Dependancies","metadata":{}},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:07:31.493007Z","iopub.execute_input":"2024-05-07T12:07:31.493386Z","iopub.status.idle":"2024-05-07T12:07:38.310112Z","shell.execute_reply.started":"2024-05-07T12:07:31.493354Z","shell.execute_reply":"2024-05-07T12:07:38.309198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport py7zr\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import transforms, models\nfrom torch.utils.data import DataLoader, Dataset\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report, accuracy_score, roc_curve, auc, precision_recall_curve\nfrom sklearn.preprocessing import label_binarize\nfrom itertools import cycle","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:07:38.311743Z","iopub.execute_input":"2024-05-07T12:07:38.311989Z","iopub.status.idle":"2024-05-07T12:07:59.105724Z","shell.execute_reply.started":"2024-05-07T12:07:38.311964Z","shell.execute_reply":"2024-05-07T12:07:59.104998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading and Preprocessing the data","metadata":{}},{"cell_type":"code","source":"# Print all file paths\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:07:59.106661Z","iopub.execute_input":"2024-05-07T12:07:59.107004Z","iopub.status.idle":"2024-05-07T12:07:59.112026Z","shell.execute_reply.started":"2024-05-07T12:07:59.106979Z","shell.execute_reply":"2024-05-07T12:07:59.111344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract training images\ntrain_archive_path = '/kaggle/input/cifar-10/train.7z'\ntrain_extract_path = \"/kaggle/working/train_images\"\n\ndef extract_7z_archive(archive_path, extract_path):\n    with py7zr.SevenZipFile(archive_path, mode='r') as z:\n        z.extractall(path=extract_path)\n\nextract_7z_archive(train_archive_path, train_extract_path)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:07:59.113624Z","iopub.execute_input":"2024-05-07T12:07:59.113855Z","iopub.status.idle":"2024-05-07T12:08:23.983630Z","shell.execute_reply.started":"2024-05-07T12:07:59.113832Z","shell.execute_reply":"2024-05-07T12:08:23.982820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load trainLabels.csv file\ntrain_labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\ntrain_labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:08:23.984631Z","iopub.execute_input":"2024-05-07T12:08:23.984877Z","iopub.status.idle":"2024-05-07T12:08:24.017284Z","shell.execute_reply.started":"2024-05-07T12:08:23.984854Z","shell.execute_reply":"2024-05-07T12:08:24.016625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a custom dataset class\nclass CustomDataset(Dataset):\n    def __init__(self, data_dir, labels_df, transform=None):\n        self.data_dir = data_dir\n        self.labels_df = labels_df\n        self.transform = transform\n        self.label_map = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}\n\n    def __len__(self):\n        return len(self.labels_df)\n\n    def __getitem__(self, idx):\n        img_name = os.path.join(self.data_dir, 'train', str(self.labels_df.iloc[idx, 0]) + '.png')\n        image = Image.open(img_name)\n        label = self.labels_df.iloc[idx, 1]\n        label = self.label_map[label]  # Convert label to numerical value\n        if self.transform:\n            image = self.transform(image)\n        return image, label\n","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:08:24.018254Z","iopub.execute_input":"2024-05-07T12:08:24.018505Z","iopub.status.idle":"2024-05-07T12:08:24.024817Z","shell.execute_reply.started":"2024-05-07T12:08:24.018479Z","shell.execute_reply":"2024-05-07T12:08:24.024044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define transforms for data preprocessing\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  \n    transforms.ToTensor(),\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:08:24.025744Z","iopub.execute_input":"2024-05-07T12:08:24.026434Z","iopub.status.idle":"2024-05-07T12:08:24.035462Z","shell.execute_reply.started":"2024-05-07T12:08:24.026404Z","shell.execute_reply":"2024-05-07T12:08:24.034855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create custom dataset\ntrain_dataset = CustomDataset(train_extract_path, train_labels_df, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:08:24.036318Z","iopub.execute_input":"2024-05-07T12:08:24.036555Z","iopub.status.idle":"2024-05-07T12:08:24.053822Z","shell.execute_reply.started":"2024-05-07T12:08:24.036532Z","shell.execute_reply":"2024-05-07T12:08:24.053061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting into training and test sets","metadata":{}},{"cell_type":"code","source":"# Split dataset into training and validation sets\ntrain_set, val_set = train_test_split(train_dataset, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:08:24.054681Z","iopub.execute_input":"2024-05-07T12:08:24.054984Z","iopub.status.idle":"2024-05-07T12:09:48.673801Z","shell.execute_reply.started":"2024-05-07T12:08:24.054958Z","shell.execute_reply":"2024-05-07T12:09:48.672855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create DataLoader objects for the training and validation sets\ntrain_loader = DataLoader(train_set, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_set, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:09:48.676752Z","iopub.execute_input":"2024-05-07T12:09:48.677086Z","iopub.status.idle":"2024-05-07T12:09:48.682076Z","shell.execute_reply.started":"2024-05-07T12:09:48.677040Z","shell.execute_reply":"2024-05-07T12:09:48.681251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the ResNet-18 (CNN) model","metadata":{}},{"cell_type":"code","source":"# Define the CNN model\nclass CNNModel(nn.Module):\n    def __init__(self, num_classes):\n        super(CNNModel, self).__init__()\n        self.backbone = models.resnet18(pretrained=True)\n        num_features = self.backbone.fc.in_features\n        self.backbone.fc = nn.Linear(num_features, num_classes)\n\n    def forward(self, x):\n        return self.backbone(x)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:09:48.683033Z","iopub.execute_input":"2024-05-07T12:09:48.683313Z","iopub.status.idle":"2024-05-07T12:09:48.702216Z","shell.execute_reply.started":"2024-05-07T12:09:48.683285Z","shell.execute_reply":"2024-05-07T12:09:48.701470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Instantiate the model\nnum_classes = 10\nmodel = CNNModel(num_classes)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:09:48.703257Z","iopub.execute_input":"2024-05-07T12:09:48.703543Z","iopub.status.idle":"2024-05-07T12:09:49.437882Z","shell.execute_reply.started":"2024-05-07T12:09:48.703516Z","shell.execute_reply":"2024-05-07T12:09:49.436743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define loss function and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:09:49.439192Z","iopub.execute_input":"2024-05-07T12:09:49.439512Z","iopub.status.idle":"2024-05-07T12:09:49.445224Z","shell.execute_reply.started":"2024-05-07T12:09:49.439478Z","shell.execute_reply":"2024-05-07T12:09:49.444321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nnum_epochs = 10\nfor epoch in range(num_epochs):\n    model.train()\n    for images, labels in train_loader:\n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, torch.tensor(labels))  # Convert labels to Tensor\n        loss.backward()\n        optimizer.step()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-07T12:09:49.446430Z","iopub.execute_input":"2024-05-07T12:09:49.446731Z","iopub.status.idle":"2024-05-07T13:31:28.425669Z","shell.execute_reply.started":"2024-05-07T12:09:49.446701Z","shell.execute_reply":"2024-05-07T13:31:28.424710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"# Evaluate the model\nmodel.eval()\nval_preds = []\nval_labels = []\nwith torch.no_grad():\n    for images, labels in val_loader:\n        outputs = model(images)\n        _, predicted = torch.max(outputs.data, 1)\n        val_preds.extend(predicted.tolist())\n        val_labels.extend(labels.tolist())\n\nval_accuracy = accuracy_score(val_labels, val_preds)\nprint(\"Validation Accuracy:\", val_accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:31:28.426856Z","iopub.execute_input":"2024-05-07T13:31:28.427159Z","iopub.status.idle":"2024-05-07T13:32:08.623013Z","shell.execute_reply.started":"2024-05-07T13:31:28.427128Z","shell.execute_reply":"2024-05-07T13:32:08.622117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DataLoader named val_loader containing validation data\nval_images, val_labels = next(iter(val_loader))\n\n# Evaluate the model\nmodel.eval()\nwith torch.no_grad():\n    val_outputs = model(val_images)\n    _, val_predicted = torch.max(val_outputs, 1)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:32:08.624148Z","iopub.execute_input":"2024-05-07T13:32:08.624459Z","iopub.status.idle":"2024-05-07T13:32:08.745972Z","shell.execute_reply.started":"2024-05-07T13:32:08.624428Z","shell.execute_reply":"2024-05-07T13:32:08.745132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Confusion matrix\ncm = confusion_matrix(val_labels, val_predicted)\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:32:08.747007Z","iopub.execute_input":"2024-05-07T13:32:08.747293Z","iopub.status.idle":"2024-05-07T13:32:09.114297Z","shell.execute_reply.started":"2024-05-07T13:32:08.747264Z","shell.execute_reply":"2024-05-07T13:32:09.113556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Binarize the labels\nval_labels_bin = label_binarize(val_labels, classes=range(10))\n\n# Compute precision and recall for each class\nprecision = dict()\nrecall = dict()\nfor i in range(10):\n    precision[i], recall[i], _ = precision_recall_curve(val_labels_bin[:, i], val_outputs[:, i])\n\n# Plot precision-recall curve for each class\nplt.figure(figsize=(10, 8))\ncolors = cycle(['navy', 'turquoise', 'darkorange', 'cornflowerblue', 'teal', 'red', 'green', 'yellow', 'blue', 'purple'])\nfor i, color in zip(range(10), colors):\n    plt.plot(recall[i], precision[i], color=color, lw=2, label='Class {}'.format(i))\nplt.xlabel('Recall')\nplt.ylabel('Precision')\nplt.title('Precision-Recall Curve for Each Class')\nplt.legend(loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:32:09.115258Z","iopub.execute_input":"2024-05-07T13:32:09.115523Z","iopub.status.idle":"2024-05-07T13:32:09.360773Z","shell.execute_reply.started":"2024-05-07T13:32:09.115497Z","shell.execute_reply":"2024-05-07T13:32:09.360038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Binarize the labels\nval_labels_bin = label_binarize(val_labels, classes=range(10))\n\n# Compute ROC curve and ROC area for each class\nfpr = dict()\ntpr = dict()\nroc_auc = dict()\nfor i in range(10):\n    fpr[i], tpr[i], _ = roc_curve(val_labels_bin[:, i], val_outputs[:, i])\n    roc_auc[i] = auc(fpr[i], tpr[i])\n\n# Plot ROC curve for each class\nplt.figure(figsize=(10, 8))\ncolors = cycle(['navy', 'turquoise', 'darkorange', 'cornflowerblue', 'teal', 'red', 'green', 'yellow', 'blue', 'purple'])\nfor i, color in zip(range(10), colors):\n    plt.plot(fpr[i], tpr[i], color=color, lw=2, label='ROC curve of class {0} (area = {1:0.2f})'.format(i, roc_auc[i]))\nplt.plot([0, 1], [0, 1], color='gray', lw=2, linestyle='--')\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('Receiver Operating Characteristic Curve for Each Class')\nplt.legend(loc=\"lower right\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:32:09.361714Z","iopub.execute_input":"2024-05-07T13:32:09.361947Z","iopub.status.idle":"2024-05-07T13:32:09.620940Z","shell.execute_reply.started":"2024-05-07T13:32:09.361924Z","shell.execute_reply":"2024-05-07T13:32:09.620214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating submission using test images","metadata":{}},{"cell_type":"code","source":"# Extract images from the test set\ntest_extract_path = \"/kaggle/working/test_images\"\ntest_archive_path = '/kaggle/input/cifar-10/test.7z'\n\nwith py7zr.SevenZipFile(test_archive_path, mode='r') as z:\n    z.extractall(path=test_extract_path)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:32:09.621901Z","iopub.execute_input":"2024-05-07T13:32:09.622169Z","iopub.status.idle":"2024-05-07T13:35:06.743521Z","shell.execute_reply.started":"2024-05-07T13:32:09.622142Z","shell.execute_reply":"2024-05-07T13:35:06.742771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adjust the directory path\ntest_extract_path = os.path.join(test_extract_path, \"test\")","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:35:06.744479Z","iopub.execute_input":"2024-05-07T13:35:06.744724Z","iopub.status.idle":"2024-05-07T13:35:06.748054Z","shell.execute_reply.started":"2024-05-07T13:35:06.744700Z","shell.execute_reply":"2024-05-07T13:35:06.747427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the extracted images and preprocess them\ntransform = transforms.Compose([\n    transforms.Resize((32, 32)),\n    transforms.ToTensor(),\n])\n\ntest_images = []\ntest_ids = []\n\nfor filename in os.listdir(test_extract_path):\n    if filename.endswith('.png'):\n        img_id = filename.split('.')[0]  # Extract image ID from filename\n        img_path = os.path.join(test_extract_path, filename)\n        try:\n            image = Image.open(img_path).convert(\"RGB\")\n            image = transform(image)\n            test_images.append(image)\n            test_ids.append(int(img_id))\n        except Exception as e:\n            print(f\"Error loading image {img_path}: {e}\")\n\nif len(test_images) > 0:\n    test_images = torch.stack(test_images)\n\n    # Define class names\n    class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n                   'dog', 'frog', 'horse', 'ship', 'truck']\n\n    # Make predictions using the trained model\n    model.eval()\n    with torch.no_grad():\n        outputs = model(test_images)\n        _, predicted = torch.max(outputs, 1)\n        predicted_labels = [class_names[p.item()] for p in predicted]\n\n    # Create a DataFrame for submission\n    submission_df = pd.DataFrame({'id': test_ids, 'label': predicted_labels})\n\n    # Save the predictions in a submission CSV file\n    submission_df.to_csv('/kaggle/working/submission.csv', index=False)\nelse:\n    print(\"No image files found in the test directory.\")","metadata":{"execution":{"iopub.status.busy":"2024-05-07T13:35:06.748804Z","iopub.execute_input":"2024-05-07T13:35:06.749013Z","iopub.status.idle":"2024-05-07T13:38:10.106152Z","shell.execute_reply.started":"2024-05-07T13:35:06.748991Z","shell.execute_reply":"2024-05-07T13:38:10.105345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}