Spaces:
Runtime error
Runtime error
| 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/swin-transformers") | |
| def softmax(x): | |
| return np.exp(x) / np.sum(np.exp(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, | |
| examples = ["dog4.png", "ship10.png", "automobile1.png", "airplane10.png"], | |
| title="Image classification with Swin Transfromers", | |
| description = "Model for classifying images from the CIFAR dataset using a vision transformer trained with small data.", | |
| article = "Author: Kelvin Idanwekhai" | |
| ) | |
| iface.launch() |