{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This installs a library to easily extract the .7z files\n!pip install py7zr\n\n# Import all necessary libraries\nimport os\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset, DataLoader, random_split\nfrom torchvision import models\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.preprocessing import LabelEncoder\n\n# Check if GPU is available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-17T10:23:35.614086Z","iopub.execute_input":"2025-09-17T10:23:35.614448Z","iopub.status.idle":"2025-09-17T10:23:56.415120Z","shell.execute_reply.started":"2025-09-17T10:23:35.614423Z","shell.execute_reply":"2025-09-17T10:23:56.414297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\n\n# Extract training images\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r') as z:\n    z.extractall(path='/kaggle/working/')\n    \n# Extract test images\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n    z.extractall(path='/kaggle/working/')\n\nprint(\"Extraction complete!\")\nprint(\"Train images:\", len(os.listdir('/kaggle/working/train/')))\nprint(\"Test images:\", len(os.listdir('/kaggle/working/test/')))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-17T10:24:12.906744Z","iopub.execute_input":"2025-09-17T10:24:12.907024Z","iopub.status.idle":"2025-09-17T10:26:36.305238Z","shell.execute_reply.started":"2025-09-17T10:24:12.906995Z","shell.execute_reply":"2025-09-17T10:26:36.304403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the training labels\ntrain_labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n# Map the image id to its label (e.g., '1' -> 'frog')\nlabel_dict = dict(zip(train_labels_df['id'], train_labels_df['label']))\nle = LabelEncoder()\ntrain_labels_df['label_encoded'] = le.fit_transform(train_labels_df['label'])\nlabel_encoded_dict = dict(zip(train_labels_df['id'], train_labels_df['label_encoded']))\n\n# Define image transformations (preprocessing)\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])\n])\n\nclass CIFAR10Dataset(Dataset):\n    def __init__(self, image_dir, label_dict=None, transform=None):\n        self.image_dir = image_dir\n        self.image_filenames = os.listdir(image_dir)\n        self.label_dict = label_dict\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_filenames)\n\n    def __getitem__(self, idx):\n        img_name = self.image_filenames[idx]\n        img_path = os.path.join(self.image_dir, img_name)\n        # Extract the ID from the filename (e.g., '1.png' -> 1)\n        img_id = int(os.path.splitext(img_name)[0])\n        image = Image.open(img_path).convert('RGB')\n        \n        if self.transform:\n            image = self.transform(image)\n            \n        if self.label_dict is not None:\n            # If it's the training set, get the label\n            label = self.label_dict[img_id]\n            return image, label\n        else:\n            # If it's the test set, just return the image and ID\n            return image, img_id\n\n# Create the full training dataset\nfull_train_dataset = CIFAR10Dataset(\n    image_dir='/kaggle/working/train/',\n    label_dict=label_encoded_dict,\n    transform=transform\n)\n\n# Let's split the training data into train and validation sets (90%/10%)\ntrain_size = int(0.9 * len(full_train_dataset))\nval_size = len(full_train_dataset) - train_size\ntrain_dataset, val_dataset = random_split(full_train_dataset, [train_size, val_size])\n\n# Create DataLoaders to feed data in batches\nbatch_size = 128\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=2, pin_memory=True)\nval_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)\n\nprint(f\"Training batches: {len(train_loader)}\")\nprint(f\"Validation batches: {len(val_loader)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-17T10:27:23.148112Z","iopub.execute_input":"2025-09-17T10:27:23.148707Z","iopub.status.idle":"2025-09-17T10:27:23.226488Z","shell.execute_reply.started":"2025-09-17T10:27:23.148679Z","shell.execute_reply":"2025-09-17T10:27:23.225770Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load a pre-trained ResNet18 model and modify it for CIFAR-10 (32x32 images)\nmodel = models.resnet18(weights='IMAGENET1K_V1')\n\n# Modify the first convolutional layer to accept 32x32 images better (smaller kernel & stride)\nmodel.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)\n# Modify the final fully connected layer for our 10 classes\nnum_ftrs = model.fc.in_features\nmodel.fc = nn.Linear(num_ftrs, 10)\n\nmodel = model.to(device) # Move model to GPU\n\n# Define loss function and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-4)\n# Learning rate scheduler to help convergence\nscheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-17T10:27:40.607963Z","iopub.execute_input":"2025-09-17T10:27:40.608887Z","iopub.status.idle":"2025-09-17T10:27:41.464718Z","shell.execute_reply.started":"2025-09-17T10:27:40.608855Z","shell.execute_reply":"2025-09-17T10:27:41.463943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_epochs = 15\ntrain_losses, val_accuracies = [], []\n\nfor epoch in range(num_epochs):\n    # Training Phase\n    model.train()\n    running_loss = 0.0\n    for batch_idx, (data, target) in enumerate(train_loader):\n        data, target = data.to(device), target.to(device)\n        \n        optimizer.zero_grad()\n        output = model(data)\n        loss = criterion(output, target)\n        loss.backward()\n        optimizer.step()\n        \n        running_loss += loss.item()\n        \n    avg_train_loss = running_loss / len(train_loader)\n    train_losses.append(avg_train_loss)\n    \n    # Validation Phase\n    model.eval()\n    all_preds = []\n    all_targets = []\n    with torch.no_grad():\n        for data, target in val_loader:\n            data, target = data.to(device), target.to(device)\n            output = model(data)\n            _, preds = torch.max(output, 1)\n            all_preds.extend(preds.cpu().numpy())\n            all_targets.extend(target.cpu().numpy())\n            \n    val_accuracy = accuracy_score(all_targets, all_preds)\n    val_accuracies.append(val_accuracy)\n    \n    scheduler.step()\n    print(f'Epoch {epoch+1}/{num_epochs} - Train Loss: {avg_train_loss:.4f}, Val Acc: {val_accuracy:.4f}')\n\n# Plot training history\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(train_losses)\nplt.title('Training Loss')\nplt.subplot(1, 2, 2)\nplt.plot(val_accuracies)\nplt.title('Validation Accuracy')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-17T10:28:17.952058Z","iopub.execute_input":"2025-09-17T10:28:17.952358Z","iopub.status.idle":"2025-09-17T10:32:41.316804Z","shell.execute_reply.started":"2025-09-17T10:28:17.952336Z","shell.execute_reply":"2025-09-17T10:32:41.315981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create Dataset and DataLoader for the test set\ntest_dataset = CIFAR10Dataset(\n    image_dir='/kaggle/working/test/',\n    label_dict=None, # No labels for test set\n    transform=transform\n)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=2)\n\n# Predict on the test set\nmodel.eval()\ntest_preds = {}\nwith torch.no_grad():\n    for data, img_ids in test_loader:\n        data = data.to(device)\n        output = model(data)\n        _, preds = torch.max(output, 1)\n        \n        # Map the numerical predictions back to class names (e.g., 0 -> 'airplane')\n        pred_classes = le.inverse_transform(preds.cpu().numpy())\n        \n        for img_id, pred_class in zip(img_ids, pred_classes):\n            test_preds[img_id] = pred_class\n\n# Create the submission DataFrame\nsubmission_df = pd.DataFrame.from_dict(test_preds, orient='index').reset_index()\nsubmission_df.columns = ['id', 'label']\nsubmission_df = submission_df.sort_values(by='id') # Ensure IDs are in order\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\n\nprint(\"Submission file created!\")\nprint(submission_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-17T10:33:42.594851Z","iopub.execute_input":"2025-09-17T10:33:42.595568Z","iopub.status.idle":"2025-09-17T10:35:43.219643Z","shell.execute_reply.started":"2025-09-17T10:33:42.595539Z","shell.execute_reply":"2025-09-17T10:35:43.218889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}