File size: 1,971 Bytes
c998166 cb9421b c998166 |
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 |
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
# 1.Import and class names setup
class_names = ['CNV','DME','DRUSEN','NORMAL']
# 2. Model annd transforms prepration
model = tf.keras.models.load_model(
'best_model_lg.keras', custom_objects=None, compile=True, safe_mode=True
)
# Load save weights
# 3.prediction function (predict())
def load_and_prep_imgg(filename, img_shape=224, scale=True):
img = tf.io.read_file(filename)
img = tf.io.decode_image(img)
img = tf.image.resize(img, size=[img_shape, img_shape])
if scale:
return img / 255
else:
return img
def predict(img) -> Tuple[Dict,float] :
start_time = timer()
image = load_and_prep_imgg(img)
image = Image.open(image)
pred_img = model.predict(tf.expand_dims(img, axis=0))
pred_class = class_names[pred_img.argmax()]
st.write(f"Predicted brain tumor is: {pred_class} with probability: {pred_img.max():.2f}")
end_time = timer()
pred_time = round(end_time - start_time , 4)
return pred_class , pred_time
### 4. Gradio app - our Gradio interface + launch command
title = 'FoodVision Big'
description = 'Feature Extraxtion VGG model to classifiy Macular Diseases by OCT '
article = 'created at Tensorflow Model Deployment'
# Create example list
example_list = [['examples/'+ example] for example in os.listdir('examples')]
example_list
# create a gradio demo
demo = gr.Interface(fn=predict ,
inputs=gr.Image(type='pil'),
outputs=[gr.Label(num_top_classes = 3 , label= 'prediction'),
gr.Number(label= 'Prediction time (s)')],
examples = example_list,
title = title,
description = description,
article= article)
# Launch the demo
demo.launch(debug= False) |