卷积的计算——nn.Conv2d(Torch.nn里的Convolution Layers模块里的Conv2d类)

 **前置知识:

1、张量和通道

张量:多维数组,用来表示数据(图像、视频等)

通道:图像数据的一部分,表示不同的颜色或特征层

通道只是张量的其中一个维度

以一张RGB图像为例,

该图像数据可以用一个三维张量(shape等于(C,H,W))来表示

其中C表示通道数(对于RGB图像,C=3),H表示高度,W表示宽度

再以一批64张图像组成的数据对象来看,

它可以用一个四维张量(shape等于(N,C,H,W))来表示

其中N表示批次大小(一批次图像的数量)

关于通道的补充:

2、Reshape:输入数据和卷积核张量的重塑

input = torch.reshape(input, (1, 1, 5, 5))

  • 1第一个 1 表示有 1 张图片(批量大小)
  • 1第二个 1 表示输入的通道数(这里是单通道,表示灰度图像)
  • 55:分别表示图像的高度和宽度

kernel = torch.reshape(kernel, (1, 1, 3, 3))

  • 1第一个 1 表示有 1 个卷积核(输出通道数)
  • 1第二个 1 表示卷积核的输入通道数(与input的输入通道数匹配)
  • 33:分别表示卷积核的高度和宽度

通过重塑,增加的信息主要是关于批量大小和通道数,

这使得输入和卷积核符合 conv2d 函数的要求

(输入张量必须是四维的,形状为 (N, C, H, W),

卷积核(权重)必须是四维的,形状为 (out_channels, in_channels, kernel_height, kernel_width):)

补充:

一张2*2RGB图像与一个3*3卷积核的卷积:

reshape后:

input的形状是(1,3,2,2),1表示1张图片

卷积核的形状是(1,3,3,3),1表示1个输出通道

3、output=F.conv2d(input,kernel,stride=1,padding=1)

input输入张量(4维)
kernel卷积核(4维)
stride卷积核移动的步幅(可以是一个整数或元组,默认为 1)
padding在输入的边缘添加零填充(帮助保持输出的空间尺寸)

**代码:

步骤:

import torch.nn.functional as F

定义input、kernel——>修改成四维形状reshape——>使用conv2d进行卷积

灰度图像的卷积:帮助识别图像中的特定特征,如边缘或纹理

import torch
import torch.nn.functional as F

input=torch.tensor([
    [1,2,0,3,1],
    [0,1,2,3,1],
    [1,2,1,0,0],
    [5,2,3,1,1],
    [2,1,0,1,1]
])

#卷积核
kernel=torch.tensor([
    [1,2,1],
    [0,1,0],
    [2,1,0]
])

print(input.shape)
print(kernel.shape)

input=torch.reshape(input,(1,1,5,5))  #图片数,图层数(通道数),宽,高
kernel=torch.reshape(kernel,(1,1,3,3))

print(input.shape)
print(kernel.shape)

output1=F.conv2d(input,kernel,stride=1)
print(output1)

output2=F.conv2d(input,kernel,stride=2)
print(output2)

output3=F.conv2d(input,kernel,stride=1,padding=1)
print(output3)

class NLayerDiscriminator(nn.Module): def init(self, input_nc=3, ndf=64, n_layers=3, norm_layer=nn.BatchNorm2d, use_sigmoid=False, use_parallel=True): super(NLayerDiscriminator, self).init() self.use_parallel = use_parallel if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d self.conv1 = nn.Conv2d(input_nc, ndf, kernel_size=3, padding=1) self.conv_offset1 = nn.Conv2d(ndf, 18, kernel_size=3, stride=1, padding=1) init_offset1 = torch.Tensor(np.zeros([18, ndf, 3, 3])) self.conv_offset1.weight = torch.nn.Parameter(init_offset1) # 初始化为0 self.conv_mask1 = nn.Conv2d(ndf, 9, kernel_size=3, stride=1, padding=1) init_mask1 = torch.Tensor(np.zeros([9, ndf, 3, 3]) + np.array([0.5])) self.conv_mask1.weight = torch.nn.Parameter(init_mask1) # 初始化为0.5 kw = 4 padw = int(np.ceil((kw-1)/2)) nf_mult = 1 for n in range(1, n_layers): nf_mult_prev = nf_mult nf_mult = min(2n, 8) self.sequence2 = [ nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=2, padding=padw, bias=use_bias), norm_layer(ndf * nf_mult), nn.LeakyReLU(0.2, True) ] nf_mult_prev = nf_mult nf_mult = min(2n_layers, 8) self.sequence2 += [ nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=1, padding=padw, bias=use_bias), norm_layer(ndf * nf_mult), nn.LeakyReLU(0.2, True) ] self.sequence2 += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)] if use_sigmoid: self.sequence2 += [nn.Sigmoid()] def forward(self, input): input = self.conv1(input) offset1 = self.conv_offset1(input) mask1 = torch.sigmoid(self.conv_mask1(input)) sequence1 = [ torchvision.ops.deform_conv2d(input=input, offset=offset1, weight=self.conv1.weight, mask=mask1, padding=(1, 1)) ] sequence2 = sequence1 + self.sequence2 self.model = nn.Sequential(*sequence2) nn.LeakyReLU(0.2, True) return self.model(input),上述代码中:出现错误:torchvision.ops.deform_conv2d(input=input, offset=offset1,RuntimeError: Expected weight_c.size(1) * n_weight_grps == input_c.size(1) to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.)
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值