Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Create a Gradio App using Blocks
|
| 5 |
with gr.Blocks() as demo:
|
|
@@ -11,15 +15,21 @@ with gr.Blocks() as demo:
|
|
| 11 |
with gr.Accordion("Click for Instructions:"):
|
| 12 |
gr.Markdown(
|
| 13 |
"""
|
| 14 |
-
*
|
| 15 |
""")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def modelTraining(img):
|
|
|
|
| 18 |
mnist = tf.keras.datasets.mnist
|
| 19 |
|
|
|
|
| 20 |
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
| 21 |
x_train, x_test = x_train / 255.0, x_test / 255.0
|
| 22 |
|
|
|
|
| 23 |
model = tf.keras.models.Sequential([
|
| 24 |
tf.keras.layers.Flatten(input_shape=(28, 28)),
|
| 25 |
tf.keras.layers.Dense(128, activation='relu'),
|
|
@@ -27,25 +37,34 @@ with gr.Blocks() as demo:
|
|
| 27 |
tf.keras.layers.Dense(10)
|
| 28 |
])
|
| 29 |
|
|
|
|
|
|
|
| 30 |
predictions = model(x_train[:1]).numpy()
|
| 31 |
-
|
|
|
|
| 32 |
tf.nn.softmax(predictions).numpy()
|
| 33 |
-
|
|
|
|
| 34 |
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
|
| 35 |
loss_fn(y_train[:1], predictions).numpy()
|
| 36 |
-
|
|
|
|
| 37 |
model.compile(optimizer='adam',
|
| 38 |
loss=loss_fn,
|
| 39 |
metrics=['accuracy'])
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
model.fit(x_train, y_train, epochs=5)
|
| 43 |
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
|
| 44 |
|
| 45 |
print("Test accuracy: ", test_acc)
|
| 46 |
|
| 47 |
# Define any necessary preprocessing steps for the image input here
|
|
|
|
|
|
|
| 48 |
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
|
|
|
|
| 49 |
# Make a prediction using the model
|
| 50 |
prediction = probability_model.predict(img)
|
| 51 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
import PIL
|
| 6 |
+
import PIL.Image
|
| 7 |
|
| 8 |
# Create a Gradio App using Blocks
|
| 9 |
with gr.Blocks() as demo:
|
|
|
|
| 15 |
with gr.Accordion("Click for Instructions:"):
|
| 16 |
gr.Markdown(
|
| 17 |
"""
|
| 18 |
+
* uploading an image will setup, train, and evaluate the base model
|
| 19 |
""")
|
| 20 |
|
| 21 |
+
# Train, evaluate and test a ML
|
| 22 |
+
# image classification model for
|
| 23 |
+
# clothes images
|
| 24 |
def modelTraining(img):
|
| 25 |
+
# clothing dataset
|
| 26 |
mnist = tf.keras.datasets.mnist
|
| 27 |
|
| 28 |
+
#split the training data in to a train/test sets
|
| 29 |
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
| 30 |
x_train, x_test = x_train / 255.0, x_test / 255.0
|
| 31 |
|
| 32 |
+
# create the neural net layers
|
| 33 |
model = tf.keras.models.Sequential([
|
| 34 |
tf.keras.layers.Flatten(input_shape=(28, 28)),
|
| 35 |
tf.keras.layers.Dense(128, activation='relu'),
|
|
|
|
| 37 |
tf.keras.layers.Dense(10)
|
| 38 |
])
|
| 39 |
|
| 40 |
+
#make a post-training predition on the
|
| 41 |
+
#training set data
|
| 42 |
predictions = model(x_train[:1]).numpy()
|
| 43 |
+
|
| 44 |
+
# converts the logits into a probability
|
| 45 |
tf.nn.softmax(predictions).numpy()
|
| 46 |
+
|
| 47 |
+
#create and train the loss function
|
| 48 |
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
|
| 49 |
loss_fn(y_train[:1], predictions).numpy()
|
| 50 |
+
|
| 51 |
+
# compile the model with the loss function
|
| 52 |
model.compile(optimizer='adam',
|
| 53 |
loss=loss_fn,
|
| 54 |
metrics=['accuracy'])
|
| 55 |
|
| 56 |
+
# train the model - 5 runs
|
| 57 |
+
# evaluate the model on the test set
|
| 58 |
model.fit(x_train, y_train, epochs=5)
|
| 59 |
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
|
| 60 |
|
| 61 |
print("Test accuracy: ", test_acc)
|
| 62 |
|
| 63 |
# Define any necessary preprocessing steps for the image input here
|
| 64 |
+
# the image can be passed as a PIL or numpy
|
| 65 |
+
# create the final model for production
|
| 66 |
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
|
| 67 |
+
|
| 68 |
# Make a prediction using the model
|
| 69 |
prediction = probability_model.predict(img)
|
| 70 |
|