Spaces:
Build error
Build error
Initial Commit
Browse filesAdded the model inference pipeline.
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from huggingface_hub import from_pretrained_keras
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
IMAGE_SIZE = 72
|
| 7 |
+
# labels taken from https://huggingface.co/datasets/cifar10
|
| 8 |
+
labels = {0: "airplane",
|
| 9 |
+
1: "automobile"
|
| 10 |
+
2: "bird"
|
| 11 |
+
3: "cat"
|
| 12 |
+
4: "deer"
|
| 13 |
+
5: "dog"
|
| 14 |
+
6: "frog"
|
| 15 |
+
7: "horse"
|
| 16 |
+
8: "ship"
|
| 17 |
+
9: "truck"}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
model = from_pretrained_keras("keras-io/randaugment")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def predict_img_label(img):
|
| 25 |
+
inp = tf.image.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
|
| 26 |
+
pred = model.predict(tf.expand_dims(inp, 0)).flatten()
|
| 27 |
+
return {labels[i]: float(pred[i]) for i in range(len(labels))}
|
| 28 |
+
|
| 29 |
+
image = gr.inputs.Image()
|
| 30 |
+
label = gr.outputs.Label(num_top_classes=3)
|
| 31 |
+
|
| 32 |
+
gr.Interface(
|
| 33 |
+
fn=predict_img_label, inputs=image, outputs=label, interpretation="default").launch()
|
| 34 |
+
|
| 35 |
+
title = "Image Classification Model Using RandAugment"
|
| 36 |
+
description = "Upload an image to classify images"
|
| 37 |
+
|
| 38 |
+
article = "<div style='text-align: center;'><a href='https://github.com/BishmoyPaul' target='_blank'>Space by Bishmoy Paul</a><br><a href='https://keras.io/examples/vision/randaugment/' target='_blank'>Keras example by Sayak Paul</a></div>"
|
| 39 |
+
gr.Interface(predict_img_label, inputs=image, outputs=label, allow_flagging=False, analytics_enabled=False,
|
| 40 |
+
title=title, description=description, article=article).launch(enable_queue=True)
|