File size: 2,916 Bytes
6ad9d8e
9b783d7
6ad9d8e
 
 
9b783d7
 
 
 
6ad9d8e
6aca538
 
6ad9d8e
 
 
 
 
6aca538
9b783d7
 
 
 
 
 
 
 
 
 
 
 
6aca538
9b783d7
 
 
 
6aca538
9b783d7
 
 
 
 
6aca538
 
 
 
 
 
9b783d7
6aca538
 
9b783d7
6aca538
9b783d7
6aca538
 
9b783d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27295df
 
 
 
9b783d7
 
6ad9d8e
 
 
6aca538
 
 
9b783d7
 
 
6ad9d8e
6aca538
6ad9d8e
9b783d7
 
 
6aca538
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import json
from pprint import pprint

import tensorflow as tf
import tensorflow.keras as keras
from gradio import Interface, inputs, outputs

RESNET50_SIZE = 256
RESNET101_SIZE = 360

DEVICE = "/cpu:0"


with open("./tags.json", "rt", encoding="utf-8") as f:
    tags = json.load(f)


with tf.device(DEVICE):
    model_resnet50 = keras.Sequential(
        [
            keras.applications.resnet.ResNet50(
                include_top=False,
                weights=None,
                input_shape=(RESNET50_SIZE, RESNET50_SIZE, 3),
            ),
            keras.layers.Conv2D(filters=len(tags), kernel_size=(1, 1), padding="same"),
            keras.layers.BatchNormalization(epsilon=1.001e-5),
            keras.layers.GlobalAveragePooling2D(name="avg_pool"),
            keras.layers.Activation("sigmoid"),
        ]
    )
    model_resnet50.load_weights("./tf_model_resnet50.h5")

with tf.device(DEVICE):
    model_resnet101 = keras.Sequential(
        [
            keras.applications.resnet.ResNet101(
                include_top=False,
                weights=None,
                input_shape=(RESNET101_SIZE, RESNET101_SIZE, 3),
            ),
            keras.layers.Conv2D(filters=len(tags), kernel_size=(1, 1), padding="same"),
            keras.layers.BatchNormalization(epsilon=1.001e-5),
            keras.layers.GlobalAveragePooling2D(name="avg_pool"),
            keras.layers.Activation("sigmoid"),
        ]
    )
    model_resnet101.load_weights("./tf_model_resnet101.h5")


def predict_resnet50(img):
    with tf.device(DEVICE):
        img = tf.image.resize_with_pad(img, RESNET50_SIZE, RESNET50_SIZE)
        img = tf.image.per_image_standardization(img)
        data = tf.expand_dims(img, 0)
        out, *_ = model_resnet50(data)
    return out


def predict_resnet101(img):
    with tf.device(DEVICE):
        img = tf.image.resize_with_pad(img, RESNET101_SIZE, RESNET101_SIZE)
        img = tf.image.per_image_standardization(img)
        data = tf.expand_dims(img, 0)
        out, *_ = model_resnet101(data)
    return out


def main(img, hide: float, model: str):
    if model.endswith("50"):
        out = predict_resnet50(img)
    elif model.endswith("101"):
        out = predict_resnet101(img)
    else:
        raise ValueError(f"Invalid model type: {model!r}")
    result = {
        tag: confidence
        for i, tag in enumerate(tags)
        if (confidence := float(out[i].numpy())) >= hide
    }
    pprint(result)
    return result


image = inputs.Image(label="Upload your image here!")
hide_threshold = inputs.Slider(
    label="Hide confidence lower than", default=0.5, maximum=1, minimum=0
)
select_model = inputs.Radio(
    ["ResNet50", "ResNet101"], label="Select model", type="value"
)

labels = outputs.Label(label="Tags", type="confidences")

interface = Interface(
    main, inputs=[image, hide_threshold, select_model], outputs=[labels]
)
interface.launch()