-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_model.py
136 lines (101 loc) · 3.47 KB
/
train_model.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
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
from matplotlib import pyplot as plt
from skimage.feature import hog
from matplotlib import patches
from sklearn.datasets import fetch_mldata
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import pickle
from pull_digit import labeled_user_image
import cv2
mnist = fetch_mldata('mnist-original', data_home="dataset")
x = np.array(mnist.data, 'int16')
y = np.array(mnist.target, 'int')
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.33, random_state=42)
def train_model(x_train, y_train):
list_hog_fd = []
for feature in x_train:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1),
visualise=False)
list_hog_fd.append(fd)
x_train = np.array(list_hog_fd, 'float64')
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
# save the model to disk
filename = 'digit_knn_model.sav'
pickle.dump(knn, open(filename, 'wb'))
def knn_score(x_test, y_test):
filename = 'digit_knn_model.sav'
# load the model from disk
knn = pickle.load(open(filename, 'rb'))
list_hog_fd = []
for feature in x_test:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1),
visualise=False)
list_hog_fd.append(fd)
x_test = np.array(list_hog_fd, 'float64')
score = knn.score(x_test, y_test)
print("score is : ")
print(np.round(score * 100, 2), "%")
return x_test, y_test
def knn_predict(image):
filename = 'digit_knn_model.sav'
# load the model from disk
knn = pickle.load(open(filename, 'rb'))
expected = knn.predict(image)
return expected[0]
def read_image(rects, nums):
xx = []
yy = []
for n in range(len(nums)):
x = rects[n][0][0]
y = rects[n][0][1]
xx.append(x)
yy.append(y)
max_x = np.max(xx)
max_y = np.max(yy)
digits = np.zeros((max_x + 5, max_y + 5), np.object)
return digits
def get_string(arr):
dd = []
for x in range(arr.shape[0] - 1):
for y in range(arr.shape[1] - 1):
if type(arr[x][y]) == list:
dd.append(arr[x][y][0])
print("the numers in the image are :")
print(', '.join(str(x) for x in dd))
def new(image, padd):
fig, ax = plt.subplots(1)
ax.imshow(image)
nums, ph, rects = labeled_user_image(image, padd)
digits = read_image(rects, nums)
for n in range(len(nums)):
rect = patches.Rectangle(rects[n][0], rects[n][1], rects[n][2], linewidth=1, edgecolor='g',
facecolor='none')
ax.add_patch(rect)
ex = knn_predict(nums[n])
digits[rects[n][0][0]][rects[n][0][1]] = [ex]
ax.text(rects[n][0][0] + 3, rects[n][0]
[1] - 3, str(int(ex)), style='italic')
plt.axis("off")
get_string(digits)
plt.show()
def view(image, pad):
nums, ph, rects = labeled_user_image(image, pad)
plt.figure()
for n in range(len(ph)):
plt.subplot(8, 11, n + 1)
plt.imshow(ph[n], "gray")
ex = knn_predict(nums[n])
title_obj = plt.title(str(ex))
plt.setp(title_obj, color='r')
plt.axis("off")
plt.show()
def main():
# train the model and save it to "digit_knn_model"
train_model(x_train, y_train)
# get the socer of the model
im, t = knn_score(x_test, y_test)
if __name__ == '__main__':
main()