[PYTHON] CNN (1) for image classification (for beginners)

Although it is not handled in practice, it is recorded as learning. This time, the convolutional neural network (CNN) It seems to be effective for image recognition analysis ...

CNN The point is that there is a convolution layer and a pooling layer in addition to the normal neural network.

Convolution

The most important part of converting features into data from images. The image data is 5x5x1 data. Set the weighting in the 3x3x3 kernel and calculate all patterns. 9 patterns in the figure below. The result is called a feature map. スクリーンショット 2020-01-31 8.35.35.png

Pooling

Pooling is a method of reducing a large image while leaving important information. As a result, the dimension of the data can be reduced, the calculation speed can be suppressed, and learning can proceed. スクリーンショット 2020-01-31 8.56.07.png There are two types ・ Max pooling → Set the maximum value in the kernel ・ Avg pooling → Calculate the average value of all the numbers in the kernel

Specific data dimension reduction (= compression method)

By folding, the number of dimensions of image data is reduced (= compressed) by calculating the image data within the size of the kernel and extracting new features. The formula is as follows. スクリーンショット 2020-03-08 16.15.06.png By pooling, the data is compressed while emphasizing the features. The method is to either the maximum value in the kernel or the average value.

Move the sample

Use the MNIST dataset.

%matplotlib inline
import keras
from keras.datasets import mnist
import matplotlib.pyplot as plt

#Read data. Divided into learning data and training data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

#Display of MNIST data
fig = plt.figure(figsize=(9, 9))
fig.subplots_adjust(left=0, right=1, bottom=0, top=0.5, hspace=0.05, wspace=0.05)
for i in range(81):
    ax = fig.add_subplot(9, 9, i + 1, xticks=[], yticks=[])
    ax.imshow(x_train[i].reshape((28, 28)), cmap='gray')

It comes out like this. スクリーンショット 2020-04-16 8.29.36.png


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
img_rows, img_cols = 28, 28

(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')

y_train = y_train.astype('int32')
y_test = y_test.astype('int32')
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test =  keras.utils.np_utils.to_categorical(y_test, num_classes)

#The part from the convolution to the pooling
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))

Summary

The conceptual image was organized.

reference

[Mechanism of convolutional neural network] (https://postd.cc/how-do-convolutional-neural-networks-work/) [What is a convolutional neural network often used in image processing] (https://kenyu-life.com/2019/03/07/convolutional_neural_network/)

Recommended Posts

CNN (1) for image classification (for beginners)
Roadmap for beginners
Challenge image classification with TensorFlow2 + Keras CNN 1 ~ Move for the time being ~
Basic principles of image recognition technology (for beginners)
Spacemacs settings (for beginners)
python textbook for beginners
CNN 1 Image Recognition Basics
Dijkstra algorithm for beginners
OpenCV for Python beginners
Caffe Model Zoo for beginners [Age and gender classification]
Approximate Nearest Neighbor Search for Similar Image Analysis (For Beginners) (1)
[PyTorch] Image classification of CIFAR-10
I tried AutoGluon's Image Classification
Learning flow for Python beginners
[For beginners] kaggle exercise (merucari)
Linux distribution recommended for beginners
ROC curve for multiclass classification
Easy image classification with TensorFlow
Python3 environment construction (for beginners)
Application of CNN2 image recognition
Overview of Docker (for beginners)
Python #function 2 for super beginners
Seaborn basics for beginners ④ pairplot
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
[For Kaggle beginners] Titanic (LightGBM)
Reference resource summary (for beginners)
Linux command memorandum [for beginners]
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
Convenient Linux shortcuts (for beginners)
Challenge image classification by TensorFlow2 + Keras 1-Move for the time being-
[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
Pandas basics for beginners ① Reading & processing
TensorFlow MNIST For ML Beginners Translation
Decision tree (for beginners) -Code edition-
Pandas basics for beginners ⑧ Digit processing
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for super beginners Python # dictionary type 1 for super beginners
Seaborn basics for beginners ② Histogram (distplot)
[For beginners] Django -Development environment construction-
[For beginners] Script within 10 lines (1.folium)
Logistic Regression (for beginners) -Code Edition-
What is scraping? [Summary for beginners]
Personal notes for python image processing
Python #index for super beginners, slices
Recommended container image for Python applications
Image classification with wide-angle fundus image dataset
<For beginners> python library <For machine learning>
TensorFlow Tutorial MNIST For ML Beginners
Frequently used Linux commands (for beginners)
[Must-see for beginners] Basics of Linux
Python #len function for super beginners
Beginners use Python for web scraping (1)
Speech Recognition: Genre Classification Part2-Music Genre Classification CNN
Run unittests in Python (for beginners)
What is xg boost (1) (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners