mnist_cnn_one_iteration.py
Go to the documentation of this file.
1 '''
2 Save CNN network and one sample of train data.
3 
4 Run one iteration of training of convnet on the MNIST dataset.
5 '''
6 
7 from __future__ import print_function
8 import numpy as np
9 np.random.seed(1337) # for reproducibility
10 
11 from keras.datasets import mnist
12 from keras.models import Sequential
13 from keras.layers.core import Dense, Dropout, Activation, Flatten
14 from keras.layers.convolutional import Convolution2D, MaxPooling2D
15 from keras.utils import np_utils
16 
17 batch_size = 128
18 nb_classes = 10
19 nb_epoch = 1
20 
21 # input image dimensions
22 img_rows, img_cols = 28, 28
23 # number of convolutional filters to use
24 nb_filters = 4
25 # size of pooling area for max pooling
26 nb_pool = 2
27 # convolution kernel size
28 nb_conv = 3
29 
30 # the data, shuffled and split between train and test sets
31 (X_train, y_train), (X_test, y_test) = mnist.load_data()
32 
33 X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
34 X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
35 X_train = X_train.astype('float32')
36 X_test = X_test.astype('float32')
37 X_train /= 255
38 X_test /= 255
39 print('X_train shape:', X_train.shape)
40 print(X_train.shape[0], 'train samples')
41 print(X_test.shape[0], 'test samples')
42 
43 # convert class vectors to binary class matrices
44 Y_train = np_utils.to_categorical(y_train, nb_classes)
45 Y_test = np_utils.to_categorical(y_test, nb_classes)
46 
47 model = Sequential()
48 
49 model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid',
50  input_shape=(1, img_rows, img_cols)))
51 model.add(Activation('relu'))
52 model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
53 model.add(Activation('relu'))
54 model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
55 model.add(Dropout(0.25))
56 
57 model.add(Flatten())
58 model.add(Dense(6))
59 model.add(Activation('relu'))
60 model.add(Dropout(0.5))
61 model.add(Dense(nb_classes))
62 model.add(Activation('softmax'))
63 
64 model.compile(loss='categorical_crossentropy',
65  optimizer='adadelta')
66 
67 model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
68  verbose=1, validation_data=(X_test, Y_test))
69 
70 # store model
71 with open('./my_nn_arch.json', 'w') as fout:
72  fout.write(model.to_json())
73 model.save_weights('./my_nn_weights.h5', overwrite=True)
74 
75 # store one sample in text file
76 with open("./sample_mnist.dat", "w") as fin:
77  fin.write("1 28 28\n")
78  a = X_train[0,0]
79  for b in a:
80  fin.write(str(b)+'\n')
81 
82 # get prediction on saved sample
83 # c++ output should be the same ;)
84 print('Prediction on saved sample:')
85 print(str(model.predict(X_train[:1])))
86 # on my pc I got:
87 #[[ 0.01091413 0.00714095 0.06447848 0.39298642 0.00306931 0.36678436
88 # 0.0646545 0.00475969 0.08085762 0.00435456]]
89 
int open(const char *, int)
Opens a file descriptor.
static QCString str