File size: 1,497 Bytes
f85da4b
 
 
 
 
 
 
80028a8
f85da4b
 
1627a17
 
 
 
0277979
f85da4b
 
 
4af3eba
f85da4b
 
1627a17
524e94c
f85da4b
 
 
80028a8
 
 
1627a17
f85da4b
4d634a3
 
f85da4b
4d634a3
 
62c65d0
 
 
f85da4b
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
# Workaround to install the lib without "setup.py"
import sys
from git import Repo
Repo.clone_from("https://github.com/dimitreOliveira/hub.git", "./hub")
sys.path.append("/hub")

import gradio as gr
import tensorflow as tf
from hub.tensorflow_hub.hf_utils import pull_from_hub

import requests
# Download human-readable labels for ImageNet.
response = requests.get("https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt")
labels = [x for x in response.text.split("\n") if x != ""]

model = pull_from_hub(repo_id="Dimitre/mobilenet_v3_small")

def preprocess(image):
    image = image.reshape((-1, 224, 224, 3)) # (batch_size, height, width, num_channels)
    return image / 255.

def postprocess(prediction):
    return {labels[i]: float(prediction[i]) for i in range(len(labels))}

def predict_fn(image):
    image = preprocess(image)
    logits = model(image)
    probs = tf.nn.softmax(logits, axis=1)[0].numpy()
    scores = postprocess(probs)
    return scores

description = "Using the power of CLIP and a simple small CNN, find images from movies based on what you draw!"

iface = gr.Interface(fn=predict_fn, 
                    title="ImageNet classification with mobilenet",
                    description="Predict from wich ImageNet class your images belongs",
                    inputs=gr.Image(shape=(224, 224)), 
                    outputs=gr.Label(num_top_classes=5),
                    examples=["apples.jpeg", "banana.jpeg", "car.jpeg"])
iface.launch()