Spaces:
Sleeping
Sleeping
Bijan k commited on
Commit ·
7317b71
1
Parent(s): e0df8aa
Testing a different configuration
Browse files- app.py +39 -41
- requirements.txt +2 -3
app.py
CHANGED
|
@@ -1,74 +1,72 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import tensorflow as tf
|
| 4 |
-
from tensorflow
|
| 5 |
-
from tensorflow.keras.models import
|
| 6 |
-
from tensorflow.keras
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
model = Model(inputs=input_tensor, outputs=x)
|
| 20 |
|
| 21 |
model.compile(
|
| 22 |
-
optimizer=
|
| 23 |
-
loss=
|
| 24 |
metrics=["accuracy"],
|
| 25 |
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
try:
|
| 29 |
-
model.load_weights("my_model.h5")
|
| 30 |
-
except:
|
| 31 |
-
print("Could not load weights. Using untrained model.")
|
| 32 |
|
| 33 |
|
| 34 |
def classify_image(image):
|
| 35 |
# Preprocess the image
|
| 36 |
image_gray = tf.image.rgb_to_grayscale(image)
|
| 37 |
-
image_tensor = tf.
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
image_tensor = tf.expand_dims(image_tensor, 0)
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
| 43 |
prediction = model.predict(image_tensor)
|
| 44 |
|
| 45 |
-
# Convert the prediction to a
|
| 46 |
-
|
| 47 |
-
confidences = {str(idx): float(prediction[0][idx]) for idx in top_indices}
|
| 48 |
|
| 49 |
-
return
|
| 50 |
|
| 51 |
|
| 52 |
-
title = "MNIST Model 98%
|
| 53 |
-
description = "Model trained on MNIST dataset using
|
| 54 |
-
article = "
|
| 55 |
|
| 56 |
-
example_list = []
|
| 57 |
-
if os.path.exists("examples"):
|
| 58 |
-
example_list = [
|
| 59 |
-
["examples/" + example]
|
| 60 |
-
for example in os.listdir("examples")
|
| 61 |
-
if os.path.isfile(os.path.join("examples", example))
|
| 62 |
-
]
|
| 63 |
|
| 64 |
interface = gr.Interface(
|
| 65 |
fn=classify_image,
|
| 66 |
inputs=gr.Image(type="pil"),
|
| 67 |
-
outputs=gr.Label(num_top_classes=3),
|
| 68 |
examples=example_list,
|
| 69 |
title=title,
|
| 70 |
description=description,
|
| 71 |
article=article,
|
| 72 |
)
|
| 73 |
|
|
|
|
| 74 |
interface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import tensorflow as tf
|
| 4 |
+
from tensorflow import keras
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from tensorflow.keras import layers
|
| 7 |
+
from efficientnet.tfkeras import EfficientNetB0
|
| 8 |
+
|
| 9 |
+
model = keras.Sequential(
|
| 10 |
+
[
|
| 11 |
+
layers.Input(shape=(32, 32, 1)),
|
| 12 |
+
layers.experimental.preprocessing.Rescaling(1.0 / 255),
|
| 13 |
+
EfficientNetB0(include_top=False, weights=None, input_shape=(32, 32, 1)),
|
| 14 |
+
layers.GlobalAveragePooling2D(),
|
| 15 |
+
layers.Dropout(0.2),
|
| 16 |
+
layers.Dense(10),
|
| 17 |
+
]
|
| 18 |
+
)
|
|
|
|
| 19 |
|
| 20 |
model.compile(
|
| 21 |
+
optimizer=keras.optimizers.Adam(learning_rate=1e-3),
|
| 22 |
+
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
| 23 |
metrics=["accuracy"],
|
| 24 |
)
|
| 25 |
|
| 26 |
+
model = load_model("my_model.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
def classify_image(image):
|
| 30 |
# Preprocess the image
|
| 31 |
image_gray = tf.image.rgb_to_grayscale(image)
|
| 32 |
+
image_tensor = tf.convert_to_tensor(image_gray)
|
| 33 |
|
| 34 |
+
# Resize the image to 28x28.
|
| 35 |
+
image_tensor = tf.image.resize(image_tensor, (32, 32))
|
| 36 |
+
|
| 37 |
+
# Cast the data to float32.
|
| 38 |
+
image_tensor = tf.cast(image_tensor, tf.float32)
|
| 39 |
+
|
| 40 |
+
# Add a batch dimension.
|
| 41 |
image_tensor = tf.expand_dims(image_tensor, 0)
|
| 42 |
|
| 43 |
+
# Normalize the data.
|
| 44 |
+
image_tensor = image_tensor / 255.0
|
| 45 |
+
|
| 46 |
+
# Get the prediction.
|
| 47 |
prediction = model.predict(image_tensor)
|
| 48 |
|
| 49 |
+
# Convert the prediction to a string label.
|
| 50 |
+
prediction_label = str(prediction.argmax())
|
|
|
|
| 51 |
|
| 52 |
+
return prediction_label
|
| 53 |
|
| 54 |
|
| 55 |
+
title = "MNIST Model 98%acc"
|
| 56 |
+
description = "Model trained on MNIST dataset using efficientnet to classify MNIST images with 98% accuracy"
|
| 57 |
+
article = "for source code you can visit [my github](https://github.com/Bijan-K/Tensorflow-MNIST-98Acc.git) (gradio + training code)."
|
| 58 |
|
| 59 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
interface = gr.Interface(
|
| 62 |
fn=classify_image,
|
| 63 |
inputs=gr.Image(type="pil"),
|
| 64 |
+
outputs=gr.Label(num_top_classes=3, label="Predictions"),
|
| 65 |
examples=example_list,
|
| 66 |
title=title,
|
| 67 |
description=description,
|
| 68 |
article=article,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
|
| 72 |
interface.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
tensorflow==2.
|
| 2 |
-
keras==2.9.0
|
| 3 |
efficientnet==1.1.1
|
| 4 |
-
gradio
|
|
|
|
| 1 |
+
tensorflow==2.6.0
|
|
|
|
| 2 |
efficientnet==1.1.1
|
| 3 |
+
gradio
|