| |
|
|
| import numpy as np |
| import matplotlib.pyplot as plt |
| import seaborn as sns |
| import cv2 |
| from cv2 import cv2_imshow |
| import PIL |
| import tensorflow as tf |
| tf.random.set_seed(3) |
| from tensorflow import keras |
| from keras.datasets import mnist |
| from sklearn.metrics import confusion_matrix |
|
|
|
|
| |
|
|
| (x_train,y_train),(x_test,y_test) = mnist.load_data() |
| type(x_train) |
| |
|
|
| print(x_train.shape,y_train.shape,x_test.shape,y_test.shape) |
| |
| |
| |
| |
| |
|
|
| print(x_train[10]) |
| print(x_train[10].shape) |
| |
| plt.imshow(x_train[25]) |
|
|
| |
| print(y_train[25]) |
|
|
| |
| print(y_train.shape,y_test.shape) |
| |
| print(np.unique(y_train)) |
|
|
| |
| print(np.unique(y_test)) |
|
|
| |
|
|
| |
| |
|
|
| x_train = x_train/255 |
| x_test = x_test/255 |
| |
|
|
| print(x_train[10]) |
| |
| |
|
|
| model = keras.Sequential([ |
| keras.layers.Flatten(input_shape=(28,28)), |
| keras.layers.Dense(50,activation='relu'), |
| keras.layers.Dense(50,activation='relu'), |
| keras.layers.Dense(10,activation='sigmoid') |
|
|
|
|
|
|
| ]) |
| |
|
|
| model.compile(optimizer='adam',loss = 'sparse_categorical_crossentropy',metrics=['accuracy']) |
| |
|
|
| model.fit(x_train,y_train,epochs=10,) |
|
|
| |
|
|
|
|
|
|
|
|
| |
|
|
| loss,accuracy = model.evaluate(x_test,y_test) |
| print(accuracy) |
| |
|
|
| print(x_test.shape) |
| |
|
|
| plt.imshow(x_test[0]) |
| plt.show() |
| print(y_test[0]) |
| Y_pred = model.predict(x_test) |
| print(Y_pred.shape) |
| print(Y_pred[0]) |
|
|
| |
|
|
| |
|
|
| Label_for_first_image = np.argmax(Y_pred[0]) |
| print(Label_for_first_image) |
| |
|
|
| Y_pred_label = [np.argmax(i) for i in Y_pred] |
| print(Y_pred_label) |
|
|
|
|
| |
| |
|
|
| |
| conf_max = confusion_matrix(y_test,Y_pred_label) |
| print(conf_max) |
| plt.figure(figsize=(15,7)) |
| sns.heatmap(conf_max,annot=True,fmt='d',cmap='Blues') |
|
|
|
|
| |
| input_image_path = '/content/download.png' |
|
|
| input_image = cv2.imread(input_image_path) |
|
|
| type(input_image) |
| print(input_image) |
| cv2_imshow(input_image) |
| input_image.shape |
| Grayscale = cv2.cvtColor(input_image,cv2.COLOR_RGB2GRAY) |
| Grayscale.shape |
| input_image_resize = cv2.resize(Grayscale,(28,28)) |
| input_image_resize.shape |
| cv2_imshow(input_image_resize) |
| input_image_resize = input_image_resize/255 |
| input_reshaped = np.reshape(input_image_resize,[1,28,28]) |
| input_prediction = model.predict(input_reshaped) |
| print(input_prediction) |
| input_pred_label = np.argmax(input_prediction) |
| print(input_pred_label) |
| |
| input_image_path = input("Path of the image to be predicted :") |
|
|
| input_image = cv2.imread(input_image_path) |
|
|
| cv2_imshow(input_image) |
|
|
| Grayscale = cv2.cvtColor(input_image,cv2.COLOR_RGB2GRAY) |
|
|
| input_image_resize = cv2.resize(Grayscale,(28,28)) |
|
|
| input_image_resize = input_image_resize/255 |
|
|
| input_reshaped = np.reshape(input_image_resize,[1,28,28]) |
|
|
| input_prediction = model.predict(input_reshaped) |
|
|
| input_pred_label = np.argmax(input_prediction) |
|
|
| print("the Handwritten digit recognized as : ",input_pred_label) |
|
|
|
|
|
|
| import gradio as gr |
|
|
|
|
| def predict_image(img): |
| img_3d=img.reshape(-1,28,28) |
| im_resize=img_3d/255.0 |
| prediction=model.predict(im_resize) |
| pred=np.argmax(input_prediction) |
| return pred |
|
|
| iface = gr.Interface(predict_image, inputs="sketchpad", outputs="label") |
|
|
| iface.launch(debug='False') |
|
|