| 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( |
| 'best_model_lg.keras', custom_objects=None, compile=True, safe_mode=True |
| ) |
|
|
| |
|
|
| |
|
|
| 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 |
|
|
| |
|
|
| title = 'FoodVision Big' |
| description = 'Feature Extraxtion VGG model to classifiy Macular Diseases by OCT ' |
| article = 'created at 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 time (s)')], |
| examples = example_list, |
| title = title, |
| description = description, |
| article= article) |
|
|
| |
| demo.launch(debug= False) |