当前位置:网站首页>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

Original picture 
Vertical filter
Final effect 
Vertical filter
Final effect 
ⅡMax Pooling

Max Pooling Then the size is reduced to half of the original size 
Ⅲ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

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]

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])

Look at the first layer using the second convolution kernel ( common 64 Convolution kernels ), The resulting image
plt.imshow(pred[0][0,:,:,2])

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])

Look at the second layer using the second convolution kernel ( common 64 Convolution kernels ), The resulting image
plt.imshow(pred[1][0,:,:,2])

summary
Convolution layer 26*26, To Max Polling The layer becomes 13 * 13
And Max Polling The characteristics of layer objects are more obvious
边栏推荐
- OpenFOAM:底层
- What is the difference between TP5 and tp6?
- [reinforcement learning notes] common symbols in reinforcement learning
- How to choose an account opening broker? Is it safe to open an account online?
- Is it safe to open an account at Huatai Securities?
- Easyplayer streaming media player plays HLS video. Technical optimization of slow starting speed
- About i/o -- the relationship between memory and CPU and disk
- ¥3000 | 录「TBtools」视频,交个朋友&拿现金奖!
- (翻译)采用字母间距提高全大写文本可读性的方式
- LVS-DR模式单网段案例
猜你喜欢

【OpenCV】—输入输出XML和YAML文件

Data preprocessing: discrete feature coding method

Unity--Configurable Joint——简单教程,带你入门可配置关节

cazy长安战役八卦迷宫

Analysis of a video website m3u8 non perceptual encryption

C language: count the number of characters, numbers and spaces

atguigu----17-生命周期

声纹技术(六):声纹技术的其他应用

Cazy eight trigrams maze of Chang'an campaign

某次比赛wp
随机推荐
Object. Can defineproperty also listen for array changes?
声纹技术(七):声纹技术的未来
微信小程序_7,项目练习,本地生活
声纹技术(四):声纹识别的工程部署
How can games copied from other people's libraries be displayed in their own libraries
Notes on key words in the original English work biography of jobs (II) [chapter one]
Sharepoint:sharepoint 2013 with SP1 easy installation
[summary] 1361- package JSON and package lock JSON relationship
【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?
atguigu----01-脚手架
如何实现一个系统调用
Notes on key words in the original English work biography of jobs (IV) [chapter two]
How to implement a system call
声纹技术(二):音频信号处理基础
现在网上开通股票账号安全吗?
C language: bubble sort
进击的技术er,遇见实习岗位信息
QSS buttons of different styles
Is it safe to open an account at Huatai Securities?
C language "Recursion Series": recursively realizing the n-th power of X