{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":30823,"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for 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,"execution":{"iopub.status.busy":"2025-01-10T05:46:38.519472Z","iopub.execute_input":"2025-01-10T05:46:38.519770Z","iopub.status.idle":"2025-01-10T05:46:38.523278Z","shell.execute_reply.started":"2025-01-10T05:46:38.519738Z","shell.execute_reply":"2025-01-10T05:46:38.522451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport random\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import f1_score\n\nimport torch\nfrom torch import nn\nfrom torch.optim import Adam\nfrom torch.optim.lr_scheduler import ExponentialLR\nfrom torch.utils.data import Dataset, DataLoader\n\nimport torchvision.transforms.v2 as transforms\nimport torchvision.models as models","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:38.524243Z","iopub.execute_input":"2025-01-10T05:46:38.524527Z","iopub.status.idle":"2025-01-10T05:46:43.354870Z","shell.execute_reply.started":"2025-01-10T05:46:38.524506Z","shell.execute_reply":"2025-01-10T05:46:43.353923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Configuration class\nclass Config:\n    SEED = 42\n    VAL_SPLIT = 0.2\n    IMAGE_SIZE = (64, 64)\n    BATCH_SIZE = 32\n    EPOCHS = 5\n    LEARNING_RATE = 1e-4\n    NUM_CLASSES = 5\n\nconfig = Config()\n\ndef set_seed(seed):\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n\nset_seed(config.SEED)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:43.356300Z","iopub.execute_input":"2025-01-10T05:46:43.356701Z","iopub.status.idle":"2025-01-10T05:46:43.368629Z","shell.execute_reply.started":"2025-01-10T05:46:43.356678Z","shell.execute_reply":"2025-01-10T05:46:43.367842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dataset class\nclass CassavaDataset(Dataset):\n    def __init__(self, dataframe, transform=None):\n        self.dataframe = dataframe\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        row = self.dataframe.iloc[idx]\n        image = plt.imread(row['img_path'])\n        label = row['label']\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:43.369692Z","iopub.execute_input":"2025-01-10T05:46:43.369982Z","iopub.status.idle":"2025-01-10T05:46:43.374222Z","shell.execute_reply.started":"2025-01-10T05:46:43.369960Z","shell.execute_reply":"2025-01-10T05:46:43.373545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define transforms\ntrain_transforms = transforms.Compose([\n    transforms.Resize(config.IMAGE_SIZE),\n    transforms.RandomHorizontalFlip(),\n    transforms.RandomVerticalFlip(),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\n\nval_transforms = transforms.Compose([\n    transforms.Resize(config.IMAGE_SIZE),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:43.374980Z","iopub.execute_input":"2025-01-10T05:46:43.375194Z","iopub.status.idle":"2025-01-10T05:46:43.389312Z","shell.execute_reply.started":"2025-01-10T05:46:43.375175Z","shell.execute_reply":"2025-01-10T05:46:43.388618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Data loading\ninput_path = '/kaggle/input/cassava-leaf-disease-classification/'\ntrain_df = pd.read_csv(os.path.join(input_path, 'train.csv'))\ntrain_df['img_path'] = train_df['image_id'].apply(lambda x: os.path.join(input_path, 'train_images', x))\n\ntrain_data, val_data = train_test_split(train_df, test_size=config.VAL_SPLIT, stratify=train_df['label'], random_state=config.SEED)\n\ntrain_dataset = CassavaDataset(train_data, transform=train_transforms)\nval_dataset = CassavaDataset(val_data, transform=val_transforms)\n\ntrain_loader = DataLoader(train_dataset, batch_size=config.BATCH_SIZE, shuffle=True, num_workers=2)\nval_loader = DataLoader(val_dataset, batch_size=config.BATCH_SIZE, shuffle=False, num_workers=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:43.390079Z","iopub.execute_input":"2025-01-10T05:46:43.390352Z","iopub.status.idle":"2025-01-10T05:46:43.470962Z","shell.execute_reply.started":"2025-01-10T05:46:43.390326Z","shell.execute_reply":"2025-01-10T05:46:43.470315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Model definition\nmodel = models.resnet18(pretrained=True)\nmodel.fc = nn.Linear(model.fc.in_features, config.NUM_CLASSES)\nmodel = model.cuda() if torch.cuda.is_available() else model\n\n# Loss and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = Adam(model.parameters(), lr=config.LEARNING_RATE)\nscheduler = ExponentialLR(optimizer, gamma=0.9)\n\n# Mixed precision setup\nscaler = torch.cuda.amp.GradScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:43.472742Z","iopub.execute_input":"2025-01-10T05:46:43.472973Z","iopub.status.idle":"2025-01-10T05:46:44.422781Z","shell.execute_reply.started":"2025-01-10T05:46:43.472956Z","shell.execute_reply":"2025-01-10T05:46:44.422079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training function\ndef train_one_epoch(model, loader, optimizer, criterion):\n    model.train()\n    total_loss = 0\n    correct = 0\n    total = 0\n\n    for images, labels in loader:\n        images, labels = images.cuda(), labels.cuda()\n\n        optimizer.zero_grad()\n        with torch.cuda.amp.autocast():\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n        \n        scaler.scale(loss).backward()\n        scaler.step(optimizer)\n        scaler.update()\n\n        total_loss += loss.item()\n        _, preds = torch.max(outputs, 1)\n        correct += (preds == labels).sum().item()\n        total += labels.size(0)\n\n    return total_loss / len(loader), correct / total","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:44.423634Z","iopub.execute_input":"2025-01-10T05:46:44.423896Z","iopub.status.idle":"2025-01-10T05:46:44.429308Z","shell.execute_reply.started":"2025-01-10T05:46:44.423875Z","shell.execute_reply":"2025-01-10T05:46:44.428456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Validation function\ndef validate_one_epoch(model, loader, criterion):\n    model.eval()\n    total_loss = 0\n    correct = 0\n    total = 0\n\n    with torch.no_grad():\n        for images, labels in loader:\n            images, labels = images.cuda(), labels.cuda()\n\n            with torch.cuda.amp.autocast():\n                outputs = model(images)\n                loss = criterion(outputs, labels)\n\n            total_loss += loss.item()\n            _, preds = torch.max(outputs, 1)\n            correct += (preds == labels).sum().item()\n            total += labels.size(0)\n\n    return total_loss / len(loader), correct / total","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:44.430233Z","iopub.execute_input":"2025-01-10T05:46:44.430450Z","iopub.status.idle":"2025-01-10T05:46:44.442314Z","shell.execute_reply.started":"2025-01-10T05:46:44.430433Z","shell.execute_reply":"2025-01-10T05:46:44.441670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training loop\nfor epoch in range(config.EPOCHS):\n    train_loss, train_acc = train_one_epoch(model, train_loader, optimizer, criterion)\n    val_loss, val_acc = validate_one_epoch(model, val_loader, criterion)\n    scheduler.step()\n\n    print(f\"Epoch {epoch+1}/{config.EPOCHS}\")\n    print(f\"Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}\")\n    print(f\"Val Loss: {val_loss:.4f}, Val Acc: {val_acc:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T05:46:44.443081Z","iopub.execute_input":"2025-01-10T05:46:44.443348Z","iopub.status.idle":"2025-01-10T06:15:21.880745Z","shell.execute_reply.started":"2025-01-10T05:46:44.443326Z","shell.execute_reply":"2025-01-10T06:15:21.879934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save the model\ntorch.save(model.state_dict(), 'cassava_model.pth')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T06:15:21.881787Z","iopub.execute_input":"2025-01-10T06:15:21.882162Z","iopub.status.idle":"2025-01-10T06:15:21.958490Z","shell.execute_reply.started":"2025-01-10T06:15:21.882136Z","shell.execute_reply":"2025-01-10T06:15:21.957849Z"}},"outputs":[],"execution_count":null}]}