{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "Y2EgzZ7x4wEO" }, "source": [ "#### **PROJECT TITLE :** `AI-Powered Medical Image Analysis Tool for Chest (X-Ray and CT-Scan) Diseases Detection`\n", "\n", "
\n", "
\n", "\n", "### Component : `Model Deployment + Testing on Random Images`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "1TmpNqBc1-Y2" }, "outputs": [], "source": [ "# Let us first import all the necessary libraries required for this project\n", "\n", "import tensorflow as tf\n", "import torch\n", "import cv2\n", "import sklearn\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import os\n", "from torchvision import datasets, transforms, models\n", "from torch.utils.data import DataLoader, Dataset\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "import torchvision\n", "import torch.nn.functional as F\n", "from PIL import Image\n", "\n", "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix, roc_curve, auc\n", "\n", "\n", "# Later on, as per requirement, more libraries wil be imported" ] }, { "cell_type": "markdown", "metadata": { "id": "wnk9WBze56wW" }, "source": [ "
\n", "
\n", "\n", "### STEP - 1 : Pre-Processing Functions" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "fdXDwKiV5HFK" }, "outputs": [], "source": [ "def adjust_brightness_contrast(image, alpha=1.2, beta=50):\n", " \"\"\"\n", " Adjusting brightness and contrast of the image.\n", " Parameters:\n", " - image: Input image (numpy array).\n", " - alpha: Contrast control [1.0-3.0].\n", " - beta: Brightness control [0-100].\n", " Returns:\n", " - Adjusted image.\n", " \"\"\"\n", " return cv2.convertScaleAbs(image, alpha=alpha, beta=beta)\n", "\n", "def apply_histogram_equalization(image):\n", " \"\"\"Applying histogram equalization to enhance contrast.\"\"\"\n", " channels = cv2.split(image)\n", " eq_channels = [cv2.equalizeHist(ch) for ch in channels]\n", " return cv2.merge(eq_channels)\n", "\n", "def apply_clahe(image, clip_limit=2.0, tile_grid_size=(8, 8)):\n", " \"\"\"Applying CLAHE for local contrast enhancement.\"\"\"\n", " clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)\n", " channels = cv2.split(image)\n", " clahe_channels = [clahe.apply(ch) for ch in channels]\n", " return cv2.merge(clahe_channels)\n", "\n", "def apply_gaussian_blur(image, kernel_size=(3, 3)):\n", " \"\"\"Applying Gaussian blur for denoising.\"\"\"\n", " return cv2.GaussianBlur(image, kernel_size, 0)\n", "\n", "def apply_sharpening(image):\n", " \"\"\"Applying edge enhancement using a sharpening filter.\"\"\"\n", " kernel = np.array([[0, -1, 0],\n", " [-1, 5, -1],\n", " [0, -1, 0]])\n", " return cv2.filter2D(image, -1, kernel)\n", "\n", "def normalize_image(image):\n", " \"\"\"Normalizing the image to zero mean and unit variance.\"\"\"\n", " image = (image - np.mean(image)) / np.std(image)\n", " return image\n", "\n", "def resize_image(image, width, height):\n", " \"\"\"Resizing the image to the desired dimensions with anti-aliasing.\"\"\"\n", " return cv2.resize(image, (width, height), interpolation=cv2.INTER_CUBIC)" ] }, { "cell_type": "markdown", "metadata": { "id": "jRUZ008d6p1T" }, "source": [ "
\n", "
\n", "\n", "### STEP - 2 : Pre-processing Pipeline" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "KgcX83yc40g-" }, "outputs": [], "source": [ "def preprocess_single_image(img_path, img_height=224, img_width=224):\n", " \"\"\"\n", " Preprocessing a single image as per the training pipeline.\n", " \"\"\"\n", " # Loading image in BGR format\n", " image = cv2.imread(img_path, cv2.IMREAD_COLOR)\n", " if image is None:\n", " raise ValueError(f\"Image not found: {img_path}\")\n", "\n", " # Converting BGR to RGB\n", " image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", "\n", " # Applying preprocessing steps\n", " image = apply_histogram_equalization(image)\n", " image = apply_clahe(image)\n", " image = apply_gaussian_blur(image)\n", " image = apply_sharpening(image)\n", " image = adjust_brightness_contrast(image, alpha=1.2, beta=50)\n", "\n", " # Resizing and normalize\n", " image = resize_image(image, img_width, img_height)\n", " image = normalize_image(image)\n", "\n", " # Converting to PIL image and applyinh transformations\n", " image = Image.fromarray(image.astype(np.uint8))\n", " transform = transforms.Compose([\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", " ])\n", " image_tensor = transform(image).unsqueeze(0) # Adding batch dimension\n", " return image_tensor" ] }, { "cell_type": "markdown", "metadata": { "id": "0KLCcPYa6x_3" }, "source": [ "
\n", "
\n", "\n", "### STEP - 3 : Loading the Model" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hfTSNNQR8Gsu", "outputId": "ef8a7e0b-25e5-4a00-d6e0-70bde079b08c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using device: cuda\n" ] } ], "source": [ "# Detecting GPU if available\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "print(f\"Using device: {device}\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "6tvYopjB7OAS" }, "outputs": [], "source": [ "from torchvision.models import densenet121\n", "from torchvision.transforms import Resize\n", "\n", "# Simplified ViT-like transformer module\n", "class SimpleViT(nn.Module):\n", " def __init__(self, input_dim, num_heads, mlp_dim, num_layers):\n", " super(SimpleViT, self).__init__()\n", " # Reduced TransformerEncoder layer complexity\n", " self.transformer_blocks = nn.ModuleList([\n", " nn.TransformerEncoderLayer(\n", " d_model=input_dim,\n", " nhead=num_heads,\n", " dim_feedforward=mlp_dim,\n", " dropout=0.1\n", " ) for _ in range(num_layers)\n", " ])\n", "\n", " def forward(self, x):\n", " # Flattening the spatial dimensions\n", " B, C, H, W = x.shape\n", " x = x.flatten(2).permute(2, 0, 1) # Reshaping for transformer\n", " for block in self.transformer_blocks:\n", " x = block(x)\n", " x = x.permute(1, 2, 0).reshape(B, C, H, W) # Restoring the original shape\n", " return x\n", "\n", "\n", "# Adjusted Hybrid DenseNet + Simplified ViT Architecture\n", "class LightweightHybridDenseNetViT(nn.Module):\n", " def __init__(self):\n", " super(LightweightHybridDenseNetViT, self).__init__()\n", "\n", " # Loading a lighter DenseNet backbone\n", " self.densenet = densenet121(pretrained=False) # Base DenseNet backbone\n", "\n", " # Reducing the output channels from DenseNet to smaller dimensions\n", " self.conv_reduce = nn.Conv2d(1024, 64, kernel_size=1) # Fewer channels\n", "\n", " # ViT processing module with reduced complexity\n", " self.vit = SimpleViT(input_dim=64, num_heads=2, mlp_dim=128, num_layers=1)\n", "\n", " # Task-specific classification heads\n", " self.fc_pneumonia = nn.Linear(64, 1) # Binary classification (Pneumonia)\n", " self.fc_tuberculosis = nn.Linear(64, 1) # Binary classification (Tuberculosis)\n", " self.fc_lung_cancer = nn.Linear(64, 4) # Multi-class output (Lung Cancer)\n", "\n", " def forward(self, x):\n", " # Extracting DenseNet features\n", " x = self.densenet.features(x) # Extracting DenseNet feature maps\n", " x = self.conv_reduce(x) # Reducing the number of feature channels\n", "\n", " # Passing through simplified ViT module\n", " x = self.vit(x)\n", "\n", " # Applying Global Average Pooling (GAP)\n", " x = x.mean(dim=[2, 3]) # Pooling across spatial dimensions\n", "\n", " # Task-specific classification\n", " pneumonia_output = torch.sigmoid(self.fc_pneumonia(x)) # Binary sigmoid output\n", " tuberculosis_output = torch.sigmoid(self.fc_tuberculosis(x)) # Binary sigmoid output\n", " lung_cancer_output = self.fc_lung_cancer(x) # Multi-class logits\n", "\n", " return pneumonia_output, tuberculosis_output, lung_cancer_output" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "P4vHdzkA52Oe", "outputId": "c10c4f4a-e6d9-4e3c-d34a-4dc53a306c66" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "e:\\anaconda3\\envs\\CVIP_ENV\\Lib\\site-packages\\torchvision\\models\\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n", " warnings.warn(\n", "e:\\anaconda3\\envs\\CVIP_ENV\\Lib\\site-packages\\torchvision\\models\\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=None`.\n", " warnings.warn(msg)\n", "C:\\Users\\dpsvn\\AppData\\Local\\Temp\\ipykernel_31420\\2949034845.py:8: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " model.load_state_dict(torch.load(\"CVIP_PROJ_model_FINAL.pth\", map_location=device)) # Mapping to the correct device\n" ] }, { "data": { "text/plain": [ "LightweightHybridDenseNetViT(\n", " (densenet): DenseNet(\n", " (features): Sequential(\n", " (conv0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n", " (norm0): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu0): ReLU(inplace=True)\n", " (pool0): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n", " (denseblock1): _DenseBlock(\n", " (denselayer1): _DenseLayer(\n", " (norm1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(64, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer2): _DenseLayer(\n", " (norm1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(96, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer3): _DenseLayer(\n", " (norm1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer4): _DenseLayer(\n", " (norm1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(160, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer5): _DenseLayer(\n", " (norm1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer6): _DenseLayer(\n", " (norm1): BatchNorm2d(224, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(224, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " )\n", " (transition1): _Transition(\n", " (norm): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu): ReLU(inplace=True)\n", " (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n", " )\n", " (denseblock2): _DenseBlock(\n", " (denselayer1): _DenseLayer(\n", " (norm1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer2): _DenseLayer(\n", " (norm1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(160, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer3): _DenseLayer(\n", " (norm1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer4): _DenseLayer(\n", " (norm1): BatchNorm2d(224, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(224, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer5): _DenseLayer(\n", " (norm1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer6): _DenseLayer(\n", " (norm1): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(288, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer7): _DenseLayer(\n", " (norm1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(320, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer8): _DenseLayer(\n", " (norm1): BatchNorm2d(352, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(352, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer9): _DenseLayer(\n", " (norm1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(384, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer10): _DenseLayer(\n", " (norm1): BatchNorm2d(416, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(416, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer11): _DenseLayer(\n", " (norm1): BatchNorm2d(448, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(448, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer12): _DenseLayer(\n", " (norm1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(480, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " )\n", " (transition2): _Transition(\n", " (norm): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu): ReLU(inplace=True)\n", " (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n", " )\n", " (denseblock3): _DenseBlock(\n", " (denselayer1): _DenseLayer(\n", " (norm1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer2): _DenseLayer(\n", " (norm1): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(288, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer3): _DenseLayer(\n", " (norm1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(320, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer4): _DenseLayer(\n", " (norm1): BatchNorm2d(352, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(352, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer5): _DenseLayer(\n", " (norm1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(384, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer6): _DenseLayer(\n", " (norm1): BatchNorm2d(416, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(416, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer7): _DenseLayer(\n", " (norm1): BatchNorm2d(448, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(448, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer8): _DenseLayer(\n", " (norm1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(480, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer9): _DenseLayer(\n", " (norm1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer10): _DenseLayer(\n", " (norm1): BatchNorm2d(544, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(544, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer11): _DenseLayer(\n", " (norm1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(576, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer12): _DenseLayer(\n", " (norm1): BatchNorm2d(608, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(608, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer13): _DenseLayer(\n", " (norm1): BatchNorm2d(640, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(640, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer14): _DenseLayer(\n", " (norm1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(672, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer15): _DenseLayer(\n", " (norm1): BatchNorm2d(704, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(704, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer16): _DenseLayer(\n", " (norm1): BatchNorm2d(736, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(736, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer17): _DenseLayer(\n", " (norm1): BatchNorm2d(768, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(768, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer18): _DenseLayer(\n", " (norm1): BatchNorm2d(800, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(800, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer19): _DenseLayer(\n", " (norm1): BatchNorm2d(832, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(832, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer20): _DenseLayer(\n", " (norm1): BatchNorm2d(864, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(864, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer21): _DenseLayer(\n", " (norm1): BatchNorm2d(896, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(896, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer22): _DenseLayer(\n", " (norm1): BatchNorm2d(928, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(928, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer23): _DenseLayer(\n", " (norm1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(960, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer24): _DenseLayer(\n", " (norm1): BatchNorm2d(992, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(992, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " )\n", " (transition3): _Transition(\n", " (norm): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu): ReLU(inplace=True)\n", " (conv): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n", " )\n", " (denseblock4): _DenseBlock(\n", " (denselayer1): _DenseLayer(\n", " (norm1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer2): _DenseLayer(\n", " (norm1): BatchNorm2d(544, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(544, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer3): _DenseLayer(\n", " (norm1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(576, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer4): _DenseLayer(\n", " (norm1): BatchNorm2d(608, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(608, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer5): _DenseLayer(\n", " (norm1): BatchNorm2d(640, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(640, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer6): _DenseLayer(\n", " (norm1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(672, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer7): _DenseLayer(\n", " (norm1): BatchNorm2d(704, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(704, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer8): _DenseLayer(\n", " (norm1): BatchNorm2d(736, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(736, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer9): _DenseLayer(\n", " (norm1): BatchNorm2d(768, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(768, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer10): _DenseLayer(\n", " (norm1): BatchNorm2d(800, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(800, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer11): _DenseLayer(\n", " (norm1): BatchNorm2d(832, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(832, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer12): _DenseLayer(\n", " (norm1): BatchNorm2d(864, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(864, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer13): _DenseLayer(\n", " (norm1): BatchNorm2d(896, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(896, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer14): _DenseLayer(\n", " (norm1): BatchNorm2d(928, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(928, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer15): _DenseLayer(\n", " (norm1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(960, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " (denselayer16): _DenseLayer(\n", " (norm1): BatchNorm2d(992, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu1): ReLU(inplace=True)\n", " (conv1): Conv2d(992, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (relu2): ReLU(inplace=True)\n", " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n", " )\n", " )\n", " (norm5): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " )\n", " (classifier): Linear(in_features=1024, out_features=1000, bias=True)\n", " )\n", " (conv_reduce): Conv2d(1024, 64, kernel_size=(1, 1), stride=(1, 1))\n", " (vit): SimpleViT(\n", " (transformer_blocks): ModuleList(\n", " (0): TransformerEncoderLayer(\n", " (self_attn): MultiheadAttention(\n", " (out_proj): NonDynamicallyQuantizableLinear(in_features=64, out_features=64, bias=True)\n", " )\n", " (linear1): Linear(in_features=64, out_features=128, bias=True)\n", " (dropout): Dropout(p=0.1, inplace=False)\n", " (linear2): Linear(in_features=128, out_features=64, bias=True)\n", " (norm1): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n", " (norm2): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n", " (dropout1): Dropout(p=0.1, inplace=False)\n", " (dropout2): Dropout(p=0.1, inplace=False)\n", " )\n", " )\n", " )\n", " (fc_pneumonia): Linear(in_features=64, out_features=1, bias=True)\n", " (fc_tuberculosis): Linear(in_features=64, out_features=1, bias=True)\n", " (fc_lung_cancer): Linear(in_features=64, out_features=4, bias=True)\n", ")" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Loading the saved model\n", "img_size = 224 # Matching the dimensions used during training\n", "patch_size = 8\n", "\n", "\n", "model = LightweightHybridDenseNetViT().to(device)\n", "\n", "model.load_state_dict(torch.load(\"model_FINAL.pth\", map_location=device)) # Mapping to the correct device\n", "model.to(device) # Moving the model to GPU/CPU\n", "model.eval() # Setting to evaluation mode" ] }, { "cell_type": "markdown", "metadata": { "id": "55xG6fIf-Juc" }, "source": [ "
\n", "
\n", "\n", "### STEP - 4 : Preprocessing the Image and Moving to GPU" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "gBSz6oqF-KpK" }, "outputs": [], "source": [ "image_tensor = preprocess_single_image(\"Deploy-Test Images/642x361_SLIDE_4_What_Does_Lung_Cancer_Look_Like.jpg\") # Path to the random image\n", "image_tensor = image_tensor.to(device) # Moving the input tensor to the same device" ] }, { "cell_type": "markdown", "metadata": { "id": "Xqcipj_lDUFk" }, "source": [ "
\n", "
\n", "\n", "### STEP - 5 : Performing Inference on GPU" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CYfM_IZRDOSJ", "outputId": "bc1ff79f-200c-4938-ddae-113316912060" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pneumonia Probability: 0.9692\n", "TB Probability: 0.8210\n", "Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0384\n", " large.cell.carcinoma_left.hilum: 0.0717\n", " NORMAL: 0.7883\n", " squamous.cell.carcinoma_left.hilum: 0.1015\n" ] } ], "source": [ "# Performing inference\n", "with torch.no_grad():\n", " pneumonia_output, tb_output, lung_cancer_output = model(image_tensor)\n", "\n", "# Converting outputs to probabilities\n", "pneumonia_prob = pneumonia_output.item() # Sigmoid ensures output in [0, 1]\n", "tb_prob = tb_output.item()\n", "lung_cancer_probs = F.softmax(lung_cancer_output, dim=1).squeeze().tolist()\n", "\n", "# Class names for lung cancer\n", "lung_cancer_classes = [\n", " \"adenocarcinoma_left.lower.lobe\",\n", " \"large.cell.carcinoma_left.hilum\",\n", " \"NORMAL\",\n", " \"squamous.cell.carcinoma_left.hilum\"\n", "]\n", "\n", "# Printing results\n", "print(f\"Pneumonia Probability: {pneumonia_prob:.4f}\")\n", "print(f\"TB Probability: {tb_prob:.4f}\")\n", "\n", "# Printing lung cancer probabilities with class names\n", "print(\"Lung Cancer Probabilities:\")\n", "for class_name, prob in zip(lung_cancer_classes, lung_cancer_probs):\n", " print(f\" {class_name}: {prob:.4f}\")" ] }, { "cell_type": "markdown", "metadata": { "id": "O99Sb29nEI9y" }, "source": [ "
\n", "
\n", "\n", "### STEP - 6 : Taking a directory of images into consideration" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "vjKToO-WDcDj" }, "outputs": [], "source": [ "# Directory containing the test images\n", "\n", "image_folder = \"Deploy-Test Images\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ee6Ct_z3ETpS", "outputId": "18727f97-3ee7-4f66-8678-95ff1aed58a4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Image: 4994014ef5c834e4803541aa1dc874_big_gallery.jpg\n", " Pneumonia Probability: 0.9534\n", " TB Probability: 0.9138\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0689\n", " large.cell.carcinoma_left.hilum: 0.1746\n", " NORMAL: 0.4685\n", " squamous.cell.carcinoma_left.hilum: 0.2880\n", "\n", "Image: 4ceeb362df213ccf2c0b4d6388bba1_gallery.jpg\n", " Pneumonia Probability: 0.9514\n", " TB Probability: 0.7690\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0709\n", " large.cell.carcinoma_left.hilum: 0.1399\n", " NORMAL: 0.5900\n", " squamous.cell.carcinoma_left.hilum: 0.1993\n", "\n", "Image: 642x361_SLIDE_4_What_Does_Lung_Cancer_Look_Like.jpg\n", " Pneumonia Probability: 0.9692\n", " TB Probability: 0.8210\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0384\n", " large.cell.carcinoma_left.hilum: 0.0717\n", " NORMAL: 0.7883\n", " squamous.cell.carcinoma_left.hilum: 0.1015\n", "\n", "Image: Chest_radiograph_of_miliary_tuberculosis_2.jpg\n", " Pneumonia Probability: 0.9638\n", " TB Probability: 0.0253\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.2013\n", " large.cell.carcinoma_left.hilum: 0.3206\n", " NORMAL: 0.1717\n", " squamous.cell.carcinoma_left.hilum: 0.3064\n", "\n", "Image: Dense_opacity_of_primary_pulmonary_tuberculosis.jpg\n", " Pneumonia Probability: 0.9611\n", " TB Probability: 0.5479\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.1466\n", " large.cell.carcinoma_left.hilum: 0.2521\n", " NORMAL: 0.1795\n", " squamous.cell.carcinoma_left.hilum: 0.4219\n", "\n", "Image: eXtmE1V2XgsjZK2JolVQ5g_Border_of_left_atrium.jpg\n", " Pneumonia Probability: 0.3524\n", " TB Probability: 0.9830\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0413\n", " large.cell.carcinoma_left.hilum: 0.0669\n", " NORMAL: 0.8216\n", " squamous.cell.carcinoma_left.hilum: 0.0702\n", "\n", "Image: iStock_22401848_MEDIUM-58262cb63df78c6f6adebb27.jpg\n", " Pneumonia Probability: 0.8128\n", " TB Probability: 0.9765\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0307\n", " large.cell.carcinoma_left.hilum: 0.0801\n", " NORMAL: 0.7960\n", " squamous.cell.carcinoma_left.hilum: 0.0932\n", "\n", "Image: lung-scan-article-l.jpg\n", " Pneumonia Probability: 0.9594\n", " TB Probability: 0.5150\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0991\n", " large.cell.carcinoma_left.hilum: 0.1557\n", " NORMAL: 0.5415\n", " squamous.cell.carcinoma_left.hilum: 0.2036\n", "\n", "Image: lungcancer.jpg\n", " Pneumonia Probability: 0.5294\n", " TB Probability: 0.6075\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0749\n", " large.cell.carcinoma_left.hilum: 0.0500\n", " NORMAL: 0.8214\n", " squamous.cell.carcinoma_left.hilum: 0.0537\n", "\n", "Image: Normal_posteroanterior_(PA)_chest_radiograph_(X-ray).jpg\n", " Pneumonia Probability: 0.9510\n", " TB Probability: 0.4999\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0698\n", " large.cell.carcinoma_left.hilum: 0.1852\n", " NORMAL: 0.4874\n", " squamous.cell.carcinoma_left.hilum: 0.2576\n", "\n", "Image: photo.jpg\n", " Pneumonia Probability: 0.5363\n", " TB Probability: 0.2967\n", " Lung Cancer Probabilities:\n", " adenocarcinoma_left.lower.lobe: 0.0169\n", " large.cell.carcinoma_left.hilum: 0.0072\n", " NORMAL: 0.9690\n", " squamous.cell.carcinoma_left.hilum: 0.0069\n" ] } ], "source": [ "# Looping through the folder, preprocessing each image, and running inference\n", "results = []\n", "for image_name in os.listdir(image_folder):\n", " image_path = os.path.join(image_folder, image_name)\n", "\n", " if not image_name.lower().endswith(('.png', '.jpg', '.jpeg')): # Ensuring it's an image file\n", " continue\n", "\n", " # Preprocessing the image\n", " image_tensor = preprocess_single_image(image_path)\n", " image_tensor = image_tensor.to(device)\n", "\n", " # Performing inference\n", " with torch.no_grad():\n", " pneumonia_output, tb_output, lung_cancer_output = model(image_tensor)\n", "\n", " # Converting outputs to probabilities\n", " pneumonia_prob = pneumonia_output.item()\n", " tb_prob = tb_output.item()\n", " lung_cancer_probs = F.softmax(lung_cancer_output, dim=1).squeeze().tolist()\n", "\n", " # Appending the results for the current image\n", " results.append({\n", " \"image_name\": image_name,\n", " \"pneumonia_prob\": pneumonia_prob,\n", " \"tb_prob\": tb_prob,\n", " \"lung_cancer_probs\": lung_cancer_probs\n", " })\n", "\n", " lung_cancer_classes = [\n", " \"adenocarcinoma_left.lower.lobe\",\n", " \"large.cell.carcinoma_left.hilum\",\n", " \"NORMAL\",\n", " \"squamous.cell.carcinoma_left.hilum\"\n", " ]\n", "\n", " # Printing the results\n", " print(f\"\\nImage: {image_name}\")\n", " print(f\" Pneumonia Probability: {pneumonia_prob:.4f}\")\n", " print(f\" TB Probability: {tb_prob:.4f}\")\n", "\n", " # Printing lung cancer probabilities with class names\n", " print(\" Lung Cancer Probabilities:\")\n", " for class_name, prob in zip(lung_cancer_classes, lung_cancer_probs):\n", " print(f\" {class_name}: {prob:.4f}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "TByqsA1pEkdf" }, "outputs": [], "source": [] } ], "metadata": { "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [] }, "kernelspec": { "display_name": "AlooBonda", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 0 }