Spaces:
Sleeping
Sleeping
File size: 1,550 Bytes
c65e18e |
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 |
import numpy as np
import pandas as pd
import gradio as gr
from matplotlib import pyplot as plt
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(input_data):
# 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"]
# Reshape the input data to match the neural network architecture
X = input_data.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=gr.inputs.Image(shape=(28, 28)),
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()
|