forked from PythonNorte/intro_redes_neuronales_acuriale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_cnn.py
114 lines (90 loc) · 3.31 KB
/
mnist_cnn.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
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import Adadelta
from keras.metrics import MAE, MSE
from keras.objectives import categorical_crossentropy
from keras.utils import np_utils
from keras import backend as K
batch_size = 128
nb_classes = 10
nb_epoch = 12
# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
if K.image_dim_ordering() == 'th':
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
# y_train[0] = 5 --> luego es [0,0,0,0,1,0,0,0,0,0]
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1]), padding='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1]), padding='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
optimizer = Adadelta()
metrics = ['accuracy', MAE, MSE]
loss = categorical_crossentropy
model.compile(loss=loss, optimizer=optimizer, metrics=metrics)
model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch,
validation_data=(X_test, Y_test), verbose=True)
score = model.evaluate(X_test, Y_test, verbose=True)
print('Test score:', score[0])
print('Test accuracy:', score[1])
y_predict = model.predict(X_test, verbose=True)
n = 10
f = plt.figure(figsize=(10, 4))
for i in range(n):
# display noisy
ax = plt.subplot(2, n, i + 1)
plt.imshow(X_test[i].reshape(28, 28), 'gray')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display noisy
ax = plt.subplot(2, n, i + 1+ n)
plt.plot(y_predict[i], '*')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_title('y_test: {}'.format(y_test[i]))
#plt.show()
f.savefig('predictDigits.png', bbox_inches='tight')