feat: add code, model and evaluation files
Browse files- .gitattributes +1 -0
- lstm_mnist.py +151 -0
- my_model.keras +3 -0
- seven.png +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
my_model.keras filter=lfs diff=lfs merge=lfs -text
|
lstm_mnist.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
5 |
+
|
6 |
+
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import numpy as np
|
9 |
+
import tensorflow as tf
|
10 |
+
from tensorflow.keras.datasets import mnist
|
11 |
+
|
12 |
+
# Load the MNIST dataset
|
13 |
+
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
|
14 |
+
|
15 |
+
|
16 |
+
# In[2]:
|
17 |
+
|
18 |
+
|
19 |
+
X_train = train_images.astype('float32') / 255.0
|
20 |
+
X_test = test_images.astype('float32') / 255.0
|
21 |
+
|
22 |
+
|
23 |
+
# In[3]:
|
24 |
+
|
25 |
+
|
26 |
+
y_train = train_labels
|
27 |
+
y_test = test_labels
|
28 |
+
|
29 |
+
|
30 |
+
# In[4]:
|
31 |
+
|
32 |
+
|
33 |
+
import keras
|
34 |
+
import matplotlib.pyplot as plt
|
35 |
+
import numpy as np
|
36 |
+
import tensorflow as tf
|
37 |
+
from tensorflow.keras.datasets import mnist
|
38 |
+
import tensorflow.keras as keras # Import Keras from TensorFlow
|
39 |
+
from tensorflow.keras.optimizers import Adam # Import the Adam optimizer
|
40 |
+
|
41 |
+
model = keras.Sequential()
|
42 |
+
model.add(keras.layers.Reshape((28, 28), input_shape=(28, 28)))
|
43 |
+
model.add(keras.layers.LSTM(128, return_sequences=True))
|
44 |
+
model.add(keras.layers.Dropout(0.2))
|
45 |
+
model.add(keras.layers.LSTM(128))
|
46 |
+
model.add(keras.layers.Dropout(0.2))
|
47 |
+
model.add(keras.layers.Dense(64, activation='relu'))
|
48 |
+
model.add(keras.layers.Dropout(0.2))
|
49 |
+
model.add(keras.layers.Dense(10, activation='softmax'))
|
50 |
+
|
51 |
+
# Define a learning rate schedule
|
52 |
+
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
|
53 |
+
initial_learning_rate=0.001,
|
54 |
+
decay_steps=10000,
|
55 |
+
decay_rate=0.9
|
56 |
+
)
|
57 |
+
|
58 |
+
# Create an optimizer with the learning rate schedule
|
59 |
+
optimizer = Adam(learning_rate=lr_schedule)
|
60 |
+
|
61 |
+
# Compile the model
|
62 |
+
model.compile(loss='sparse_categorical_crossentropy',
|
63 |
+
optimizer=optimizer,
|
64 |
+
metrics=['accuracy'])
|
65 |
+
|
66 |
+
# Print model summary
|
67 |
+
model.summary()
|
68 |
+
|
69 |
+
# Train the model
|
70 |
+
history = model.fit(X_train, y_train, epochs=3, validation_data=(X_test, y_test))
|
71 |
+
|
72 |
+
|
73 |
+
# In[5]:
|
74 |
+
|
75 |
+
|
76 |
+
import matplotlib.pyplot as plt
|
77 |
+
|
78 |
+
def predict_number(input_image):
|
79 |
+
# Preprocess the input image (assuming it's a grayscale image)
|
80 |
+
input_image = input_image.astype('float32') / 255.0
|
81 |
+
input_image = np.reshape(input_image, (1, 28, 28)) # Reshape to match the model's input shape
|
82 |
+
|
83 |
+
prediction = model.predict(input_image)
|
84 |
+
|
85 |
+
predicted_number = np.argmax(prediction)
|
86 |
+
|
87 |
+
return predicted_number
|
88 |
+
|
89 |
+
input_image = test_images[0] # Replace with your own image
|
90 |
+
predicted_number = predict_number(input_image)
|
91 |
+
|
92 |
+
plt.imshow(input_image.reshape((28, 28)), cmap=plt.cm.binary)
|
93 |
+
plt.title(f"Predicted Number: {predicted_number}")
|
94 |
+
plt.show()
|
95 |
+
|
96 |
+
|
97 |
+
print("Predicted Number:", predicted_number)
|
98 |
+
|
99 |
+
|
100 |
+
# In[6]:
|
101 |
+
|
102 |
+
|
103 |
+
# Save the trained model to the current working directory
|
104 |
+
model.save('my_model.keras')
|
105 |
+
|
106 |
+
|
107 |
+
# In[9]:
|
108 |
+
|
109 |
+
|
110 |
+
import numpy as np
|
111 |
+
import tensorflow as tf
|
112 |
+
from tensorflow.keras.preprocessing import image
|
113 |
+
from tensorflow.keras.models import load_model
|
114 |
+
import matplotlib.pyplot as plt
|
115 |
+
from PIL import Image
|
116 |
+
|
117 |
+
# Load your trained model (replace 'my_model.h5' with your model's filename)
|
118 |
+
model = load_model('my_model.keras')
|
119 |
+
|
120 |
+
# Define a function to predict the number from an input image file
|
121 |
+
def predict_number_from_image(image_path):
|
122 |
+
# Load and preprocess the input image
|
123 |
+
img = image.load_img(image_path, target_size=(28, 28), color_mode='grayscale')
|
124 |
+
img_array = image.img_to_array(img)
|
125 |
+
img_array /= 255.0
|
126 |
+
img_array = np.reshape(img_array, (1, 28, 28, 1)) # Reshape to match the model's input shape
|
127 |
+
|
128 |
+
# Make a prediction using the trained model
|
129 |
+
prediction = model.predict(img_array)
|
130 |
+
|
131 |
+
# Get the index of the class with the highest probability
|
132 |
+
predicted_number = np.argmax(prediction)
|
133 |
+
|
134 |
+
return predicted_number
|
135 |
+
|
136 |
+
# Example usage:
|
137 |
+
# Provide an input image (as a file path)
|
138 |
+
# Replace 'your_image_path.png' with the path to your image file
|
139 |
+
input_image_path = 'seven.png'
|
140 |
+
|
141 |
+
predicted_number = predict_number_from_image(input_image_path)
|
142 |
+
|
143 |
+
# Load and display the input image
|
144 |
+
img = Image.open(input_image_path)
|
145 |
+
plt.imshow(img, cmap='gray')
|
146 |
+
plt.title(f"Predicted Number: {predicted_number}")
|
147 |
+
plt.show()
|
148 |
+
|
149 |
+
# Print the predicted number
|
150 |
+
print("Predicted Number:", predicted_number)
|
151 |
+
|
my_model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:76ba241eda4f72bc0293d51b74f159edd9d98d1b03a60ab45b59aede46171e27
|
3 |
+
size 2693165
|
seven.png
ADDED
![]() |