Commit ·
7e7dedb
1
Parent(s): 506ccb9
Added application file
Browse files- app.py +32 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from tensorflow import keras
|
| 5 |
+
|
| 6 |
+
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
|
| 7 |
+
|
| 8 |
+
x_train = x_train / 255.0
|
| 9 |
+
x_test = x_test / 255.0
|
| 10 |
+
|
| 11 |
+
model = keras.models.Sequential([
|
| 12 |
+
keras.layers.Flatten(input_shape=(512, 512)), # Diese Schicht nimmt unser 2D-Bild und verwandelt es in ein 1D-Array
|
| 13 |
+
keras.layers.Dense(10, activation='relu'), # Als Nächstes kommen zwei Schichten mit 512 künstlichen Neuronen. Als Funktion wählen wir 'relu' f(x) = max(0,x)
|
| 14 |
+
keras.layers.Dense(10, activation='relu'),
|
| 15 |
+
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
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
| 19 |
+
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=6)
|
| 20 |
+
|
| 21 |
+
def classify(input):
|
| 22 |
+
image = np.expand_dims(np.array(Image.fromarray(input['layers'][0]).resize((28,28),resample=Image.Resampling.BILINEAR), dtype=int), axis=0)#[:,:,0]
|
| 23 |
+
prediction = model.predict(image).tolist()[0]
|
| 24 |
+
return {str(i): float(prediction[i]) for i in range(10)}
|
| 25 |
+
|
| 26 |
+
input_sketchpad = gr.Paint(image_mode="L", brush=gr.components.image_editor.Brush(default_color="rgb(156, 104, 200)"))
|
| 27 |
+
|
| 28 |
+
gr.Interface(fn=classify,
|
| 29 |
+
inputs=input_sketchpad,
|
| 30 |
+
outputs="label",
|
| 31 |
+
allow_flagging=False,
|
| 32 |
+
theme=gr.themes.Soft()).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PIL
|
| 2 |
+
numpy
|
| 3 |
+
gradio
|
| 4 |
+
tensorflow
|