awacke1 commited on
Commit
c1fcd9c
1 Parent(s): d4a71bb

Create new file

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_keras
2
+ import tensorflow as tf
3
+ from tensorflow_addons.optimizers import AdamW
4
+ import numpy as np
5
+ import gradio as gr
6
+
7
+ tf.keras.optimizers.AdamW = AdamW
8
+ model = from_pretrained_keras("keras-io/vit_small_ds_v2")
9
+
10
+
11
+
12
+ def softmax(x):
13
+ f_x = np.exp(x) / np.sum(np.exp(x))
14
+ return f_x
15
+
16
+ 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"]
17
+
18
+ def classify_image(image):
19
+ image = image.reshape((-1, 32, 32, 3))
20
+ pred = model.predict(image)
21
+ prediction = softmax(pred)[0]
22
+ return {labels[i]: float(prediction[i]) for i in range(100)}
23
+
24
+ image = gr.inputs.Image(shape=(32,32))
25
+ label = gr.outputs.Label(num_top_classes=5)
26
+
27
+ iface = gr.Interface(classify_image,image,label,
28
+ #outputs=[
29
+ # gr.outputs.Textbox(label="Engine issue"),
30
+ # gr.outputs.Textbox(label="Engine issue score")],
31
+ examples=["dinosaur.jpg"],
32
+ title="Image classification on CIFAR-100",
33
+ description = "Model for classifying images from the CIFAR dataset using a vision transformer trained with small data.",
34
+ article = "Author: <a href=\"https://huggingface.co/joheras\">Jónathan Heras</a>"
35
+ # examples = ["sample.csv"],
36
+ )
37
+
38
+
39
+ iface.launch()