{"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":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":31193,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ========================================================\n# Final Project: Cassava Leaf Disease Classification\n# Deluxe Version with Full Plots for Each Model\n# 1. CNN From Scratch\n# 2. MobileNetV3 (Pretrained)\n# 3. EfficientNet-B3 (Pretrained)\n# ========================================================","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-14T09:29:51.848250Z","iopub.execute_input":"2025-12-14T09:29:51.848600Z","iopub.status.idle":"2025-12-14T09:29:51.866556Z","shell.execute_reply.started":"2025-12-14T09:29:51.848575Z","shell.execute_reply":"2025-12-14T09:29:51.865416Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, models\nfrom torchvision.io import read_image\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, confusion_matrix, classification_report\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\nimport numpy as np\n\ntorch.cuda.empty_cache()\nos.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"expandable_segments:True\"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Paths ------------------\nCSV_PATH = '/kaggle/input/cassava-leaf-disease-classification/train.csv'\nIMG_DIR  = '/kaggle/input/cassava-leaf-disease-classification/train_images'\n\n# ------------------ Load & Split ------------------\ndf = pd.read_csv(CSV_PATH)\n\ntrain_df, temp_df = train_test_split(df, test_size=0.3, stratify=df['label'], random_state=42)\nval_df,   test_df = train_test_split(temp_df, test_size=1/3, stratify=temp_df['label'], random_state=42)\n\nprint(f\"Train: {len(train_df)} | Validation: {len(val_df)} | Test: {len(test_df)}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Dataset ------------------\nclass CassavaDataset(Dataset):\n    def __init__(self, dataframe, transform=None):\n        self.df = dataframe.reset_index(drop=True)\n        self.transform = transform\n    def __len__(self): return len(self.df)\n    def __getitem__(self, idx):\n        img_name = self.df.iloc[idx, 0]\n        image = read_image(os.path.join(IMG_DIR, img_name)).float() / 255.0\n        if image.shape[0] == 1: image = image.repeat(3, 1, 1)\n        label = self.df.iloc[idx, 1]\n        if self.transform: image = self.transform(image)\n        return image, label\n\n# ------------------ Transforms  ------------------\ntransform = transforms.Compose([\n    transforms.Resize((336, 336)),  \n    transforms.RandomHorizontalFlip(p=0.5),\n    transforms.RandomRotation(30),\n    transforms.ColorJitter(0.3,0.3,0.3),\n    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\n\ntrain_dataset = CassavaDataset(train_df, transform)\nval_dataset   = CassavaDataset(val_df,   transform)\ntest_dataset  = CassavaDataset(test_df,  transform)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Loaders (batch_size=32 ) ------------------\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True,  pin_memory=True)\nval_loader   = DataLoader(val_dataset,   batch_size=32, shuffle=False, pin_memory=True)\ntest_loader  = DataLoader(test_dataset,  batch_size=32, shuffle=False, pin_memory=True)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Models ------------------\n# 1. CNN From Scratch\nclass CNNFromScratch(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.features = nn.Sequential(\n            nn.Conv2d(3, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.MaxPool2d(2),\n            nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.MaxPool2d(2),\n        )\n        self.classifier = nn.Sequential(\n            nn.Flatten(),\n            nn.Linear(256 * 21 * 21, 512), nn.ReLU(), nn.Dropout(0.5),\n            nn.Linear(512, 5)\n        )\n    def forward(self, x):\n        x = self.features(x)\n        x = self.classifier(x)\n        return x\n\n# 2. MobileNetV3 Pretrained\nmobilenet = models.mobilenet_v3_large(weights='IMAGENET1K_V1')\nmobilenet.classifier[3] = nn.Linear(mobilenet.classifier[3].in_features, 5)\n\n# 3. EfficientNet-B3 Pretrained\neffnet = models.efficientnet_b3(weights='IMAGENET1K_V1')\neffnet.classifier[1] = nn.Linear(1536, 5)\n\n# Move to GPU\nscratch_model = CNNFromScratch().to('cuda')\nmobilenet.to('cuda')\neffnet.to('cuda')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Training Function with Best Model Saving ------------------\ndef train_model(model, name, epochs=14, lr=3e-4):\n    optimizer = optim.AdamW(model.parameters(), lr=lr)\n    criterion = nn.CrossEntropyLoss()\n\n    train_accs, val_accs = [], []\n    train_losses, val_losses = [], []\n\n    best_val_acc = 0.0\n    best_path = f\"best_{name.lower().replace(' ', '_')}.pth\"\n\n    print(f\"\\nTraining {name} ({epochs} epochs)\")\n    for epoch in range(epochs):\n        # Train\n        model.train()\n        train_loss = 0.0\n        correct, total = 0, 0\n        for images, labels in tqdm(train_loader, desc=f\"{name} Epoch {epoch+1:02d}/{epochs}\"):\n            images, labels = images.cuda(), labels.cuda()\n            optimizer.zero_grad()\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n\n            train_loss += loss.item()\n            _, predicted = torch.max(outputs, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n        train_acc = 100 * correct / total\n        avg_train_loss = train_loss / len(train_loader)\n        train_accs.append(train_acc)\n        train_losses.append(avg_train_loss)\n\n        # Validation\n        model.eval()\n        val_loss = 0.0\n        correct, total = 0, 0\n        with torch.no_grad():\n            for images, labels in val_loader:\n                images, labels = images.cuda(), labels.cuda()\n                outputs = model(images)\n                loss = criterion(outputs, labels)\n                val_loss += loss.item()\n                _, predicted = torch.max(outputs, 1)\n                total += labels.size(0)\n                correct += (predicted == labels).sum().item()\n\n        val_acc = 100 * correct / total\n        avg_val_loss = val_loss / len(val_loader)\n        val_accs.append(val_acc)\n        val_losses.append(avg_val_loss)\n\n        print(f\"{name} - Epoch {epoch+1:02d} → Train Acc: {train_acc:.2f}% | Val Acc: {val_acc:.2f}%\")\n\n        # Save best\n        if val_acc > best_val_acc:\n            best_val_acc = val_acc\n            torch.save(model.state_dict(), best_path)\n            print(f\"   → Best {name} saved! (Val Acc: {val_acc:.2f}%)\")\n\n    return train_accs, val_accs, train_losses, val_losses, best_path, best_val_acc","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Train All Models ------------------\nscratch_train_acc, scratch_val_acc, scratch_train_loss, scratch_val_loss, scratch_path, scratch_best = train_model(scratch_model, \"CNN From Scratch\", epochs=8, lr=1e-3)\nmobilenet_train_acc, mobilenet_val_acc, mobilenet_train_loss, mobilenet_val_loss, mobilenet_path, mobilenet_best = train_model(mobilenet, \"MobileNetV3\", epochs=8, lr=3e-4)\ntorch.cuda.empty_cache()\neffnet_train_acc, effnet_val_acc, effnet_train_loss, effnet_val_loss, effnet_path, effnet_best = train_model(effnet, \"EfficientNet-B3\", epochs=8, lr=3e-4)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Load Best Models & Test Accuracy ------------------\ndef load_and_test(path, model, name):\n    model.load_state_dict(torch.load(path))\n    model.eval()\n    correct = 0\n    total = 0\n    with torch.no_grad():\n        for images, labels in test_loader:\n            images, labels = images.cuda(), labels.cuda()\n            outputs = model(images)\n            _, predicted = torch.max(outputs, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n    acc = 100 * correct / total\n    print(f\"{name} - Test Accuracy (10%): {acc:.2f}%\")\n    return acc\n\nscratch_test = load_and_test(scratch_path, scratch_model, \"CNN From Scratch\")\nmobilenet_test = load_and_test(mobilenet_path, mobilenet, \"MobileNetV3\")\neffnet_test = load_and_test(effnet_path, effnet, \"EfficientNet-B3\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ------------------ Deluxe Plots ------------------\nepochs = range(1, len(scratch_val_acc)+1)\n\n# Accuracy Curves (Train + Val)\nplt.figure(figsize=(14, 6))\nplt.plot(epochs, scratch_train_acc, label='Scratch Train', linestyle='--', color='red')\nplt.plot(epochs, scratch_val_acc, label='Scratch Val', color='red')\nplt.plot(epochs, mobilenet_train_acc, label='MobileNet Train', linestyle='--', color='orange')\nplt.plot(epochs, mobilenet_val_acc, label='MobileNet Val', color='orange')\nplt.plot(epochs, effnet_train_acc, label='EfficientNet Train', linestyle='--', color='green')\nplt.plot(epochs, effnet_val_acc, label='EfficientNet Val', color='green')\nplt.title('Training & Validation Accuracy Comparison')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy (%)')\nplt.legend()\nplt.grid(True)\nplt.show()\n\n# Loss Curves\nplt.figure(figsize=(14, 6))\nplt.plot(epochs, scratch_train_loss, label='Scratch Train Loss', linestyle='--', color='red')\nplt.plot(epochs, scratch_val_loss, label='Scratch Val Loss', color='red')\nplt.plot(epochs, mobilenet_train_loss, label='MobileNet Train Loss', linestyle='--', color='orange')\nplt.plot(epochs, mobilenet_val_loss, label='MobileNet Val Loss', color='orange')\nplt.plot(epochs, effnet_train_loss, label='EfficientNet Train Loss', linestyle='--', color='green')\nplt.plot(epochs, effnet_val_loss, label='EfficientNet Val Loss', color='green')\nplt.title('Training & Validation Loss Comparison')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.grid(True)\nplt.show()\n\n# Test Accuracy Bar\nmodels = ['CNN Scratch', 'MobileNetV3', 'EfficientNet-B3']\ntest_accs = [scratch_test, mobilenet_test, effnet_test]\n\nplt.figure(figsize=(10, 6))\nbars = plt.bar(models, test_accs, color=['red', 'orange', 'green'])\nplt.title('Test Accuracy on 10% Hold-out Set')\nplt.ylabel('Accuracy (%)')\nfor bar in bars:\n    yval = bar.get_height()\n    plt.text(bar.get_x() + bar.get_width()/2, yval + 0.5, f\"{yval:.2f}%\", ha='center')\nplt.ylim(0, 100)\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Final Table\nprint(\"\\n\" + \"=\"*80)\nprint(\"FINAL COMPARISON\")\nprint(\"=\"*80)\nprint(f\"{'Model':<25} {'Best Val Acc':<15} {'Test Acc (10%)'}\")\nprint(\"-\"*80)\nprint(f\"{'CNN From Scratch':<25} {scratch_best:.2f}%{' ':>10} {scratch_test:.2f}%\")\nprint(f\"{'MobileNetV3':<25} {mobilenet_best:.2f}%{' ':>10} {mobilenet_test:.2f}%\")\nprint(f\"{'EfficientNet-B3':<25} {effnet_best:.2f}%{' ':>10} {effnet_test:.2f}%\")\nprint(\"=\"*80)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}