t3n50r commited on
Commit
e62d683
·
1 Parent(s): 24d7304

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/swin-transformers")
9
+
10
+
11
+
12
+ def softmax(x):
13
+ return np.exp(x) / np.sum(np.exp(x))
14
+
15
+ 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"]
16
+
17
+ def classify_image(image):
18
+ image = image.reshape((-1, 32, 32, 3))
19
+ pred = model.predict(image)
20
+ prediction = softmax(pred)[0]
21
+ return {labels[i]: float(prediction[i]) for i in range(100)}
22
+
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
+ examples = ["dog4.png", "ship10.png", "automobile1.png", "airplane10.png"],
29
+ title="Image classification with Swin Transfromers",
30
+ description = "Model for classifying images from the CIFAR dataset using a vision transformer trained with small data.",
31
+ article = "Author: <a href=\"https://huggingface.co/joheras\">Jónathan Heras</a>"
32
+ )
33
+
34
+
35
+ iface.launch()