File size: 1,767 Bytes
0763043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import gradio as gr
import numpy as np
from PIL import Image

TITLE = "Handwritten Digit Recognition Demo"

DESCRIPTION = "This demo employs a basic CNN architecture inspired by [MIT 6.S191’s Lab2 Part1](https://github.com/aamini/introtodeeplearning/blob/master/lab2/Part1_MNIST.ipynb). "\
              "It achieves about 98% accuracy on the MNIST test dataset but may perform poorly, particularly with digits 8 and 9, likely due to suboptimal image preprocessing."

model = tf.keras.saving.load_model("tf_model_mnist")


def preprocess(image):
    """ Normalize Gradio image to MNIST format """
    image = image.resize((28, 28), Image.Resampling.HAMMING)
    img_array = np.asarray(image, dtype=np.float32)
    for i in range(img_array.shape[0]):
        for j in range(img_array.shape[1]):
            alpha = img_array[i, j, 3]
            if alpha == 0.:
                img_array[i, j] = [0., 0., 0., 255.]
            else:
                img_array[i, j] = [255., 255., 255., 255.]

    new_image = Image.fromarray(img_array.astype(np.uint8), "RGBA")
    new_image = new_image.convert("L")
    image_array  = tf.keras.utils.img_to_array(new_image)
    image_array = (np.expand_dims(image_array, axis=0)/255.).astype(np.float32)
    return image_array, new_image


def predict(img):
    img = img["composite"]
    input_arr, new_image = preprocess(img)
    print("input:", input_arr.shape)
    predictions = model.predict(input_arr)
    return {str(i): predictions[0][i] for i in range(10)}, new_image


input_image = gr.Sketchpad(
        layers=False,
        type="pil",
    )
demo = gr.Interface(
    title=TITLE,
    description=DESCRIPTION,
    predict,
    inputs=input_image,
    outputs=['label', 'image']
)


demo.launch()