-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_digit.py
62 lines (54 loc) · 1.67 KB
/
predict_digit.py
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
#from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
#from keras.applications.vgg16 import preprocess_input
#import numpy as np
#from matplotlib import image as mpimg
#from matplotlib import pyplot as plt
import keras
from keras.layers.core import *
from keras.layers import *
from keras.models import Model, Sequential
from keras import backend as K
from keras.datasets import mnist
from keras.optimizers import *
from PIL import Image
import sys
img_rows, img_cols = 28, 28
input_shape=(img_rows, img_cols,1)
def get_model():
model = Sequential()
model.add(Convolution2D(32, 3, 3,activation='relu',
input_shape=input_shape ))
model.add(Convolution2D(64,3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
#model.add(Dense(128, activation='relu'))
#model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(Adam(),"categorical_crossentropy",metrics=['accuracy'])
model.load_weights("model0.h5")
return model
model=get_model()
def preprocess_img(img_name, out_shape=(28,28)):
img_orig=Image.open(img_name)
img = img_orig.convert('L')
img=img.resize(out_shape, Image.ANTIALIAS)
img=np.array(img)
img=img.astype('float32')
img/=255
img=1-img
img_np=np.expand_dims(img,-1)
return img_np
def predict_drawn_img(img_name):
global model
img=preprocess_img(img_name)
inp=np.expand_dims(img,0)
pred=model.predict(inp)
ans=np.argmax(pred,1)
return ans
if __name__=="__main__":
fname=sys.argv[1]
print(predict_drawn_img(fname)[0])