当前位置:网站首页>4、 Convolution neural networks

4、 Convolution neural networks

2022-06-25 08:51:00 Beyond proverb

One 、CNN(Convolution Neural Networks)

Basic idea of convolutional neural network : Identify the characteristics of an object , To judge objects
Convolution Convolution: filter filter The values in and the pixel values of the picture are multiplied and added ,6 * 6 Accumulate once ( The steps are 1) become 4 * 4
Max Pooling: After convolution 4 * 4 Images , Partition selection maximum (2*2 selection ), become 2 * 2;
Max Pooling The function is to enhance the characteristics , Reduce data

Ⅰ Convolution

 Insert picture description here
Original picture  Insert picture description here
Vertical filter  Insert picture description here Final effect  Insert picture description here

Vertical filter  Insert picture description here Final effect  Insert picture description here

ⅡMax Pooling

 Insert picture description here
Max Pooling Then the size is reduced to half of the original size
 Insert picture description here

ⅢCNN Convolution neural network is convolution +Max Pooling

Two 、 Comparison of fully connected and convolutional networks

Traditional fully connected network

Fully connected network loss: 0.2400 - acc: 0.9113

from tensorflow import keras
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))


train_images_y = train_images/255
#model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
#optimizer="adam",loss="sparse_categorical_crossentropy",metrics=[`'accuracy']
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=['accuracy'])


model.fit(train_images_y,train_labels,epochs=10)
""" Epoch 1/10 60000/60000 [==============================] - 2s 38us/sample - loss: 0.4977 - acc: 0.8257 Epoch 2/10 60000/60000 [==============================] - 2s 41us/sample - loss: 0.3779 - acc: 0.8637 Epoch 3/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.3390 - acc: 0.8762 Epoch 4/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.3158 - acc: 0.8847 Epoch 5/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.2971 - acc: 0.8899 Epoch 6/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.2829 - acc: 0.8963 Epoch 7/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.2702 - acc: 0.8999 Epoch 8/10 60000/60000 [==============================] - 2s 38us/sample - loss: 0.2584 - acc: 0.9035 Epoch 9/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.2507 - acc: 0.9059 Epoch 10/10 60000/60000 [==============================] - 2s 39us/sample - loss: 0.2400 - acc: 0.9113 """

Convolutional neural networks

Convolutional neural networks loss: 0.0964 - acc: 0.9640

from tensorflow import keras
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
model = keras.Sequential()

model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(keras.layers.MaxPooling2D(2,2))
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(keras.layers.MaxPooling2D(2,2))

model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))


train_images_y = train_images/255
#model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
#optimizer="adam",loss="sparse_categorical_crossentropy",metrics=[`'accuracy']
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=['accuracy'])


model.fit(train_images_y.reshape(-1,28,28,1),train_labels,epochs=10)
""" Epoch 1/10 60000/60000 [==============================] - 43s 710us/sample - loss: 0.4380 - acc: 0.8408 Epoch 2/10 60000/60000 [==============================] - 41s 682us/sample - loss: 0.2923 - acc: 0.8920 Epoch 3/10 60000/60000 [==============================] - 41s 680us/sample - loss: 0.2485 - acc: 0.9082 Epoch 4/10 60000/60000 [==============================] - 41s 681us/sample - loss: 0.2164 - acc: 0.9190 Epoch 5/10 60000/60000 [==============================] - 41s 681us/sample - loss: 0.1886 - acc: 0.9297 Epoch 6/10 60000/60000 [==============================] - 41s 680us/sample - loss: 0.1654 - acc: 0.9376 Epoch 7/10 60000/60000 [==============================] - 41s 686us/sample - loss: 0.1462 - acc: 0.9446 Epoch 8/10 60000/60000 [==============================] - 41s 681us/sample - loss: 0.1254 - acc: 0.9525 Epoch 9/10 60000/60000 [==============================] - 42s 706us/sample - loss: 0.1115 - acc: 0.9579 Epoch 10/10 60000/60000 [==============================] - 47s 780us/sample - loss: 0.0964 - acc: 0.9640 """

Obviously ,CNN It has a higher accuracy than the traditional fully connected network , The loss function is small , But the training time is long

