Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Class names
|
| 6 |
+
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
|
| 7 |
+
'dog', 'frog', 'horse', 'ship', 'truck']
|
| 8 |
+
|
| 9 |
+
# Load trained model
|
| 10 |
+
model = load_model("ResNet50_cifar10_best_fr.h5")
|
| 11 |
+
|
| 12 |
+
# Define the preprocessing function
|
| 13 |
+
def preprocess_image(img):
|
| 14 |
+
img = tf.image.resize(img, (32, 32))
|
| 15 |
+
img = img / 255.0
|
| 16 |
+
img = tf.expand_dims(img, axis=0)
|
| 17 |
+
return img
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Define the postprocessing function
|
| 21 |
+
def process_prediction(prediction):
|
| 22 |
+
predicted_class_index = int(prediction.argmax())
|
| 23 |
+
predicted_class_name = class_names[predicted_class_index]
|
| 24 |
+
return predicted_class_name
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Define the prediction function
|
| 28 |
+
def predict_cifar10(img):
|
| 29 |
+
preprocessed_img = preprocess_image(img)
|
| 30 |
+
prediction = model.predict(preprocessed_img)
|
| 31 |
+
return process_prediction(prediction)
|
| 32 |
+
|
| 33 |
+
# Create Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=predict_cifar10,
|
| 36 |
+
inputs=[gr.Image(label="Input Image")],
|
| 37 |
+
outputs=[gr.Label(label="Predicted Class")],
|
| 38 |
+
title="CIFAR-10 Image Classifier",
|
| 39 |
+
description="Upload an image to classify it using a CIFAR-10 model."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the interface
|
| 43 |
+
iface.launch()
|