|
import gradio as gr |
|
|
|
from timeit import default_timer as timer |
|
from typing import Tuple , Dict |
|
import tensorflow as tf |
|
import numpy as np |
|
|
|
from PIL import Image |
|
import os |
|
|
|
|
|
class_names = ['CNV','DME','DRUSEN','NORMAL'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model( |
|
|
|
'combined_model.keras', custom_objects=None, compile=True, safe_mode=False |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_and_prep_imgg(img : Image.Image, img_shape=224, scale=True): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img = img.resize((img_shape, img_shape)) |
|
img = np.array(img) |
|
if img.shape[-1] == 1: |
|
img = np.stack([img] * 3, axis=-1) |
|
img = tf.convert_to_tensor(img, dtype=tf.float32) |
|
if scale: |
|
return img / 255.0 |
|
else: |
|
return img |
|
|
|
def predict(img) -> Tuple[Dict,float,float] : |
|
|
|
start_time = timer() |
|
|
|
image = load_and_prep_imgg(img) |
|
|
|
|
|
pred_img = model.predict(tf.expand_dims(image, axis=0)) |
|
pred_class = class_names[pred_img.argmax()] |
|
print(f"Predicted macular diseases is: {pred_class} with probability: {pred_img.max():.2f}") |
|
|
|
pred_probbb = pred_img.max() * 100 |
|
|
|
|
|
end_time = timer() |
|
pred_time = round(end_time - start_time , 4) |
|
|
|
return pred_class , pred_probbb , pred_time |
|
|
|
|
|
|
|
title = 'Macular Disease Classification' |
|
description = 'Feature Extraction VGG model to classify Macular Diseases by OCT' |
|
article = 'Created with TensorFlow Model Deployment' |
|
|
|
|
|
example_list = [['examples/'+ example] for example in os.listdir('examples')] |
|
example_list |
|
|
|
|
|
demo = gr.Interface(fn=predict , |
|
inputs=gr.Image(type='pil'), |
|
outputs=[gr.Label(num_top_classes = 3 , label= 'prediction'), |
|
gr.Number(label= 'Prediction Probabilities'), |
|
gr.Number(label= 'Prediction time (s)')], |
|
examples = example_list, |
|
title = title, |
|
description = description, |
|
article= article) |
|
|
|
|
|
demo.launch(debug= False) |