当前位置:网站首页>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 .
边栏推荐
- 2022年聚合工艺考试题及模拟考试
- C#读写应用程序配置文件App.exe.config,并在界面上显示
- Leecode question brushing record 1310 subarray XOR query
- Pytest automated testing - compare robotframework framework
- 分布式事务-解决方案
- Overview of the construction details of Meizhou veterinary laboratory
- Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
- Quelques outils dont les chiens scientifiques pourraient avoir besoin
- MySQL winter vacation self-study 2022 12 (5)
- 2022年上海市安全员C证考试题模拟考试题库及答案
猜你喜欢

Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

2022危险化学品生产单位安全生产管理人员题库及答案

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

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

OdeInt与GPU

分布式事务-解决方案

无器械健身

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and

2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
随机推荐
细数软件研发效能的七宗罪
Basic exercise of test questions hexadecimal to decimal
Solve the problem that the external chain file of Qiankun sub application cannot be obtained
pytorch中常用数据集的使用方法
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
洗个冷水澡吧
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
Dual contractual learning: text classification via label aware data augmentation reading notes
Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
Pytorch(三) —— 函数优化
Maixll dock quick start
RDF query language SPARQL
C#读写应用程序配置文件App.exe.config,并在界面上显示
Extension fragment
Thoughts on the construction of Meizhou cell room
C - detailed explanation of operators and summary of use cases
Leecode question brushing record 1310 subarray XOR query
Take a cold bath
2022危险化学品生产单位安全生产管理人员题库及答案
如何看待智慧城市建设中的改变和机遇?