File size: 3,293 Bytes
c65e18e
 
8b6ef4c
c65e18e
c3a61ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c65e18e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b6ef4c
c65e18e
 
 
 
8b6ef4c
 
3b528ea
 
8b6ef4c
c65e18e
 
 
 
 
 
 
 
8b6ef4c
c65e18e
 
 
 
 
 
 
 
 
c3a61ad
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import numpy as np
import gradio as gr
from PIL import Image

def ReLU(Z):
    return np.maximum(Z, 0)

def softmax(Z):
    A = np.exp(Z) / np.sum(np.exp(Z), axis=0)
    return A

def init_params():
    W1 = np.random.rand(10, 784) - 0.5
    b1 = np.random.rand(10, 1) - 0.5
    W2 = np.random.rand(10, 10) - 0.5
    b2 = np.random.rand(10, 1) - 0.5
    return W1, b1, W2, b2

def forward_prop(W1, b1, W2, b2, X):
    Z1 = W1.dot(X) + b1
    A1 = ReLU(Z1)
    Z2 = W2.dot(A1) + b2
    A2 = softmax(Z2)
    return Z1, A1, Z2, A2

def get_predictions(A2):
    return np.argmax(A2, axis=0)

def make_predictions(X, W1, b1, W2, b2):
    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)
    predictions = get_predictions(A2)
    return predictions

def predict_digit(img):
    # Load the trained parameters
    params = np.load("trained_params.npz", allow_pickle=True)
    W1, b1, W2, b2 = params["W1"], params["b1"], params["W2"], params["b2"]

    # Convert the sketchpad drawing to grayscale and resize it to (28, 28)
    img_pil = Image.fromarray(np.uint8(img * 255)).convert("L").resize((28, 28))

    # Convert the image to a NumPy array and normalize it
    X = np.array(img_pil).reshape((784, 1)) / 255.

    # Get the prediction
    prediction = make_predictions(X, W1, b1, W2, b2)

    return int(prediction)

iface = gr.Interface(
    fn=predict_digit,
    inputs="sketchpad",
    outputs=gr.outputs.Label(num_top_classes=3),
    live=True,
    capture_session=True,
    title="Handwritten Digit Recognizer",
    description="Draw a digit using your mouse, and the model will try to recognize it.",
)

if __name__ == "__main__":
    iface.launch()

'''import numpy as np
import gradio as gr
from PIL import Image

def ReLU(Z):
    return np.maximum(Z, 0)

def softmax(Z):
    A = np.exp(Z) / sum(np.exp(Z))
    return A

def init_params():
    W1 = np.random.rand(10, 784) - 0.5
    b1 = np.random.rand(10, 1) - 0.5
    W2 = np.random.rand(10, 10) - 0.5
    b2 = np.random.rand(10, 1) - 0.5
    return W1, b1, W2, b2

def forward_prop(W1, b1, W2, b2, X):
    Z1 = W1.dot(X) + b1
    A1 = ReLU(Z1)
    Z2 = W2.dot(A1) + b2
    A2 = softmax(Z2)
    return Z1, A1, Z2, A2

def get_predictions(A2):
    return np.argmax(A2, 0)

def make_predictions(X, W1, b1, W2, b2):
    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)
    predictions = get_predictions(A2)
    return predictions

def predict_digit(img):
    # Load the trained parameters
    params = np.load("trained_params.npz", allow_pickle=True)
    W1, b1, W2, b2 = params["W1"], params["b1"], params["W2"], params["b2"]

    # Convert the sketchpad drawing to grayscale and resize it to (28, 28)
    img_pil = Image.fromarray(np.uint8(img * 255)).convert("L").resize((28, 28))

    # Convert the image to a NumPy array and normalize it
    X = np.array(img_pil).reshape((784, 1)) / 255.

    # Get the prediction
    prediction = make_predictions(X, W1, b1, W2, b2)

    return int(prediction)

iface = gr.Interface(
    fn=predict_digit,
    inputs="sketchpad",
    outputs=gr.outputs.Label(num_top_classes=3),
    live=True,
    capture_session=True,
    title="Handwritten Digit Recognizer",
    description="Draw a digit using your mouse, and the model will try to recognize it.",
)

if __name__ == "__main__":
    iface.launch()
'''