3、 ... and 、 Analysis of convolutional neural networks

On the basis of convolution neural network training , Look at the network structure
Seven layers !!!

Output Shape

The original image pixels are 2828, The filter here is 33 Convolution kernel , so
The first image becomes 26*26,64 by 64 Convolution kernels ( filter ), After a convolution , An image becomes 64 It's an image
The second floor , Change the image to a quarter of its original size , Halve the length and width , Turned into 13 * 13
The third level , Convolution layer , Convolution kernel 3 * 3, Get rid of 2 Pixels , become 11 * 11 Image
The fourth level , Change the image to a quarter of its original size , Halve the length and width , Turned into 5 * 5
The fifth floor ,flatten Flatten all pixels ,5 * 5 * 64=1600

Param Parameters

first floor ,33 Convolution kernel ( filter ) Yes 64 individual ,33*64=576, There is one for each bias, so 576+64=640 Parameters
The second layer has no adjustment parameters , Just changed the size , So the parameter is 0
The third level , Connected to the first floor 576 Parameters , With its own 64 Convolution kernel connection ,576 * 64=36864, Plus each one's bias,36864 + 64=36928
The fourth layer has no adjustment parameters , Just changed the size , So the parameter is 0
The fifth floor , Flattening operation , Is to expand all the pixel values into one line , No adjustment parameters , It's just a change in size , So the parameter is 0
The sixth floor , What the code sets is 128 Neurons , Therefore, it is fully connected with the upper layer ,1600 * 128=204800, Plus, every one has one bias, so 204800 + 128 = 204928
The seventh floor , Namely 10 Just classification , upper story 128 Neurons and this 10 Neurons are all arranged ,128 * 10 =1280, In addition to each bias,1280+10=1290

model.summary()
""" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_2 (Conv2D) (None, 26, 26, 64) 640 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 13, 13, 64) 0 _________________________________________________________________ conv2d_3 (Conv2D) (None, 11, 11, 64) 36928 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________ flatten_2 (Flatten) (None, 1600) 0 _________________________________________________________________ dense_4 (Dense) (None, 128) 204928 _________________________________________________________________ dense_5 (Dense) (None, 10) 1290 ================================================================= Total params: 243,786 Trainable params: 243,786 Non-trainable params: 0 _________________________________________________________________ """

Four 、 View each layer of network in detail

Perform seven layer network analysis on the first image in the test set

test_images_y = test_images/255# Because the training image was normalized during the previous training , Therefore, it is also necessary to normalize the test image during the test 
layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs=model.input,outputs=layer_outputs)
pred = activation_model.predict(test_images_y[0].reshape(1,28,28,1))# Here we test the first image 
pred

 Insert picture description here
The predicted results include the results of the seven layer network

len(pred)
""" 7 """

pred[ Layer network 0-6 There are seven floors ][0,:,:, The convolution kernels 1-64 altogether 64 Convolution kernels ]

Layer 1 network , Convolutional shape

pred[0].shape
""" (1, 26, 26, 64) """

first 0 It means the first floor , Convolution layer
the second 0 It's for drawing , It has to be for 0
:,: Show all the information of the picture
first 1 Represents the first convolution kernel ( filter )

pred[0][0,:,:,1]

 Insert picture description here

first floor — Convolution layer

Look at the first layer using the first convolution kernel ( common 64 Convolution kernels ), The resulting image

plt.imshow(pred[0][0,:,:,1])

 Insert picture description here

Look at the first layer using the second convolution kernel ( common 64 Convolution kernels ), The resulting image

plt.imshow(pred[0][0,:,:,2])

 Insert picture description here

The second floor —Max Polling layer

Look at the second layer using the first convolution kernel ( common 64 Convolution kernels ), The resulting image

plt.imshow(pred[1][0,:,:,1])

 Insert picture description here
Look at the second layer using the second convolution kernel ( common 64 Convolution kernels ), The resulting image

plt.imshow(pred[1][0,:,:,2])

 Insert picture description here

summary

Convolution layer 26*26, To Max Polling The layer becomes 13 * 13
And Max Polling The characteristics of layer objects are more obvious

原网站

版权声明
本文为[Beyond proverb]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250755452244.html