I am using Gradio as GUI to test a machine learning algorithm, specifically the classic problem of reading a 28 by 28 pixel image of a handwritten digit. I want the GUI to be able to draw and take an input and then determine the digit. So, I have the following code (machine learning model not shown):
import gradio
import gradio as gr
import tensorflow as tf
import numpy as np
new_model = tf.keras.models.load_model('digits.model')
def classify(input):
input = np.reshape(input, (1, 28, 28))
prediction = new_model.predict(input).tolist()[0]
return {str(i): prediction[i] for i in range(10)}
label = gr.outputs.Label(num_top_classes=10)
interface = gr.Interface(fn=classify, inputs="sketchpad", outputs=label,
live=True)
interface.launch()
However, I run into two issues:
The interface is always claims to be 100% sure about its answer, and I want it to show percentages for all of the digits.
When I draw the digit to fill up the canvas, it does not work. When I draw it smaller to fill up only the middle portion, it works.
I tried to look through the Gradio documentation and reshape the canvas to be default 28 by 28 pixel and not have to deal with reshaping the images, but I had no luck.