|
|
|
"""gradio_deploy.ipynb |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/13X2E9v7GxryXyT39R5CzxrNwxfA6KMFJ |
|
""" |
|
import os |
|
import gradio as gr |
|
from PIL import Image |
|
from timeit import default_timer as timer |
|
from tensorflow import keras |
|
import numpy as np |
|
|
|
MODEL = keras.models.load_model("convnet_from_scratch_with_augmentation.keras") |
|
|
|
def predict(img): |
|
|
|
|
|
start_time = timer() |
|
|
|
|
|
features = Image.open(img) |
|
features = features.resize((180, 180)) |
|
features = np.array(features).reshape(1, 180,180,3) |
|
|
|
|
|
|
|
pred_labels_and_probs = {'dog':float(MODEL.predict(features))} if MODEL.predict(features)> 0.5 else {'cat':1-float(MODEL.predict(features))} |
|
|
|
|
|
pred_time = round(timer() - start_time, 5) |
|
|
|
|
|
return pred_labels_and_probs, pred_time |
|
|
|
|
|
example_list = [["examples/" + example] for example in os.listdir("examples")] |
|
title = "Classification Demo" |
|
description = "Cat/Dog classification Tensorflow model with Augmented small dataset" |
|
|
|
|
|
demo = gr.Interface(fn=predict, |
|
inputs=gr.Image(type='filepath'), |
|
outputs=[gr.Label(label="Predictions"), |
|
gr.Number(label="Prediction time (s)")], |
|
examples=example_list, |
|
title=title, |
|
description=description,) |
|
|
|
|
|
demo.launch() |
|
|
|
|