# load MNIST data
training_data, validation_data, test_data = ml.load_data_wrapper()
# show the input data
index = 12
x, y = training_data[index]
print(x.shape, y.shape)
plt.imshow(x.reshape((28,28)), 'gray')
(784, 1) (10, 1)
<matplotlib.image.AxesImage at 0x20ebd9607b8>
2018-08-10 15:53:08,671: INFO: epoch 0/20 acc : 0.5617
2018-08-10 15:53:20,840: INFO: epoch 2/20 acc : 0.9071
2018-08-10 15:53:32,694: INFO: epoch 4/20 acc : 0.9325
2018-08-10 15:53:44,229: INFO: epoch 6/20 acc : 0.9453
2018-08-10 15:53:55,543: INFO: epoch 8/20 acc : 0.951
2018-08-10 15:54:06,958: INFO: epoch 10/20 acc : 0.9551
2018-08-10 15:54:18,337: INFO: epoch 12/20 acc : 0.9601
2018-08-10 15:54:29,797: INFO: epoch 14/20 acc : 0.9606
2018-08-10 15:54:41,107: INFO: epoch 16/20 acc : 0.962
2018-08-10 15:54:52,640: INFO: epoch 18/20 acc : 0.9626
# create neural network
# units of all layers
architech = [784, 64, 32, 10]
# iteration numbers
num_iter = 20
# learning rate
alpha = 0.03
# regularization parameter
lambd = 1.2
model = nn.NeuralNetwork(architech, num_iter, alpha, lambd)
# train model
model.train_model(training_data, test_data=validation_data)
acc = model.evaluate(test_data)
print("Test data acc : {0}".format(acc))
2018-08-10 16:02:20,053: INFO: epoch 0/20 acc : 0.8191
2018-08-10 16:02:30,764: INFO: epoch 2/20 acc : 0.9089
2018-08-10 16:02:41,723: INFO: epoch 4/20 acc : 0.9321
2018-08-10 16:02:52,766: INFO: epoch 6/20 acc : 0.9437
2018-08-10 16:03:03,718: INFO: epoch 8/20 acc : 0.953
2018-08-10 16:03:14,544: INFO: epoch 10/20 acc : 0.954
2018-08-10 16:03:26,088: INFO: epoch 12/20 acc : 0.9592
2018-08-10 16:03:38,092: INFO: epoch 14/20 acc : 0.9607
2018-08-10 16:03:49,147: INFO: epoch 16/20 acc : 0.963
2018-08-10 16:03:59,940: INFO: epoch 18/20 acc : 0.9638
<matplotlib.image.AxesImage at 0x20ebe352a20>
<matplotlib.image.AxesImage at 0x20ebe3b09e8>
<matplotlib.image.AxesImage at 0x20ebe411a90>
h, w = t_image.shape
vertical = [np.sum(t_image[:, i]) for i in range(w)]
horizontal = [np.sum(t_image[i, :]) for i in range(h)]
plt.subplot(1,2,1)
plt.title("vertical")
plt.plot(range(w), vertical)
plt.subplot(1,2,2)
plt.title("horizontal")
plt.plot(range(h), horizontal)
[<matplotlib.lines.Line2D at 0x20ec3fa3eb8>]
w_pos = []
h_pos = []
padding = 100
start = 0
for i in range(w-1):
if vertical[i] == 0 and vertical[i+1] != 0:
start = i
elif vertical[i] != 0 and vertical[i+1] == 0:
w_pos.append((start, (i-start)))
for i in range(h-1):
if horizontal[i] == 0 and horizontal[i+1] != 0:
start = i
elif horizontal[i] != 0 and horizontal[i+1] == 0:
h_pos.append((start, (i-start)))
img = image
numbers_img = []
raw_img = []
img_size = (28,28)
cnt = len(w_pos)
for i, pos in enumerate(w_pos):
x, w = pos
y, h = h_pos[0]
raw_img.append(image[y:y+h, x:x+w])
crop_img = cv2.resize(t_image[y-padding:y+padding+h, x-padding:x+padding+w], img_size, interpolation=cv2.INTER_AREA)
numbers_img.append(crop_img)
plt.subplot(1,cnt, i+1)
plt.imshow(crop_img, 'gray')