Spaces:
Runtime error
Runtime error
DenseNet121 Chest Xray Classifier
Browse files- .gitattributes +1 -0
- app.py +77 -0
- densenet_chest_xray_weight.pth +3 -0
- examples/COVID19(551).jpg +0 -0
- examples/NORMAL(1283).jpg +3 -0
- examples/PNEUMONIA(4112).jpg +0 -0
- model.py +27 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
examples/NORMAL(1283).jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_densenet121_model
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
from typing import Tuple, Dict
|
9 |
+
|
10 |
+
# Setup class names
|
11 |
+
class_names = ["COVID19", "NORMAL", "PNEUMONIA"]
|
12 |
+
|
13 |
+
### 2. Model and transforms preparation ###
|
14 |
+
|
15 |
+
# Create DenseNet121 model
|
16 |
+
densenet121, densenet121_transforms = create_densenet121_model(
|
17 |
+
num_classes=3, # len(class_names) would also work
|
18 |
+
)
|
19 |
+
|
20 |
+
# Load saved weights
|
21 |
+
densenet121.load_state_dict(
|
22 |
+
torch.load(
|
23 |
+
f="densenet_chest_xray_weight.pth",
|
24 |
+
map_location=torch.device("cpu"), # load to CPU
|
25 |
+
)
|
26 |
+
)
|
27 |
+
|
28 |
+
### 3. Predict function ###
|
29 |
+
|
30 |
+
# Create predict function
|
31 |
+
def predict(img) -> Tuple[Dict, float]:
|
32 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
33 |
+
"""
|
34 |
+
# Start the timer
|
35 |
+
start_time = timer()
|
36 |
+
|
37 |
+
# Transform the target image and add a batch dimension
|
38 |
+
img = densenet121_transforms(img).unsqueeze(0)
|
39 |
+
|
40 |
+
# Put model into evaluation mode and turn on inference mode
|
41 |
+
effnetb2.eval()
|
42 |
+
with torch.inference_mode():
|
43 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
44 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
45 |
+
|
46 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
47 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
48 |
+
|
49 |
+
# Calculate the prediction time
|
50 |
+
pred_time = round(timer() - start_time, 5)
|
51 |
+
|
52 |
+
# Return the prediction dictionary and prediction time
|
53 |
+
return pred_labels_and_probs, pred_time
|
54 |
+
|
55 |
+
### 4. Gradio app ###
|
56 |
+
|
57 |
+
# Create title, description and article strings
|
58 |
+
title = "Chest X-ray Analysis for COVID-19, Pneumonia, and Normal Cases using DenseNet121"
|
59 |
+
description = "Utilizing Deep Learning for accurate detection and classification of Chest X-ray images."
|
60 |
+
article = "This project employs the DenseNet121 model to analyze Chest X-ray images for classification into COVID-19, Pneumonia, and Normal cases. Leveraging the capabilities of Deep Learning, the model ensures precise and reliable results, contributing to improved medical diagnostics."
|
61 |
+
|
62 |
+
# Create examples list from "examples/" directory
|
63 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
64 |
+
|
65 |
+
# Create the Gradio demo
|
66 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
67 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
68 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
69 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
70 |
+
# Create examples list from "examples/" directory
|
71 |
+
examples=example_list,
|
72 |
+
title=title,
|
73 |
+
description=description,
|
74 |
+
article=article)
|
75 |
+
|
76 |
+
# Launch the demo!
|
77 |
+
demo.launch()
|
densenet_chest_xray_weight.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:41e385bc0798f93345f9669ea6e5e80062b4147d680614b0725587088bcef5ca
|
3 |
+
size 28446341
|
examples/COVID19(551).jpg
ADDED
examples/NORMAL(1283).jpg
ADDED
Git LFS Details
|
examples/PNEUMONIA(4112).jpg
ADDED
model.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
|
7 |
+
def create_densenet121_model(num_classes:int=3,
|
8 |
+
seed:int=42):
|
9 |
+
# Create DenseNet121 pretrained weights, transforms and model
|
10 |
+
weights = torchvision.models.DenseNet121_Weights.DEFAULT
|
11 |
+
transforms = weights.transforms()
|
12 |
+
model = torchvision.models.densenet121(weights=weights)
|
13 |
+
|
14 |
+
# Freeze all layers in base model
|
15 |
+
for param in model.parameters():
|
16 |
+
param.requires_grad = False
|
17 |
+
|
18 |
+
# Change classifier head with random seed for reproducibility
|
19 |
+
torch.manual_seed(seed)
|
20 |
+
|
21 |
+
model.classifier = torch.nn.Sequential(
|
22 |
+
torch.nn.Dropout(p=0.2, inplace=True),
|
23 |
+
torch.nn.Linear(in_features=1024,
|
24 |
+
out_features=3,
|
25 |
+
bias=True))
|
26 |
+
|
27 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|