Spaces:
Runtime error
Runtime error
File size: 1,402 Bytes
94c5d83 |
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 |
import gradio as gr
import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
num_objects = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = num_objects.load_data()
for i in range(9):
#define subplot
plt.subplot(330 + 1 + i)
#plot of raw pixel data
plt.imshow(training_images[i])
training_images = training_images / 255.0
test_images = test_images / 255.0
from tensorflow.keras.layers import Flatten, Dense
model = tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
Dense(256, activation='relu'),
Dense(256, activation='relu'),
Dense(128, activation='relu'),
Dense(10, activation=tf.nn.softmax)])
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=10) #how many times u go through the dataset
test=test_images[0].reshape(-1,28,28)
pred=model.predict(test)
print(pred)
def predict_image(img):
img_3d=img.reshape(-1,28,28)
im_resize=img_3d/255.0
prediction=model.predict(im_resize)
pred=np.argmax(prediction)
return pred
iface = gr.Interface(predict_image, inputs="sketchpad", outputs="label")
iface.launch(debug='True') |