Convolutional Neural Network Case: Chinese Font Recognition - Lishu and Xingkai

1. AlexNet network structure
1.1 AlexNet introduction:
It was designed in 2012 by Hinton, the winner of the ImageNet competition, and his student Krizhevsky Alex.
The first entrant in the ImageNet competition to use a convolutional neural network.
1.2 AlexNet network structure: 8 layers
Convolution layer
Pooling layer
Convolution
layer Pooling layer
Convolution layer
Convolution layer
Convolution layer
Pooling layer Output layer : Three fully connected
layers Activation function; use Dropout to randomly ignore some neurons to avoid model overfitting; use overlapping maximum pooling in CNN (step size smaller than convolution kernel); propose Local Response Normalization (LRN) , was gradually replaced by BN (Batch Normalization); using CUDA to accelerate the training of neural networks, using the powerful computing power of GPU; using Data Augmentation technology to increase the sample size. 2. Case: Chinese font recognition - Lishu and Xingkai 2.1 Data preparation 2.2 Construct data generator (1) Data generator
insert image description here









insert image description here

from keras.preprocessing.image import ImageDataGenerator

IMSIZE=227

validation_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
    './data/ChineseStyle/test/',
    target_size=(IMSIZE, IMSIZE),
    batch_size=200,
    class_mode='categorical')

train_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
    './data/ChineseStyle/train',
    target_size=(IMSIZE, IMSIZE),
    batch_size=200,
    class_mode='categorical')

(2) output image

from matplotlib import pyplot as plt

plt.figure()
fig,ax = plt.subplots(2,5)
fig.set_figheight(7)
fig.set_figwidth(15)
ax=ax.flatten()
X,Y=next(validation_generator)
for i in range(10): ax[i].imshow(X[i,:,:,:])

insert image description here

2.3 AlexNet code implementation

from keras.layers import Activation,Conv2D, BatchNormalization, Dense
from keras.layers import Dropout, Flatten, Input, MaxPooling2D, ZeroPadding2D
from keras import Model

IMSIZE = 227
input_layer = Input([IMSIZE,IMSIZE,3])
x = input_layer
x = Conv2D(96,[11,11],strides = [4,4], activation = 'relu')(x) 
x = MaxPooling2D([3,3], strides = [2,2])(x)    
x = Conv2D(256,[5,5],padding = "same", activation = 'relu')(x)
x = MaxPooling2D([3,3], strides = [2,2])(x)
x = Conv2D(384,[3,3],padding = "same", activation = 'relu')(x) 
x = Conv2D(384,[3,3],padding = "same", activation = 'relu')(x) 
x = Conv2D(256,[3,3],padding = "same", activation = 'relu')(x) 
x = MaxPooling2D([3,3], strides = [2,2])(x)
x = Flatten()(x)   
x = Dense(4096,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = Dense(4096,activation = 'relu')(x)
x = Dropout(0.5)(x)
x = Dense(2,activation = 'softmax')(x) 
output_layer=x
model=Model(input_layer,output_layer)
model.summary()

2.4 Compile and run AlexNet

from keras.optimizers import Adam
model.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.001),metrics=['accuracy'])
model.fit_generator(train_generator,epochs=20,validation_data=validation_generator)

insert image description here

Guess you like

Origin blog.csdn.net/qq_31391601/article/details/115049033