MNIST (DCNN) with Keras (TensorFlow backend)

MNIST (DCNN) in Keras

What is Keras

Library to make Theano and TensorFlow easier to use

Keras

Execution environment

EC2 (AWS) g2.2xlarge instance (Oregon = US West) Python 2.7.6 TensorFlow 0.8.0

The AWS instance was initialized using another person's AMI, but if you want to introduce it yourself, refer to the following

It looks like you don't need to build from source anymore to run TensorFlow on an EC2 GPU instance?

Install Keras

Do as per Documentation Assuming that TensorFlow is already installed, add'sudo' if necessary

pip install scipy
pip install scikit-learn
pip install pyyaml
apt-get install libhdf5-dev
pip install h5py
pip install keras

Once from python

import keras

Run Keras as and edit'~ / .keras / keras.json' as follows

"backend": "theano"

"backend": "tensorflow"

code

mnist.py


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Activation, Flatten, Dense
from keras.callbacks import EarlyStopping

(X_train, y_train), (X_test, y_test) = mnist.load_data()
nb_classes = 10

X_train = X_train.reshape(-1, 1, 28, 28).astype('float32')
X_test = X_test.reshape(-1, 1, 28, 28).astype('float32')
X_train /= 255
X_test /= 255
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)

model = Sequential()

model.add(Convolution2D(nb_filter = 16, nb_row = 3, nb_col = 3, border_mode = 'same', input_shape = (1, 28, 28)))
model.add(Activation('relu'))

model.add(Convolution2D(nb_filter = 32, nb_row = 3, nb_col = 3, border_mode = 'same'))
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size = (2, 2), border_mode = 'same'))

model.add(Convolution2D(nb_filter = 64, nb_row = 3, nb_col = 3, border_mode = 'same'))
model.add(Activation('relu'))

model.add(Convolution2D(nb_filter = 128, nb_row = 3, nb_col = 3, border_mode = 'same'))
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size = (2, 2), border_mode = 'same'))

model.add(Flatten())

model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

early_stopping = EarlyStopping(monitor = 'val_loss', patience = 2)

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(X_train, y_train, nb_epoch = 5, batch_size = 100, callbacks = [early_stopping])
score = model.evaluate(X_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Referenced page

Deep Learning Library Keras Keras Documentation

Recommended Posts

MNIST (DCNN) with Keras (TensorFlow backend)
MNIST (DCNN) with skflow
Try TensorFlow MNIST with RNN
Challenge image classification with TensorFlow2 + Keras 3 ~ Visualize MNIST data ~
[TensorFlow] [Keras] Neural network construction with Keras
Compare raw TensorFlow with tf.contrib.learn and Keras
Run Keras with CNTK backend from CentOS
Zundokokiyoshi with TensorFlow
Breakout with Tensorflow
I tried to move GAN (mnist) with keras
Code for TensorFlow MNIST Begginer / Expert with Japanese comments
Summary of Tensorflow / Keras
Classify mnist numbers by unsupervised learning with keras [Autoencoder]
Image recognition with keras
Use the Cognitive Took Kit (CNTK) with the Keras backend
Reading data with TensorFlow
Kyotei forecast with TensorFlow
Identify the name from the flower image with keras (tensorflow)
Try MNIST with VAT (Virtual Adversarial Training) in Keras
CIFAR-10 tutorial with Keras
Multivariate LSTM with Keras
I tried to implement Grad-CAM with keras and tensorflow
Learn Wasserstein GAN with Keras model and TensorFlow optimization
Try regression with TensorFlow
2020/02 Python 3.7 + TensorFlow 2.1 + Keras 2.3.1 + YOLOv3 Object detection with the latest version
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
Translate Getting Started With TensorFlow
Try deep learning with TensorFlow
Use TensorFlow with Intellij IDEA
Read the keras mnist sample
Install Keras (used with Anaconda)
Multiple regression analysis with Keras
Approximate sin function with TensorFlow
Auto Encodder notes with Keras
Implemented word2vec with Theano + Keras
Sentence generation with GRU (keras)
Tuning Keras parameters with Keras Tuner
Jetson Nano JETPACK 44.1 (2020/10/21) with Tensorflow
Easy image classification with TensorFlow
Stock price forecast with tensorflow
Easily build CNN with Keras
Implemented Efficient GAN with keras
Think about dropouts with MNIST
I touched Tensorflow and keras
Image recognition with Keras + OpenCV
Challenge image classification by TensorFlow2 + Keras 4 ~ Let's predict with trained model ~
Creating a Tensorflow Sequential model with original images added to MNIST
Challenge image classification with TensorFlow2 + Keras 9-Learning, saving and loading models-
[Improved version] Try MNIST with VAT (Virtual Adversarial Training) in Keras
[Reading Notes] Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow Chapter 1