{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":9197489,"sourceType":"datasetVersion","datasetId":5560526}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# https://www.kaggle.com/competitions/cassava-leaf-disease-classification/discussion/200308","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-18T14:55:58.375416Z","iopub.execute_input":"2024-08-18T14:55:58.375787Z","iopub.status.idle":"2024-08-18T14:55:58.380718Z","shell.execute_reply.started":"2024-08-18T14:55:58.375758Z","shell.execute_reply":"2024-08-18T14:55:58.379810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os                                         # To work with OS commands\nimport pandas as pd\nfrom PIL import Image                             # To read Images\nfrom termcolor import colored                     # To Colorfull output\nfrom datetime import datetime                     # To calculate durations\n\nimport numpy as np                                # To work with Numpy arrays\nimport matplotlib.pyplot as plt                   # To Visualization\nimport seaborn as sns                             # To Visualization\n\nimport torch                                      # To work with TORCH framework\nimport torch.nn as nn                             # To work with Neural Networks\nimport torchvision                                # To work with image datasets\nimport torchvision.transforms as transforms       # To create data transforms\nfrom torch.utils.data import Dataset, DataLoader,random_split\n\nfrom torchvision.models import efficientnet_b5, EfficientNet_B5_Weights # Pretrained model with its weights\n\nfrom sklearn.metrics import confusion_matrix, classification_report # To Evaluate the result","metadata":{"execution":{"iopub.status.busy":"2024-08-19T05:28:24.592353Z","iopub.execute_input":"2024-08-19T05:28:24.593056Z","iopub.status.idle":"2024-08-19T05:28:24.602427Z","shell.execute_reply.started":"2024-08-19T05:28:24.593004Z","shell.execute_reply":"2024-08-19T05:28:24.600818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### JUST Write your Model Structure here and then import model which will fix up your weights and biases . We have confirmed this!\nBy Checking the model weights . Also when running  'if param.requires_grad:' , We get the convol layer[6] too which We define in\nour custom model but didnt write in this notebook . but still we get that !! So it means Our model is\nfully loaded by this steps only !!","metadata":{}},{"cell_type":"code","source":"from torchvision.models import vgg16\nvgg16_model= vgg16\n\nnum_classes =5\n\nclass CNN(nn.Module) :\n    def __init__(self, num_classes) :\n        super(CNN, self).__init__()\n        ####  CONVs\n        self.conv_layers = vgg16_model     \n        ####  Dense\n        self.dense_layers = nn.Sequential(\n            nn.Linear(1000, 256),  # UPDATE !efficientnet_model has 1000 classes in its layer thats why we take from there\n            nn.ReLU(),\n            nn.Linear(256, 64),\n            nn.ReLU(),\n            nn.Linear(64, 5)    # The number 5 corresponds to the number of output classes\n        )\n    def forward(self, X) :\n        out = self.conv_layers(X)\n         # the output tensor is typically multi-dimensional (e.g., [batch_size, channels, height, width]). out.size(0): This is the batch size\n        out = out.view(out.size(0), -1)    # used to flatten the output tensor\n        out = self.dense_layers(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2024-08-19T05:32:25.012989Z","iopub.execute_input":"2024-08-19T05:32:25.014212Z","iopub.status.idle":"2024-08-19T05:32:25.021768Z","shell.execute_reply.started":"2024-08-19T05:32:25.014148Z","shell.execute_reply":"2024-08-19T05:32:25.020609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Dont miss this bcz We saved our model in CPU model in the actual notebook \n\n> map_location=torch.device('cpu') ","metadata":{}},{"cell_type":"code","source":"# Load the model with map_location set to 'cpu'\nmodel = torch.load('/kaggle/input/my-full-model/my_VGG16_full_model.pth', map_location=torch.device('cpu'))\n\nprint(type(model))  # This should now show the correct type\ndevice = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nprint(device)\n\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-08-19T05:34:28.524793Z","iopub.execute_input":"2024-08-19T05:34:28.525711Z","iopub.status.idle":"2024-08-19T05:34:28.995025Z","shell.execute_reply.started":"2024-08-19T05:34:28.525673Z","shell.execute_reply":"2024-08-19T05:34:28.993958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print weights after training\nfor name, param in model.named_parameters():\n    if param.requires_grad:                     \n        ## we get conv-layers[6] printed here bcz We unfreeze the last layer as 'true' so that it get trained..that means our model was correctly loaded\n        print(f\"Layer: {name}, Weight shape: {param.data.shape}\")\n        print(f\"Trained weights:\\n{param.data}\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-19T05:34:30.097535Z","iopub.execute_input":"2024-08-19T05:34:30.097927Z","iopub.status.idle":"2024-08-19T05:34:30.122428Z","shell.execute_reply.started":"2024-08-19T05:34:30.097896Z","shell.execute_reply":"2024-08-19T05:34:30.121344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose(\n    [        \n        transforms.ToTensor(),                         # Convert images to tensor\n        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # Normalize R, G, B channels\n    ]\n)\n\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2024-08-18T14:56:09.508771Z","iopub.execute_input":"2024-08-18T14:56:09.509030Z","iopub.status.idle":"2024-08-18T14:56:09.524018Z","shell.execute_reply.started":"2024-08-18T14:56:09.509009Z","shell.execute_reply":"2024-08-18T14:56:09.523133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir='/kaggle/input/cassava-leaf-disease-classification/test_images'\n\nclass TestDataset(Dataset):\n    def __init__(self, root_dir, transform=None):\n        self.root_dir = root_dir\n        self.transform = transform\n        self.image_names = [f for f in os.listdir(root_dir) if os.path.isfile(os.path.join(root_dir, f))]\n    \n    def __len__(self):\n        return len(self.image_names)\n    \n    def __getitem__(self, idx):\n        img_name = os.path.join(self.root_dir, self.image_names[idx])  # Get image path\n        image = Image.open(img_name).convert('RGB')  # Open image and convert to RGB\n        \n        if self.transform:\n            image = self.transform(image)  # Apply transformations if any\n        \n        return image, self.image_names[idx]  # Return image and filename\n    \nprint(\"transforming test images to tensors done!!!!!!!\")\n\n\ntest_dataset = TestDataset(root_dir=test_dir, transform=transform)\n                           \ntest_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)\n\nnum_files = len(test_dataset)\n\nprint(f'Number of files in the test dataset: {num_files}')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-18T14:56:09.525175Z","iopub.execute_input":"2024-08-18T14:56:09.525461Z","iopub.status.idle":"2024-08-18T14:56:09.538061Z","shell.execute_reply.started":"2024-08-18T14:56:09.525438Z","shell.execute_reply":"2024-08-18T14:56:09.537144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ensure the model is in evaluation mode\n#model.to(device)\nmodel.eval()\n\n# Initialize lists to store predictions and filenames\npredictions = []\nimg_names = []\n\n# Load the dataset\ntest_dataset = TestDataset(root_dir=test_dir, transform=transform)\ntest_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)\n\n# Start prediction\nt0 = datetime.now()\n\nwith torch.no_grad():\n    for images, file_names in test_loader:\n        images = images.to(device)                 ## ENSURE IT !!!!!!!!!\n        outputs = model(images)\n        _, preds = torch.max(outputs, 1)\n        \n        # Convert predictions and filenames to lists\n        predictions.extend(preds.cpu().numpy())\n        img_names.extend(file_names)\n\n# Create a DataFrame for submission\nsubmission_df = pd.DataFrame({\n    'image_id': img_names,\n    'label': predictions\n})\n\n# Save the DataFrame to a CSV file\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\n\n# Calculate and print duration\ndt = datetime.now() - t0\nprint(f\"Prediction Duration: {dt}\")\n\nprint(\"Submission CSV created successfully.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-18T14:57:01.170176Z","iopub.execute_input":"2024-08-18T14:57:01.170927Z","iopub.status.idle":"2024-08-18T14:57:01.745710Z","shell.execute_reply.started":"2024-08-18T14:57:01.170892Z","shell.execute_reply":"2024-08-18T14:57:01.744715Z"},"trusted":true},"execution_count":null,"outputs":[]}]}