当前位置:网站首页>Building a simple template of neural network with keras (3) -- CNN convolutional neural network (handwritten numeral picture recognition)

Building a simple template of neural network with keras (3) -- CNN convolutional neural network (handwritten numeral picture recognition)

2021-09-15 08:14:19 Cai junshuai

# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337) #for reproducibility Reproducibility 
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential# By layer 
from keras.layers import Dense, Activation,Convolution2D, MaxPooling2D, Flatten
import matplotlib.pyplot as plt
from keras.optimizers import RMSprop
from keras.optimizers import Adam
       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

from mnist Download handwritten digital picture dataset , Picture is 28*28, The color of each pixel (0 To 255) Change it to (0 pour 1), Label y Turn into 10 Length , if 1, It's in 1 Place for 1, The rest are marked 0.

#dowmload the mnisst the path '~/.keras/datasets/' if it is the first time to be called
#x shape (60000 28*28),y shape(10000,)
(x_train,y_train),(x_test,y_test) = mnist.load_data()#0-9 Image data set 

#data pre-processing
x_train = x_train.reshape(-1,1,28,28)#-1 There is no limit to the number of Representatives ,1 For height , The height of black-and-white photos is 1
x_test = x_test.reshape(-1,1,28,28)
y_train = np_utils.to_categorical(y_train, num_classes=10) # Change the label to 10 Length , if 1, It's in 1 Place for 1, The rest are marked 0
y_test = np_utils.to_categorical(y_test,num_classes=10)
       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Next build CNN

Convolution -> Pooling -> Convolution -> Pooling

Make pictures from (1,28,28)->(32,28,28)->(32,14,14)-> (64,14,14) -> (64,7,7)

#Another way to build CNN
model = Sequential()

#Conv layer 1 output shape (32,28,28)
model.add(Convolution2D(
        nb_filter =32,# The filter is installed 32 individual , Each filter will scan the picture , You'll get another whole picture , So what I got later was 32 layer 
        nb_row=5,
        nb_col=5,
        border_mode='same', #padding method
        input_shape=(1,      #channels  The channel number 
                     28,28),  #height & width  Length and width 
        ))
model.add(Activation('relu'))

#Pooling layer 1 (max pooling) output shape (32,14,14)
model.add(MaxPooling2D(
        pool_size=(2,2), #2*2
        strides=(2,2),  # Jump both long and wide, and then pool once 
        border_mode='same', #paddingmethod
        ))

#Conv layers 2 output shape (64,14,14)
model.add(Convolution2D(64,5,5,border_mode='same'))
model.add(Activation('relu'))

#Pooling layers 2 (max pooling) output shape (64,7,7)
model.add(MaxPooling2D(pool_size=(2,2), border_mode='same'))
       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

Construct a fully connected neural network

#Fully connected layer 1 input shape (64*7*7) = (3136)
#Flatten  Wipe three dimensions into one dimension , Full connection 
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))

#Fully connected layer 2 to shape (10) for 10 classes
model.add(Dense(10)) # Output 10 A unit of 
model.add(Activation('softmax')) #softmax Used to classify 

#Another way to define optimizer
adam = Adam(lr=1e-4)

# We add metrics to get more results you want to see
model.compile( # compile 
        optimizer = adam,
        loss = 'categorical_crossentropy',
        metrics=['accuracy'], # Calculate at the same time when updating accuracy
        )
       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

Training and testing

print("Training~~~~~~~~")
#Another way to train the model
model.fit(x_train,y_train, epochs=1, batch_size=32) # Training 2 Large numbers , Each batch 32 individual 

print("\nTesting~~~~~~~~~~")
#Evalute the model with the metrics we define earlier
loss,accuracy = model.evaluate(x_test,y_test)

print('\ntest loss:',loss)
print('\ntest accuracy:', accuracy)
       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337) #for reproducibility Reproducibility 
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential# By layer 
from keras.layers import Dense, Activation,Convolution2D, MaxPooling2D, Flatten
import matplotlib.pyplot as plt
from keras.optimizers import RMSprop
from keras.optimizers import Adam

#dowmload the mnisst the path '~/.keras/datasets/' if it is the first time to be called
#x shape (60000 28*28),y shape(10000,)
(x_train,y_train),(x_test,y_test) = mnist.load_data()#0-9 Image data set 

#data pre-processing
x_train = x_train.reshape(-1,1,28,28)#-1 There is no limit to the number of Representatives ,1 For height , The height of black-and-white photos is 1
x_test = x_test.reshape(-1,1,28,28)
y_train = np_utils.to_categorical(y_train, num_classes=10) # Change the label to 10 Length , if 1, It's in 1 Place for 1, The rest are marked 0
y_test = np_utils.to_categorical(y_test,num_classes=10)

#Another way to build CNN
model = Sequential()

#Conv layer 1 output shape (32,28,28)
model.add(Convolution2D(
        nb_filter =32,# The filter is installed 32 individual , Each filter will scan the picture , You'll get another whole picture , So what I got later was 32 layer 
        nb_row=5,
        nb_col=5,
        border_mode='same', #padding method
        input_shape=(1,      #channels  The channel number 
                     28,28),  #height & width  Length and width 
        ))
model.add(Activation('relu'))

#Pooling layer 1 (max pooling) output shape (32,14,14)
model.add(MaxPooling2D(
        pool_size=(2,2), #2*2
        strides=(2,2),  # Jump both long and wide, and then pool once 
        border_mode='same', #paddingmethod
        ))

#Conv layers 2 output shape (64,14,14)
model.add(Convolution2D(64,5,5,border_mode='same'))
model.add(Activation('relu'))

#Pooling layers 2 (max pooling) output shape (64,7,7)
model.add(MaxPooling2D(pool_size=(2,2), border_mode='same'))

#Fully connected layer 1 input shape (64*7*7) = (3136)
#Flatten  Wipe three dimensions into one dimension , Full connection 
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))

#Fully connected layer 2 to shape (10) for 10 classes
model.add(Dense(10)) # Output 10 A unit of 
model.add(Activation('softmax')) #softmax Used to classify 

#Another way to define optimizer
adam = Adam(lr=1e-4)

# We add metrics to get more results you want to see
model.compile( # compile 
        optimizer = adam,
        loss = 'categorical_crossentropy',
        metrics=['accuracy'], # Calculate at the same time when updating accuracy
        )

print("Training~~~~~~~~")
#Another way to train the model
model.fit(x_train,y_train, epochs=1, batch_size=32) # Training 2 Large numbers , Each batch 32 individual 

print("\nTesting~~~~~~~~~~")
#Evalute the model with the metrics we define earlier
loss,accuracy = model.evaluate(x_test,y_test)

print('\ntest loss:',loss)
print('\ntest accuracy:', accuracy)
        
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.

Output :

 use Keras Building neural networks   Simple template ( 3、 ... and )—— CNN  Convolutional neural networks ( Handwritten digital image recognition )..._lua

 

# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337) #for reproducibility Reproducibility 
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential# By layer 
from keras.layers import Dense, Activation,Convolution2D, MaxPooling2D, Flatten
import matplotlib.pyplot as plt
from keras.optimizers import RMSprop
from keras.optimizers import Adam
      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

 

版权声明
本文为[Cai junshuai]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/09/20210909135057751z.html

随机推荐