vit-small-ds / app.py
joheras's picture
images
79112c6
raw
history blame
2.16 kB
from huggingface_hub import from_pretrained_keras
import tensorflow as tf
from tensorflow_addons.optimizers import AdamW
import numpy as np
import gradio as gr
tf.keras.optimizers.AdamW = AdamW
model = from_pretrained_keras("keras-io/vit_small_ds_v2")
def softmax(x):
f_x = np.exp(x) / np.sum(np.exp(x))
return f_x
labels = ["apple", "aquarium_fish", "baby", "bear", "beaver", "bed", "bee", "beetle", "bicycle", "bottle", "bowl", "boy", "bridge", "bus", "butterfly", "camel", "can", "castle", "caterpillar", "cattle", "chair", "chimpanzee", "clock", "cloud", "cockroach", "couch", "cra", "crocodile", "cup", "dinosaur", "dolphin", "elephant", "flatfish", "forest", "fox", "girl", "hamster", "house", "kangaroo", "keyboard", "lamp", "lawn_mower", "leopard", "lion", "lizard", "lobster", "man", "maple_tree", "motorcycle", "mountain", "mouse", "mushroom", "oak_tree", "orange", "orchid", "otter", "palm_tree", "pear", "pickup_truck", "pine_tree", "plain", "plate", "poppy", "porcupine", "possum", "rabbit", "raccoon", "ray", "road", "rocket", "rose", "sea", "seal", "shark", "shrew", "skunk", "skyscraper", "snail", "snake", "spider", "squirrel", "streetcar", "sunflower", "sweet_pepper", "table", "tank", "telephone", "television", "tiger", "tractor", "train", "trout", "tulip", "turtle", "wardrobe", "whale", "willow_tree", "wolf", "woman", "worm"]
def classify_image(image):
image = image.reshape((1, 32, 32, 3))
pred = model.predict(image)
prediction = softmax(pred)[0]
return {labels[i]: float(prediction[i]) for i in range(100)}
image = gr.inputs.Image()
label = gr.outputs.Label(num_top_classes=5)
iface = gr.Interface(classify_image,image,label,
#outputs=[
# gr.outputs.Textbox(label="Engine issue"),
# gr.outputs.Textbox(label="Engine issue score")],
examples=["dinosaur.jpg","forest.jpg"],
title="Image classification on CIFAR-100",
description = "Model for classifying images from the CIFAR dataset using a vision transformer trained with small data.",
article = "Author: <a href=\"https://huggingface.co/joheras\">Jónathan Heras</a>"
# examples = ["sample.csv"],
)
iface.launch()