Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from matplotlib import pyplot as plt
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
num_objects = tf.keras.datasets.mnist
|
| 8 |
+
(training_images, training_labels), (test_images, test_labels) = num_objects.load_data()
|
| 9 |
+
|
| 10 |
+
for i in range(9):
|
| 11 |
+
#define subplot
|
| 12 |
+
plt.subplot(330 + 1 + i)
|
| 13 |
+
#plot of raw pixel data
|
| 14 |
+
plt.imshow(training_images[i])
|
| 15 |
+
|
| 16 |
+
training_images = training_images / 255.0
|
| 17 |
+
test_images = test_images / 255.0
|
| 18 |
+
|
| 19 |
+
from tensorflow.keras.layers import Flatten, Dense
|
| 20 |
+
model = tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
|
| 21 |
+
Dense(256, activation='relu'),
|
| 22 |
+
Dense(256, activation='relu'),
|
| 23 |
+
Dense(128, activation='relu'),
|
| 24 |
+
Dense(10, activation=tf.nn.softmax)])
|
| 25 |
+
|
| 26 |
+
model.compile(optimizer = 'adam',
|
| 27 |
+
loss = 'sparse_categorical_crossentropy',
|
| 28 |
+
metrics=['accuracy'])
|
| 29 |
+
|
| 30 |
+
model.fit(training_images, training_labels, epochs=10) #how many times u go through the dataset
|
| 31 |
+
|
| 32 |
+
test=test_images[0].reshape(-1,28,28)
|
| 33 |
+
pred=model.predict(test)
|
| 34 |
+
print(pred)
|
| 35 |
+
|
| 36 |
+
def predict_image(img):
|
| 37 |
+
img_3d=img.reshape(-1,28,28)
|
| 38 |
+
im_resize=img_3d/255.0
|
| 39 |
+
prediction=model.predict(im_resize)
|
| 40 |
+
pred=np.argmax(prediction)
|
| 41 |
+
return pred
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(predict_image, inputs="sketchpad", outputs="label")
|
| 44 |
+
|
| 45 |
+
iface.launch(debug='True')
|