Spaces:
Sleeping
Sleeping
Commit
·
d213efc
0
Parent(s):
Duplicate from Aziizzz/ChestXrayClassification
Browse filesCo-authored-by: mohamed aziz cherif <Aziizzz@users.noreply.huggingface.co>
- .gitattributes +35 -0
- README.md +14 -0
- alexnet_pretrained.pth +3 -0
- app.py +107 -0
- examples/example1.jpg +0 -0
- examples/example2.jpg +0 -0
- examples/example3.jpg +0 -0
- requirements.txt +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz 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
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: ChestXrayClassification
|
| 3 |
+
emoji: 🌖
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.39.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: openrail
|
| 11 |
+
duplicated_from: Aziizzz/ChestXrayClassification
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
alexnet_pretrained.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9685c58431c076bac73b4d0999c2cb62b4c7cd6f28cb5aa622c16dc83e54d736
|
| 3 |
+
size 9920219
|
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### 1. Imports and class names setup ###
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from timeit import default_timer as timer
|
| 7 |
+
from typing import Tuple, Dict
|
| 8 |
+
import torchvision
|
| 9 |
+
|
| 10 |
+
from torch import nn
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def create_effnetb2_model(num_classes: int = 1,
|
| 14 |
+
seed: int = 42):
|
| 15 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
num_classes (int, optional): number of classes in the classifier head.
|
| 19 |
+
Defaults to 3.
|
| 20 |
+
seed (int, optional): random seed value. Defaults to 42.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
| 24 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
| 25 |
+
"""
|
| 26 |
+
# Create EffNetB2 pretrained weights, transforms and model
|
| 27 |
+
weights = torchvision.models.AlexNet_Weights.DEFAULT
|
| 28 |
+
transforms = weights.transforms()
|
| 29 |
+
model = torchvision.models.alexnet(weights=weights)
|
| 30 |
+
|
| 31 |
+
# Freeze all layers in base model
|
| 32 |
+
for param in model.parameters():
|
| 33 |
+
param.requires_grad = False
|
| 34 |
+
|
| 35 |
+
# Change classifier head with random seed for reproducibility
|
| 36 |
+
torch.manual_seed(seed)
|
| 37 |
+
model.classifier = nn.Sequential(
|
| 38 |
+
nn.Dropout(p=0.2,),
|
| 39 |
+
nn.Linear(in_features=9216, out_features=1),
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return model, transforms
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Setup class names
|
| 46 |
+
class_names = ["Normal", "Pneumonia"]
|
| 47 |
+
|
| 48 |
+
### 2. Model and transforms preparation ###
|
| 49 |
+
|
| 50 |
+
# Create EffNetB2 model
|
| 51 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
| 52 |
+
num_classes=1, # len(class_names) would also work
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Load saved weights
|
| 56 |
+
effnetb2.load_state_dict(
|
| 57 |
+
torch.load(
|
| 58 |
+
f="alexnet_pretrained.pth",
|
| 59 |
+
map_location=torch.device("cpu"), # load to CPU
|
| 60 |
+
)
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 65 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
| 66 |
+
"""
|
| 67 |
+
# Start the timer
|
| 68 |
+
start_time = timer()
|
| 69 |
+
|
| 70 |
+
# Transform the target image and add a batch dimension
|
| 71 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
| 72 |
+
|
| 73 |
+
# Put model into evaluation mode and turn on inference mode
|
| 74 |
+
effnetb2.eval()
|
| 75 |
+
with torch.inference_mode():
|
| 76 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 77 |
+
pred_probs = torch.sigmoid(effnetb2(img)).squeeze()
|
| 78 |
+
|
| 79 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 80 |
+
pred_labels_and_probs = {
|
| 81 |
+
'Normal': 1-pred_probs.item(), 'Pneumonia': pred_probs.item()}
|
| 82 |
+
|
| 83 |
+
# Calculate the prediction time
|
| 84 |
+
pred_time = round(timer() - start_time, 5)
|
| 85 |
+
|
| 86 |
+
# Return the prediction dictionary and prediction time
|
| 87 |
+
return pred_labels_and_probs, pred_time
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
example_list = [[f"examples/example{i+1}.jpg"] for i in range(3)]
|
| 91 |
+
# Create title, description and article strings
|
| 92 |
+
title = "ChestXray Classification"
|
| 93 |
+
description = "An Alexnet computer vision model to classify images of Xray Chest images as Normal or Pneumonia."
|
| 94 |
+
article = "Created at (https://github.com/azizche/chest_xray_Classification)."
|
| 95 |
+
|
| 96 |
+
# Create the Gradio demo
|
| 97 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
| 98 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
| 99 |
+
outputs=[gr.Label(num_top_classes=2, label="Predictions"), # what are the outputs?
|
| 100 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
| 101 |
+
examples=example_list,
|
| 102 |
+
title=title,
|
| 103 |
+
description=description,
|
| 104 |
+
article=article)
|
| 105 |
+
|
| 106 |
+
# Launch the demo!
|
| 107 |
+
demo.launch()
|
examples/example1.jpg
ADDED
|
examples/example2.jpg
ADDED
|
examples/example3.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|