pytorch进行上采样的种类实例

Jade ·
更新时间:2024-09-21
· 742 次阅读

1、其中再语义分割比较常用的上采样:

其实现方法为:

def upconv2x2(in_channels, out_channels, mode='transpose'): if mode == 'transpose': # 这个上采用需要设置其输入通道,输出通道.其中kernel_size、stride # 大小要跟对应下采样设置的值一样大小。这样才可恢复到相同的wh。这里时反卷积操作。 return nn.ConvTranspose2d( in_channels, out_channels, kernel_size=2, stride=2) else: # out_channels is always going to be the same # as in_channels # 这里不会改变通道数,其中scale_factor是上采用的放大因子,其是相对于当前的 # 输入大小的倍数 return nn.Sequential( nn.Upsample(mode='bilinear', scale_factor=2, align_corners=True)) # 这里的代码是在这里设置多一个卷积,这样子就起到了可以修改其输出通道的功能了。 # 相当于功能跟ConvTranspose2d()差不多,只是上采样的方法不同 conv1x1((in_channels, out_channels)) def conv1x1(in_channels, out_channels, groups=1): return nn.Sequential(nn.Conv2d( in_channels, out_channels, kernel_size=1, groups=groups, stride=1), nn.BatchNorm2d(out_channels))

另一种上采样的方法是,参考代码:segnet_pytorch:

# Stage 5 x51 = F.relu(self.bn51(self.conv51(x4p))) x52 = F.relu(self.bn52(self.conv52(x51))) x53 = F.relu(self.bn53(self.conv53(x52))) #这个id5记录的是池化操作时最大值的index,其要设置参数return_indices为True x5p, id5 = F.max_pool2d(x53,kernel_size=2, stride=2,return_indices=True) # Stage 5d #这个是进行最大值上采样的函数,其是根据id5来把值放到什么位置,其它位置没有值的地方 补0 x5d = F.max_unpool2d(x5p, id5, kernel_size=2, stride=2) x53d = F.relu(self.bn53d(self.conv53d(x5d))) x52d = F.relu(self.bn52d(self.conv52d(x53d))) x51d = F.relu(self.bn51d(self.conv51d(x52d)))

测试例子:

#测试上采样 m=nn.MaxPool2d((3,3),stride=(1,1),return_indices=True) upm=nn.MaxUnpool2d((3,3),stride=(1,1)) data4=torch.randn(1,1,3,3) output5,indices=m(data4) output6=upm(output5,indices) print('\ndata4:',data4, '\nmaxPool2d',output5, '\nindices:',indices, '\noutput6:',output6)

其输出为:

data4: tensor([[[[ 2.3151, -1.0391, 0.1074], [ 1.9360, 0.2524, 2.3735], [-0.1151, 0.4684, -1.8800]]]]) maxPool2d tensor([[[[2.3735]]]]) indices: tensor([[[[5]]]]) output6: tensor([[[[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 2.3735], [0.0000, 0.0000, 0.0000]]]])

以上这篇pytorch进行上采样的种类实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。

您可能感兴趣的文章:pytorch中的上采样以及各种反操作,求逆操作详解pytorch 自定义数据集加载方法



pytorch 采样 上采样

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章