alok94 commited on
Commit
89cc6fb
·
1 Parent(s): 7380584
Files changed (4) hide show
  1. app.py +61 -0
  2. digitmodel.sav +0 -0
  3. requirements.txt +17 -0
  4. test.py +50 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow import keras
3
+ from tensorflow.keras import Sequential
4
+ from tensorflow.keras.layers import Dense, Flatten
5
+ import matplotlib.pyplot as plt
6
+ import gradio as gr
7
+ import numpy as np
8
+ #%matplotlib inline
9
+
10
+
11
+ objt=tf.keras.datasets.mnist
12
+ (X_train, y_train), (X_test,y_test)=objt.load_data()
13
+
14
+ print(X_train.shape)
15
+
16
+ print(y_train)
17
+
18
+ for i in range(9):
19
+ plt.subplot(330+1+i)
20
+ plt.imshow(X_train[i])
21
+ plt.show()
22
+
23
+ X_train=X_train/255.0
24
+ X_test=X_test/255.0
25
+
26
+ model=tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
27
+ Dense(784,activation='relu'),
28
+ Dense(650,activation='relu'),
29
+ Dense(550,activation='relu'),
30
+ Dense(450,activation='relu'),
31
+ Dense(350,activation='relu'),
32
+ Dense(256,activation='relu'),
33
+ Dense(128,activation='relu'),
34
+ Dense(80,activation='relu'),
35
+ Dense(40,activation='relu'),
36
+
37
+ Dense(10,activation=tf.nn.softmax)])
38
+
39
+ model.compile(optimizer='adam',
40
+ loss='sparse_categorical_crossentropy',
41
+ metrics=['accuracy'])
42
+ model.fit(X_train,y_train, epochs=10)
43
+ model.save("keras_digit_temp.h5")
44
+ test=X_test[0].reshape(-1,28,28)
45
+ predicted=model.predict(test)
46
+ print(predicted)
47
+
48
+ def prdict_digit(img):
49
+ loaded_model = keras.models.load_model('keras_digit_temp.h5')
50
+ img_3d=img.reshape(-1,28,28)
51
+ img_resized=img_3d/255.0
52
+ pred_prob=loaded_model.predict(img_resized)
53
+ predicted_val=np.argmax(pred_prob)
54
+ return int(predicted_val)
55
+
56
+ iface=gr.Interface(prdict_digit, inputs='sketchpad', outputs='label').launch()
57
+
58
+ iface.launch(debug='true')
59
+
60
+
61
+
digitmodel.sav ADDED
Binary file (643 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tensorflow
2
+ keras
3
+
4
+ scikit_learn<1.3.0
5
+ seaborn==0.12.2
6
+
7
+ torchvision<0.15.2
8
+ gradio
9
+
10
+ #streamlit==1.26.0
11
+
12
+
13
+
14
+
15
+
16
+
17
+
test.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ #import streamlit as st
3
+ from sklearn.neural_network import MLPClassifier
4
+ import torchvision.datasets as datasets
5
+ import seaborn as sns
6
+ import pickle
7
+
8
+ #dark mode seaborn
9
+ sns.set_style("darkgrid")
10
+
11
+ mnist_trainset = datasets.MNIST(root='./data', train=True, download=True, transform=None)
12
+ mnist_testset = datasets.MNIST(root='./data', train=False, download=True, transform=None)
13
+
14
+ print(mnist_trainset.data.shape)
15
+ print(mnist_testset.data.shape)
16
+ print(mnist_trainset.targets.shape)
17
+ print(mnist_testset.targets.shape)
18
+ X_train = mnist_trainset.data
19
+ y_train = mnist_trainset.targets
20
+ X_test = mnist_testset.data
21
+ y_test = mnist_testset.targets
22
+
23
+ X_train = X_train.numpy()
24
+ X_test = X_test.numpy()
25
+ y_train = y_train.numpy()
26
+ y_test = y_test.numpy()
27
+
28
+ X_train = X_train.reshape(60000, 784)/255.0
29
+ X_test = X_test.reshape(10000, 784)/255.0
30
+
31
+ #train the model
32
+ mlp = MLPClassifier(hidden_layer_sizes=(32,32))
33
+ mlp.fit(X_train, y_train)
34
+
35
+ #print the accuracies
36
+ print("Training Accuracy: ", mlp.score(X_train, y_train))
37
+ print("Testing Accuracy: ", mlp.score(X_test, y_test))
38
+
39
+ pickle.dump(mlp, open("digitmodel.sav", 'wb'))
40
+
41
+
42
+ loaded_model = pickle.load(open("digitmodel.sav", 'rb')) #loding saved model
43
+
44
+ def predict(img):
45
+ img = img.reshape(1,784)/255.0
46
+ prediction = loaded_model.predict(img)[0]
47
+ return int(prediction)
48
+
49
+ gr.Interface(fn= predict, inputs = "sketchpad", outputs ="label").launch()
50
+