当前位置:网站首页>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
边栏推荐
- Notes on key words in the original English work biography of jobs (VI) [chapter three]
- 【OpenCV】—离散傅里叶变换
- Jmeter中的断言使用讲解
- 配置、软件配置项、软件配置管理项辨析
- C#启动程序传递参数丢失双引号,如何解决?
- 各种同步学习笔记
- 紧急行政中止令下达 Juul暂时可以继续在美国销售电子烟产品
- On which platform is it safe to buy shares and open an account? Ask for sharing
- cazy长安战役八卦迷宫
- How to increase the monthly salary of software testing from 10K to 30K? Only automated testing can do it
猜你喜欢
Emergency administrative suspension order issued Juul can continue to sell electronic cigarette products in the United States for the time being
C#启动程序传递参数丢失双引号,如何解决?
C language: find all integers that can divide y and are odd numbers, and put them in the array indicated by B in the order from small to large
【OpenCV】—离散傅里叶变换
About i/o -- the relationship between memory and CPU and disk
Exchange: manage calendar permissions
nodejs 使用Express框架demo
紧急行政中止令下达 Juul暂时可以继续在美国销售电子烟产品
Jmeter接口测试,关联接口实现步骤(token)
Lvs-dr mode multi segment case
随机推荐
Level 6 easy to mix words
Sharepoint:sharepoint 2013 with SP1 easy installation
IC研发常用英文术语缩写
[reinforcement learning notes] common symbols in reinforcement learning
《乔布斯传》英文原著重点词汇笔记(三)【 chapter one】
SharePoint:SharePoint 2013 with SP1 简易安装
What is the difference between TP5 and tp6?
What is the file that tp6 automatically executes? What does the tp6 core class library do?
Various synchronous learning notes
紧急行政中止令下达 Juul暂时可以继续在美国销售电子烟产品
City Chain technology platform, really Realizing value Internet reconstruction!
atguigu----01-脚手架
How to implement a system call
打新债安全性有多高啊
Notes on key words in the original English work biography of jobs (VI) [chapter three]
On which platform is it safe to buy shares and open an account? Ask for sharing
RTOS 多线程下hardfault问题总结
华泰证券在上面开股票账户安全吗?
通过客户经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
socket编程——poll模型