Download app.py from starcode-tech/mnist-image-classification: direct link, hf CLI and curl.
- Browser
- Download file 1.57 kB
-
https://huggingface.co/spaces/starcode-tech/mnist-image-classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/starcode-tech/mnist-image-classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/starcode-tech/mnist-image-classification/resolve/main/app.py
1.57 kB
| import numpy as np | |
| import gradio as gr | |
| from PIL import Image | |
| from tensorflow import keras | |
| model = keras.models.Sequential([ | |
| keras.layers.Flatten(input_shape=(28, 28)), # Diese Schicht nimmt unser 2D-Bild und verwandelt es in ein 1D-Array | |
| keras.layers.Dense(512, activation='relu'), # Als Nächstes kommen zwei Schichten mit 512 künstlichen Neuronen. Als Funktion wählen wir 'relu' f(x) = max(0,x) | |
| keras.layers.Dense(512, activation='relu'), | |
| keras.layers.Dense(10, activation='softmax') # Die letzte Schicht besteht aus 10 Neuronen, die für unsere 10 Zahlen stehen. Die 'softmax' Funktion wandelt die Ergebnisse der vorherigen Schicht in Wahrscheinlichkeiten | |
| ]) | |
| model.compile(optimizer=keras.optimizers.Adam(0.001), | |
| loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), | |
| metrics=[keras.metrics.SparseCategoricalAccuracy()]) | |
| model.load_weights('./weights/mnist.weights.h5') | |
| def classify(input): | |
| image = np.expand_dims(np.array(Image.fromarray(input['layers'][0]).resize((28,28), resample=Image.Resampling.BILINEAR), dtype=int), axis=0) | |
| prediction = model.predict(image).tolist()[0] | |
| return {str(i): float(prediction[i]) for i in range(10)} | |
| input_sketchpad = gr.Paint(image_mode="L", brush=gr.components.image_editor.Brush(default_color="rgb(156, 104, 200)")) | |
| output_lable = gr.Label() | |
| gr.Interface(fn=classify, | |
| inputs=input_sketchpad, | |
| outputs=output_lable, | |
| flagging_mode='never', | |
| theme=gr.themes.Soft()).launch() |