{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# torch for core pytorch libraray\nimport torch\n\n# torch.nn contains functions for defining neural networks\nimport torch.nn as nn\n\n# torch.optim contains optimization \nimport torch.optim as optim\n\n# torchvision helps in handling image datasets\nimport torchvision\n\n# torchvision.transforms contains func for preprocessing images\nimport torchvision.transforms as transforms\n\n# loading of resnet-18\nfrom torchvision import models, datasets\n\n# contains data loading utilities\nfrom torch.utils.data import DataLoader, random_split\n\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:17:28.743128Z","iopub.execute_input":"2025-01-29T16:17:28.743541Z","iopub.status.idle":"2025-01-29T16:17:28.764734Z","shell.execute_reply.started":"2025-01-29T16:17:28.743498Z","shell.execute_reply":"2025-01-29T16:17:28.76331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for utilizing gpu \ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:17:38.028328Z","iopub.execute_input":"2025-01-29T16:17:38.028731Z","iopub.status.idle":"2025-01-29T16:17:38.03385Z","shell.execute_reply.started":"2025-01-29T16:17:38.028704Z","shell.execute_reply":"2025-01-29T16:17:38.032694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform_cifar = transforms.Compose([\n    transforms.Resize((64, 64)),  # Resize to 64x64 pixels (if you want uniform size across datasets)\n    transforms.ToTensor(),  # Convert to tensor\n    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),  # Normalize to [-1, 1] for CIFAR-10\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:17:43.064798Z","iopub.execute_input":"2025-01-29T16:17:43.065163Z","iopub.status.idle":"2025-01-29T16:17:43.070901Z","shell.execute_reply.started":"2025-01-29T16:17:43.065135Z","shell.execute_reply":"2025-01-29T16:17:43.069369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cifar_trainset = torchvision.datasets.CIFAR10(\n    root='./data', train=True, download=True, transform=transform_cifar)\ncifar_testset = torchvision.datasets.CIFAR10(\n    root='./data', train=False, download=True, transform=transform_cifar)\n\n# Create DataLoaders\ncifar_trainloader = DataLoader(cifar_trainset, batch_size=64, shuffle=True)\ncifar_testloader = DataLoader(cifar_testset, batch_size=64, shuffle=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:17:47.3526Z","iopub.execute_input":"2025-01-29T16:17:47.353005Z","iopub.status.idle":"2025-01-29T16:17:49.566435Z","shell.execute_reply.started":"2025-01-29T16:17:47.352978Z","shell.execute_reply":"2025-01-29T16:17:49.56534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport os\n\ninput_path = '/kaggle/input/dogs-vs-cats'\n\n# Define the unzipped folder where you want to store the dataset\nunzip_dir = '/kaggle/working/dogs-vs-cats/'\n\n# Unzip training data\nwith zipfile.ZipFile(os.path.join(input_path, 'train.zip'), 'r') as zip_ref:\n    zip_ref.extractall(os.path.join(unzip_dir, 'train'))\n\n# Unzip test data\nwith zipfile.ZipFile(os.path.join(input_path, 'test1.zip'), 'r') as zip_ref:\n    zip_ref.extractall(os.path.join(unzip_dir, 'test'))\n\n# Verifying \nprint(os.listdir(os.path.join(unzip_dir, 'train')))\nprint(os.listdir(os.path.join(unzip_dir, 'test')))\n\n# Define transformations for the images\ntransform_catDogs = transforms.Compose([\n    transforms.Resize((64, 64)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\n\n# Load training and test datasets\ntrain_dataset = datasets.ImageFolder(root=os.path.join(unzip_dir, 'train'), transform=transform_catDogs)\ntest_dataset = datasets.ImageFolder(root=os.path.join(unzip_dir, 'test'), transform=transform_catDogs)\n\n# Create DataLoader for training and test sets\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:17:55.106931Z","iopub.execute_input":"2025-01-29T16:17:55.107355Z","iopub.status.idle":"2025-01-29T16:18:07.962828Z","shell.execute_reply.started":"2025-01-29T16:17:55.107325Z","shell.execute_reply":"2025-01-29T16:18:07.961401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn as nn\nclass CNN(nn.Module):\n    def __init__(self, activation_function, weight_init):\n        super(CNN, self).__init__()\n        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)\n        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)\n        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.fc1 = nn.Linear(128 * 8 * 8, 512)\n        self.fc2 = nn.Linear(512, 10)  # 10 classes for CIFAR-10\n\n        self.activation = activation_function  \n\n        # Apply weight initialization\n        self.weight_init(weight_init)\n\n    def forward(self, x):\n        x = self.pool(self.activation(self.conv1(x)))\n        x = self.pool(self.activation(self.conv2(x)))\n        x = self.pool(self.activation(self.conv3(x)))\n        x = x.view(-1, 128 * 8 * 8)\n        x = self.activation(self.fc1(x))\n        x = self.fc2(x)\n        return x\n\n    def weight_init(self, init_type):\n        \"\"\" Apply weight initialization to model \"\"\"\n        if init_type == 'xavier':\n            nn.init.xavier_normal_(self.conv1.weight)\n            nn.init.xavier_normal_(self.conv2.weight)\n            nn.init.xavier_normal_(self.conv3.weight)\n            nn.init.xavier_normal_(self.fc1.weight)\n            nn.init.xavier_normal_(self.fc2.weight)\n        elif init_type == 'kaiming':\n            nn.init.kaiming_normal_(self.conv1.weight, nonlinearity='relu')\n            nn.init.kaiming_normal_(self.conv2.weight, nonlinearity='relu')\n            nn.init.kaiming_normal_(self.conv3.weight, nonlinearity='relu')\n            nn.init.kaiming_normal_(self.fc1.weight, nonlinearity='relu')\n            nn.init.kaiming_normal_(self.fc2.weight, nonlinearity='relu')\n        elif init_type == 'random':\n            nn.init.normal_(self.conv1.weight, 0, 0.01)\n            nn.init.normal_(self.conv2.weight, 0, 0.01)\n            nn.init.normal_(self.conv3.weight, 0, 0.01)\n            nn.init.normal_(self.fc1.weight, 0, 0.01)\n            nn.init.normal_(self.fc2.weight, 0, 0.01)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:27:34.787995Z","iopub.execute_input":"2025-01-29T16:27:34.788499Z","iopub.status.idle":"2025-01-29T16:27:34.801763Z","shell.execute_reply.started":"2025-01-29T16:27:34.788462Z","shell.execute_reply":"2025-01-29T16:27:34.800636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import the functional API from PyTorch\nimport torch.nn.functional as F  \n\n# 3 activation functions \nactivation_functions = {\n    \"relu\": F.relu,\n    \"tanh\": torch.tanh,\n    \"leaky_relu\": F.leaky_relu\n}\n\n# Optimizers\noptimizers = {\n    \"sgd\": optim.SGD,\n    \"adam\": optim.Adam,\n    \"rmsprop\": optim.RMSprop\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:27:38.68096Z","iopub.execute_input":"2025-01-29T16:27:38.68159Z","iopub.status.idle":"2025-01-29T16:27:38.692545Z","shell.execute_reply.started":"2025-01-29T16:27:38.681532Z","shell.execute_reply":"2025-01-29T16:27:38.690944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_model(model, train_loader, criterion, optimizer, device, epochs=5):\n    model.to(device)\n    model.train()\n\n    for epoch in range(epochs):\n        running_loss = 0.0\n        correct = 0\n        total = 0\n\n        for images, labels in 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            _, predicted = torch.max(outputs, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n        accuracy = 100 * correct / total\n        print(f\"Epoch {epoch+1}/{epochs} - Loss: {running_loss:.4f}, Accuracy: {accuracy:.2f}%\")\n\n    print(\"Training Complete!\")\n    return model\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:27:43.322779Z","iopub.execute_input":"2025-01-29T16:27:43.323346Z","iopub.status.idle":"2025-01-29T16:27:43.334043Z","shell.execute_reply.started":"2025-01-29T16:27:43.323302Z","shell.execute_reply":"2025-01-29T16:27:43.331952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.save(model.state_dict(), \"best_model.pth\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:27:51.357624Z","iopub.execute_input":"2025-01-29T16:27:51.358031Z","iopub.status.idle":"2025-01-29T16:27:51.417828Z","shell.execute_reply.started":"2025-01-29T16:27:51.358Z","shell.execute_reply":"2025-01-29T16:27:51.416312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def test_model(model, test_loader, criterion, device):\n    model.to(device)\n    model.eval()\n\n    correct = 0\n    total = 0\n    test_loss = 0.0\n\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\n            test_loss += loss.item()\n            _, predicted = torch.max(outputs, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n    accuracy = 100 * correct / total\n    avg_loss = test_loss / len(test_loader)\n\n    print(f\"Test Accuracy: {accuracy:.2f}%\")\n    print(f\"Test Loss: {avg_loss:.4f}\")\n    return accuracy, avg_loss\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:27:54.407244Z","iopub.execute_input":"2025-01-29T16:27:54.407641Z","iopub.status.idle":"2025-01-29T16:27:54.415695Z","shell.execute_reply.started":"2025-01-29T16:27:54.407614Z","shell.execute_reply":"2025-01-29T16:27:54.413959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set device (use GPU if available)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Choose activation function and weight initialization method\nactivation_fn = F.relu  # Change to F.tanh or F.leaky_relu for other experiments\nweight_init = 'xavier'  # Change to 'kaiming' or 'random' for other experiments\n\n# Create model\nmodel = CNN(activation_fn, weight_init).to(device)  # Now passing both arguments\n\n# Define loss function & optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Train model\ntrained_model = train_model(model, train_loader, criterion, optimizer, device, epochs=10)\n\n# Save trained model\ntorch.save(trained_model.state_dict(), \"best_model.pth\")\n\n# Load trained model before testing\nbest_model = CNN(activation_fn, weight_init)\nbest_model.load_state_dict(torch.load(\"best_model.pth\"))\nbest_model.to(device)\n\n# Test model\ntest_model(best_model, test_loader, criterion, device)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T16:28:34.070569Z","iopub.execute_input":"2025-01-29T16:28:34.070914Z"}},"outputs":[],"execution_count":null}]}