{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport torch \nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.utils.data import TensorDataset, DataLoader\nimport cv2\n\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-28T11:06:23.872315Z","iopub.execute_input":"2025-05-28T11:06:23.872555Z","iopub.status.idle":"2025-05-28T11:06:28.883028Z","shell.execute_reply.started":"2025-05-28T11:06:23.872532Z","shell.execute_reply":"2025-05-28T11:06:28.882448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cassava-leaf-disease-classification/train.csv\")\n\nID = df[\"image_id\"].values\nY = df[\"label\"].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:30:26.91187Z","iopub.execute_input":"2025-05-26T06:30:26.912214Z","iopub.status.idle":"2025-05-26T06:30:26.951801Z","shell.execute_reply.started":"2025-05-26T06:30:26.912195Z","shell.execute_reply":"2025-05-26T06:30:26.951117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm\nX = []\n\nfor i in tqdm(range(len(ID))):\n    file_name = ID[i]\n    loc = \"/kaggle/input/cassava-leaf-disease-classification/train_images/\"\n    image = cv2.imread(loc+file_name)\n    image = cv2.resize(image,(256,256))\n    X.append(image)\n\nX = np.array(X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:30:29.635366Z","iopub.execute_input":"2025-05-26T06:30:29.635652Z","iopub.status.idle":"2025-05-26T06:34:32.243166Z","shell.execute_reply.started":"2025-05-26T06:30:29.635631Z","shell.execute_reply":"2025-05-26T06:34:32.242579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X.reshape(-1,3,256,256)\nplt.imshow(X[1][0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:35:59.63039Z","iopub.execute_input":"2025-05-26T06:35:59.631007Z","iopub.status.idle":"2025-05-26T06:35:59.840172Z","shell.execute_reply.started":"2025-05-26T06:35:59.630982Z","shell.execute_reply":"2025-05-26T06:35:59.839565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.DataFrame(Y).value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:23:01.59556Z","iopub.execute_input":"2025-05-26T06:23:01.59586Z","iopub.status.idle":"2025-05-26T06:23:01.606125Z","shell.execute_reply.started":"2025-05-26T06:23:01.595834Z","shell.execute_reply":"2025-05-26T06:23:01.605125Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class leafie(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3,16,3,padding = 1)\n        self.conv2 = nn.Conv2d(16,32,3, padding = 1, stride = 1)\n        self.conv3 = nn.Conv2d(32, 64,3,padding = 1,stride = 1)\n        self.conv4 = nn.Conv2d(64, 128,3,padding = 1,stride = 1)\n\n        self.norm1 = nn.BatchNorm2d(16)\n        self.norm2 = nn.BatchNorm2d(32)\n        self.norm3 = nn.BatchNorm2d(64)\n        self.norm4 = nn.BatchNorm2d(128)\n\n        self.flat = nn.Flatten()\n        self.pool = nn.MaxPool2d(2,2)\n\n        self.fc1 = nn.Linear(64*64*128,1024)\n        self.fc2 = nn.Linear(1024, 512)\n        self.fc3 = nn.Linear(512,5)\n        self.relu = nn.ReLU()\n\n    def forward(self,x):\n        x = self.conv1(x)\n        x = self.norm1(x)\n        x = self.relu(x)\n        x = self.pool(x)\n        x = self.conv2(x)\n        x = self.norm2(x)\n        x = self.relu(x)\n        x = self.conv3(x)\n        x = self.norm3(x)\n        x = self.relu(x)\n        x = self.pool(x)\n        x = self.conv4(x)\n        x = self.norm4(x)\n        x = self.relu(x)\n        x = self.flat(x)\n        x = self.fc1(x)\n        x = self.relu(x)\n        x = self.fc2(x)\n        x = self.relu(x)\n        x = self.fc3(x)\n\n        return F.log_softmax(x, dim =1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:38:55.018931Z","iopub.execute_input":"2025-05-26T06:38:55.019217Z","iopub.status.idle":"2025-05-26T06:38:55.026679Z","shell.execute_reply.started":"2025-05-26T06:38:55.019197Z","shell.execute_reply":"2025-05-26T06:38:55.026046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train,x_test, y_train,y_test = train_test_split(X, Y,test_size =0.1, random_state=42)\n\nx_train_ts = torch.tensor(x_train, dtype= torch.float32)\ny_train_ts = torch.tensor(y_train, dtype= torch.long)\nx_test_ts = torch.tensor(x_test, dtype= torch.float32)\ny_test_ts = torch.tensor(y_test, dtype= torch.long)\n\ntrain_ds = TensorDataset(x_train_ts, y_train_ts)\ntest_ds = TensorDataset(x_test_ts, y_test_ts)\n\ntrain_loader = DataLoader(train_ds, batch_size = 10, shuffle = True)\ntest_loader = DataLoader(test_ds, batch_size = 10, shuffle = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:36:06.248262Z","iopub.execute_input":"2025-05-26T06:36:06.248955Z","iopub.status.idle":"2025-05-26T06:36:13.707739Z","shell.execute_reply.started":"2025-05-26T06:36:06.248932Z","shell.execute_reply":"2025-05-26T06:36:13.707167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torchvision.models import resnet50","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-26T06:50:58.827892Z","iopub.execute_input":"2025-05-26T06:50:58.828149Z","iopub.status.idle":"2025-05-26T06:50:59.619725Z","shell.execute_reply.started":"2025-05-26T06:50:58.82813Z","shell.execute_reply":"2025-05-26T06:50:59.61918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\")\nmodel = resnet50(pretrained = True)\nmodel = model.to(device)\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr = 0.001)\n\nepochs = 7\n\nfor i in range(epochs):\n    loop = tqdm(train_loader, desc=f\"Epoch {i+1}/{epochs}\")\n\n    for x,y in loop:\n        x = x.to(device)\n        y = y.to(device)\n\n        pred = model(x)\n        loss = criterion(pred, y)\n\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    print(f\"Epochs: {epochs+1} Loss: {loss.item()}\")\n        ","metadata":{"trusted":true,"execution":{"execution_failed":"2025-05-26T08:09:46.283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\n\ncorrect = 0\ntotal = 0\n\nwith torch.no_grad():\n    for x,y in test_loader:\n        x = x.to(device)\n        y = y.to(device)\n        pred = model(x)\n        _, prediction = pred.max(1)\n\n        total += y.size(0)\n        correct += (prediction == label).sum()\n\nprint(f\"Accuracy: {correct/total}\")","metadata":{"trusted":true,"execution":{"execution_failed":"2025-05-26T08:09:46.282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_t = pd.read_csv(\"/kaggle/input/cassava-leaf-disease-classification/train.csv\")\n\nID_t = df_t[\"image_id\"].values\n\nfrom tqdm import tqdm\nX_t = []\n\nfor i in tqdm(range(len(ID_t))):\n    file_name = ID_t[i]\n    loc = \"/kaggle/input/cassava-leaf-disease-classification/train_images/\"\n    image = cv2.imread(loc+file_name)\n    image = cv2.resize(image,(256,256))\n    X.append(image)\n\nX = np.array(X)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}