Skip to content

Instantly share code, notes, and snippets.

@keuv-grvl
Created June 2, 2022 07:55
Show Gist options
  • Save keuv-grvl/77d2cced0ec1e273c2a514a7809da554 to your computer and use it in GitHub Desktop.
Save keuv-grvl/77d2cced0ec1e273c2a514a7809da554 to your computer and use it in GitHub Desktop.
Gradio example for image classification
import gradio as gr
import numpy as np
import tensorflow as tf
import requests
# Load a classification model
model = tf.keras.applications.MobileNetV3Large(
input_shape=None,
alpha=1.0,
minimalistic=False,
include_top=True,
weights="imagenet",
input_tensor=None,
classes=1000,
pooling=None,
dropout_rate=0.2,
classifier_activation="softmax",
include_preprocessing=True,
)
# Download human-readable labels for ImageNet
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")
def pred(inp):
predictions = model.predict(inp[np.newaxis, ...]).flatten().tolist()
confidences = dict(zip(labels, predictions))
return confidences
demo = gr.Interface(
fn=pred,
inputs=gr.inputs.Image(shape=(224, 224)),
outputs=gr.outputs.Label(num_top_classes=15),
)
demo.launch(share=False)
demo.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment