Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
| 1 |
-
import tensorflow as tf
|
| 2 |
-
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from src.load_dataset.load_dataset import classes
|
| 5 |
-
|
| 6 |
-
nm_model = tf.keras.models.load_model("
|
| 7 |
-
|
| 8 |
-
resnet_model = tf.keras.models.load_model("../models/newmodel.h5")
|
| 9 |
-
|
| 10 |
-
inception_model = tf.keras.models.load_model("
|
| 11 |
-
|
| 12 |
-
cifar10_labels = classes
|
| 13 |
-
models = [
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def classify_image(input_image, model_name):
|
| 17 |
-
try:
|
| 18 |
-
input_image = tf.image.resize(input_image, (32, 32))
|
| 19 |
-
labels = cifar10_labels
|
| 20 |
-
model = get_model(model_name)
|
| 21 |
-
input_image = tf.expand_dims(input_image, axis=0)
|
| 22 |
-
predictions = model.predict(input_image).flatten()
|
| 23 |
-
top_indices = predictions.argsort()[-10:][::-1]
|
| 24 |
-
confidences = {labels[i]: float(predictions[i]) for i in top_indices}
|
| 25 |
-
return confidences
|
| 26 |
-
except Exception as e:
|
| 27 |
-
return {"error": str(e)}
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def get_model(model_name):
|
| 31 |
-
if model_name == "MobileNetBased Model":
|
| 32 |
-
return nm_model
|
| 33 |
-
elif model_name == "ResNetBased Model":
|
| 34 |
-
|
| 35 |
-
elif model_name == "InceptionBased Model":
|
| 36 |
-
return inception_model
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
interface = gr.Interface(
|
| 40 |
-
fn=classify_image,
|
| 41 |
-
inputs=[gr.Image(type="numpy", image_mode="RGB", label="Input Image"),
|
| 42 |
-
gr.Dropdown(models, label="Model Choice")],
|
| 43 |
-
outputs=gr.Label(num_top_classes=3, label="Predictions"),
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
interface.launch(debug=False, share=True)
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from src.load_dataset.load_dataset import classes
|
| 5 |
+
|
| 6 |
+
nm_model = tf.keras.models.load_model("mn_model.keras")
|
| 7 |
+
|
| 8 |
+
# resnet_model = tf.keras.models.load_model("../models/newmodel.h5")
|
| 9 |
+
|
| 10 |
+
inception_model = tf.keras.models.load_model("inception_v3.keras")
|
| 11 |
+
|
| 12 |
+
cifar10_labels = classes
|
| 13 |
+
models = [ "MobileNetBased Model", "InceptionBased Model"]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def classify_image(input_image, model_name):
|
| 17 |
+
try:
|
| 18 |
+
input_image = tf.image.resize(input_image, (32, 32))
|
| 19 |
+
labels = cifar10_labels
|
| 20 |
+
model = get_model(model_name)
|
| 21 |
+
input_image = tf.expand_dims(input_image, axis=0)
|
| 22 |
+
predictions = model.predict(input_image).flatten()
|
| 23 |
+
top_indices = predictions.argsort()[-10:][::-1]
|
| 24 |
+
confidences = {labels[i]: float(predictions[i]) for i in top_indices}
|
| 25 |
+
return confidences
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return {"error": str(e)}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_model(model_name):
|
| 31 |
+
if model_name == "MobileNetBased Model":
|
| 32 |
+
return nm_model
|
| 33 |
+
# elif model_name == "ResNetBased Model":
|
| 34 |
+
# return resnet_model
|
| 35 |
+
elif model_name == "InceptionBased Model":
|
| 36 |
+
return inception_model
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
interface = gr.Interface(
|
| 40 |
+
fn=classify_image,
|
| 41 |
+
inputs=[gr.Image(type="numpy", image_mode="RGB", label="Input Image"),
|
| 42 |
+
gr.Dropdown(models, label="Model Choice")],
|
| 43 |
+
outputs=gr.Label(num_top_classes=3, label="Predictions"),
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
interface.launch(debug=False, share=True)
|