File size: 1,820 Bytes
f7dfb0f
5b75e0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707d390
5b75e0d
707d390
5b75e0d
707d390
5b75e0d
707d390
5b75e0d
 
 
 
 
 
 
 
 
 
 
 
 
b0fdc69
5b75e0d
 
 
 
707d390
 
 
 
 
 
 
350a7a1
 
 
 
 
 
 
 
 
 
 
 
5b75e0d
350a7a1
 
5b75e0d
 
350a7a1
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import tensorflow as tf
import numpy as np

num_classes = 200
IMG_HEIGHT = 300
IMG_WIDTH = 300

with open("classlabel.txt", 'r') as file:
    CLASS_LABEL = [x.strip() for x in file.readlines()]

def normalize_image(img):
    img = tf.cast(img, tf.float32) / 255.0
    img = tf.image.resize(img, (IMG_HEIGHT, IMG_WIDTH), method='bilinear')
    return img

def load_model(model_name):
    # Load the model based on the model_name input
    if model_name == 1:
        return tf.keras.models.load_model("model/Xception.h5")
    elif model_name == 2:
        return tf.keras.models.load_model("model/InceptionV3.h5")
    elif model_name == 3:
        return tf.keras.models.load_model("model/InceptionResNetV2.h5")
    elif model_name == 4:
        return tf.keras.models.load_model("model/DenseNet201.h5")
    else:
        raise ValueError("Invalid model_name")

def predict_top_classes(img, model_name):
    img = img.convert('RGB')
    img_data = normalize_image(img)
    x = np.array(img_data)
    x = np.expand_dims(x, axis=0)
    model = load_model(model_name)
    temp = model.predict(x)

    idx = np.argsort(np.squeeze(temp))[::-1]
    top5_value = np.asarray([temp[0][i] for i in idx[0:5]])
    top5_idx = idx[0:5]

    return {CLASS_LABEL[i]: str(v) for i, v in zip(top5_idx, top5_value)}

models = {
    "Xception": 1,
    "InceptionV3": 2,
    "InceptionResNetV2": 3,
    "DenseNet201": 4
}

def dropdown_example(choice, img):
    model_name = models[choice]
    return predict_top_classes(img, model_name)

dropdown = gr.inputs.Dropdown(
    choices=list(models.keys()),
    type="str",
    label="Select a model"
)

image_input = gr.inputs.Image(type='pil')

interface = gr.Interface(
    fn=dropdown_example,
    inputs=[dropdown, image_input],
    outputs='label'
)

interface.launch()