File size: 3,886 Bytes
95193b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()


# In[2]:


X_train = train_images.astype('float32') / 255.0
X_test = test_images.astype('float32') / 255.0


# In[3]:


y_train = train_labels
y_test = test_labels


# In[4]:


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  # Import Keras from TensorFlow
from tensorflow.keras.optimizers import Adam  # Import the Adam optimizer

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'))

# Define a learning rate schedule
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate=0.001,
    decay_steps=10000,
    decay_rate=0.9
)

# Create an optimizer with the learning rate schedule
optimizer = Adam(learning_rate=lr_schedule)

# Compile the model
model.compile(loss='sparse_categorical_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])

# Print model summary
model.summary()

# Train the model
history = model.fit(X_train, y_train, epochs=3, validation_data=(X_test, y_test))


# In[5]:


import matplotlib.pyplot as plt

def predict_number(input_image):
    # Preprocess the input image (assuming it's a grayscale image)
    input_image = input_image.astype('float32') / 255.0
    input_image = np.reshape(input_image, (1, 28, 28))  # Reshape to match the model's input shape

    prediction = model.predict(input_image)

    predicted_number = np.argmax(prediction)

    return predicted_number

input_image = test_images[0]  # Replace with your own image
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)


# In[6]:


# Save the trained model to the current working directory
model.save('my_model.keras')


# In[9]:


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

# Load your trained model (replace 'my_model.h5' with your model's filename)
model = load_model('my_model.keras')

# Define a function to predict the number from an input image file
def predict_number_from_image(image_path):
    # Load and preprocess the input image
    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))  # Reshape to match the model's input shape

    # Make a prediction using the trained model
    prediction = model.predict(img_array)

    # Get the index of the class with the highest probability
    predicted_number = np.argmax(prediction)

    return predicted_number

# Example usage:
# Provide an input image (as a file path)
# Replace 'your_image_path.png' with the path to your image file
input_image_path = 'seven.png'

predicted_number = predict_number_from_image(input_image_path)

# Load and display the input image
img = Image.open(input_image_path)
plt.imshow(img, cmap='gray')
plt.title(f"Predicted Number: {predicted_number}")
plt.show()

# Print the predicted number
print("Predicted Number:", predicted_number)