Spaces:
Build error
Build error
Uploading the main app files
Browse files- DenseNet121d_22_From_Scratch_model0.pth +3 -0
- app.py +74 -0
- examples/16_pco_7.jpg +0 -0
- examples/18_pco_4.jpg +0 -0
- examples/2_normal_21.jpg +0 -0
- model.py +19 -0
- requirements.txt +4 -0
DenseNet121d_22_From_Scratch_model0.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a62ef98a230da5ece3c44c04fc24b955a362740c3fbbe2b0d09b37b719b06dc7
|
| 3 |
+
size 28571079
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = ['infected', 'notinfected']
|
| 12 |
+
|
| 13 |
+
### 2. Model and transforms preparation ###
|
| 14 |
+
|
| 15 |
+
# Create an instance of trained DenseNet121 model
|
| 16 |
+
Dense121, transform = create_DenseNet121_model()
|
| 17 |
+
|
| 18 |
+
### 3. Predict function ###
|
| 19 |
+
|
| 20 |
+
# Create predict function
|
| 21 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 22 |
+
"""
|
| 23 |
+
Transforms and performs a prediction on img then returns prediction and time taken
|
| 24 |
+
"""
|
| 25 |
+
# Start the timer
|
| 26 |
+
start_time = timer()
|
| 27 |
+
|
| 28 |
+
# Transform the target image and add a batch dimension
|
| 29 |
+
img = transform(img).unsqueeze(0)
|
| 30 |
+
|
| 31 |
+
# Put model into evaluation mode and turn on the inference mode
|
| 32 |
+
Dense121.eval()
|
| 33 |
+
with torch.inference_mode():
|
| 34 |
+
# Pass the transformed image through the model and turn the prediction logit intp prediction probability
|
| 35 |
+
pred_logit = Dense121(img).squeeze()
|
| 36 |
+
pred_prob = torch.sigmoid(pred_logit)
|
| 37 |
+
pred_label = torch.round(pred_prob)
|
| 38 |
+
pred_label = pred_label.type(torch.int64)
|
| 39 |
+
pred_class = class_names[pred_label.cpu()]
|
| 40 |
+
# pred_prob = float(pred_prob) # This line and next one are for formatting the pred_prob to print only 4 decimal places
|
| 41 |
+
# pred_prob = round(pred_prob, 4)
|
| 42 |
+
|
| 43 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 44 |
+
pred_label = pred_class
|
| 45 |
+
|
| 46 |
+
# Calculate the prediction time
|
| 47 |
+
pred_time = round(timer() - start_time, 5)
|
| 48 |
+
|
| 49 |
+
# Return the prediction dictionary and prediction time
|
| 50 |
+
return pred_label, pred_time
|
| 51 |
+
|
| 52 |
+
### 4. Gradio app ###
|
| 53 |
+
|
| 54 |
+
# Create title and description strings
|
| 55 |
+
title = "PCOS Detector in Ultrasound Images"
|
| 56 |
+
description = "A DenseNet121 feature extractor computer vision model trained from scratch to classify ultrasound images of ovaries into PCOS infected or not infected."
|
| 57 |
+
article= "Code implementation available at [GitHub](https://github.com/haidary99?tab=repositories)"
|
| 58 |
+
|
| 59 |
+
# Create examples list from "examples/" directory
|
| 60 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 61 |
+
|
| 62 |
+
# Create the Gradio demo
|
| 63 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
| 64 |
+
inputs=gr.Image(type="pil"),
|
| 65 |
+
outputs=[gr.Label(label="Model Prediction"),
|
| 66 |
+
gr.Number(label="Prediction time (s)")],
|
| 67 |
+
# Create examples list from "examples/" directory
|
| 68 |
+
examples=example_list,
|
| 69 |
+
title=title,
|
| 70 |
+
description=description,
|
| 71 |
+
article=article)
|
| 72 |
+
|
| 73 |
+
# Launch the demo
|
| 74 |
+
demo.launch()
|
examples/16_pco_7.jpg
ADDED
|
examples/18_pco_4.jpg
ADDED
|
examples/2_normal_21.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!pip install timm==0.6.13
|
| 2 |
+
import torch
|
| 3 |
+
import timm
|
| 4 |
+
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
|
| 7 |
+
def create_DenseNet121_model(): # Returns trained DenseNet121 model and its transforms
|
| 8 |
+
model_file = "DenseNet121d_22_From_Scratch_model0.pth"
|
| 9 |
+
model = torch.load(model_file, map_location=torch.device('cpu'))
|
| 10 |
+
|
| 11 |
+
transform = transforms.Compose([
|
| 12 |
+
transforms.Resize((224, 224)), # 1. Reshape all images to 224x224
|
| 13 |
+
transforms.ToTensor(), # Turn pixel values to between 0 & 1
|
| 14 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], # 3. A mean of [0.485, 0.456, 0.406] (across each colour channel)
|
| 15 |
+
std=[0.229, 0.224, 0.225]), # 4. A standard deviation of [0.229, 0.224, 0.225] (across each colour channel)
|
| 16 |
+
transforms.Grayscale() ##### change number of color channels from 3 to 1 (I added this)
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
return model, transform
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.12.0
|
| 2 |
+
torchvision==0.13.0
|
| 3 |
+
gradio==3.1.4
|
| 4 |
+
timm==0.6.13
|