| 9 | from torchinfo import summary |
| 10 | |
| 11 | class ConvBlock(torch.nn.Module): |
| 12 | def __init__(self,in_channels,out_channels, |
| 13 | discriminator = False,use_act = True, |
| 14 | use_bn = True,**kwargs): |
| 15 | super(ConvBlock, self).__init__() |
| 16 | self.use_act = use_act |
| 17 | self.cnn = torch.nn.Conv2d(in_channels,out_channels,**kwargs,bias=not use_bn) |
| 18 | #Identity()表示输入是什么输出就是什么 |
| 19 | #要加深网络,有些层是不改变输入数据的维度的, |
| 20 | #在增减网络的过程中我们就可以用identity占个位置,这样网络整体层数永远不变, |
| 21 | self.bn = torch.nn.BatchNorm2d(out_channels) if use_bn else torch.nn.Identity() |
| 22 | #对于generator使用PReLU |
| 23 | #对于discriminator使用LeakReLU |
| 24 | self.act = ( |
| 25 | torch.nn.LeakyReLU(negative_slope=0.2,inplace=True) |
| 26 | if discriminator else torch.nn.PReLU(num_parameters=out_channels) |
| 27 | ) |
| 28 | def forward(self,x): |
| 29 | out = self.act(self.bn(self.cnn(x))) if self.use_act else self.bn(self.cnn(x)) |
| 30 | return out |
| 31 | |
| 32 | #采用PixelShuffle进行上采样 |
| 33 | class UpsampleBlock(torch.nn.Module): |