Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +44 -0
- kia_fashion_mnist_keras_model.h5 +3 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load the pre-trained Fashion MNIST model
|
7 |
+
model_path = "kia_fashion_mnist_keras_model.h5"
|
8 |
+
model = tf.keras.models.load_model(model_path)
|
9 |
+
|
10 |
+
# Labels for Fashion MNIST
|
11 |
+
labels = [
|
12 |
+
'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
|
13 |
+
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'
|
14 |
+
]
|
15 |
+
|
16 |
+
def predict_fashion(image):
|
17 |
+
# Convert image to grayscale if not already and resize
|
18 |
+
image = Image.fromarray(image).convert('L').resize((28, 28))
|
19 |
+
# Normalize the image
|
20 |
+
image = np.array(image) / 255.0
|
21 |
+
# Reshape for model input
|
22 |
+
image = image.reshape(1, 28, 28, 1)
|
23 |
+
|
24 |
+
# Make a prediction
|
25 |
+
predictions = model.predict(image)
|
26 |
+
prediction = np.argmax(predictions, axis=1)[0]
|
27 |
+
confidence = np.max(predictions)
|
28 |
+
|
29 |
+
# Prepare the output
|
30 |
+
result = f"Predicted category: {labels[prediction]} with confidence: {confidence:.2f}"
|
31 |
+
return result
|
32 |
+
|
33 |
+
# Create Gradio interface
|
34 |
+
input_image = gr.Image(image_mode='L')
|
35 |
+
output_label = gr.Label()
|
36 |
+
interface = gr.Interface(fn=predict_fashion,
|
37 |
+
inputs=input_image,
|
38 |
+
outputs=output_label,
|
39 |
+
examples=["images/0.png", "images/1.png", "images/2.png", "images/3.png"],
|
40 |
+
title="Fashion MNIST Classifier",
|
41 |
+
description="Drag and drop an image or select an example below to predict the fashion category.")
|
42 |
+
|
43 |
+
# Launch the interface
|
44 |
+
interface.launch()
|
kia_fashion_mnist_keras_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a74c184830e9363668121b5767ef4627dde185d6e5e474d025fea66b286ddd05
|
3 |
+
size 20068320
|