Digit Recognizer (using ANN)
Digit Recognizer (using ANN)
Steps to be followed:
1. Gather the data (MINST dataset)
2. Normalize the dataset
3. Neural network architecture
4. Compile the model
5. Fit the model (Train the model)
6. Evaluate the model
7. Save the model ( Architecture, model weights)
8. Use the model in streamlit canvas.
Code to create a model:
import numpy as np
from tensorflow import keras
from keras import layers
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
print(x_train.shape)
print(x_test.shape)
print(y_train)
print(y_test)
# lets see some random images and its labels
import random
import matplotlib.pyplot as plt
i = random.randint(0,60000)
plt.imshow(x_train[i],cmap='gray') # Color map
plt.title([y_train[i]])
plt.show()
# How many images are there in every digit?
import numpy as np
np.unique(y_train,return_counts=True)
Out[ ]:
np.unique(y_test,return_counts=True)
Out[ ]:
x_train[0]
Out[ ]:
# Normalization : Scaling down the value to a specific range(0-1)
x_train=x_train/255
x_test = x_test/255
# AFter Normalization
print(x_train.max())
print(x_train.min())
from keras.layers import Dense
from keras.layers import Flatten
model = keras.Sequential()
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(392,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
history = model.fit(x_train,y_train,epochs=10,validation_split=0.2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
Out[ ]:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
Out[ ]:
# Evaluate on test data
y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred,axis=1)
y_pred
Out[ ]:
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
accuracy_score(y_pred,y_test)
Out[ ]:
confusion_matrix(y_pred,y_test)
Out[ ]:
print(classification_report(y_pred,y_test))
In [ ]:
# save the model
model.save('mnist.hdf5')
Use this model in the canvas:
Output:
Example code(github): Click here
NN)
No comments
If you have any doubts, Please let me know