[PYTHON] Easily build CNN with Keras

Notes on building CNNs with Keras and classifying them using the CIFAR-10 dataset

Library to import

from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPool2D
from keras.optimizers import Adam

from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras.utils import plot_model
from keras.callbacks import TensorBoard

from keras.datasets import cifar10
from keras.utils import np_utils

Data preparation

Download CIFAR-10

(X_train, y_train),(X_test, y_test) = cifar10.load_data()

Is stored.

Normalize pixel values

Normalize each pixel value in the range 0-1

#Type conversion to float
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
#Normalize each pixel value
X_train /= 255.0
X_test /= 255.0

Convert to One-Hot representation

Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

Convert to one-hot representation for nb_classes classes with np_utils.to_categorical. Since there are 10 classes this time, we set nb_classes = 10.

Model definition

Actually define the model,

#Model definition
model = Sequential()

model.add(Conv2D(32,3,input_shape=(32,32,3)))
model.add(Activation('relu'))
model.add(Conv2D(32,3))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(64,3))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(1.0))

model.add(Dense(nb_classes, activation='softmax'))

adam = Adam(lr=1e-4)

model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=["accuracy"])

Define a model object with model.Sequential (). It is possible to add elements with model.add (). The first model.add () adds a 32x32 convolution layer (kernel 3x3), and since the input image is a 32x32 RGB image, the input layer is `ʻinput_shape (32,32,3). It can be defined by ``. Use Activation () to add the activation function, and Dense () to add the fully connected layer. Set model.compile () to obtain Adam as the optimization method, cross entropy as the error function, and accuracy as the calculation.

Execution of learning

history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_split=0.1)

You can learn with model.fit (). This time, we set nb_epoch = 100 to set the number of epochs to 100. varidation_split is a parameter that uses some percentage of the training data for the test. This time, 10% is used for the test.

Data visualization

Visualize your data with Keras

#Plot the model
plot_model(model, to_file='./model2.png')

Save trained model

Since the trained model can be saved as a file, the model configuration data can be saved as a JSON file and the model weight data can be saved as an HDF5 file.

json_string = model.to_json()
open(os.path.join('./', 'cnn_model.json'), 'w').write(json_string)

model.save_weights(os.path.join('./', 'cnn_model_weight.hdf5'))

Since we are using Tensorflow for the backend this time, we can also use Tensorboar below. [Keras / TensorFlow] Using TensorBoard with Keras

Summary

CNN was constructed with Keras, and 10 categories were classified with CIFAR-10. From here, I will try various things.

Source Code https://github.com/sasayabaku/Machine-Learning/blob/master/Example_CNN/cifar10.ipynb

References

Keras Documentation Deep Learning starting with Keras

Recommended Posts

Easily build CNN with Keras
Easily build virtual machines with Vagrant
Easily build a development environment with Laragon
Easily daemonized with Supervisor
Image recognition with keras
CIFAR-10 tutorial with Keras
Easily beep with python
Multivariate LSTM with Keras
Build python3.x with pyenv
Easily build HPC on AWS with genuine AWS Cfn Cluster
Build Keras AI Prediction Engine in 1 Hour with GCP
Easily take C program coverage with configure's build options!
CNN implementation with just numpy
Easily serverless with Python with chalice
Install Keras (used with Anaconda)
Multiple regression analysis with Keras
Auto Encodder notes with Keras
Easily write if-elif with lambda
Implemented word2vec with Theano + Keras
Try running CNN with ChainerRL
Sentence generation with GRU (keras)
Tuning Keras parameters with Keras Tuner
Build python3 environment with ubuntu 16.04
Build python environment with direnv
Use Maxout + CNN with Pylearn2
Implemented Efficient GAN with keras
Survive Christmas with character-level CNN
I tried handwriting recognition of runes with CNN using Keras
Let's build git-cat with Python
Easily build network infrastructure and EC2 with AWS CDK Python
Easily cProfile with a decorator
Image recognition with Keras + OpenCV
Easily build a natural language processing model with BERT + LightGBM + optuna
Build FastAPI + uvicorn + nginx with docker-compose
Decrypt the QR code with CNN
MNIST (DCNN) with Keras (TensorFlow backend)
Build python virtual environment with virtualenv
Predict Kaggle's Titanic with keras (kaggle ⑦)
Build a deb file with Docker
Easily implement subcommands with python click
Easily handle lists with python + sqlite3
Build Mysql + Python environment with docker
Easily switch pyenv virtualenvs with peco
[AWS] Build an ECR with AWS CDK
[TensorFlow] [Keras] Neural network construction with Keras
Build PyPy execution environment with Docker
Build mlpy with python3.3 (64bit) (windows 64bit)
Implement Keras LSTM feedforward with numpy
Compare DCGAN and pix2pix with keras
Score-CAM implementation with keras. Comparison with Grad-CAM
Easily handle databases with Python (SQLite3)
Build a web application with Django
Prediction of sine wave with keras
Build IPython Notebook environment with boot2docker
Automatically build Python documentation with Sphinx
Beginner RNN (LSTM) | Try with Keras
[Python] Collect images easily with icrawler!
Implementation of Light CNN (Python Keras)
Easily post to twitter with Python 3
Write Reversi AI with Keras + DQN
Build Vim with MinGW. (+ lua, + python)