File size: 2,746 Bytes
b0a9fa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf8336
b0a9fa4
 
 
 
 
 
 
 
 
 
 
 
7daaf98
 
b0a9fa4
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
### 1. Imports and class names setup ###
import gradio as gr
import os
import torch

from model import create_DenseNet121_model
from timeit import default_timer as timer
from typing import Tuple, Dict

# Setup class names
class_names = ['infected', 'notinfected']

### 2. Model and transforms preparation ###

# Create an instance of trained DenseNet121 model
Dense121, transform = create_DenseNet121_model()

### 3. Predict function ###

# Create predict function
def predict(img) -> Tuple[Dict, float]:
  """
  Transforms and performs a prediction on img then returns prediction and time taken
  """
  # Start the timer
  start_time = timer()

  # Transform the target image and add a batch dimension
  img = transform(img).unsqueeze(0)
  
  # Put model into evaluation mode and turn on the inference mode
  Dense121.eval()
  with torch.inference_mode():
    # Pass the transformed image through the model and turn the prediction logit intp prediction probability
    pred_logit = Dense121(img).squeeze()
    pred_prob = torch.sigmoid(pred_logit)
    pred_label = torch.round(pred_prob)
    pred_label = pred_label.type(torch.int64)
    pred_class = class_names[pred_label.cpu()]
    # pred_prob = float(pred_prob) # This line and next one are for formatting the pred_prob to print only 4 decimal places
    # pred_prob = round(pred_prob, 4)

  # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
  pred_label = pred_class 

  # Calculate the prediction time
  pred_time = round(timer() - start_time, 5)

  # Return the prediction dictionary and prediction time
  return pred_label, pred_time

### 4. Gradio app ###

# Create title and description strings
title = "PCOS Detector in Ultrasound Images"
description = "A DenseNet121 feature extractor computer vision model trained from scratch to classify ultrasound images of ovaries into PCOS infected or not infected."
#article= "Code implementation available at [GitHub](https://github.com/haidary99?tab=repositories)"

# Create examples list from "examples/" directory
example_list = [["examples/" + example] for example in os.listdir("examples")]

# Create the Gradio demo
demo = gr.Interface(fn=predict, # mapping function from input to output
                    inputs=gr.Image(type="pil"),
                    outputs=[gr.Label(label="Model Prediction"),
                             gr.Number(label="Prediction time (s)")],
                    # Create examples list from "examples/" directory
                    examples=example_list,
                    title=title,
                    description=description)
                    #article=article)

# Launch the demo
demo.launch()