AlooBonda commited on
Commit
f033a7b
·
1 Parent(s): 5c815ba

Project Pushed to HuggingFace

Browse files
Deploy-Test Images/4994014ef5c834e4803541aa1dc874_big_gallery.jpg ADDED
Deploy-Test Images/4ceeb362df213ccf2c0b4d6388bba1_gallery.jpg ADDED
Deploy-Test Images/642x361_SLIDE_4_What_Does_Lung_Cancer_Look_Like.jpg ADDED
Deploy-Test Images/Chest_radiograph_of_miliary_tuberculosis_2.jpg ADDED
Deploy-Test Images/Dense_opacity_of_primary_pulmonary_tuberculosis.jpg ADDED
Deploy-Test Images/Normal_posteroanterior_(PA)_chest_radiograph_(X-ray).jpg ADDED
Deploy-Test Images/eXtmE1V2XgsjZK2JolVQ5g_Border_of_left_atrium.jpg ADDED
Deploy-Test Images/iStock_22401848_MEDIUM-58262cb63df78c6f6adebb27.jpg ADDED
Deploy-Test Images/lung-scan-article-l.jpg ADDED
Deploy-Test Images/lungcancer.jpg ADDED
Deploy-Test Images/photo.jpg ADDED
README.md CHANGED
@@ -1,13 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- title: ChestDiseaseDetection
3
- emoji: 🏆
4
- colorFrom: pink
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.9.1
8
- app_file: app.py
9
- pinned: false
10
- short_description: Chest Disease Detection (Custom DenseNet-ViT Architecture)
 
 
 
 
 
 
 
 
 
 
11
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
+ # Chest Disease Detection Using Custom DenseNet-ViT (Hybrid) Architecture
2
+
3
+ <br>
4
+ <div style="display: flex; justify-content: space-between;">
5
+ <img src="https://github.com/SoubhikSinha/Chest-Disease-Detection-Using-Custom-DenseNet-ViT-Architecture/blob/main/Deploy-Test%20Images/4ceeb362df213ccf2c0b4d6388bba1_gallery.jpg" style="height: 200px; width: auto;">
6
+ <img src="https://github.com/SoubhikSinha/Chest-Disease-Detection-Using-Custom-DenseNet-ViT-Architecture/blob/main/Deploy-Test%20Images/642x361_SLIDE_4_What_Does_Lung_Cancer_Look_Like.jpg?raw=true" style="height: 200px; width: auto;">
7
+ </div>
8
+
9
+
10
+ Acknowledgements
11
+ ---
12
+ I would like to express my heartfelt gratitude to the creators of the datasets used in this project : `Paul Timothy Mooney` for the *Chest X-Ray Pneumonia Dataset*, `Tawsifur Rahman` for the *Tuberculosis Chest X-Ray Dataset*, and `Mohamed Hanyyy` for the *Chest CT-Scan Images Dataset*, all available on Kaggle. Their invaluable contributions provided the foundation for the development of this project. I also extend my sincere thanks to my peers and mentors for their guidance, support, and feedback, which played a crucial role in the success of this project.
13
+
14
+ <br>
15
+
16
+ About the Project
17
+ ---
18
+ This project focuses on developing an AI-driven medical image analysis system aimed at diagnosing three major thoracic diseases : *[Pneumonia](https://medlineplus.gov/pneumonia.html), [Tuberculosis](https://www.who.int/news-room/fact-sheets/detail/tuberculosis#:~:text=Tuberculosis%20(TB)%20is%20an%20infectious,been%20infected%20with%20TB%20bacteria.), and [Lung cancer](https://medlineplus.gov/lungcancer.html#:~:text=Lung%20cancer%20is%20cancer%20that,differently%20and%20are%20treated%20differently.)*. By leveraging advanced deep learning techniques, the system processes radiological images, including chest [X-rays](https://www.nibib.nih.gov/science-education/science-topics/x-rays#:~:text=X%2Drays%20are%20a%20form,and%20structures%20inside%20the%20body.) and [CT scan](https://my.clevelandclinic.org/health/diagnostics/4808-ct-computed-tomography-scan) images, to classify pathologies and provide probability estimates for each condition. The tool enhances diagnostic accuracy and efficiency, supporting healthcare professionals in clinical decision-making and showcasing the potential for integrating AI into modern medical workflows. To make the system more accessible, a user-friendly application based on the *[Gradio](https://www.gradio.app/)* framework is developed, allowing users to upload chest imaging data and receive real-time predictions with associated probabilities. The application provides probabilistic predictions for pneumonia and tuberculosis, and multi-class probabilistic predictions for lung cancer, acting as an intuitive interface that bridges complex AI models and clinical practice for both medical professionals and researchers.
19
+
20
+ <br>
21
+
22
+ Datasets
23
+ ---
24
+
25
+ - [Chest X-Ray Images for Pneumonia](https://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia) ▶️ Consists of 5,856 images, with 3,878 labeled as `infected` and 1,349 labeled as `normal`.
26
+ - [Chest X-Ray Images for Tuberculosis](https://www.kaggle.com/datasets/tawsifurrahman/tuberculosis-tb-chest-xray-dataset) ▶️ Contains 4,200 images, of which 700 are labeled as `infected` and 3,500 as `normal`.
27
+ - [Chest CT-Scan Images](https://www.kaggle.com/datasets/mohamedhanyyy/chest-ctscan-images) ▶️ Comprises 1,000 CT-scan images, focusing on the classification of lung cancer types, specifically *[Adenocarcinoma](https://my.clevelandclinic.org/health/diseases/21652-adenocarcinoma-cancers), [Large Cell Carcinoma (LCC)](https://lcfamerica.org/about-lung-cancer/diagnosis/types/large-cell-carcinomas/), and [Squamous Cell Carcinoma (SCC)](https://www.skincancer.org/skin-cancer-information/squamous-cell-carcinoma/)*.
28
+
29
+ <br>
30
+
31
+ Image Preprocessing Techniques
32
  ---
33
+ 1. **Histogram Equalization** ▶️ A technique that enhances the contrast of an image by redistributing its pixel intensity values, making the histogram more uniform.
34
+
35
+ 2. **CLAHE (Contrast Limited Adaptive Histogram Equalization)** : A variant of histogram equalization that operates locally on small regions (tiles) of the image to prevent over-amplification of noise in homogeneous areas.
36
+
37
+ 3. **Gaussian Blur** ▶️ A smoothing technique used to reduce noise and detail in an image by applying a Gaussian filter, which averages pixel values based on their distance from the center.
38
+
39
+ 4. **Resizing with Anti-Aliasing** ▶️ A process of changing an image's dimensions while using anti-aliasing techniques to smooth the image and reduce visual distortion or pixelation.
40
+
41
+ 5. **Edge Enhancement (Sharpening)** ▶️ A technique used to enhance the edges of objects in an image by amplifying high-frequency components, making them more distinct.
42
+
43
+ 6. **Intensity Normalization** ▶️ A method of adjusting the pixel values of an image to a specific range, typically [0, 1] or [0, 255], to standardize the image intensity for further processing.
44
+
45
+ 7. **Tensor Conversion and Normalization to [-1, 1] Range** ▶️ The process of converting image data into a tensor format and normalizing pixel values to a range of [-1, 1] for neural network compatibility and improved model performance.
46
+
47
+ 8. **Brightness and Contrast** ▶️ Techniques that adjust the overall brightness (lightness) and contrast (difference between light and dark areas) of an image to enhance visual clarity or correct lighting conditions.
48
+
49
+ <br>
50
+
51
+ DenseNet-ViT Hybrid Architecture
52
  ---
53
+ The `LightweightHybridDenseNetViT` architecture combines a [DenseNet](https://arxiv.org/abs/1608.06993) backbone with a simplified [Vision Transformer (ViT)](https://arxiv.org/abs/2010.11929) module for tasks like pneumonia, tuberculosis, and lung cancer classification. Below is a detailed breakdown of each component of the architecture :
54
+
55
+ ### 1. **DenseNet Backbone (`self.densenet`)** :
56
+
57
+ - **DenseNet-121** : This is a pretrained model from the `torchvision` library, specifically [DenseNet121](https://pytorch.org/vision/main/models/generated/torchvision.models.densenet121.html). DenseNet is a type of convolutional neural network where each layer receives input from all previous layers, facilitating feature reuse and mitigating the vanishing gradient problem.
58
+ - **Feature Extraction** : The DenseNet is used primarily as a feature extractor. The model is loaded without pretrained weights (`pretrained=False`), and it outputs a rich set of features which are passed to subsequent layers.
59
+
60
+ ### 2. **Feature Channel Reduction (`self.conv_reduce`)** :
61
+
62
+ - **1x1 Convolution Layer** : After passing through DenseNet, the feature maps are typically high-dimensional (e.g., 1024 channels). To reduce the number of channels, a 1x1 convolution (`self.conv_reduce`) is applied, which reduces the 1024 channels down to 64. This reduction helps in minimizing the computational complexity and is a necessary step before passing the feature maps to the transformer module.
63
+
64
+ ### 3. **Simplified Vision Transformer (`self.vit`)** :
65
+
66
+ - **Simplified ViT Module** : A Vision Transformer (ViT) is used for processing the feature maps from DenseNet. ViTs operate on sequences of patches extracted from an image and learn relationships between these patches. However, in this case, a **simplified ViT** module is employed to reduce the complexity compared to traditional ViTs.
67
+ - **TransformerEncoderLayer** : Each ViT block contains a `TransformerEncoderLayer`, which performs self-attention and then a feed-forward transformation (MLP - Multi-Level Perceptron). The ViT module here uses a smaller model with only 1 layer (`num_layers=1`), 2 attention heads (`num_heads=2`), and a smaller hidden dimension of 128 (`mlp_dim=128`).
68
+ - **Flattening and Reshaping** : Before passing the input feature map through the transformer, the input tensor is flattened to a sequence of tokens (patches) by using the `x.flatten(2).permute(2, 0, 1)` operation. This transforms the feature map from the shape `(B, C, H, W)` into a sequence that the transformer can process. After the transformer processes this sequence, the result is reshaped back into the spatial dimensions of the input.
69
+
70
+ ### 4. **Global Average Pooling (GAP)** :
71
+
72
+ - **Pooling across Spatial Dimensions** : The output from the ViT module is a set of feature maps with dimensions `(B, C, H, W)`. To summarize this spatial information, **Global Average Pooling (GAP)** is applied (`x.mean(dim=[2, 3])`). GAP calculates the average of each feature map across the spatial dimensions (`H` and `W`), resulting in a vector of size `(B, C)`, where `C` is the number of channels (64 in this case). This operation reduces the spatial resolution, allowing the model to focus on the global features in the image.
73
+
74
+ ### 5. **Task-Specific Classification Heads** :
75
+
76
+ After the feature maps are pooled, the resulting vector is passed to three different fully connected (FC) layers for classification:
77
+
78
+ - **Pneumonia Output (`self.fc_pneumonia`)** : A single output neuron with a sigmoid activation function, indicating a binary classification for pneumonia (0 or 1). The output is passed through the sigmoid activation to produce a probability.
79
+ - **Tuberculosis Output (`self.fc_tuberculosis`)** : Similar to the pneumonia output, this FC layer also has a single output neuron for binary classification of tuberculosis.
80
+ - **Lung Cancer Output (`self.fc_lung_cancer`)** : This output head has 4 output neurons, which corresponds to a multi-class classification task for lung cancer. The model outputs raw logits (scores for each class), which can be used for multi-class classification tasks.
81
+
82
+ ### 6. **Forward Pass** :
83
+
84
+ - **Input Processing** : The input image `x` is passed through DenseNet’s convolutional layers to extract feature maps.
85
+ - **Feature Reduction** : A 1x1 convolution reduces the number of output channels.
86
+ - **Transformer Processing** : The reduced feature maps are passed through the simplified Vision Transformer.
87
+ - **Pooling** : Global Average Pooling is applied to the output of the transformer.
88
+ - **Classification** : The pooled features are passed through the task-specific classification heads to produce predictions for pneumonia, tuberculosis, and lung cancer.
89
+
90
+ ### Summary of Workflow :
91
+
92
+ 1. **DenseNet** extracts features from the input image.
93
+ 2. **Conv1x1 layer** reduces the number of output channels.
94
+ 3. **Simplified ViT** module processes the features, learning long-range dependencies between patches.
95
+ 4. **Global Average Pooling** aggregates spatial information.
96
+ 5. **Classification heads** output predictions for multiple tasks (pneumonia, tuberculosis, lung cancer).
97
+
98
+ This architecture efficiently combines the strengths of both DenseNet (feature reuse and deep representations) and Vision Transformers (modeling long-range dependencies), while keeping the model lightweight and computationally feasible for real-time or resource-constrained applications.
99
+
100
+ <br>
101
+
102
+ How to run the project ?
103
+ ---
104
+ ### **1. Clone the Repository**
105
+ Clone the repository to your local machine :<br>
106
+ ```bash
107
+ git clone https://github.com/SoubhikSinha/Chest-Disease-Detection-Using-Custom-DenseNet-ViT-Architecture.git
108
+ ```
109
+ ### **2. Virtual Environment (Creation + Activation)**
110
+
111
+ Navigate to the repository's root directory and create a Conda virtual environment :<br>
112
+ ```bash
113
+ cd Chest-Disease-Detection-Using-Custom-DenseNet-ViT-Architecture
114
+ ./create.sh
115
+ ```
116
+ <br>
117
+
118
+ This will :
119
+
120
+ - Create a Conda environment (`Chest_Disease`)
121
+ - Activate the environment
122
+ - Install the required dependencies listed in `requirements.txt`
123
+
124
+ **NOTE** : You may execute these commands in your machine's default CLI tool **if it supports scripting language files (.sh)**. If you encounter issues (especially on Windows), you may need to execute the commands using [Git Bash](https://git-scm.com/downloads). Ensure that you are running Git Bash from the **root directory** of the cloned repository.
125
+ <br>
126
+ If the `.sh` script execution isn't working in your default CLI, try the following steps in **Git Bash** :
127
+
128
+ 1. Install Git Bash from the link above.
129
+ 2. Open Git Bash and navigate to the repository directory.
130
+ 3. Execute the commands in the terminal within Git Bash.
131
+
132
+ Alternatively, if you don't want to rely on `.sh` scripts, you can manually execute the necessary steps :
133
+ ```bash
134
+ conda create --name Chest_Disease python=3.x -y
135
+ conda activate Chest_Disease
136
+ pip install -r requirements.txt
137
+ ```
138
+
139
+ ### **3. Running the Project**
140
+ Once your environment is set up, you can run the project and start the Gradio-based web application by executing the following command :<br>
141
+ ```bash
142
+ ./run.sh
143
+ ```
144
+ <br>
145
+
146
+
147
+ Alternatively, you can run the commands manually by executing :
148
+ ```bash
149
+ conda activate Chest_Disease
150
+ python final_project_Gradio_App.py
151
+ ```
152
 
153
+ ### **4. Accessing the Gradio Web Application**
154
+ Once the application starts, it will provide a link to the Gradio interface (usually something like `http://x.x.x.x:7860/`). Open this URL in your browser to interact with the web application.
create.sh ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Checking if requirements.txt exists
4
+ if [ ! -f requirements.txt ]; then
5
+ echo "Error: requirements.txt not found! Please ensure it exists in the current directory."
6
+ exit 1
7
+ fi
8
+
9
+ # Defining the environment name
10
+ env_name="Chest_Disease"
11
+
12
+ # Creating the conda environment
13
+ conda create --name "$env_name" --yes python=3.11
14
+ if [ $? -ne 0 ]; then
15
+ echo "Error: Failed to create the conda environment."
16
+ exit 1
17
+ fi
18
+
19
+ echo "Conda environment '$env_name' created successfully."
20
+
21
+ # Activating the environment
22
+ source $(conda info --base)/etc/profile.d/conda.sh
23
+ conda activate "$env_name"
24
+ if [ $? -ne 0 ]; then
25
+ echo "Error: Failed to activate the conda environment."
26
+ exit 1
27
+ fi
28
+
29
+ echo "Conda environment '$env_name' activated."
30
+
31
+ # Installing dependencies from requirements.txt
32
+ pip install -r requirements.txt
33
+ if [ $? -ne 0 ]; then
34
+ echo "Error: Failed to install dependencies from requirements.txt."
35
+ exit 1
36
+ fi
37
+
38
+ echo "Dependencies installed successfully."
39
+ echo "Your conda environment '$env_name' is ready and activated."
final_project_Gradio_App.py ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Let us first import all the necessary libraries required for this project
2
+
3
+ import tensorflow as tf
4
+ import torch
5
+ import cv2
6
+ import sklearn
7
+ import numpy as np
8
+ import pandas as pd
9
+ import matplotlib.pyplot as plt
10
+ import seaborn as sns
11
+ import os
12
+ from torchvision import datasets, transforms, models
13
+ from torch.utils.data import DataLoader, Dataset
14
+ import torch.nn as nn
15
+ import torch.optim as optim
16
+ import torchvision
17
+ import torch.nn.functional as F
18
+ from PIL import Image
19
+
20
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix, roc_curve, auc
21
+
22
+
23
+ # Later on, as per requirement, more libraries wil be imported
24
+
25
+ import gradio as gr
26
+
27
+ def adjust_brightness_contrast(image, alpha=1.2, beta=50):
28
+ """
29
+ Adjusting brightness and contrast of the image.
30
+ Parameters:
31
+ - image: Input image (numpy array).
32
+ - alpha: Contrast control [1.0-3.0].
33
+ - beta: Brightness control [0-100].
34
+ Returns:
35
+ - Adjusted image.
36
+ """
37
+ return cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
38
+
39
+ def apply_histogram_equalization(image):
40
+ """Applying histogram equalization to enhance contrast."""
41
+ channels = cv2.split(image)
42
+ eq_channels = [cv2.equalizeHist(ch) for ch in channels]
43
+ return cv2.merge(eq_channels)
44
+
45
+ def apply_clahe(image, clip_limit=2.0, tile_grid_size=(8, 8)):
46
+ """Applying CLAHE for local contrast enhancement."""
47
+ clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
48
+ channels = cv2.split(image)
49
+ clahe_channels = [clahe.apply(ch) for ch in channels]
50
+ return cv2.merge(clahe_channels)
51
+
52
+ def apply_gaussian_blur(image, kernel_size=(3, 3)):
53
+ """Applying Gaussian blur for denoising."""
54
+ return cv2.GaussianBlur(image, kernel_size, 0)
55
+
56
+ def apply_sharpening(image):
57
+ """Applying edge enhancement using a sharpening filter."""
58
+ kernel = np.array([[0, -1, 0],
59
+ [-1, 5, -1],
60
+ [0, -1, 0]])
61
+ return cv2.filter2D(image, -1, kernel)
62
+
63
+ def normalize_image(image):
64
+ """Normalizing the image to zero mean and unit variance."""
65
+ image = (image - np.mean(image)) / np.std(image)
66
+ return image
67
+
68
+ def resize_image(image, width, height):
69
+ """Resizing the image to the desired dimensions with anti-aliasing."""
70
+ return cv2.resize(image, (width, height), interpolation=cv2.INTER_CUBIC)
71
+
72
+
73
+ def preprocess_single_image(pil_image, img_height=224, img_width=224):
74
+ """
75
+ Preprocessing a single image as per the training pipeline.
76
+ Parameters:
77
+ - pil_image: Input PIL image.
78
+ - img_height, img_width: Dimensions to resize the image.
79
+ Returns:
80
+ - Preprocessed image tensor.
81
+ """
82
+ # Converting PIL image to numpy array
83
+ image = np.array(pil_image)
84
+
85
+ # Ensuring the image is in RGB format
86
+ if len(image.shape) == 2 or image.shape[2] == 1: # Grayscale image
87
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
88
+
89
+ # Applying preprocessing steps
90
+ image = apply_histogram_equalization(image)
91
+ image = apply_clahe(image)
92
+ image = apply_gaussian_blur(image)
93
+ image = apply_sharpening(image)
94
+ image = adjust_brightness_contrast(image, alpha=1.2, beta=50)
95
+
96
+ # Resizing and normalization
97
+ image = resize_image(image, img_width, img_height)
98
+ image = normalize_image(image)
99
+
100
+ # Converting to PIL image and applying transformations
101
+ image = Image.fromarray(image.astype(np.uint8))
102
+ transform = transforms.Compose([
103
+ transforms.ToTensor(),
104
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
105
+ ])
106
+ image_tensor = transform(image).unsqueeze(0) # Adding batch dimension
107
+ return image_tensor
108
+
109
+
110
+ # Detecting GPU if available
111
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
112
+
113
+ from torchvision.models import densenet121
114
+ from torchvision.transforms import Resize
115
+
116
+ # Simplified ViT-like transformer module
117
+ class SimpleViT(nn.Module):
118
+ def __init__(self, input_dim, num_heads, mlp_dim, num_layers):
119
+ super(SimpleViT, self).__init__()
120
+ # Reduced TransformerEncoder layer complexity
121
+ self.transformer_blocks = nn.ModuleList([
122
+ nn.TransformerEncoderLayer(
123
+ d_model=input_dim,
124
+ nhead=num_heads,
125
+ dim_feedforward=mlp_dim,
126
+ dropout=0.1
127
+ ) for _ in range(num_layers)
128
+ ])
129
+
130
+ def forward(self, x):
131
+ # Flattening the spatial dimensions
132
+ B, C, H, W = x.shape
133
+ x = x.flatten(2).permute(2, 0, 1) # Reshaping for transformer
134
+ for block in self.transformer_blocks:
135
+ x = block(x)
136
+ x = x.permute(1, 2, 0).reshape(B, C, H, W) # Restoring the original shape
137
+ return x
138
+
139
+
140
+ # Adjusted Hybrid DenseNet + Simplified ViT Architecture
141
+ class LightweightHybridDenseNetViT(nn.Module):
142
+ def __init__(self):
143
+ super(LightweightHybridDenseNetViT, self).__init__()
144
+
145
+ # Loading a lighter DenseNet backbone
146
+ self.densenet = densenet121(pretrained=False) # Base DenseNet backbone
147
+
148
+ # Reducing the output channels from DenseNet to smaller dimensions
149
+ self.conv_reduce = nn.Conv2d(1024, 64, kernel_size=1) # Fewer channels
150
+
151
+ # ViT processing module with reduced complexity
152
+ self.vit = SimpleViT(input_dim=64, num_heads=2, mlp_dim=128, num_layers=1)
153
+
154
+ # Task-specific classification heads
155
+ self.fc_pneumonia = nn.Linear(64, 1) # Binary classification (Pneumonia)
156
+ self.fc_tuberculosis = nn.Linear(64, 1) # Binary classification (Tuberculosis)
157
+ self.fc_lung_cancer = nn.Linear(64, 4) # Multi-class output (Lung Cancer)
158
+
159
+ def forward(self, x):
160
+ # Extracting DenseNet features
161
+ x = self.densenet.features(x) # Extracting DenseNet feature maps
162
+ x = self.conv_reduce(x) # Reducing the number of feature channels
163
+
164
+ # Passing through simplified ViT module
165
+ x = self.vit(x)
166
+
167
+ # Applying Global Average Pooling (GAP)
168
+ x = x.mean(dim=[2, 3]) # Pooling across spatial dimensions
169
+
170
+ # Task-specific classification
171
+ pneumonia_output = torch.sigmoid(self.fc_pneumonia(x)) # Binary sigmoid output
172
+ tuberculosis_output = torch.sigmoid(self.fc_tuberculosis(x)) # Binary sigmoid output
173
+ lung_cancer_output = self.fc_lung_cancer(x) # Multi-class logits
174
+
175
+ return pneumonia_output, tuberculosis_output, lung_cancer_output
176
+
177
+
178
+ # Loading the saved model
179
+ img_size = 224 # Matching the dimensions used during training
180
+ patch_size = 8
181
+
182
+
183
+ model = LightweightHybridDenseNetViT().to(device)
184
+
185
+ model.load_state_dict(torch.load("model_FINAL.pth", map_location=device)) # Mapping to the correct device
186
+ model.to(device) # Moving the model to GPU/CPU
187
+ model.eval() # Setting to evaluation mode
188
+
189
+
190
+ # Function to pre-process the image and perform inference
191
+ def predict_image(image):
192
+ """
193
+ Predicts the probabilities of Pneumonia, TB, and Lung Cancer from the input image.
194
+ """
195
+ # Preprocessing the image
196
+ image_tensor = preprocess_single_image(image, img_height=224, img_width=224)
197
+ image_tensor = image_tensor.to(device)
198
+
199
+ # Performing inference
200
+ with torch.no_grad():
201
+ pneumonia_output, tb_output, lung_cancer_output = model(image_tensor)
202
+
203
+ # Getting the probabilities
204
+ pneumonia_prob = pneumonia_output.item()
205
+ tb_prob = tb_output.item()
206
+ lung_cancer_probs = F.softmax(lung_cancer_output, dim=1).squeeze().tolist()
207
+
208
+ # Class names for lung cancer
209
+ lung_cancer_classes = [
210
+ "adenocarcinoma_left.lower.lobe",
211
+ "large.cell.carcinoma_left.hilum",
212
+ "NORMAL",
213
+ "squamous.cell.carcinoma_left.hilum"
214
+ ]
215
+
216
+ # Preparing the result as a dictionary
217
+ result = {
218
+ "Pneumonia Probability": f"{pneumonia_prob:.4f}",
219
+ "TB Probability": f"{tb_prob:.4f}",
220
+ "Lung Cancer Probabilities": {class_name: f"{prob:.4f}" for class_name, prob in zip(lung_cancer_classes, lung_cancer_probs)}
221
+ }
222
+
223
+ return result
224
+
225
+ # Gradio Interface
226
+ iface = gr.Interface(fn=predict_image,
227
+ inputs=gr.Image(type="pil"),
228
+ outputs=gr.JSON(),
229
+ title="Probabilistic Lung Disease Detection",
230
+ description="An AI-powered tool that analyzes and predicts probabilities for lung diseases, including Pneumonia, Tuberculosis, and Lung Cancer.")
231
+
232
+ iface.launch()
final_project_deploy_local.ipynb ADDED
@@ -0,0 +1,1162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "Y2EgzZ7x4wEO"
7
+ },
8
+ "source": [
9
+ "#### **PROJECT TITLE :** `AI-Powered Medical Image Analysis Tool for Chest (X-Ray and CT-Scan) Diseases Detection`\n",
10
+ "\n",
11
+ "<br>\n",
12
+ "<br>\n",
13
+ "\n",
14
+ "### Component : `Model Deployment + Testing on Random Images`"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 1,
20
+ "metadata": {
21
+ "id": "1TmpNqBc1-Y2"
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "# Let us first import all the necessary libraries required for this project\n",
26
+ "\n",
27
+ "import tensorflow as tf\n",
28
+ "import torch\n",
29
+ "import cv2\n",
30
+ "import sklearn\n",
31
+ "import numpy as np\n",
32
+ "import pandas as pd\n",
33
+ "import matplotlib.pyplot as plt\n",
34
+ "import seaborn as sns\n",
35
+ "import os\n",
36
+ "from torchvision import datasets, transforms, models\n",
37
+ "from torch.utils.data import DataLoader, Dataset\n",
38
+ "import torch.nn as nn\n",
39
+ "import torch.optim as optim\n",
40
+ "import torchvision\n",
41
+ "import torch.nn.functional as F\n",
42
+ "from PIL import Image\n",
43
+ "\n",
44
+ "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix, roc_curve, auc\n",
45
+ "\n",
46
+ "\n",
47
+ "# Later on, as per requirement, more libraries wil be imported"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "markdown",
52
+ "metadata": {
53
+ "id": "wnk9WBze56wW"
54
+ },
55
+ "source": [
56
+ "<br>\n",
57
+ "<br>\n",
58
+ "\n",
59
+ "### <b>STEP - 1 : Pre-Processing Functions</b>"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": 3,
65
+ "metadata": {
66
+ "id": "fdXDwKiV5HFK"
67
+ },
68
+ "outputs": [],
69
+ "source": [
70
+ "def adjust_brightness_contrast(image, alpha=1.2, beta=50):\n",
71
+ " \"\"\"\n",
72
+ " Adjusting brightness and contrast of the image.\n",
73
+ " Parameters:\n",
74
+ " - image: Input image (numpy array).\n",
75
+ " - alpha: Contrast control [1.0-3.0].\n",
76
+ " - beta: Brightness control [0-100].\n",
77
+ " Returns:\n",
78
+ " - Adjusted image.\n",
79
+ " \"\"\"\n",
80
+ " return cv2.convertScaleAbs(image, alpha=alpha, beta=beta)\n",
81
+ "\n",
82
+ "def apply_histogram_equalization(image):\n",
83
+ " \"\"\"Applying histogram equalization to enhance contrast.\"\"\"\n",
84
+ " channels = cv2.split(image)\n",
85
+ " eq_channels = [cv2.equalizeHist(ch) for ch in channels]\n",
86
+ " return cv2.merge(eq_channels)\n",
87
+ "\n",
88
+ "def apply_clahe(image, clip_limit=2.0, tile_grid_size=(8, 8)):\n",
89
+ " \"\"\"Applying CLAHE for local contrast enhancement.\"\"\"\n",
90
+ " clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)\n",
91
+ " channels = cv2.split(image)\n",
92
+ " clahe_channels = [clahe.apply(ch) for ch in channels]\n",
93
+ " return cv2.merge(clahe_channels)\n",
94
+ "\n",
95
+ "def apply_gaussian_blur(image, kernel_size=(3, 3)):\n",
96
+ " \"\"\"Applying Gaussian blur for denoising.\"\"\"\n",
97
+ " return cv2.GaussianBlur(image, kernel_size, 0)\n",
98
+ "\n",
99
+ "def apply_sharpening(image):\n",
100
+ " \"\"\"Applying edge enhancement using a sharpening filter.\"\"\"\n",
101
+ " kernel = np.array([[0, -1, 0],\n",
102
+ " [-1, 5, -1],\n",
103
+ " [0, -1, 0]])\n",
104
+ " return cv2.filter2D(image, -1, kernel)\n",
105
+ "\n",
106
+ "def normalize_image(image):\n",
107
+ " \"\"\"Normalizing the image to zero mean and unit variance.\"\"\"\n",
108
+ " image = (image - np.mean(image)) / np.std(image)\n",
109
+ " return image\n",
110
+ "\n",
111
+ "def resize_image(image, width, height):\n",
112
+ " \"\"\"Resizing the image to the desired dimensions with anti-aliasing.\"\"\"\n",
113
+ " return cv2.resize(image, (width, height), interpolation=cv2.INTER_CUBIC)"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "markdown",
118
+ "metadata": {
119
+ "id": "jRUZ008d6p1T"
120
+ },
121
+ "source": [
122
+ "<br>\n",
123
+ "<br>\n",
124
+ "\n",
125
+ "### <b>STEP - 2 : Pre-processing Pipeline</b>"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": 4,
131
+ "metadata": {
132
+ "id": "KgcX83yc40g-"
133
+ },
134
+ "outputs": [],
135
+ "source": [
136
+ "def preprocess_single_image(img_path, img_height=224, img_width=224):\n",
137
+ " \"\"\"\n",
138
+ " Preprocessing a single image as per the training pipeline.\n",
139
+ " \"\"\"\n",
140
+ " # Loading image in BGR format\n",
141
+ " image = cv2.imread(img_path, cv2.IMREAD_COLOR)\n",
142
+ " if image is None:\n",
143
+ " raise ValueError(f\"Image not found: {img_path}\")\n",
144
+ "\n",
145
+ " # Converting BGR to RGB\n",
146
+ " image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
147
+ "\n",
148
+ " # Applying preprocessing steps\n",
149
+ " image = apply_histogram_equalization(image)\n",
150
+ " image = apply_clahe(image)\n",
151
+ " image = apply_gaussian_blur(image)\n",
152
+ " image = apply_sharpening(image)\n",
153
+ " image = adjust_brightness_contrast(image, alpha=1.2, beta=50)\n",
154
+ "\n",
155
+ " # Resizing and normalize\n",
156
+ " image = resize_image(image, img_width, img_height)\n",
157
+ " image = normalize_image(image)\n",
158
+ "\n",
159
+ " # Converting to PIL image and applyinh transformations\n",
160
+ " image = Image.fromarray(image.astype(np.uint8))\n",
161
+ " transform = transforms.Compose([\n",
162
+ " transforms.ToTensor(),\n",
163
+ " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n",
164
+ " ])\n",
165
+ " image_tensor = transform(image).unsqueeze(0) # Adding batch dimension\n",
166
+ " return image_tensor"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "markdown",
171
+ "metadata": {
172
+ "id": "0KLCcPYa6x_3"
173
+ },
174
+ "source": [
175
+ "<br>\n",
176
+ "<br>\n",
177
+ "\n",
178
+ "### <b>STEP - 3 : Loading the Model</b>"
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": 2,
184
+ "metadata": {
185
+ "colab": {
186
+ "base_uri": "https://localhost:8080/"
187
+ },
188
+ "id": "hfTSNNQR8Gsu",
189
+ "outputId": "ef8a7e0b-25e5-4a00-d6e0-70bde079b08c"
190
+ },
191
+ "outputs": [
192
+ {
193
+ "name": "stdout",
194
+ "output_type": "stream",
195
+ "text": [
196
+ "Using device: cuda\n"
197
+ ]
198
+ }
199
+ ],
200
+ "source": [
201
+ "# Detecting GPU if available\n",
202
+ "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
203
+ "print(f\"Using device: {device}\")"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "code",
208
+ "execution_count": 6,
209
+ "metadata": {
210
+ "id": "6tvYopjB7OAS"
211
+ },
212
+ "outputs": [],
213
+ "source": [
214
+ "from torchvision.models import densenet121\n",
215
+ "from torchvision.transforms import Resize\n",
216
+ "\n",
217
+ "# Simplified ViT-like transformer module\n",
218
+ "class SimpleViT(nn.Module):\n",
219
+ " def __init__(self, input_dim, num_heads, mlp_dim, num_layers):\n",
220
+ " super(SimpleViT, self).__init__()\n",
221
+ " # Reduced TransformerEncoder layer complexity\n",
222
+ " self.transformer_blocks = nn.ModuleList([\n",
223
+ " nn.TransformerEncoderLayer(\n",
224
+ " d_model=input_dim,\n",
225
+ " nhead=num_heads,\n",
226
+ " dim_feedforward=mlp_dim,\n",
227
+ " dropout=0.1\n",
228
+ " ) for _ in range(num_layers)\n",
229
+ " ])\n",
230
+ "\n",
231
+ " def forward(self, x):\n",
232
+ " # Flattening the spatial dimensions\n",
233
+ " B, C, H, W = x.shape\n",
234
+ " x = x.flatten(2).permute(2, 0, 1) # Reshaping for transformer\n",
235
+ " for block in self.transformer_blocks:\n",
236
+ " x = block(x)\n",
237
+ " x = x.permute(1, 2, 0).reshape(B, C, H, W) # Restoring the original shape\n",
238
+ " return x\n",
239
+ "\n",
240
+ "\n",
241
+ "# Adjusted Hybrid DenseNet + Simplified ViT Architecture\n",
242
+ "class LightweightHybridDenseNetViT(nn.Module):\n",
243
+ " def __init__(self):\n",
244
+ " super(LightweightHybridDenseNetViT, self).__init__()\n",
245
+ "\n",
246
+ " # Loading a lighter DenseNet backbone\n",
247
+ " self.densenet = densenet121(pretrained=False) # Base DenseNet backbone\n",
248
+ "\n",
249
+ " # Reducing the output channels from DenseNet to smaller dimensions\n",
250
+ " self.conv_reduce = nn.Conv2d(1024, 64, kernel_size=1) # Fewer channels\n",
251
+ "\n",
252
+ " # ViT processing module with reduced complexity\n",
253
+ " self.vit = SimpleViT(input_dim=64, num_heads=2, mlp_dim=128, num_layers=1)\n",
254
+ "\n",
255
+ " # Task-specific classification heads\n",
256
+ " self.fc_pneumonia = nn.Linear(64, 1) # Binary classification (Pneumonia)\n",
257
+ " self.fc_tuberculosis = nn.Linear(64, 1) # Binary classification (Tuberculosis)\n",
258
+ " self.fc_lung_cancer = nn.Linear(64, 4) # Multi-class output (Lung Cancer)\n",
259
+ "\n",
260
+ " def forward(self, x):\n",
261
+ " # Extracting DenseNet features\n",
262
+ " x = self.densenet.features(x) # Extracting DenseNet feature maps\n",
263
+ " x = self.conv_reduce(x) # Reducing the number of feature channels\n",
264
+ "\n",
265
+ " # Passing through simplified ViT module\n",
266
+ " x = self.vit(x)\n",
267
+ "\n",
268
+ " # Applying Global Average Pooling (GAP)\n",
269
+ " x = x.mean(dim=[2, 3]) # Pooling across spatial dimensions\n",
270
+ "\n",
271
+ " # Task-specific classification\n",
272
+ " pneumonia_output = torch.sigmoid(self.fc_pneumonia(x)) # Binary sigmoid output\n",
273
+ " tuberculosis_output = torch.sigmoid(self.fc_tuberculosis(x)) # Binary sigmoid output\n",
274
+ " lung_cancer_output = self.fc_lung_cancer(x) # Multi-class logits\n",
275
+ "\n",
276
+ " return pneumonia_output, tuberculosis_output, lung_cancer_output"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "code",
281
+ "execution_count": 7,
282
+ "metadata": {
283
+ "colab": {
284
+ "base_uri": "https://localhost:8080/"
285
+ },
286
+ "id": "P4vHdzkA52Oe",
287
+ "outputId": "c10c4f4a-e6d9-4e3c-d34a-4dc53a306c66"
288
+ },
289
+ "outputs": [
290
+ {
291
+ "name": "stderr",
292
+ "output_type": "stream",
293
+ "text": [
294
+ "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",
295
+ " warnings.warn(\n",
296
+ "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",
297
+ " warnings.warn(msg)\n",
298
+ "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",
299
+ " model.load_state_dict(torch.load(\"CVIP_PROJ_model_FINAL.pth\", map_location=device)) # Mapping to the correct device\n"
300
+ ]
301
+ },
302
+ {
303
+ "data": {
304
+ "text/plain": [
305
+ "LightweightHybridDenseNetViT(\n",
306
+ " (densenet): DenseNet(\n",
307
+ " (features): Sequential(\n",
308
+ " (conv0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n",
309
+ " (norm0): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
310
+ " (relu0): ReLU(inplace=True)\n",
311
+ " (pool0): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n",
312
+ " (denseblock1): _DenseBlock(\n",
313
+ " (denselayer1): _DenseLayer(\n",
314
+ " (norm1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
315
+ " (relu1): ReLU(inplace=True)\n",
316
+ " (conv1): Conv2d(64, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
317
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
318
+ " (relu2): ReLU(inplace=True)\n",
319
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
320
+ " )\n",
321
+ " (denselayer2): _DenseLayer(\n",
322
+ " (norm1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
323
+ " (relu1): ReLU(inplace=True)\n",
324
+ " (conv1): Conv2d(96, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
325
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
326
+ " (relu2): ReLU(inplace=True)\n",
327
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
328
+ " )\n",
329
+ " (denselayer3): _DenseLayer(\n",
330
+ " (norm1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
331
+ " (relu1): ReLU(inplace=True)\n",
332
+ " (conv1): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
333
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
334
+ " (relu2): ReLU(inplace=True)\n",
335
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
336
+ " )\n",
337
+ " (denselayer4): _DenseLayer(\n",
338
+ " (norm1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
339
+ " (relu1): ReLU(inplace=True)\n",
340
+ " (conv1): Conv2d(160, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
341
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
342
+ " (relu2): ReLU(inplace=True)\n",
343
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
344
+ " )\n",
345
+ " (denselayer5): _DenseLayer(\n",
346
+ " (norm1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
347
+ " (relu1): ReLU(inplace=True)\n",
348
+ " (conv1): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
349
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
350
+ " (relu2): ReLU(inplace=True)\n",
351
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
352
+ " )\n",
353
+ " (denselayer6): _DenseLayer(\n",
354
+ " (norm1): BatchNorm2d(224, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
355
+ " (relu1): ReLU(inplace=True)\n",
356
+ " (conv1): Conv2d(224, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
357
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
358
+ " (relu2): ReLU(inplace=True)\n",
359
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
360
+ " )\n",
361
+ " )\n",
362
+ " (transition1): _Transition(\n",
363
+ " (norm): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
364
+ " (relu): ReLU(inplace=True)\n",
365
+ " (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
366
+ " (pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
367
+ " )\n",
368
+ " (denseblock2): _DenseBlock(\n",
369
+ " (denselayer1): _DenseLayer(\n",
370
+ " (norm1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
371
+ " (relu1): ReLU(inplace=True)\n",
372
+ " (conv1): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
373
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
374
+ " (relu2): ReLU(inplace=True)\n",
375
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
376
+ " )\n",
377
+ " (denselayer2): _DenseLayer(\n",
378
+ " (norm1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
379
+ " (relu1): ReLU(inplace=True)\n",
380
+ " (conv1): Conv2d(160, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
381
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
382
+ " (relu2): ReLU(inplace=True)\n",
383
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
384
+ " )\n",
385
+ " (denselayer3): _DenseLayer(\n",
386
+ " (norm1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
387
+ " (relu1): ReLU(inplace=True)\n",
388
+ " (conv1): Conv2d(192, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
389
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
390
+ " (relu2): ReLU(inplace=True)\n",
391
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
392
+ " )\n",
393
+ " (denselayer4): _DenseLayer(\n",
394
+ " (norm1): BatchNorm2d(224, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
395
+ " (relu1): ReLU(inplace=True)\n",
396
+ " (conv1): Conv2d(224, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
397
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
398
+ " (relu2): ReLU(inplace=True)\n",
399
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
400
+ " )\n",
401
+ " (denselayer5): _DenseLayer(\n",
402
+ " (norm1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
403
+ " (relu1): ReLU(inplace=True)\n",
404
+ " (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
405
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
406
+ " (relu2): ReLU(inplace=True)\n",
407
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
408
+ " )\n",
409
+ " (denselayer6): _DenseLayer(\n",
410
+ " (norm1): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
411
+ " (relu1): ReLU(inplace=True)\n",
412
+ " (conv1): Conv2d(288, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
413
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
414
+ " (relu2): ReLU(inplace=True)\n",
415
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
416
+ " )\n",
417
+ " (denselayer7): _DenseLayer(\n",
418
+ " (norm1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
419
+ " (relu1): ReLU(inplace=True)\n",
420
+ " (conv1): Conv2d(320, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
421
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
422
+ " (relu2): ReLU(inplace=True)\n",
423
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
424
+ " )\n",
425
+ " (denselayer8): _DenseLayer(\n",
426
+ " (norm1): BatchNorm2d(352, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
427
+ " (relu1): ReLU(inplace=True)\n",
428
+ " (conv1): Conv2d(352, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
429
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
430
+ " (relu2): ReLU(inplace=True)\n",
431
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
432
+ " )\n",
433
+ " (denselayer9): _DenseLayer(\n",
434
+ " (norm1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
435
+ " (relu1): ReLU(inplace=True)\n",
436
+ " (conv1): Conv2d(384, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
437
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
438
+ " (relu2): ReLU(inplace=True)\n",
439
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
440
+ " )\n",
441
+ " (denselayer10): _DenseLayer(\n",
442
+ " (norm1): BatchNorm2d(416, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
443
+ " (relu1): ReLU(inplace=True)\n",
444
+ " (conv1): Conv2d(416, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
445
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
446
+ " (relu2): ReLU(inplace=True)\n",
447
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
448
+ " )\n",
449
+ " (denselayer11): _DenseLayer(\n",
450
+ " (norm1): BatchNorm2d(448, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
451
+ " (relu1): ReLU(inplace=True)\n",
452
+ " (conv1): Conv2d(448, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
453
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
454
+ " (relu2): ReLU(inplace=True)\n",
455
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
456
+ " )\n",
457
+ " (denselayer12): _DenseLayer(\n",
458
+ " (norm1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
459
+ " (relu1): ReLU(inplace=True)\n",
460
+ " (conv1): Conv2d(480, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
461
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
462
+ " (relu2): ReLU(inplace=True)\n",
463
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
464
+ " )\n",
465
+ " )\n",
466
+ " (transition2): _Transition(\n",
467
+ " (norm): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
468
+ " (relu): ReLU(inplace=True)\n",
469
+ " (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
470
+ " (pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
471
+ " )\n",
472
+ " (denseblock3): _DenseBlock(\n",
473
+ " (denselayer1): _DenseLayer(\n",
474
+ " (norm1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
475
+ " (relu1): ReLU(inplace=True)\n",
476
+ " (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
477
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
478
+ " (relu2): ReLU(inplace=True)\n",
479
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
480
+ " )\n",
481
+ " (denselayer2): _DenseLayer(\n",
482
+ " (norm1): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
483
+ " (relu1): ReLU(inplace=True)\n",
484
+ " (conv1): Conv2d(288, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
485
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
486
+ " (relu2): ReLU(inplace=True)\n",
487
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
488
+ " )\n",
489
+ " (denselayer3): _DenseLayer(\n",
490
+ " (norm1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
491
+ " (relu1): ReLU(inplace=True)\n",
492
+ " (conv1): Conv2d(320, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
493
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
494
+ " (relu2): ReLU(inplace=True)\n",
495
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
496
+ " )\n",
497
+ " (denselayer4): _DenseLayer(\n",
498
+ " (norm1): BatchNorm2d(352, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
499
+ " (relu1): ReLU(inplace=True)\n",
500
+ " (conv1): Conv2d(352, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
501
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
502
+ " (relu2): ReLU(inplace=True)\n",
503
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
504
+ " )\n",
505
+ " (denselayer5): _DenseLayer(\n",
506
+ " (norm1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
507
+ " (relu1): ReLU(inplace=True)\n",
508
+ " (conv1): Conv2d(384, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
509
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
510
+ " (relu2): ReLU(inplace=True)\n",
511
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
512
+ " )\n",
513
+ " (denselayer6): _DenseLayer(\n",
514
+ " (norm1): BatchNorm2d(416, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
515
+ " (relu1): ReLU(inplace=True)\n",
516
+ " (conv1): Conv2d(416, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
517
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
518
+ " (relu2): ReLU(inplace=True)\n",
519
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
520
+ " )\n",
521
+ " (denselayer7): _DenseLayer(\n",
522
+ " (norm1): BatchNorm2d(448, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
523
+ " (relu1): ReLU(inplace=True)\n",
524
+ " (conv1): Conv2d(448, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
525
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
526
+ " (relu2): ReLU(inplace=True)\n",
527
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
528
+ " )\n",
529
+ " (denselayer8): _DenseLayer(\n",
530
+ " (norm1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
531
+ " (relu1): ReLU(inplace=True)\n",
532
+ " (conv1): Conv2d(480, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
533
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
534
+ " (relu2): ReLU(inplace=True)\n",
535
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
536
+ " )\n",
537
+ " (denselayer9): _DenseLayer(\n",
538
+ " (norm1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
539
+ " (relu1): ReLU(inplace=True)\n",
540
+ " (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
541
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
542
+ " (relu2): ReLU(inplace=True)\n",
543
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
544
+ " )\n",
545
+ " (denselayer10): _DenseLayer(\n",
546
+ " (norm1): BatchNorm2d(544, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
547
+ " (relu1): ReLU(inplace=True)\n",
548
+ " (conv1): Conv2d(544, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
549
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
550
+ " (relu2): ReLU(inplace=True)\n",
551
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
552
+ " )\n",
553
+ " (denselayer11): _DenseLayer(\n",
554
+ " (norm1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
555
+ " (relu1): ReLU(inplace=True)\n",
556
+ " (conv1): Conv2d(576, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
557
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
558
+ " (relu2): ReLU(inplace=True)\n",
559
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
560
+ " )\n",
561
+ " (denselayer12): _DenseLayer(\n",
562
+ " (norm1): BatchNorm2d(608, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
563
+ " (relu1): ReLU(inplace=True)\n",
564
+ " (conv1): Conv2d(608, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
565
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
566
+ " (relu2): ReLU(inplace=True)\n",
567
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
568
+ " )\n",
569
+ " (denselayer13): _DenseLayer(\n",
570
+ " (norm1): BatchNorm2d(640, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
571
+ " (relu1): ReLU(inplace=True)\n",
572
+ " (conv1): Conv2d(640, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
573
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
574
+ " (relu2): ReLU(inplace=True)\n",
575
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
576
+ " )\n",
577
+ " (denselayer14): _DenseLayer(\n",
578
+ " (norm1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
579
+ " (relu1): ReLU(inplace=True)\n",
580
+ " (conv1): Conv2d(672, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
581
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
582
+ " (relu2): ReLU(inplace=True)\n",
583
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
584
+ " )\n",
585
+ " (denselayer15): _DenseLayer(\n",
586
+ " (norm1): BatchNorm2d(704, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
587
+ " (relu1): ReLU(inplace=True)\n",
588
+ " (conv1): Conv2d(704, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
589
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
590
+ " (relu2): ReLU(inplace=True)\n",
591
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
592
+ " )\n",
593
+ " (denselayer16): _DenseLayer(\n",
594
+ " (norm1): BatchNorm2d(736, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
595
+ " (relu1): ReLU(inplace=True)\n",
596
+ " (conv1): Conv2d(736, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
597
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
598
+ " (relu2): ReLU(inplace=True)\n",
599
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
600
+ " )\n",
601
+ " (denselayer17): _DenseLayer(\n",
602
+ " (norm1): BatchNorm2d(768, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
603
+ " (relu1): ReLU(inplace=True)\n",
604
+ " (conv1): Conv2d(768, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
605
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
606
+ " (relu2): ReLU(inplace=True)\n",
607
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
608
+ " )\n",
609
+ " (denselayer18): _DenseLayer(\n",
610
+ " (norm1): BatchNorm2d(800, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
611
+ " (relu1): ReLU(inplace=True)\n",
612
+ " (conv1): Conv2d(800, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
613
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
614
+ " (relu2): ReLU(inplace=True)\n",
615
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
616
+ " )\n",
617
+ " (denselayer19): _DenseLayer(\n",
618
+ " (norm1): BatchNorm2d(832, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
619
+ " (relu1): ReLU(inplace=True)\n",
620
+ " (conv1): Conv2d(832, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
621
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
622
+ " (relu2): ReLU(inplace=True)\n",
623
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
624
+ " )\n",
625
+ " (denselayer20): _DenseLayer(\n",
626
+ " (norm1): BatchNorm2d(864, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
627
+ " (relu1): ReLU(inplace=True)\n",
628
+ " (conv1): Conv2d(864, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
629
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
630
+ " (relu2): ReLU(inplace=True)\n",
631
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
632
+ " )\n",
633
+ " (denselayer21): _DenseLayer(\n",
634
+ " (norm1): BatchNorm2d(896, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
635
+ " (relu1): ReLU(inplace=True)\n",
636
+ " (conv1): Conv2d(896, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
637
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
638
+ " (relu2): ReLU(inplace=True)\n",
639
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
640
+ " )\n",
641
+ " (denselayer22): _DenseLayer(\n",
642
+ " (norm1): BatchNorm2d(928, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
643
+ " (relu1): ReLU(inplace=True)\n",
644
+ " (conv1): Conv2d(928, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
645
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
646
+ " (relu2): ReLU(inplace=True)\n",
647
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
648
+ " )\n",
649
+ " (denselayer23): _DenseLayer(\n",
650
+ " (norm1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
651
+ " (relu1): ReLU(inplace=True)\n",
652
+ " (conv1): Conv2d(960, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
653
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
654
+ " (relu2): ReLU(inplace=True)\n",
655
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
656
+ " )\n",
657
+ " (denselayer24): _DenseLayer(\n",
658
+ " (norm1): BatchNorm2d(992, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
659
+ " (relu1): ReLU(inplace=True)\n",
660
+ " (conv1): Conv2d(992, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
661
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
662
+ " (relu2): ReLU(inplace=True)\n",
663
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
664
+ " )\n",
665
+ " )\n",
666
+ " (transition3): _Transition(\n",
667
+ " (norm): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
668
+ " (relu): ReLU(inplace=True)\n",
669
+ " (conv): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
670
+ " (pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
671
+ " )\n",
672
+ " (denseblock4): _DenseBlock(\n",
673
+ " (denselayer1): _DenseLayer(\n",
674
+ " (norm1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
675
+ " (relu1): ReLU(inplace=True)\n",
676
+ " (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
677
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
678
+ " (relu2): ReLU(inplace=True)\n",
679
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
680
+ " )\n",
681
+ " (denselayer2): _DenseLayer(\n",
682
+ " (norm1): BatchNorm2d(544, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
683
+ " (relu1): ReLU(inplace=True)\n",
684
+ " (conv1): Conv2d(544, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
685
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
686
+ " (relu2): ReLU(inplace=True)\n",
687
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
688
+ " )\n",
689
+ " (denselayer3): _DenseLayer(\n",
690
+ " (norm1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
691
+ " (relu1): ReLU(inplace=True)\n",
692
+ " (conv1): Conv2d(576, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
693
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
694
+ " (relu2): ReLU(inplace=True)\n",
695
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
696
+ " )\n",
697
+ " (denselayer4): _DenseLayer(\n",
698
+ " (norm1): BatchNorm2d(608, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
699
+ " (relu1): ReLU(inplace=True)\n",
700
+ " (conv1): Conv2d(608, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
701
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
702
+ " (relu2): ReLU(inplace=True)\n",
703
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
704
+ " )\n",
705
+ " (denselayer5): _DenseLayer(\n",
706
+ " (norm1): BatchNorm2d(640, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
707
+ " (relu1): ReLU(inplace=True)\n",
708
+ " (conv1): Conv2d(640, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
709
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
710
+ " (relu2): ReLU(inplace=True)\n",
711
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
712
+ " )\n",
713
+ " (denselayer6): _DenseLayer(\n",
714
+ " (norm1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
715
+ " (relu1): ReLU(inplace=True)\n",
716
+ " (conv1): Conv2d(672, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
717
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
718
+ " (relu2): ReLU(inplace=True)\n",
719
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
720
+ " )\n",
721
+ " (denselayer7): _DenseLayer(\n",
722
+ " (norm1): BatchNorm2d(704, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
723
+ " (relu1): ReLU(inplace=True)\n",
724
+ " (conv1): Conv2d(704, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
725
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
726
+ " (relu2): ReLU(inplace=True)\n",
727
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
728
+ " )\n",
729
+ " (denselayer8): _DenseLayer(\n",
730
+ " (norm1): BatchNorm2d(736, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
731
+ " (relu1): ReLU(inplace=True)\n",
732
+ " (conv1): Conv2d(736, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
733
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
734
+ " (relu2): ReLU(inplace=True)\n",
735
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
736
+ " )\n",
737
+ " (denselayer9): _DenseLayer(\n",
738
+ " (norm1): BatchNorm2d(768, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
739
+ " (relu1): ReLU(inplace=True)\n",
740
+ " (conv1): Conv2d(768, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
741
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
742
+ " (relu2): ReLU(inplace=True)\n",
743
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
744
+ " )\n",
745
+ " (denselayer10): _DenseLayer(\n",
746
+ " (norm1): BatchNorm2d(800, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
747
+ " (relu1): ReLU(inplace=True)\n",
748
+ " (conv1): Conv2d(800, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
749
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
750
+ " (relu2): ReLU(inplace=True)\n",
751
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
752
+ " )\n",
753
+ " (denselayer11): _DenseLayer(\n",
754
+ " (norm1): BatchNorm2d(832, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
755
+ " (relu1): ReLU(inplace=True)\n",
756
+ " (conv1): Conv2d(832, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
757
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
758
+ " (relu2): ReLU(inplace=True)\n",
759
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
760
+ " )\n",
761
+ " (denselayer12): _DenseLayer(\n",
762
+ " (norm1): BatchNorm2d(864, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
763
+ " (relu1): ReLU(inplace=True)\n",
764
+ " (conv1): Conv2d(864, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
765
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
766
+ " (relu2): ReLU(inplace=True)\n",
767
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
768
+ " )\n",
769
+ " (denselayer13): _DenseLayer(\n",
770
+ " (norm1): BatchNorm2d(896, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
771
+ " (relu1): ReLU(inplace=True)\n",
772
+ " (conv1): Conv2d(896, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
773
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
774
+ " (relu2): ReLU(inplace=True)\n",
775
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
776
+ " )\n",
777
+ " (denselayer14): _DenseLayer(\n",
778
+ " (norm1): BatchNorm2d(928, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
779
+ " (relu1): ReLU(inplace=True)\n",
780
+ " (conv1): Conv2d(928, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
781
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
782
+ " (relu2): ReLU(inplace=True)\n",
783
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
784
+ " )\n",
785
+ " (denselayer15): _DenseLayer(\n",
786
+ " (norm1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
787
+ " (relu1): ReLU(inplace=True)\n",
788
+ " (conv1): Conv2d(960, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
789
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
790
+ " (relu2): ReLU(inplace=True)\n",
791
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
792
+ " )\n",
793
+ " (denselayer16): _DenseLayer(\n",
794
+ " (norm1): BatchNorm2d(992, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
795
+ " (relu1): ReLU(inplace=True)\n",
796
+ " (conv1): Conv2d(992, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
797
+ " (norm2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
798
+ " (relu2): ReLU(inplace=True)\n",
799
+ " (conv2): Conv2d(128, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
800
+ " )\n",
801
+ " )\n",
802
+ " (norm5): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
803
+ " )\n",
804
+ " (classifier): Linear(in_features=1024, out_features=1000, bias=True)\n",
805
+ " )\n",
806
+ " (conv_reduce): Conv2d(1024, 64, kernel_size=(1, 1), stride=(1, 1))\n",
807
+ " (vit): SimpleViT(\n",
808
+ " (transformer_blocks): ModuleList(\n",
809
+ " (0): TransformerEncoderLayer(\n",
810
+ " (self_attn): MultiheadAttention(\n",
811
+ " (out_proj): NonDynamicallyQuantizableLinear(in_features=64, out_features=64, bias=True)\n",
812
+ " )\n",
813
+ " (linear1): Linear(in_features=64, out_features=128, bias=True)\n",
814
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
815
+ " (linear2): Linear(in_features=128, out_features=64, bias=True)\n",
816
+ " (norm1): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n",
817
+ " (norm2): LayerNorm((64,), eps=1e-05, elementwise_affine=True)\n",
818
+ " (dropout1): Dropout(p=0.1, inplace=False)\n",
819
+ " (dropout2): Dropout(p=0.1, inplace=False)\n",
820
+ " )\n",
821
+ " )\n",
822
+ " )\n",
823
+ " (fc_pneumonia): Linear(in_features=64, out_features=1, bias=True)\n",
824
+ " (fc_tuberculosis): Linear(in_features=64, out_features=1, bias=True)\n",
825
+ " (fc_lung_cancer): Linear(in_features=64, out_features=4, bias=True)\n",
826
+ ")"
827
+ ]
828
+ },
829
+ "execution_count": 7,
830
+ "metadata": {},
831
+ "output_type": "execute_result"
832
+ }
833
+ ],
834
+ "source": [
835
+ "# Loading the saved model\n",
836
+ "img_size = 224 # Matching the dimensions used during training\n",
837
+ "patch_size = 8\n",
838
+ "\n",
839
+ "\n",
840
+ "model = LightweightHybridDenseNetViT().to(device)\n",
841
+ "\n",
842
+ "model.load_state_dict(torch.load(\"model_FINAL.pth\", map_location=device)) # Mapping to the correct device\n",
843
+ "model.to(device) # Moving the model to GPU/CPU\n",
844
+ "model.eval() # Setting to evaluation mode"
845
+ ]
846
+ },
847
+ {
848
+ "cell_type": "markdown",
849
+ "metadata": {
850
+ "id": "55xG6fIf-Juc"
851
+ },
852
+ "source": [
853
+ "<br>\n",
854
+ "<br>\n",
855
+ "\n",
856
+ "### <b>STEP - 4 : Preprocessing the Image and Moving to GPU</b>"
857
+ ]
858
+ },
859
+ {
860
+ "cell_type": "code",
861
+ "execution_count": 8,
862
+ "metadata": {
863
+ "id": "gBSz6oqF-KpK"
864
+ },
865
+ "outputs": [],
866
+ "source": [
867
+ "image_tensor = preprocess_single_image(\"Deploy-Test Images/642x361_SLIDE_4_What_Does_Lung_Cancer_Look_Like.jpg\") # Path to the random image\n",
868
+ "image_tensor = image_tensor.to(device) # Moving the input tensor to the same device"
869
+ ]
870
+ },
871
+ {
872
+ "cell_type": "markdown",
873
+ "metadata": {
874
+ "id": "Xqcipj_lDUFk"
875
+ },
876
+ "source": [
877
+ "<br>\n",
878
+ "<br>\n",
879
+ "\n",
880
+ "### <b>STEP - 5 : Performing Inference on GPU</b>"
881
+ ]
882
+ },
883
+ {
884
+ "cell_type": "code",
885
+ "execution_count": 9,
886
+ "metadata": {
887
+ "colab": {
888
+ "base_uri": "https://localhost:8080/"
889
+ },
890
+ "id": "CYfM_IZRDOSJ",
891
+ "outputId": "bc1ff79f-200c-4938-ddae-113316912060"
892
+ },
893
+ "outputs": [
894
+ {
895
+ "name": "stdout",
896
+ "output_type": "stream",
897
+ "text": [
898
+ "Pneumonia Probability: 0.9692\n",
899
+ "TB Probability: 0.8210\n",
900
+ "Lung Cancer Probabilities:\n",
901
+ " adenocarcinoma_left.lower.lobe: 0.0384\n",
902
+ " large.cell.carcinoma_left.hilum: 0.0717\n",
903
+ " NORMAL: 0.7883\n",
904
+ " squamous.cell.carcinoma_left.hilum: 0.1015\n"
905
+ ]
906
+ }
907
+ ],
908
+ "source": [
909
+ "# Performing inference\n",
910
+ "with torch.no_grad():\n",
911
+ " pneumonia_output, tb_output, lung_cancer_output = model(image_tensor)\n",
912
+ "\n",
913
+ "# Converting outputs to probabilities\n",
914
+ "pneumonia_prob = pneumonia_output.item() # Sigmoid ensures output in [0, 1]\n",
915
+ "tb_prob = tb_output.item()\n",
916
+ "lung_cancer_probs = F.softmax(lung_cancer_output, dim=1).squeeze().tolist()\n",
917
+ "\n",
918
+ "# Class names for lung cancer\n",
919
+ "lung_cancer_classes = [\n",
920
+ " \"adenocarcinoma_left.lower.lobe\",\n",
921
+ " \"large.cell.carcinoma_left.hilum\",\n",
922
+ " \"NORMAL\",\n",
923
+ " \"squamous.cell.carcinoma_left.hilum\"\n",
924
+ "]\n",
925
+ "\n",
926
+ "# Printing results\n",
927
+ "print(f\"Pneumonia Probability: {pneumonia_prob:.4f}\")\n",
928
+ "print(f\"TB Probability: {tb_prob:.4f}\")\n",
929
+ "\n",
930
+ "# Printing lung cancer probabilities with class names\n",
931
+ "print(\"Lung Cancer Probabilities:\")\n",
932
+ "for class_name, prob in zip(lung_cancer_classes, lung_cancer_probs):\n",
933
+ " print(f\" {class_name}: {prob:.4f}\")"
934
+ ]
935
+ },
936
+ {
937
+ "cell_type": "markdown",
938
+ "metadata": {
939
+ "id": "O99Sb29nEI9y"
940
+ },
941
+ "source": [
942
+ "<br>\n",
943
+ "<br>\n",
944
+ "\n",
945
+ "### <b>STEP - 6 : Taking a directory of images into consideration</b>"
946
+ ]
947
+ },
948
+ {
949
+ "cell_type": "code",
950
+ "execution_count": 10,
951
+ "metadata": {
952
+ "id": "vjKToO-WDcDj"
953
+ },
954
+ "outputs": [],
955
+ "source": [
956
+ "# Directory containing the test images\n",
957
+ "\n",
958
+ "image_folder = \"Deploy-Test Images\""
959
+ ]
960
+ },
961
+ {
962
+ "cell_type": "code",
963
+ "execution_count": 11,
964
+ "metadata": {
965
+ "colab": {
966
+ "base_uri": "https://localhost:8080/"
967
+ },
968
+ "id": "ee6Ct_z3ETpS",
969
+ "outputId": "18727f97-3ee7-4f66-8678-95ff1aed58a4"
970
+ },
971
+ "outputs": [
972
+ {
973
+ "name": "stdout",
974
+ "output_type": "stream",
975
+ "text": [
976
+ "\n",
977
+ "Image: 4994014ef5c834e4803541aa1dc874_big_gallery.jpg\n",
978
+ " Pneumonia Probability: 0.9534\n",
979
+ " TB Probability: 0.9138\n",
980
+ " Lung Cancer Probabilities:\n",
981
+ " adenocarcinoma_left.lower.lobe: 0.0689\n",
982
+ " large.cell.carcinoma_left.hilum: 0.1746\n",
983
+ " NORMAL: 0.4685\n",
984
+ " squamous.cell.carcinoma_left.hilum: 0.2880\n",
985
+ "\n",
986
+ "Image: 4ceeb362df213ccf2c0b4d6388bba1_gallery.jpg\n",
987
+ " Pneumonia Probability: 0.9514\n",
988
+ " TB Probability: 0.7690\n",
989
+ " Lung Cancer Probabilities:\n",
990
+ " adenocarcinoma_left.lower.lobe: 0.0709\n",
991
+ " large.cell.carcinoma_left.hilum: 0.1399\n",
992
+ " NORMAL: 0.5900\n",
993
+ " squamous.cell.carcinoma_left.hilum: 0.1993\n",
994
+ "\n",
995
+ "Image: 642x361_SLIDE_4_What_Does_Lung_Cancer_Look_Like.jpg\n",
996
+ " Pneumonia Probability: 0.9692\n",
997
+ " TB Probability: 0.8210\n",
998
+ " Lung Cancer Probabilities:\n",
999
+ " adenocarcinoma_left.lower.lobe: 0.0384\n",
1000
+ " large.cell.carcinoma_left.hilum: 0.0717\n",
1001
+ " NORMAL: 0.7883\n",
1002
+ " squamous.cell.carcinoma_left.hilum: 0.1015\n",
1003
+ "\n",
1004
+ "Image: Chest_radiograph_of_miliary_tuberculosis_2.jpg\n",
1005
+ " Pneumonia Probability: 0.9638\n",
1006
+ " TB Probability: 0.0253\n",
1007
+ " Lung Cancer Probabilities:\n",
1008
+ " adenocarcinoma_left.lower.lobe: 0.2013\n",
1009
+ " large.cell.carcinoma_left.hilum: 0.3206\n",
1010
+ " NORMAL: 0.1717\n",
1011
+ " squamous.cell.carcinoma_left.hilum: 0.3064\n",
1012
+ "\n",
1013
+ "Image: Dense_opacity_of_primary_pulmonary_tuberculosis.jpg\n",
1014
+ " Pneumonia Probability: 0.9611\n",
1015
+ " TB Probability: 0.5479\n",
1016
+ " Lung Cancer Probabilities:\n",
1017
+ " adenocarcinoma_left.lower.lobe: 0.1466\n",
1018
+ " large.cell.carcinoma_left.hilum: 0.2521\n",
1019
+ " NORMAL: 0.1795\n",
1020
+ " squamous.cell.carcinoma_left.hilum: 0.4219\n",
1021
+ "\n",
1022
+ "Image: eXtmE1V2XgsjZK2JolVQ5g_Border_of_left_atrium.jpg\n",
1023
+ " Pneumonia Probability: 0.3524\n",
1024
+ " TB Probability: 0.9830\n",
1025
+ " Lung Cancer Probabilities:\n",
1026
+ " adenocarcinoma_left.lower.lobe: 0.0413\n",
1027
+ " large.cell.carcinoma_left.hilum: 0.0669\n",
1028
+ " NORMAL: 0.8216\n",
1029
+ " squamous.cell.carcinoma_left.hilum: 0.0702\n",
1030
+ "\n",
1031
+ "Image: iStock_22401848_MEDIUM-58262cb63df78c6f6adebb27.jpg\n",
1032
+ " Pneumonia Probability: 0.8128\n",
1033
+ " TB Probability: 0.9765\n",
1034
+ " Lung Cancer Probabilities:\n",
1035
+ " adenocarcinoma_left.lower.lobe: 0.0307\n",
1036
+ " large.cell.carcinoma_left.hilum: 0.0801\n",
1037
+ " NORMAL: 0.7960\n",
1038
+ " squamous.cell.carcinoma_left.hilum: 0.0932\n",
1039
+ "\n",
1040
+ "Image: lung-scan-article-l.jpg\n",
1041
+ " Pneumonia Probability: 0.9594\n",
1042
+ " TB Probability: 0.5150\n",
1043
+ " Lung Cancer Probabilities:\n",
1044
+ " adenocarcinoma_left.lower.lobe: 0.0991\n",
1045
+ " large.cell.carcinoma_left.hilum: 0.1557\n",
1046
+ " NORMAL: 0.5415\n",
1047
+ " squamous.cell.carcinoma_left.hilum: 0.2036\n",
1048
+ "\n",
1049
+ "Image: lungcancer.jpg\n",
1050
+ " Pneumonia Probability: 0.5294\n",
1051
+ " TB Probability: 0.6075\n",
1052
+ " Lung Cancer Probabilities:\n",
1053
+ " adenocarcinoma_left.lower.lobe: 0.0749\n",
1054
+ " large.cell.carcinoma_left.hilum: 0.0500\n",
1055
+ " NORMAL: 0.8214\n",
1056
+ " squamous.cell.carcinoma_left.hilum: 0.0537\n",
1057
+ "\n",
1058
+ "Image: Normal_posteroanterior_(PA)_chest_radiograph_(X-ray).jpg\n",
1059
+ " Pneumonia Probability: 0.9510\n",
1060
+ " TB Probability: 0.4999\n",
1061
+ " Lung Cancer Probabilities:\n",
1062
+ " adenocarcinoma_left.lower.lobe: 0.0698\n",
1063
+ " large.cell.carcinoma_left.hilum: 0.1852\n",
1064
+ " NORMAL: 0.4874\n",
1065
+ " squamous.cell.carcinoma_left.hilum: 0.2576\n",
1066
+ "\n",
1067
+ "Image: photo.jpg\n",
1068
+ " Pneumonia Probability: 0.5363\n",
1069
+ " TB Probability: 0.2967\n",
1070
+ " Lung Cancer Probabilities:\n",
1071
+ " adenocarcinoma_left.lower.lobe: 0.0169\n",
1072
+ " large.cell.carcinoma_left.hilum: 0.0072\n",
1073
+ " NORMAL: 0.9690\n",
1074
+ " squamous.cell.carcinoma_left.hilum: 0.0069\n"
1075
+ ]
1076
+ }
1077
+ ],
1078
+ "source": [
1079
+ "# Looping through the folder, preprocessing each image, and running inference\n",
1080
+ "results = []\n",
1081
+ "for image_name in os.listdir(image_folder):\n",
1082
+ " image_path = os.path.join(image_folder, image_name)\n",
1083
+ "\n",
1084
+ " if not image_name.lower().endswith(('.png', '.jpg', '.jpeg')): # Ensuring it's an image file\n",
1085
+ " continue\n",
1086
+ "\n",
1087
+ " # Preprocessing the image\n",
1088
+ " image_tensor = preprocess_single_image(image_path)\n",
1089
+ " image_tensor = image_tensor.to(device)\n",
1090
+ "\n",
1091
+ " # Performing inference\n",
1092
+ " with torch.no_grad():\n",
1093
+ " pneumonia_output, tb_output, lung_cancer_output = model(image_tensor)\n",
1094
+ "\n",
1095
+ " # Converting outputs to probabilities\n",
1096
+ " pneumonia_prob = pneumonia_output.item()\n",
1097
+ " tb_prob = tb_output.item()\n",
1098
+ " lung_cancer_probs = F.softmax(lung_cancer_output, dim=1).squeeze().tolist()\n",
1099
+ "\n",
1100
+ " # Appending the results for the current image\n",
1101
+ " results.append({\n",
1102
+ " \"image_name\": image_name,\n",
1103
+ " \"pneumonia_prob\": pneumonia_prob,\n",
1104
+ " \"tb_prob\": tb_prob,\n",
1105
+ " \"lung_cancer_probs\": lung_cancer_probs\n",
1106
+ " })\n",
1107
+ "\n",
1108
+ " lung_cancer_classes = [\n",
1109
+ " \"adenocarcinoma_left.lower.lobe\",\n",
1110
+ " \"large.cell.carcinoma_left.hilum\",\n",
1111
+ " \"NORMAL\",\n",
1112
+ " \"squamous.cell.carcinoma_left.hilum\"\n",
1113
+ " ]\n",
1114
+ "\n",
1115
+ " # Printing the results\n",
1116
+ " print(f\"\\nImage: {image_name}\")\n",
1117
+ " print(f\" Pneumonia Probability: {pneumonia_prob:.4f}\")\n",
1118
+ " print(f\" TB Probability: {tb_prob:.4f}\")\n",
1119
+ "\n",
1120
+ " # Printing lung cancer probabilities with class names\n",
1121
+ " print(\" Lung Cancer Probabilities:\")\n",
1122
+ " for class_name, prob in zip(lung_cancer_classes, lung_cancer_probs):\n",
1123
+ " print(f\" {class_name}: {prob:.4f}\")"
1124
+ ]
1125
+ },
1126
+ {
1127
+ "cell_type": "code",
1128
+ "execution_count": null,
1129
+ "metadata": {
1130
+ "id": "TByqsA1pEkdf"
1131
+ },
1132
+ "outputs": [],
1133
+ "source": []
1134
+ }
1135
+ ],
1136
+ "metadata": {
1137
+ "accelerator": "GPU",
1138
+ "colab": {
1139
+ "gpuType": "T4",
1140
+ "provenance": []
1141
+ },
1142
+ "kernelspec": {
1143
+ "display_name": "AlooBonda",
1144
+ "language": "python",
1145
+ "name": "python3"
1146
+ },
1147
+ "language_info": {
1148
+ "codemirror_mode": {
1149
+ "name": "ipython",
1150
+ "version": 3
1151
+ },
1152
+ "file_extension": ".py",
1153
+ "mimetype": "text/x-python",
1154
+ "name": "python",
1155
+ "nbconvert_exporter": "python",
1156
+ "pygments_lexer": "ipython3",
1157
+ "version": "3.11.9"
1158
+ }
1159
+ },
1160
+ "nbformat": 4,
1161
+ "nbformat_minor": 0
1162
+ }
final_project_model_impementation.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
model_FINAL.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e916764ae14bc2c846735e5e508427220de67d1539d886d6f2189f28c799e58b
3
+ size 32946030
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ pandas
3
+ tensorflow
4
+ torch
5
+ opencv-python
6
+ scikit-learn
7
+ matplotlib
8
+ seaborn
9
+ pillow
10
+ torchvision
11
+ collection
12
+ tqdm
13
+ ipykernel
14
+ gradio
run.sh ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Defining the environment name
4
+ env_name="Chest_Disease"
5
+
6
+ # Activating the conda environment
7
+ source $(conda info --base)/etc/profile.d/conda.sh
8
+ conda activate "$env_name"
9
+ if [ $? -ne 0 ]; then
10
+ echo "Error: Failed to activate the conda environment '$env_name'."
11
+ exit 1
12
+ fi
13
+
14
+ echo "Conda environment '$env_name' activated."
15
+
16
+ # Running the Python script to launch Gradio
17
+ python final_project_Gradio_App.py
18
+ if [ $? -ne 0 ]; then
19
+ echo "Error: Failed to run the Python script."
20
+ exit 1
21
+ fi
22
+
23
+ echo "Gradio application is running."