File size: 2,113 Bytes
7462b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""gradio_deploy.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/13X2E9v7GxryXyT39R5CzxrNwxfA6KMFJ
"""

!pip install gradio

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 the timer
    start_time = timer()

    # Reading the image and size transformation
    features = Image.open(img)
    features = features.resize((180, 180))
    features = np.array(features).reshape(1, 180,180,3)

    # Create a prediction label and prediction probability dictionary for each prediction class
    # This is the required format for Gradio's output parameter
    pred_labels_and_probs = {'dog' if MODEL.predict(features)> 0.5 else 'cat':float(MODEL.predict(features))}

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

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

predict('/content/cat.1505.jpg')

# Create title, description and article strings
title = "Classification Demo"
description = "Cat/Dog classification Tensorflow model with Augmentted small dataset"

# Create the Gradio demo
demo = gr.Interface(fn=predict, # mapping function from input to output
                    inputs=gr.Image(type='filepath'), # what are the inputs?
                    outputs=[gr.Label(label="Predictions"), # what are the outputs?
                             gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
                    title=title,
                    description=description,)

# Launch the demo!
demo.launch(debug=False, # print errors locally?
            share=True) # generate a publically shareable URL?

pip install tensorflow

import PIL

import tensorflow as tf

import timeit

print(gr.__version__)
print(np.__version__)
print(tf.__version__)
print(PIL.__version__)