|
|
|
|
|
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import tensorflow as tf |
|
from tensorflow.keras.datasets import mnist |
|
|
|
|
|
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() |
|
|
|
|
|
|
|
|
|
|
|
X_train = train_images.astype('float32') / 255.0 |
|
X_test = test_images.astype('float32') / 255.0 |
|
|
|
|
|
|
|
|
|
|
|
y_train = train_labels |
|
y_test = test_labels |
|
|
|
|
|
|
|
|
|
|
|
import keras |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import tensorflow as tf |
|
from tensorflow.keras.datasets import mnist |
|
import tensorflow.keras as keras |
|
from tensorflow.keras.optimizers import Adam |
|
|
|
model = keras.Sequential() |
|
model.add(keras.layers.Reshape((28, 28), input_shape=(28, 28))) |
|
model.add(keras.layers.LSTM(128, return_sequences=True)) |
|
model.add(keras.layers.Dropout(0.2)) |
|
model.add(keras.layers.LSTM(128)) |
|
model.add(keras.layers.Dropout(0.2)) |
|
model.add(keras.layers.Dense(64, activation='relu')) |
|
model.add(keras.layers.Dropout(0.2)) |
|
model.add(keras.layers.Dense(10, activation='softmax')) |
|
|
|
|
|
lr_schedule = keras.optimizers.schedules.ExponentialDecay( |
|
initial_learning_rate=0.001, |
|
decay_steps=10000, |
|
decay_rate=0.9 |
|
) |
|
|
|
|
|
optimizer = Adam(learning_rate=lr_schedule) |
|
|
|
|
|
model.compile(loss='sparse_categorical_crossentropy', |
|
optimizer=optimizer, |
|
metrics=['accuracy']) |
|
|
|
|
|
model.summary() |
|
|
|
|
|
history = model.fit(X_train, y_train, epochs=3, validation_data=(X_test, y_test)) |
|
|
|
|
|
|
|
|
|
|
|
import matplotlib.pyplot as plt |
|
|
|
def predict_number(input_image): |
|
|
|
input_image = input_image.astype('float32') / 255.0 |
|
input_image = np.reshape(input_image, (1, 28, 28)) |
|
|
|
prediction = model.predict(input_image) |
|
|
|
predicted_number = np.argmax(prediction) |
|
|
|
return predicted_number |
|
|
|
input_image = test_images[0] |
|
predicted_number = predict_number(input_image) |
|
|
|
plt.imshow(input_image.reshape((28, 28)), cmap=plt.cm.binary) |
|
plt.title(f"Predicted Number: {predicted_number}") |
|
plt.show() |
|
|
|
|
|
print("Predicted Number:", predicted_number) |
|
|
|
|
|
|
|
|
|
|
|
|
|
model.save('my_model.keras') |
|
|
|
|
|
|
|
|
|
|
|
import numpy as np |
|
import tensorflow as tf |
|
from tensorflow.keras.preprocessing import image |
|
from tensorflow.keras.models import load_model |
|
import matplotlib.pyplot as plt |
|
from PIL import Image |
|
|
|
|
|
model = load_model('my_model.keras') |
|
|
|
|
|
def predict_number_from_image(image_path): |
|
|
|
img = image.load_img(image_path, target_size=(28, 28), color_mode='grayscale') |
|
img_array = image.img_to_array(img) |
|
img_array /= 255.0 |
|
img_array = np.reshape(img_array, (1, 28, 28, 1)) |
|
|
|
|
|
prediction = model.predict(img_array) |
|
|
|
|
|
predicted_number = np.argmax(prediction) |
|
|
|
return predicted_number |
|
|
|
|
|
|
|
|
|
input_image_path = 'seven.png' |
|
|
|
predicted_number = predict_number_from_image(input_image_path) |
|
|
|
|
|
img = Image.open(input_image_path) |
|
plt.imshow(img, cmap='gray') |
|
plt.title(f"Predicted Number: {predicted_number}") |
|
plt.show() |
|
|
|
|
|
print("Predicted Number:", predicted_number) |
|
|
|
|