Created
September 11, 2019 16:19
-
-
Save TechMaster/c63720c09f48e2896322913856ba3b8d to your computer and use it in GitHub Desktop.
Sử dụng CNN để nhận dạng chữ số viết tay MNIST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Importing the required Keras modules containing model and layers | |
| import tensorflow as tf | |
| from tensorflow.keras import layers | |
| print(tf.__version__) | |
| print("GPU Available: ", tf.test.is_gpu_available()) | |
| (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() | |
| # Reshaping the array to 4-dims so that it can work with the Keras API | |
| x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) | |
| x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) | |
| input_shape = (28, 28, 1) | |
| # Making sure that the values are float so that we can get decimal points after division | |
| x_train = x_train.astype('float32') | |
| x_test = x_test.astype('float32') | |
| # Normalizing the RGB codes by dividing it to the max RGB value. | |
| x_train /= 255 | |
| x_test /= 255 | |
| print('x_train shape:', x_train.shape) | |
| print('Number of images in x_train', x_train.shape[0]) | |
| print('Number of images in x_test', x_test.shape[0]) | |
| # Creating a Sequential Model and adding the layers | |
| model = tf.keras.Sequential() | |
| model.add(layers.Conv2D(28, kernel_size=(3, 3), input_shape=input_shape)) | |
| model.add(layers.MaxPooling2D(pool_size=(2, 2))) | |
| model.add(layers.Flatten()) # Flattening the 2D arrays for fully connected layers | |
| model.add(layers.Dense(128, activation=tf.nn.relu)) | |
| model.add(layers.Dropout(0.2)) | |
| model.add(layers.Dense(10, activation=tf.nn.softmax)) | |
| model.compile(optimizer='adam', | |
| loss='sparse_categorical_crossentropy', | |
| metrics=['accuracy']) | |
| model.fit(x=x_train, y=y_train, epochs=10) | |
| # serialize model to JSON | |
| model_json = model.to_json() | |
| with open("model.json", "w") as json_file: | |
| json_file.write(model_json) | |
| # serialize weights to HDF5 | |
| model.save_weights("model.h5") | |
| print("Saved model to disk") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment