<强> 1,其中再语义分割比较常用的上采样:强>
其实现方法为:
def upconv2x2 (in_channels out_channels模式=白谩?: 如果模式==' ' ': #这个上采用需要设置其输入通道,输出通道。其中kernel_size,跨步 #大小要跟对应下采样设置的值一样大小。这样才可恢复到相同的wh。这里时反卷积操作。 返回nn.ConvTranspose2d ( in_channels, out_channels, kernel_size=2, 步=2) 其他: # out_channels总是相同的 #作为in_channels #这里不会改变通道数,其中scale_factor是上采用的放大因子,其是相对于当前的 #输入大小的倍数 返回nn.Sequential ( 神经网络。Upsample(模式=双线性,scale_factor=2, align_corners=True)) #这里的代码是在这里设置多一个卷积,这样子就起到了可以修改其输出通道的功能了。 #相当于功能跟ConvTranspose2d()差不多,只是上采样的方法不同 conv1x1 ((in_channels out_channels)) def conv1x1 (in_channels out_channels、组=1): 返回nn.Sequential (nn.Conv2d ( in_channels, out_channels, kernel_size=1, 组=组, 步=1), nn.BatchNorm2d (out_channels))
<强>另一种上采样的方法是,参考代码:segnet_pytorch: 强>
#第五阶段 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记录的是池化操作时最大值的指数,其要设置参数return_indices为真实的 x5p id5=F。max_pool2d (x53 kernel_size=2,步=2,return_indices=True) # 5 d阶段 #这个是进行最大值上采样的函数,其是根据id5来把值放到什么位置,其它位置没有值的地方 补0 x5d=F。max_unpool2d (x5p id5 kernel_size=2,步=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)步=(1,1),return_indices=True) 芬欧蓝=nn.MaxUnpool2d((3、3)步=(1,1)) data4=torch.randn (1, 1, 3, 3) output5指数=m (data4) output6=芬欧蓝(output5指数) data4打印(' \ ndata4: ', ‘\ nmaxPool2d’, output5, “\ nindices:”,指标, ' \ noutput6:“, output6)
<强>其输出为:强>
data4:张量([[[(2.3151,-1.0391,0.1074), (1.9360,0.2524,2.3735), [-0.1151,0.4684,-1.8800]]]]) maxPool2d张量([[[[2.3735]]]]) 指标:张量([[[[5]]]]) output6:张量([[[(0.0000,0.0000,0.0000), (0.0000,0.0000,2.3735), [0.0000,0.0000,0.0000]]]])
以上这篇pytorch进行上采样的种类实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。