File size: 1,432 Bytes
7a1d742
 
 
ef6c5a1
7a1d742
c8b2a9b
7a1d742
0af9569
7a1d742
 
 
 
0af9569
7a1d742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8b2a9b
 
0af9569
 
c8b2a9b
 
7a1d742
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import tensorflow as tf
import gradio as gr

from load_dataset import classes

nm_model = tf.keras.models.load_model("mn_model.keras")

resnet_model = tf.keras.models.load_model("resnet_best.h5")

inception_model = tf.keras.models.load_model("inception_v3.keras")

cifar10_labels = classes
models = [ "InceptionBased Model", "MobileNetBased Model", "ResNetBased Model"]


def classify_image(input_image, model_name):
    try:
        input_image = tf.image.resize(input_image, (32, 32))
        labels = cifar10_labels
        model = get_model(model_name)
        input_image = tf.expand_dims(input_image, axis=0)
        predictions = model.predict(input_image).flatten()
        top_indices = predictions.argsort()[-10:][::-1]
        confidences = {labels[i]: float(predictions[i]) for i in top_indices}
        return confidences
    except Exception as e:
        return {"error": str(e)}


def get_model(model_name):
    if model_name == "MobileNetBased Model":
        return nm_model
    elif model_name == "ResNetBased Model":
        return resnet_model
    elif model_name == "InceptionBased Model":
        return inception_model


interface = gr.Interface(
    fn=classify_image,
    inputs=[gr.Image(type="numpy", image_mode="RGB", label="Input Image"),
            gr.Dropdown(models, label="Model Choice")],
    outputs=gr.Label(num_top_classes=3, label="Predictions"),
)

interface.launch(debug=False, share=True)