added app
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import from_pretrained_keras
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
CLASSES = {
|
| 7 |
+
0: "airplane",
|
| 8 |
+
1: "automobile",
|
| 9 |
+
2: "bird",
|
| 10 |
+
3: "cat",
|
| 11 |
+
4: "deer",
|
| 12 |
+
5: "dog",
|
| 13 |
+
6: "frog",
|
| 14 |
+
7: "horse",
|
| 15 |
+
8: "ship",
|
| 16 |
+
9: "truck",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
IMAGE_SIZE = 32
|
| 20 |
+
|
| 21 |
+
model = from_pretrained_keras("EdoAbati/cct")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def reshape_image(image):
|
| 25 |
+
image = tf.convert_to_tensor(image)
|
| 26 |
+
image.set_shape([None, None, 3])
|
| 27 |
+
image = tf.image.resize(images=image, size=[IMAGE_SIZE, IMAGE_SIZE])
|
| 28 |
+
image = tf.expand_dims(image, axis=0)
|
| 29 |
+
return image
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def classify_image(input_image):
|
| 33 |
+
input_image = reshape_image(input_image)
|
| 34 |
+
logits = model.predict(input_image).flatten()
|
| 35 |
+
predictions = tf.nn.softmax(logits)
|
| 36 |
+
output_labels = {CLASSES[i]: float(predictions[i]) for i in CLASSES.keys()}
|
| 37 |
+
return output_labels
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
examples = [["./bird.png"], ["./cat.png"], ["./dog.png"], ["./horse.png"]]
|
| 41 |
+
title = "Image Classification using Compact Convolutional Transformer (CCT)"
|
| 42 |
+
description = """
|
| 43 |
+
Upload an image or select one from the examples and ask the model to label it!
|
| 44 |
+
<br />
|
| 45 |
+
The model was trained on the <a href="https://www.cs.toronto.edu/~kriz/cifar.html" target="_blank">CIFAR-10 dataset</a>. Therefore, it is able to recognise these 10 classes: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck.
|
| 46 |
+
<br />
|
| 47 |
+
<br />
|
| 48 |
+
<p>
|
| 49 |
+
<b>Model: https://huggingface.co/keras-io/cct</b>
|
| 50 |
+
<br />
|
| 51 |
+
<b>Keras Example: https://keras.io/examples/vision/cct/</b>
|
| 52 |
+
</p>
|
| 53 |
+
<br />
|
| 54 |
+
"""
|
| 55 |
+
article = """
|
| 56 |
+
<div style="text-align: center;">
|
| 57 |
+
Space by <a href="https://www.linkedin.com/in/edoardoabati/" target="_blank">Edoardo Abati</a>
|
| 58 |
+
<br />
|
| 59 |
+
Keras example by <a href="https://twitter.com/RisingSayak" target="_blank">Sayak Paul</a>
|
| 60 |
+
</div>
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=classify_image,
|
| 65 |
+
inputs=gr.inputs.Image(),
|
| 66 |
+
outputs=gr.outputs.Label(),
|
| 67 |
+
examples=examples,
|
| 68 |
+
title=title,
|
| 69 |
+
description=description,
|
| 70 |
+
article=article,
|
| 71 |
+
allow_flagging="never",
|
| 72 |
+
)
|
| 73 |
+
interface.launch(enable_queue=True)
|