当前位置:网站首页>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
边栏推荐
- Oracle-单行函数大全
- 各种同步学习笔记
- C language "Recursion Series": recursively realizing the n-th power of X
- 声纹技术(一):声纹技术的前世今生
- 自定义注解之编译时注解(RetentionPolicy.CLASS)
- 【515. 在每个树行中找最大值】
- tp6自动执行的文件是哪个?tp6核心类库有什么作用呢?
- 【MYSQL】索引的理解和使用
- 《乔布斯传》英文原著重点词汇笔记(一)【 Introduction 】
- Le labyrinthe des huit diagrammes de la bataille de cazy Chang'an
猜你喜欢

City Chain technology platform, really Realizing value Internet reconstruction!

Webgl Google prompt memory out of bounds (runtimeerror:memory access out of bounds, Firefox prompt index out of bounds)

五、项目实战---识别人和马

Lvs-dr mode multi segment case

Wechat applet_ 7. Project practice, local life

2021 "Ai China" selection

LVS-DR模式多网段案例

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

【期末复习笔记】数字逻辑

About i/o -- the relationship between memory and CPU and disk
随机推荐
¥3000 | 录「TBtools」视频,交个朋友&拿现金奖!
Notes on key words in the original English work biography of jobs (IV) [chapter two]
Summary of NLP data enhancement methods
如何实现一个系统调用
微信小程序_7,项目练习,本地生活
检测点是否在多边形内
How to implement a system call
[reinforcement learning notes] common symbols in reinforcement learning
故障:Outlook 收发邮件时的 0x800CCC1A 错误
C language: count the number of words in a paragraph
[summary] 1361- package JSON and package lock JSON relationship
备战2022年金九银十必问的1000道Android面试题及答案整理,彻底解决面试的烦恼
Meaning of Jieba participle part of speech tagging
The city chain technology platform is realizing the real value Internet reconstruction!
《乔布斯传》英文原著重点词汇笔记(四)【 chapter two 】
Is it safe to open a stock account online now?
openid是什么意思?token是什么意思?
What is the difference between TP5 and tp6?
【总结】1361- package.json 与 package-lock.json 的关系
Emergency administrative suspension order issued Juul can continue to sell electronic cigarette products in the United States for the time being