Spaces:
Runtime error
Runtime error
Manu
commited on
Commit
·
af44b2b
1
Parent(s):
66fd09b
Initial commit
Browse files- .gitignore +1 -0
- app.py +52 -0
- load-model-from-huggingface.py +3 -0
- push-model-to-huggingface-with-cli.sh +7 -0
- push-model-to-huggingface-with-commit.py +36 -0
- push-model-to-huggingface-with-upload-folder.py +76 -0
- requirements.txt +3 -0
- sketchpad.py +31 -0
- static/model.png +0 -0
- training.py +74 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/saved_model
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import gradio as gr
|
3 |
+
from scipy.ndimage import zoom
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load the pre-trained model
|
8 |
+
from huggingface_hub import from_pretrained_keras
|
9 |
+
|
10 |
+
model = from_pretrained_keras("manufy/mnist_model_keras")
|
11 |
+
#model = tf.keras.models.load_model('mnist_model.keras')
|
12 |
+
|
13 |
+
def recognize_digit(input):
|
14 |
+
image = input['layers'][0]
|
15 |
+
print("Type of image variable:", type(image))
|
16 |
+
dimensions = image.shape
|
17 |
+
print("Dimensions of the image:", dimensions)
|
18 |
+
# Resize image to 28x28 using interpolation
|
19 |
+
resized_image = cv2.resize(image, (28, 28))
|
20 |
+
# Convert image to grayscale
|
21 |
+
gray_image = cv2.cvtColor(resized_image, cv2.COLOR_RGBA2GRAY)
|
22 |
+
# Reshape the image to add a single channel dimension
|
23 |
+
final_image = np.expand_dims(gray_image, axis=-1)
|
24 |
+
|
25 |
+
print("Final Dimensions of the image:", final_image.shape)
|
26 |
+
|
27 |
+
#if image.shape[-1] == 3: # If the image has 3 color channels (RGB)
|
28 |
+
# image = image.mean(axis=-1) # Convert to grayscale
|
29 |
+
# Resize the image to 28x28
|
30 |
+
#image = zoom(image, (28 / image.shape[0], 28 / image.shape[1]))
|
31 |
+
final_image = final_image.reshape((1, 28, 28, 1)).astype('float32') / 255
|
32 |
+
prediction = model.predict(final_image)
|
33 |
+
return {str(i): float(prediction[0][i]) for i in range(10)}, input['layers'][0]
|
34 |
+
#final_image[0, :, :, 0]
|
35 |
+
|
36 |
+
#return ''
|
37 |
+
|
38 |
+
# Configure the Sketchpad
|
39 |
+
sketchpad = gr.Sketchpad()
|
40 |
+
|
41 |
+
# Create the Gradio interface
|
42 |
+
interface = gr.Interface(
|
43 |
+
fn=recognize_digit,
|
44 |
+
inputs="sketchpad",
|
45 |
+
outputs=[gr.Label(num_top_classes=3), "image"],
|
46 |
+
live=True,
|
47 |
+
title="Digit Recognizer",
|
48 |
+
description="Draw a digit and let the model recognize it."
|
49 |
+
)
|
50 |
+
|
51 |
+
# Launch the interface
|
52 |
+
interface.launch()
|
load-model-from-huggingface.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_keras
|
2 |
+
|
3 |
+
model = from_pretrained_keras("manufy/mnist_model_keras")
|
push-model-to-huggingface-with-cli.sh
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#/bin/bash
|
2 |
+
# PUSH model to HuggingFace using CLI
|
3 |
+
# HUGGINGFACE_TOKEN should be set on your environment variables
|
4 |
+
# commit message is not customizable
|
5 |
+
# https://huggingface.co/docs/huggingface_hub/guides/upload
|
6 |
+
|
7 |
+
huggingface-cli upload manufy/mnist_model_keras saved_model . --token $HUGGINGFACE_TOKEN
|
push-model-to-huggingface-with-commit.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# https://huggingface.co/docs/huggingface_hub/guides/upload
|
2 |
+
|
3 |
+
# PUSH model to HuggingFace manufy/mnist_model_keras
|
4 |
+
# with custom commit message
|
5 |
+
|
6 |
+
###############################
|
7 |
+
# 1 - Initial generic imports #
|
8 |
+
###############################
|
9 |
+
|
10 |
+
from icecream import ic
|
11 |
+
ic("--- Importing generic libraries ---")
|
12 |
+
from dotenv import load_dotenv, find_dotenv
|
13 |
+
import os
|
14 |
+
from datetime import datetime
|
15 |
+
|
16 |
+
###############################################################################
|
17 |
+
# 2 - Setting up environment variable HUGGINGFACE_TOKEN with write permission #
|
18 |
+
###############################################################################
|
19 |
+
|
20 |
+
ic("--- Setting HF API token ---")
|
21 |
+
load_dotenv(find_dotenv())
|
22 |
+
token = os.getenv("HUGGINGFACE_TOKEN")
|
23 |
+
formatted_now = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
|
24 |
+
ic("--- Importing HF API ---")
|
25 |
+
from huggingface_hub import HfApi, CommitOperationAdd, CommitOperationDelete
|
26 |
+
ic("--- Executing commit with custom message ---")
|
27 |
+
api = HfApi()
|
28 |
+
operations = [
|
29 |
+
CommitOperationAdd(path_in_repo="saved_model", path_or_fileobj="mnist_model.keras")
|
30 |
+
]
|
31 |
+
api.create_commit(
|
32 |
+
repo_id="manufy/mnist_model_keras",
|
33 |
+
operations=operations,
|
34 |
+
commit_message=f"Updated model at {formatted_now}.",
|
35 |
+
token=token
|
36 |
+
)
|
push-model-to-huggingface-with-upload-folder.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# https://huggingface.co/docs/huggingface_hub/guides/upload
|
2 |
+
|
3 |
+
# PUSH model to HuggingFace manufy/mnist_model_keras
|
4 |
+
# using upload_folder, commit message is set by defaulr and not customizable
|
5 |
+
|
6 |
+
###############################
|
7 |
+
# 1 - Initial generic imports #
|
8 |
+
###############################
|
9 |
+
|
10 |
+
from icecream import ic
|
11 |
+
ic("--- Importing generic libraries ---")
|
12 |
+
from dotenv import load_dotenv, find_dotenv
|
13 |
+
import os
|
14 |
+
|
15 |
+
###############################################################################
|
16 |
+
# 2 - Setting up environment variable HUGGINGFACE_TOKEN with write permission #
|
17 |
+
###############################################################################
|
18 |
+
|
19 |
+
ic("--- Importing HF API token ---")
|
20 |
+
load_dotenv(find_dotenv())
|
21 |
+
token = os.getenv("HUGGINGFACE_TOKEN")
|
22 |
+
|
23 |
+
# The code in the comments is not needed
|
24 |
+
# the model doen't need to be loaded to be uploaded
|
25 |
+
|
26 |
+
################################################################
|
27 |
+
# 3 - Importing keras load_model #
|
28 |
+
################################################################
|
29 |
+
|
30 |
+
# ic("--- Importing tensorflow load_model ---")
|
31 |
+
# from tensorflow.keras.models import load_model
|
32 |
+
# ic("--- Importing huggingface_hub ---")
|
33 |
+
|
34 |
+
###########################
|
35 |
+
# 3 - Loading keras model #
|
36 |
+
###########################
|
37 |
+
|
38 |
+
# ic("--- Loading local keras model ---" )
|
39 |
+
# model = load_model('mnist_model.keras')
|
40 |
+
|
41 |
+
######################################
|
42 |
+
# 3 - Uploading model to HuggingFace #
|
43 |
+
######################################
|
44 |
+
|
45 |
+
ic ("--- Importing HF API ---")
|
46 |
+
from huggingface_hub import HfApi
|
47 |
+
ic("--- Settig up HF API ---")
|
48 |
+
HuggingFace_api = HfApi()
|
49 |
+
ic("--- Uploading model ---")
|
50 |
+
HuggingFace_api.upload_folder(
|
51 |
+
folder_path="saved_model/",
|
52 |
+
repo_id="manufy/mnist_model_keras",
|
53 |
+
repo_type="model",
|
54 |
+
token=token)
|
55 |
+
|
56 |
+
########################################################################
|
57 |
+
# The following are failed attempts to upload the model to HuggingFace #
|
58 |
+
########################################################################
|
59 |
+
|
60 |
+
# upload_folder("manufy/mnist_model_keras")
|
61 |
+
|
62 |
+
# not supported in keras 3
|
63 |
+
# NotImplementedError: Cannot use 'save_pretrained_keras':
|
64 |
+
# Keras 3.x is not supported.
|
65 |
+
# Please save models manually and upload them
|
66 |
+
# using `upload_folder` or `huggingface-cli upload`.
|
67 |
+
|
68 |
+
#ic("--- Saving huggingface model ---" )
|
69 |
+
#huggingface_hub.save_pretrained_keras('saved_model/')
|
70 |
+
|
71 |
+
|
72 |
+
#push_to_hub_keras(model,
|
73 |
+
# "manufy/mnist_model_keras")
|
74 |
+
# include_optimizer = True,
|
75 |
+
# tags = ["object-detection", "some_other_tag"]
|
76 |
+
#)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pencv-python
|
2 |
+
tensorflow
|
3 |
+
icecream
|
sketchpad.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
def classify(input):
|
9 |
+
image = input['layers'][0]
|
10 |
+
print("Type of image variable:", type(image))
|
11 |
+
dimensions = image.shape
|
12 |
+
print("Dimensions of the image:", dimensions)
|
13 |
+
# Resize image to 28x28 using interpolation
|
14 |
+
resized_image = cv2.resize(image, (28, 28))
|
15 |
+
# Convert image to grayscale
|
16 |
+
gray_image = cv2.cvtColor(resized_image, cv2.COLOR_RGBA2GRAY)
|
17 |
+
# Reshape the image to add a single channel dimension
|
18 |
+
final_image = np.expand_dims(gray_image, axis=-1)
|
19 |
+
|
20 |
+
print("Final image shape:", final_image.shape)
|
21 |
+
print("Final image:", final_image)
|
22 |
+
|
23 |
+
#input = np.reshape(input, (1, 28, 28))
|
24 |
+
#print(input)
|
25 |
+
return ''
|
26 |
+
|
27 |
+
label = gr.Label(num_top_classes=10)
|
28 |
+
|
29 |
+
interface = gr.Interface(fn=classify, inputs="sketchpad", outputs=label,
|
30 |
+
live=True)
|
31 |
+
interface.launch()
|
static/model.png
ADDED
![]() |
training.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from icecream import ic
|
3 |
+
ic("--- Importing tensorflow ---")
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras import layers, models
|
6 |
+
from tensorflow. keras. utils import plot_model
|
7 |
+
from tensorflow.keras import Input
|
8 |
+
|
9 |
+
# load mnist dataset
|
10 |
+
ic("------ Loading mnist dataset ------")
|
11 |
+
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
|
12 |
+
|
13 |
+
# normalize 60000 instances, 28x28 pixels 1 channel
|
14 |
+
|
15 |
+
ic("------ Normalizing data ------")
|
16 |
+
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
|
17 |
+
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255
|
18 |
+
|
19 |
+
# labels are output numbers, 0 to 9 we need to convert them to one-hot encoding
|
20 |
+
|
21 |
+
ic("------ One-hot encoding labels ------")
|
22 |
+
train_labels = tf.keras.utils.to_categorical(train_labels)
|
23 |
+
|
24 |
+
# 1 for correct digit, 0 for incorrect
|
25 |
+
|
26 |
+
|
27 |
+
ic("------ Creating model ------")
|
28 |
+
# define model
|
29 |
+
model = models.Sequential()
|
30 |
+
|
31 |
+
|
32 |
+
# Add an Input layer
|
33 |
+
model.add(Input(shape=(28, 28, 1)))
|
34 |
+
|
35 |
+
# create convolutional layer
|
36 |
+
# 32 filters, 3x3 kernel, relu activation function, input shape 28x28x1
|
37 |
+
# them create max pooling layer 2x2
|
38 |
+
|
39 |
+
#model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
|
40 |
+
model.add(layers.MaxPooling2D((2, 2)))
|
41 |
+
|
42 |
+
model.add(layers.Conv2D(64, (3, 3), activation='relu',))
|
43 |
+
model.add(layers.MaxPooling2D((2, 2)))
|
44 |
+
|
45 |
+
model.add(layers.Conv2D(64, (3, 3), activation='relu',))
|
46 |
+
model.add(layers.Flatten())
|
47 |
+
|
48 |
+
model.add(layers.Dense(64, activation='relu'))
|
49 |
+
|
50 |
+
# output layer,
|
51 |
+
|
52 |
+
model.add(layers.Dense(10, activation='softmax'))
|
53 |
+
|
54 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
55 |
+
|
56 |
+
# generate model.png with architecture plot
|
57 |
+
|
58 |
+
ic("------ Plotting model ------")
|
59 |
+
plot_model(model, to_file='static/model.png', show_shapes=True, show_layer_names=True)
|
60 |
+
|
61 |
+
|
62 |
+
# train
|
63 |
+
|
64 |
+
ic("------ Training model ------")
|
65 |
+
#model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.1)
|
66 |
+
|
67 |
+
# save model
|
68 |
+
|
69 |
+
ic("------ Saving .h5 model ------")
|
70 |
+
model.save('saved_model/keras/mnist_model.h5')
|
71 |
+
ic("------ Saving .keras model ------")
|
72 |
+
model.save('saved_model/keras/mnist_model.keras')
|
73 |
+
ic("------ Exporting .keras model ------")
|
74 |
+
model.export('saved_model/exported')
|