当前位置:网站首页>CNN convolutional neural network
CNN convolutional neural network
2022-07-29 06:47:00 【yc_ ZZ】
Catalog
One 、 Application scenarios
In the field of computer vision
Image detection

Image classification and retrieval
retrieval : Enter a product , Return similar items . Similar to Taobao map 
Super resolution reconstruction
Make the image clearer 
Medical tasks
Cell detection Font recognition, etc 
...
Two 、 Problem introduction
Why not use fully connected neural network to process images , To solve the problem of image data 2 A thought , introduce CNN
Too many parameters
A neuron in the full connection layer needs to connect each component of the input data to itself ,
For images 

When there are many parameters , The ability of model expression is very strong, which is easy to lead to Over fitting , There are very good results for training data , But for the test data can not have good results . Therefore, ordinary fully connected neural network cannot meet the requirements of image processing
solve :
1、 Local connection
Images have Strong regional , Each part has little to do with the rest , So change the full connection to local connection , Reduce the amount of parameters 
There are 100 individual 10*10 A small range of ,100 A small range, each with 10^6 Two neurons are connected , So weight parameters w1,w2…wn altogether 100 ×10 ^ 6 individual

2、 Parameters of the Shared
The image features have nothing to do with position , The same parameters are used for the local connection of each neural unit
that w1=w2=…=wn, Parameter only 100 individual 
3、 ... and 、 Network details

The input is 3D data , Directly extract the image feature vector
A two-dimensional -> The three dimensional

1、 Input layer
Three dimensions of image (height、weight、C(RGB The channel number ))
Usually , We think of the image RGB The number of channels has three dimensions of red, green and blue , So the image RGB The number of channels is usually 3
2、 Convolution layer
use : Image features are extracted by convolution kernel to get feature map
Convolution kernel
Convolution kernel is the weight parameter of local connection
Convolution kernel is left to right on the input image , Slide from top to bottom ( The idea of local connection 、 The idea of parameter sharing )
Dynamic graph understanding Output size= Input size- Convolution kernel size+1(3=5-3+1)

step
Small step size : Fine grained feature extraction Step by step : Coarse grained feature extraction ( Text tasks are often used )

padding
The output of convolution kernel becomes smaller relative to the input after the image is crossed ,CNN More than one convolution , If the convolution layer becomes smaller and smaller , Finally, the size of the image will become 1 Problems arise padding Make convolution input and output size unchanged

Convolution processing multichannel
Images are all ternary channels , We only demonstrated one channel convolution operation in the previous step , The three channels need to be separated add up to 
Ternary channel pixels are inconsistent , We need to convolute each other , Add the corresponding position and add the offset term to the final result ( Finally, the upper left corner of the feature map 3)
Be careful : The step size here is set to 2

We can have many convolution kernels to extract image features to get feature maps , Make features richer ( For example, in the picture above filterw0 And filterw1 Two green matrices are obtained , Finally stack up )
Stack convolution
A convolution kernel produces a characteristic graph , Feature extraction once is not enough , We will continue to do convolution based on the characteristic graph 
Convolution parameter calculation
For example, this is a convolution kernel , His parameters are 3×3×3=27 individual 

3、 Pooling layer
Compress the convoluted characteristic graph ( Down sampling ), Not every feature is very important . Choose the important ones to stay 
Here we only talk about maximum pooling , Because the effect is the best

Four 、 The overall architecture



