File size: 2,170 Bytes
245fc18
 
 
 
 
 
 
5be1cb4
 
245fc18
 
 
 
 
 
 
 
 
fc7adb5
5be1cb4
fb13259
245fc18
 
b9146e0
e4394e9
245fc18
 
 
 
 
79112c6
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
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(shape=(32,32))
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()