{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\n\n# Define data transformations (normalization and augmentation)\ntransform_train = transforms.Compose([\n    transforms.RandomCrop(32, padding=4),\n    transforms.RandomHorizontalFlip(),\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))  # Normalizing CIFAR10 dataset\n])\n\ntransform_test = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))\n])\n\n# Download the CIFAR10 dataset\ntrain_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)\ntest_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)\n\n# Create data loaders\ntrain_loader = DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=2)\ntest_loader = DataLoader(test_dataset, batch_size=128, shuffle=False, num_workers=2)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-05T09:11:35.407682Z","iopub.execute_input":"2024-10-05T09:11:35.408898Z","iopub.status.idle":"2024-10-05T09:11:37.015145Z","shell.execute_reply.started":"2024-10-05T09:11:35.408844Z","shell.execute_reply":"2024-10-05T09:11:37.014278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nimport torch.nn.functional as F \n\n# Define the ResNet block\nclass ResidualBlock(nn.Module):\n    def __init__(self, in_channels, out_channels, stride=1):\n        super(ResidualBlock, self).__init__()\n        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)\n        self.bn1 = nn.BatchNorm2d(out_channels)\n        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)\n        self.bn2 = nn.BatchNorm2d(out_channels)\n\n        self.shortcut = nn.Sequential()\n        if stride != 1 or in_channels != out_channels:\n            self.shortcut = nn.Sequential(\n                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),\n                nn.BatchNorm2d(out_channels)\n            )\n\n    def forward(self, x):\n        out = self.conv1(x)\n        out = self.bn1(out)\n        out = torch.relu(out)\n        out = self.conv2(out)\n        out = self.bn2(out)\n        shortcut = self.shortcut(x)\n        out += shortcut\n        out = torch.relu(out)\n        return out\n\n# Define the ResNet architecture\nclass ResNet(nn.Module):\n    def __init__(self, block, num_blocks, num_classes=10):\n        super(ResNet, self).__init__()\n        self.in_channels = 64\n\n        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)\n        self.bn1 = nn.BatchNorm2d(64)\n        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)\n        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)\n        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)\n        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)\n        self.linear = nn.Linear(512, num_classes)\n\n    def _make_layer(self, block, out_channels, num_blocks, stride):\n        strides = [stride] + [1] * (num_blocks - 1)\n        layers = []\n        for stride in strides:\n            layers.append(block(self.in_channels, out_channels, stride))\n            self.in_channels = out_channels\n        return nn.Sequential(*layers)\n\n    def forward(self, x):\n        out = self.conv1(x)\n        out = self.bn1(out)\n        out = torch.relu(out)\n        out = self.layer1(out)\n        out = self.layer2(out)\n        out = self.layer3(out)\n        out = self.layer4(out)\n        out = F.avg_pool2d(out, 4)\n        out = out.view(out.size(0), -1)\n        out = self.linear(out)\n        return out\n\n# Function to define the model (ResNet18 for CIFAR10)\ndef ResNet18():\n    return ResNet(ResidualBlock, [2, 2, 2, 2])","metadata":{"execution":{"iopub.status.busy":"2024-10-05T09:11:40.238406Z","iopub.execute_input":"2024-10-05T09:11:40.238819Z","iopub.status.idle":"2024-10-05T09:11:40.258852Z","shell.execute_reply.started":"2024-10-05T09:11:40.238777Z","shell.execute_reply":"2024-10-05T09:11:40.258032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training function\ndef train(model, train_loader, criterion, optimizer, device):\n    model.train()\n    running_loss = 0.0\n    for i, (images, labels) in enumerate(train_loader):\n        images, labels = images.to(device), labels.to(device)\n        \n        optimizer.zero_grad()\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        \n        running_loss += loss.item()\n        if (i + 1) % 100 == 0:\n            print(f\"Batch {i+1}, Loss: {running_loss / (i+1):.4f}\")\n\n# Validation function\ndef validate(model, test_loader, criterion, device):\n    model.eval()\n    total = 0\n    correct = 0\n    test_loss = 0.0\n    with torch.no_grad():\n        for images, labels in test_loader:\n            images, labels = images.to(device), labels.to(device)\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            test_loss += loss.item()\n            _, predicted = outputs.max(1)\n            total += labels.size(0)\n            correct += predicted.eq(labels).sum().item()\n    accuracy = correct / total\n    print(f\"Validation Loss: {test_loss / len(test_loader):.4f}, Accuracy: {accuracy * 100:.2f}%\")\n    return accuracy","metadata":{"execution":{"iopub.status.busy":"2024-10-05T09:11:46.424389Z","iopub.execute_input":"2024-10-05T09:11:46.424796Z","iopub.status.idle":"2024-10-05T09:11:46.435202Z","shell.execute_reply.started":"2024-10-05T09:11:46.424750Z","shell.execute_reply":"2024-10-05T09:11:46.434206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def main():\n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n    # Initialize model, loss, and optimizer\n    model = ResNet18().to(device)\n    criterion = nn.CrossEntropyLoss()\n    optimizer = optim.Adam(model.parameters(), lr=0.001)\n\n    # Train and validate the model\n    for epoch in range(20):  # Train for 10 epochs\n        print(f\"Epoch {epoch + 1}\")\n        train(model, train_loader, criterion, optimizer, device)\n        validate(model, test_loader, criterion, device)\n\n    # Save the model\n    torch.save(model, \"cifar10_resnet18.pth\")\n\nif __name__ == \"__main__\":\n    main()","metadata":{"execution":{"iopub.status.busy":"2024-10-05T09:11:51.069805Z","iopub.execute_input":"2024-10-05T09:11:51.070184Z","iopub.status.idle":"2024-10-05T09:27:02.546591Z","shell.execute_reply.started":"2024-10-05T09:11:51.070146Z","shell.execute_reply":"2024-10-05T09:27:02.545402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install  py7zr\n# Required Imports\nimport py7zr\nimport os\n\n# Define the folder path you want to create\nfolder_path = '/kaggle/working/test_comp'\n\n# Create the directory if it doesn't exist\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\n# Decompressing data\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-05T08:31:19.982087Z","iopub.execute_input":"2024-10-05T08:31:19.982515Z","iopub.status.idle":"2024-10-05T08:34:05.534039Z","shell.execute_reply.started":"2024-10-05T08:31:19.982462Z","shell.execute_reply":"2024-10-05T08:34:05.533139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom PIL import Image\nfrom tqdm import tqdm\n\n\n\n# Define the transformations for the test images (same as training)\ntest_transforms = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))  # Same normalization as training set\n])\n\n\n# Make a list of data filenames for dataset\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\n# Define dataset\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 = Image.open(self.filenames[idx])\n        transformed_img = self.transforms(img)\n        return transformed_img\n\n# Load dataset\ntest_dataset = CIFAR10(test_filenames, test_transforms)\n\ntest_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)\n\n\n# CIFAR-10 classes\nclasses = ['airplane', 'automobile', 'bird', 'cat', 'deer', \n           'dog', 'frog', 'horse', 'ship', 'truck']\n\n# Assuming the model is already trained and saved, load the model\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = torch.load('/kaggle/working/cifar10_resnet18.pth', map_location = device)  # Load trained weights\n\nresult= []\nwith torch.no_grad():\n    model.eval()\n    for batch_in in tqdm(test_loader):\n        batch_in = batch_in.to(device)\n        \n        y_pred = model(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-05T09:29:44.093288Z","iopub.execute_input":"2024-10-05T09:29:44.093680Z","iopub.status.idle":"2024-10-05T09:33:28.789459Z","shell.execute_reply.started":"2024-10-05T09:29:44.093640Z","shell.execute_reply":"2024-10-05T09:33:28.788642Z"},"trusted":true},"execution_count":null,"outputs":[]}]}