当前位置:网站首页>Neural network convolution layer
Neural network convolution layer
2022-07-01 04:45:00 【booze-J】
article
pytorch Convolution layer official document
pytorch Conv2d Official documents
The example code is as follows :
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# Be careful dataset in transform The parameter receives an object , So we need to add parentheses , In addition, when using neural network for operation, the data type required is tensor type , therefore transforms Parameters to add .
dataloader = DataLoader(dataset,batch_size=64)
# Build a simple network
class Booze(nn.Module):
# Inherit nn.Module The initialization
def __init__(self):
super().__init__()
# Note that here is to create a global variable, so we need to add a self When out_channels Far greater than in_channels The original image needs to be expanded , That is to say padding The value of cannot be set to 0 了 , According to the formula
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
# rewrite forward function
def forward(self,x):
x = self.conv1(x)
return x
# Initialize the network
obj = Booze()
# Check out the Internet
print(obj)
''' Booze( (conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1)) ) '''
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs,targets = data
output = obj(imgs)
# torch.Size([64, 3, 32, 32]) 64 Zhang 3 passageway 32X32 Pictures of the
print(imgs.shape)
# torch.Size([64, 6, 30, 30]) 64 Zhang 6 passageway 30X30 Pictures of the
print(output.shape)
# Use tensorboard visualization Note that multiple images are to be used add_images instead of add_image
writer.add_images("input",imgs,step)
# because output yes 6 The number of channels cannot be displayed , Direct visualization will report an error , So we need to deal with output Conduct reshape reshape When a number is unknown in the second parameter of , You can fill in -1, He will automatically help you calculate , Why is it unknown ? Because I just don't know how much to fill , fill 64 I'm sure not , Then changing the number of channels is equivalent to cutting out the extra pixels
torch.reshape(output,(-1,3,30,30))
writer.add_images("output",output,step)
step+=1
writer.close()
Points in the code that need attention and explanation :
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
Be careful dataset in transform The parameter receives an object , So we need to add parentheses , In addition, when using neural network for operation, the data type required is tensor type , therefore transforms Parameters to add .
# Build a simple network
class Booze(nn.Module):
# Inherit nn.Module The initialization
def __init__(self):
super().__init__()
# Note that here is to create a global variable, so we need to add a self When out_channels Far greater than in_channels The original image needs to be expanded , That is to say padding The value of cannot be set to 0 了 , According to the formula
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
# rewrite forward function
def forward(self,x):
x = self.conv1(x)
return x
In building neural network inheritance nn.Module When initializing , Creating variables creates global variables , So you need to add a before the variable self.
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=(3),stride=1,padding=0)
Be careful When out_channels Far greater than in_channels The original image needs to be expanded , That is to say padding The value of cannot be set to 0 了 , It needs to be calculated according to the formula , The formula as follows :
Above picture input The meaning of the four elements in the following tuple :
- The first element represents batch_size
- The second element represents the number of image channels
- The third element represents the height of the image matrix
- The fourth element represents the width of the image matrix
for data in dataloader:
imgs,targets = data
output = obj(imgs)
# torch.Size([64, 3, 32, 32]) 64 Zhang 3 passageway 32X32 Pictures of the
print(imgs.shape)
# torch.Size([64, 6, 30, 30]) 64 Zhang 6 passageway 30X30 Pictures of the
print(output.shape)
# Use tensorboard visualization Note that multiple images are to be used add_images instead of add_image
writer.add_images("input",imgs,step)
torch.reshape(output,(-1,3,30,30))
writer.add_images("output",output,step)
step+=1
writer.close()
In the above code writer.add_images("output",output,step) Before running, you need to output Reduce the number of channels . because output yes 6 The number of channels cannot be displayed , Direct visualization will report an error , So we need to deal with output Conduct reshape .torch.reshape(output,(-1,3,30,30))reshape When a number is unknown in the second parameter of , You can fill in -1, He will automatically help you calculate , Why is it unknown ? Because I just don't know how much to fill , fill 64 I'm sure not , Then changing the number of channels is equivalent to cutting out the extra pixels .
Use... After the above code is run tensorboard See the effect :
As can be seen from the picture above output Every step More pictures than input Every step Number of pictures , The reason is that 6 Number of channels picture reshape become 3 The number of channels is caused by pictures batch_size An increase in .
边栏推荐
- Seven crimes of counting software R & D Efficiency
- 【FTP】FTP常用命令,持续更新中……
- Quelques outils dont les chiens scientifiques pourraient avoir besoin
- The design points of voice dialogue system and the importance of multi round dialogue
- 软件研发的十大浪费:研发效能的另一面
- [godot] unity's animator is different from Godot's animplayer
- js解决浮点数相乘精度丢失问题
- Leecode records the number of good segmentation of 1525 strings
- pytorch神经网络搭建 模板
- Leecode record 1351 negative numbers in statistical ordered matrix
猜你喜欢

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

2022 t elevator repair new version test questions and t elevator repair simulation test question bank

无器械健身

C read / write application configuration file app exe. Config and display it on the interface

Extension fragment

分布式锁的实现

手动实现一个简单的栈

2022 question bank and answers for safety production management personnel of hazardous chemical production units

pytorch中常用数据集的使用方法

分布式全局唯一ID解决方案详解
随机推荐
2022年上海市安全员C证考试题模拟考试题库及答案
2022危险化学品生产单位安全生产管理人员题库及答案
STM32 photoresistor sensor & two channel AD acquisition
Day 52 - tree problem
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
The longest increasing subsequence and its optimal solution, total animal weight problem
Construction of Meizhou nursing laboratory: equipment configuration
MySQL winter vacation self-study 2022 12 (5)
无器械健身
C#读写应用程序配置文件App.exe.config,并在界面上显示
Some tools that research dogs may need
测量三相永磁同步电机的交轴直轴电感
Why is Internet thinking not suitable for AI products?
STM32 光敏电阻传感器&两路AD采集
How to view the changes and opportunities in the construction of smart cities?
C read / write application configuration file app exe. Config and display it on the interface
Pytorch(二) —— 激活函数、损失函数及其梯度
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
Solve the problem that the external chain file of Qiankun sub application cannot be obtained