|
| 1 | +""" |
| 2 | +To know more or get code samples, please visit my website: |
| 3 | +https://morvanzhou.github.io/tutorials/ |
| 4 | +Or search: 莫烦Python |
| 5 | +Thank you for supporting! |
| 6 | +""" |
| 7 | + |
| 8 | +# please note, all tutorial code are running under python3.5. |
| 9 | +# If you use the version like python2.7, please modify the code accordingly |
| 10 | + |
| 11 | +# 6 - CNN example |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +np.random.seed(1337) # for reproducibility |
| 15 | +from keras.datasets import mnist |
| 16 | +from keras.utils import np_utils |
| 17 | +from keras.models import Sequential |
| 18 | +from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten |
| 19 | +from keras.optimizers import Adam |
| 20 | + |
| 21 | +# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called |
| 22 | +# X shape (60,000 28x28), y shape (10,000, ) |
| 23 | +(X_train, y_train), (X_test, y_test) = mnist.load_data() |
| 24 | + |
| 25 | +# data pre-processing |
| 26 | +X_train = X_train.reshape(-1, 1, 28, 28) |
| 27 | +X_test = X_test.reshape(-1, 1, 28, 28) |
| 28 | +y_train = np_utils.to_categorical(y_train, nb_classes=10) |
| 29 | +y_test = np_utils.to_categorical(y_test, nb_classes=10) |
| 30 | + |
| 31 | +# Another way to build your CNN |
| 32 | +model = Sequential() |
| 33 | + |
| 34 | +# Conv layer 1 output shape (32, 28, 28) |
| 35 | +model.add(Convolution2D( |
| 36 | + nb_filter=32, |
| 37 | + nb_row=5, |
| 38 | + nb_col=5, |
| 39 | + border_mode='same', # Padding method |
| 40 | + input_shape=(1, # channels |
| 41 | + 28, 28) # height & width |
| 42 | +)) |
| 43 | +model.add(Activation('relu')) |
| 44 | + |
| 45 | +# Pooling layer 1 (max pooling) output shape (32, 14, 14) |
| 46 | +model.add(MaxPooling2D( |
| 47 | + pool_size=(2, 2), |
| 48 | + strides=(2, 2), |
| 49 | + border_mode='same', # Padding method |
| 50 | +)) |
| 51 | + |
| 52 | +# Conv layer 2 output shape (64, 14, 14) |
| 53 | +model.add(Convolution2D(64, 5, 5, border_mode='same')) |
| 54 | +model.add(Activation('relu')) |
| 55 | + |
| 56 | +# Pooling layer 2 (max pooling) output shape (64, 7, 7) |
| 57 | +model.add(MaxPooling2D(pool_size=(2, 2), border_mode='same')) |
| 58 | + |
| 59 | +# Fully connected layer 1 input shape (64 * 7 * 7) = (3136), output shape (1024) |
| 60 | +model.add(Flatten()) |
| 61 | +model.add(Dense(1024)) |
| 62 | +model.add(Activation('relu')) |
| 63 | + |
| 64 | +# Fully connected layer 2 to shape (10) for 10 classes |
| 65 | +model.add(Dense(10)) |
| 66 | +model.add(Activation('softmax')) |
| 67 | + |
| 68 | +# Another way to define your optimizer |
| 69 | +adam = Adam(lr=1e-4) |
| 70 | + |
| 71 | +# We add metrics to get more results you want to see |
| 72 | +model.compile(optimizer=adam, |
| 73 | + loss='categorical_crossentropy', |
| 74 | + metrics=['accuracy']) |
| 75 | + |
| 76 | +print('Training ------------') |
| 77 | +# Another way to train the model |
| 78 | +model.fit(X_train, y_train, nb_epoch=1, batch_size=32,) |
| 79 | + |
| 80 | +print('\nTesting ------------') |
| 81 | +# Evaluate the model with the metrics we defined earlier |
| 82 | +loss, accuracy = model.evaluate(X_test, y_test) |
| 83 | + |
| 84 | +print('\ntest loss: ', loss) |
| 85 | +print('\ntest accuracy: ', accuracy) |
| 86 | + |
| 87 | + |
0 commit comments