来自:http://blog.csdn.net/chenhaifeng2016/article/details/72763439
Keras MNIST CNN
- '''''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 keras
- from keras.datasets import mnist
- from keras.models import Sequential
- from keras.layers import Dense, Dropout, Flatten
- from keras.layers import Conv2D, MaxPooling2D
- from keras import backend as K
- batch_size = 128
- num_classes = 10
- epochs = 12
- # input image dimensions
- img_rows, img_cols = 28, 28
- # the data, shuffled and split between train and test sets
- (x_train, y_train), (x_test, y_test) = mnist.load_data()
- if K.image_data_format() == 'channels_first':
- 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 = keras.utils.to_categorical(y_train, num_classes)
- y_test = keras.utils.to_categorical(y_test, num_classes)
- model = Sequential()
- model.add(Conv2D(32, kernel_size=(3, 3),
- activation='relu',
- input_shape=input_shape))
- model.add(Conv2D(64, (3, 3), activation='relu'))
- model.add(MaxPooling2D(pool_size=(2, 2)))
- model.add(Dropout(0.25))
- model.add(Flatten())
- model.add(Dense(128, activation='relu'))
- model.add(Dropout(0.5))
- model.add(Dense(num_classes, activation='softmax'))
- model.compile(loss=keras.losses.categorical_crossentropy,
- optimizer=keras.optimizers.Adadelta(),
- metrics=['accuracy'])
- model.fit(x_train, y_train,
- batch_size=batch_size,
- epochs=epochs,
- verbose=1,
- validation_data=(x_test, y_test))
- score = model.evaluate(x_test, y_test, verbose=0)
- print('Test loss:', score[0])
- print('Test accuracy:', score[1])
TFLearn
- # -*- coding: utf-8 -*-
- """ Convolutional Neural Network for MNIST dataset classification task.
- References:
- Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. "Gradient-based
- learning applied to document recognition." Proceedings of the IEEE,
- 86(11):2278-2324, November 1998.
- Links:
- [MNIST Dataset] http://yann.lecun.com/exdb/mnist/
- """
- from __future__ import division, print_function, absolute_import
- import tflearn
- from tflearn.layers.core import input_data, dropout, fully_connected
- from tflearn.layers.conv import conv_2d, max_pool_2d
- from tflearn.layers.normalization import local_response_normalization
- from tflearn.layers.estimator import regression
- # Data loading and preprocessing
- import tflearn.datasets.mnist as mnist
- X, Y, testX, testY = mnist.load_data(one_hot=True)
- X = X.reshape([-1, 28, 28, 1])
- testX = testX.reshape([-1, 28, 28, 1])
- # Building convolutional network
- network = input_data(shape=[None, 28, 28, 1], name='input')
- network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
- network = max_pool_2d(network, 2)
- network = local_response_normalization(network)
- network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
- network = max_pool_2d(network, 2)
- network = local_response_normalization(network)
- network = fully_connected(network, 128, activation='tanh')
- network = dropout(network, 0.8)
- network = fully_connected(network, 256, activation='tanh')
- network = dropout(network, 0.8)
- network = fully_connected(network, 10, activation='softmax')
- network = regression(network, optimizer='adam', learning_rate=0.01,
- loss='categorical_crossentropy', name='target')
- # Training
- model = tflearn.DNN(network, tensorboard_verbose=0)
- model.fit({'input': X}, {'target': Y}, n_epoch=20,
- validation_set=({'input': testX}, {'target': testY}),
- snapshot_step=100, show_metric=True, run_id='convnet_mnist')
TensorLayer
- def main_test_cnn_layer():
- """Reimplementation of the TensorFlow official MNIST CNN tutorials:
- - https://www.tensorflow.org/versions/r0.8/tutorials/mnist/pros/index.html
- - https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/mnist/convolutional.py
- More TensorFlow official CNN tutorials can be found here:
- - tutorial_cifar10.py
- - https://www.tensorflow.org/versions/master/tutorials/deep_cnn/index.html
- - For simplified CNN layer see "Convolutional layer (Simplified)"
- in read the docs website.
- """
- X_train, y_train, X_val, y_val, X_test, y_test = \
- tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))
- X_train = np.asarray(X_train, dtype=np.float32)
- y_train = np.asarray(y_train, dtype=np.int64)
- X_val = np.asarray(X_val, dtype=np.float32)
- y_val = np.asarray(y_val, dtype=np.int64)
- X_test = np.asarray(X_test, dtype=np.float32)
- y_test = np.asarray(y_test, dtype=np.int64)
- print('X_train.shape', X_train.shape)
- print('y_train.shape', y_train.shape)
- print('X_val.shape', X_val.shape)
- print('y_val.shape', y_val.shape)
- print('X_test.shape', X_test.shape)
- print('y_test.shape', y_test.shape)
- print('X %s y %s' % (X_test.dtype, y_test.dtype))
- sess = tf.InteractiveSession()
- # Define the batchsize at the begin, you can give the batchsize in x and y_
- # rather than 'None', this can allow TensorFlow to apply some optimizations
- # – especially for convolutional layers.
- batch_size = 128
- x = tf.placeholder(tf.float32, shape=[batch_size, 28, 28, 1]) # [batch_size, height, width, channels]
- y_ = tf.placeholder(tf.int64, shape=[batch_size,])
- network = tl.layers.InputLayer(x, name='input')
- ## Professional conv API for tensorflow user
- # network = tl.layers.Conv2dLayer(network,
- # act = tf.nn.relu,
- # shape = [5, 5, 1, 32], # 32 features for each 5x5 patch
- # strides=[1, 1, 1, 1],
- # padding='SAME',
- # name ='cnn1') # output: (?, 28, 28, 32)
- # network = tl.layers.PoolLayer(network,
- # ksize=[1, 2, 2, 1],
- # strides=[1, 2, 2, 1],
- # padding='SAME',
- # pool = tf.nn.max_pool,
- # name ='pool1',) # output: (?, 14, 14, 32)
- # network = tl.layers.Conv2dLayer(network,
- # act = tf.nn.relu,
- # shape = [5, 5, 32, 64], # 64 features for each 5x5 patch
- # strides=[1, 1, 1, 1],
- # padding='SAME',
- # name ='cnn2') # output: (?, 14, 14, 64)
- # network = tl.layers.PoolLayer(network,
- # ksize=[1, 2, 2, 1],
- # strides=[1, 2, 2, 1],
- # padding='SAME',
- # pool = tf.nn.max_pool,
- # name ='pool2',) # output: (?, 7, 7, 64)
- ## Simplified conv API for beginner (the same with the above layers)
- network = tl.layers.Conv2d(network, n_filter=32, filter_size=(5, 5), strides=(1, 1),
- act=tf.nn.relu, padding='SAME', name='cnn1')
- network = tl.layers.MaxPool2d(network, filter_size=(2, 2), strides=(2, 2),
- padding='SAME', name='pool1')
- network = tl.layers.Conv2d(network, n_filter=64, filter_size=(5, 5), strides=(1, 1),
- act=tf.nn.relu, padding='SAME', name='cnn2')
- network = tl.layers.MaxPool2d(network, filter_size=(2, 2), strides=(2, 2),
- padding='SAME', name='pool2')
- ## end of conv
- network = tl.layers.FlattenLayer(network, name='flatten')
- network = tl.layers.DropoutLayer(network, keep=0.5, name='drop1')
- network = tl.layers.DenseLayer(network, n_units=256,
- act = tf.nn.relu, name='relu1')
- network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
- network = tl.layers.DenseLayer(network, n_units=10,
- act = tf.identity,
- name='output')
- y = network.outputs
- cost = tl.cost.cross_entropy(y, y_, 'cost')
- correct_prediction = tf.equal(tf.argmax(y, 1), y_)
- acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
- # train
- n_epoch = 200
- learning_rate = 0.0001
- print_freq = 10
- train_params = network.all_params
- train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999,
- epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)
- tl.layers.initialize_global_variables(sess)
- network.print_params()
- network.print_layers()
- print(' learning_rate: %f' % learning_rate)
- print(' batch_size: %d' % batch_size)
- for epoch in range(n_epoch):
- start_time = time.time()
- for X_train_a, y_train_a in tl.iterate.minibatches(
- X_train, y_train, batch_size, shuffle=True):
- feed_dict = {x: X_train_a, y_: y_train_a}
- feed_dict.update( network.all_drop ) # enable noise layers
- sess.run(train_op, feed_dict=feed_dict)
- if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
- print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
- train_loss, train_acc, n_batch = 0, 0, 0
- for X_train_a, y_train_a in tl.iterate.minibatches(
- X_train, y_train, batch_size, shuffle=True):
- dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable noise layers
- feed_dict = {x: X_train_a, y_: y_train_a}
- feed_dict.update(dp_dict)
- err, ac = sess.run([cost, acc], feed_dict=feed_dict)
- train_loss += err; train_acc += ac; n_batch += 1
- print(" train loss: %f" % (train_loss/ n_batch))
- print(" train acc: %f" % (train_acc/ n_batch))
- val_loss, val_acc, n_batch = 0, 0, 0
- for X_val_a, y_val_a in tl.iterate.minibatches(
- X_val, y_val, batch_size, shuffle=True):
- dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable noise layers
- feed_dict = {x: X_val_a, y_: y_val_a}
- feed_dict.update(dp_dict)
- err, ac = sess.run([cost, acc], feed_dict=feed_dict)
- val_loss += err; val_acc += ac; n_batch += 1
- print(" val loss: %f" % (val_loss/ n_batch))
- print(" val acc: %f" % (val_acc/ n_batch))
- try:
- tl.visualize.CNN2d(network.all_params[0].eval(),
- second=10, saveable=True,
- name='cnn1_'+str(epoch+1), fig_idx=2012)
- except:
- raise Exception("# You should change visualize.CNN(), if you want to save the feature images for different dataset")
- print('Evaluation')
- test_loss, test_acc, n_batch = 0, 0, 0
- for X_test_a, y_test_a in tl.iterate.minibatches(
- X_test, y_test, batch_size, shuffle=True):
- dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable noise layers
- feed_dict = {x: X_test_a, y_: y_test_a}
- feed_dict.update(dp_dict)
- err, ac = sess.run([cost, acc], feed_dict=feed_dict)
- test_loss += err; test_acc += ac; n_batch += 1
- print(" test loss: %f" % (test_loss/n_batch))
- print(" test acc: %f" % (test_acc/n_batch))