File size: 1,322 Bytes
4d5747a 6b2792f e73e2fa 6b2792f e73e2fa 6b2792f e73e2fa 6b2792f 49da6b5 fc8666d 6b2792f |
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
import keras
from matplotlib import pyplot as plt
import numpy as np
objects = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = objects.load_data()
training_images = training_images / 255.0
test_images = test_images / 255.0
from 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)
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(
fn= predict_image,
inputs= gr.Image(height=28, width=28, image_mode='L', sources='clipboard'),
outputs='label'
)
iface.launch(debug='True') |