Conv2d详细
- 查询官网https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d
- API
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
- 参数
1. in_channels (int) – 输入图像中的通道数
2. out_channels (int) - 卷积产生的通道数
3. kernel_size (int or tuple) – 卷积内核的大小
4. stride (int 或 tuple,可选) - 卷积的步幅。默认值:1
5. padding (int, tuple or str, 可选) – 添加到输入的所有四个边的填充。默认值:0
6. padding_mode (str, 可选) - 'zeros','reflect','circular'。默认值:'zeros'
7. dilation (int or tuple, 可选) - 内核元素之间的间距。默认值:1
8. groups (int, 可选) - 从输入通道到输出通道的阻塞连接数。默认值:1
9. bias (bool, 可选) - 如果 `True` ,则在输出中添加可学习的偏差。默认: `True`
- 官网直观图片网址:https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
- 代码示例
'''
原图片: torch.Size([64, 3, 32, 32])
卷积后图片: torch.Size([64, 6, 30, 30])
'''
import torch
import torchvision
from torch.utils.data import DataLoader
import torch.nn as nn
from torch.nn import Conv2d
dataset = torchvision.datasets.CIFAR10("./datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)
class conv_test(nn.Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=3,stride=1,padding=0)
def forward(self,x):
x = self.conv1(x)
return x
test1 = conv_test()
for data in dataloader:
imgs,targets = data
output = test1(imgs)
print("原图片:",imgs.shape)
print("卷积后图片:",output.shape)