5、 ... and 、 Code combat
Handwritten digit recognition
import numpy
import torch
from torch import nn
from PIL import Image
import matplotlib.pyplot as plt
import os
from torchvision import datasets, transforms,utils
# Neural network model
import torch.nn.functional as F
# Optimization function
import torch.optim as optim
# route
import os
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.5],std=[0.5])])
# Import training data
train_data = datasets.MNIST(root = "./data/",
transform=transform,
train = True,
download =False)
# Import test data
test_data = datasets.MNIST(root="./data/",
transform = transform,
train = False)
# print(len(train_data)) #60000
# print(len(test_data)) #10000
# Pack and put data_loader Set up batch size , Speed up your training , The basic single in the loader is batch The data of
train_loader = torch.utils.data.DataLoader(train_data,batch_size=64,
shuffle=True,num_workers=2)
test_loader = torch.utils.data.DataLoader(test_data,batch_size=64,
shuffle=True,num_workers=2)
# print(len(train_loader)) #938
# print(len(test_loader)) #157
# The format of a sample is [data,label], The first to store data , The second storage label
# oneimg,label=train_data[0] # Here is three-dimensional
# plt.imshow(oneimg.squeeze(0)) # You need to compress a dimension to draw a pixel matrix into a picture
# plt.show()
# configure network
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1=nn.Conv2d(1,32,kernel_size=3,padding=1,stride=1)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
# Initial size :28*28*1 Pool once 14*14 Pool secondary 7*7 To quantify 7*7* The channel number (64)
self.fc1 = nn.Linear(64 * 7 * 7, 1024) # Two pools , So it is 7*7 instead of 14*14
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 10)
def forward(self,x):
x= self.pool(F.relu(self.conv1(x))) # First convolution + Pooling
x = self.pool(F.relu(self.conv2(x))) # The second convolution + Pooling
x = x.view(-1, 64 * 7 * 7) # Flatten the data into one-dimensional
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net=CNN() # Instantiation
criterion = nn.CrossEntropyLoss()# Cross entropy loss function
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) # Random gradient descent optimization function
def train():
train_accs = []
train_loss = []
test_accs = []
for epoch in range(3):
running_loss=0.0
for i,data in enumerate(train_loader,0):
inputs, labels = data[0], data[1]
optimizer.zero_grad()
# Forward direction + Back + Optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# loss Output , One hundred each batch Output , average loss
running_loss += loss.item()
if i % 100 == 0:
print('[%d,%5d] loss :%.3f' %
(epoch, i, running_loss / 100))
running_loss = 0.0
train_loss.append(loss.item())
if i % 100==0:
torch.save(net.state_dict(),"./CNNmodel/model.pkl")
torch.save(optimizer.state_dict(),"./CNNmodel/optimizer.pkl")
def test():
loss_list = []
acc_list = []
for idx, (input, target) in enumerate(test_loader):
with torch.no_grad():
output = net(input)
cur_loss = criterion(output, target)
loss_list.append(cur_loss)
pred = output.max(dim=-1)[-1] # Get the maximum value on each line
cur_acc = pred.eq(target).float().mean() # Accuracy rate
acc_list.append(cur_acc)
print(" Average accuracy , Average loss ", numpy.mean(acc_list), numpy.mean(loss_list))
if __name__ == '__main__':
train()
test()
边栏推荐
- Use of PDO
- CNAME记录和A记录的区别
- Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
- apisix健康检查测试
- Several misunderstandings about DDoS
- day03_ 2_ task
- How to use SFTP command to access SFTP server on the development board
- What is DNS amplification attack
- Multiple IO usage
- Hongke automation SoftPLC | modk operation environment and construction steps (1) -- Introduction to operation environment
猜你喜欢

Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus

STP spanning tree principle and example of election rules

MQTT服务器搭建以及使用MQTT.fx测试

Hongke white paper | how to use TSN time sensitive network technology to build a digital factory in industry 4.0?

JMM 内存模型概念

day02_ Basic grammar

ping 原理

Ram block memory generator of vivado IP core

VMware虚拟机在物理机win10系统下如何连接外网

Sequence list and linked list
随机推荐
Base64与File之间的相互转化
Solve the error that the simulation output is STX under the frequency division module Modelsim
greenplum企业部署
Merkletree builds QT implementation UI
What is WAF protection
Conversion of fixed-point number to floating-point number of vivado IP core
Leetcode刷题记录
最新PyCharm2018破解教程
Use of PDO
The difference between DDoS attack and CC attack
Enterprise manager cannot connect to the database instance in Oracle10g solution
网站被挂马的解决方案
Design of IIR filter based on FPGA
day04_数组
Hongke education you want to enter the field of TSN? Hongke teaches you how to build TSN test system
STP spanning tree principle and example of election rules
MQTT服务器搭建以及使用MQTT.fx测试
The performance and viewing methods of websites attacked by DDoS
day03_2_作业
Complex floating point division of vivado IP core floating point

