Spaces:
Build error
Build error
File size: 2,276 Bytes
245fc18 c1591ca 245fc18 fb13259 245fc18 e4394e9 245fc18 8a664f1 d01aa07 90826d0 245fc18 |
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 |
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")
IMAGE_SIZE = 72
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):
#inp = inp.reshape((-1, 299, 299, 3))
resized_image = tf.image.resize(
tf.convert_to_tensor([image]), size=(IMAGE_SIZE, IMAGE_SIZE))
pred = model.predict(resized_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=["dog4.png","airplane4.png"],
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()
|