Data mining algorithm and practice (14): build a convolutional neural network (CNN) on kaggle to implement fashion_MNIST

Convolutional Neural Network CNN (Convolutional Neural Network) is a feed-forward neural network that recognizes image edges and textures through convolution operations, and then extracts abstract features through continuous convolution, and finally realizes image recognition. It has excellent performance in image processing and is widely used in image classification, positioning and other fields. Compared with other neural network structures, convolutional neural networks require relatively few parameters and are widely used.

table of Contents

 The general architecture of the CNN network

CNN implements fashion_MNIST classification


 The general architecture of the CNN network

The convolutional layer completes the edge and texture analysis of the data and extracts the image features. If the image is processed with a 3×3 convolution kernel, 9 pixels will be converted into 1 pixel, and the nonlinear transformation layer is the neuron Activation layer, setting neuron transfer threshold, pooling layer is a process of downsampling, reducing image pixels, fully connected layer is a linear transformation layer, extracting and outputting useful features, convolutional layer + nonlinear transformation layer +Pooling layer is the process of making the image smaller and thicker ;

Three important parameters of the convolution layer: ksize, strides, padding, which are the size of the convolution kernel (usually a 3*3 or 5*5 square matrix), the span of the convolution kernel movement, and the edge processing rules of the convolution. Use the shift+tab key in the notebook to view the parameters of the tensorflow.keras.layers.Cov2d() function:

Init signature:
tf.keras.layers.Conv2D(
    filters,
    kernel_size,
    strides=(1, 1),
    padding='valid',
    data_format=None,
    dilation_rate=(1, 1),
    activation=None,
    use_bias=True,
    kernel_initializer='glorot_uniform',
    bias_initializer='zeros',
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs,
)

The pooling layer is a down-sampling operation, which makes the image smaller. There are maximum pooling, average pooling, etc. The maximum pooling uses the MaxPooling2D() function. The parameters of the function are as follows:

Init signature:
tf.keras.layers.MaxPooling2D(
    pool_size=(2, 2),
    strides=None,
    padding='valid',
    data_format=None,
    **kwargs,
)

CNN implements fashion_MNIST classification

Generally, the CPU cannot complete the CNN network calculation. Here, the GPU provided by kaggle is used to build the CNN network to implement the classification model of fashion_MNIST. Kaggle is a data competition platform that contains many examples and solutions for data mining and deep learning, which is an excellent one Learning platform: https://www.kaggle.com/notebooks/welcome , create a notebook, select GPU acceleration:

Confirm to enable GPU acceleration:

The input image of the convolutional neural network is 4 dimensions, which are the number of batches, the length of the image, the height of the image, and the number of channels;

import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

(train_image,train_lable),(test_image,test_lable)=tf.keras.datasets.fashion_mnist.load_data()
train_image.shape

# 将图像维度扩张到4维数据
train_images=np.expand_dims(train_image,-1)
test_images=np.expand_dims(test_image,-1)
train_images.shape

model=tf.keras.Sequential([tf.keras.layers.Conv2D(32,(3,3),input_shape=train_images.shape[1:],activation="relu"),
           tf.keras.layers.Dropout(0.5),
           tf.keras.layers.MaxPooling2D(),
           tf.keras.layers.Conv2D(32,(3,3),activation="relu"),
           tf.keras.layers.Dropout(0.5),
           tf.keras.layers.GlobalMaxPooling2D(),
           tf.keras.layers.Dense(10,activation="softmax")
])
model.summary()


model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["acc"])

history=model.fit(train_images,
                  train_lable,
                  epochs=5,
                  validation_data=(test_images,test_lable)
                 )
model.evaluate(test_images,test_lable)
#plt.plot(history.epoch,history.history.get('loss'))
plt.plot(history.epoch,history.history.get('acc'))

You can see the structure and the number of parameters of this neural network:

Accuracy mapping:

Guess you like

Origin blog.csdn.net/yezonggang/article/details/106496413