| 11 | from torchvision import transforms |
| 12 | |
| 13 | class Discriminator(torch.nn.Module): |
| 14 | def __init__(self,channels_img,features_d): |
| 15 | super(Discriminator, self).__init__() |
| 16 | self.disc = torch.nn.Sequential( |
| 17 | torch.nn.Conv2d( |
| 18 | in_channels=channels_img,out_channels=features_d,kernel_size=(4,4),stride=(2,2),padding=(1,1) |
| 19 | ), |
| 20 | torch.nn.LeakyReLU(negative_slope=0.2,inplace=True), |
| 21 | self._block(in_channels=features_d,out_channels=features_d * 2,kernel_size=(4,4),stride=(2,2), |
| 22 | padding=(1,1)), |
| 23 | self._block(in_channels=features_d * 2, out_channels=features_d * 4, kernel_size=(4, 4), stride=(2, 2), |
| 24 | padding=(1, 1)), |
| 25 | self._block(in_channels=features_d * 4, out_channels=features_d * 8, kernel_size=(4, 4), stride=(2, 2), |
| 26 | padding=(1, 1)), |
| 27 | torch.nn.Conv2d(in_channels=features_d*8,out_channels=1,kernel_size=(4,4),stride=(2,2),padding=0) |
| 28 | ) |
| 29 | def _block(self,in_channels,out_channels,kernel_size,stride,padding): |
| 30 | self.conv = torch.nn.Sequential( |
| 31 | torch.nn.Conv2d(in_channels=in_channels,out_channels=out_channels,kernel_size=kernel_size,stride=stride,padding=padding,bias=False), |
| 32 | #affine=True:一个布尔值,当设置为True时,该模块具有可学习的仿射参数,以与批量规范化相同的方式初始化。默认值:False。 |
| 33 | torch.nn.InstanceNorm2d(num_features=out_channels,affine=True), |
| 34 | torch.nn.LeakyReLU(negative_slope=0.2,inplace=True) |
| 35 | ) |
| 36 | return self.conv |
| 37 | def forward(self,input): |
| 38 | x = self.disc(input) |
| 39 | return x |
| 40 | |
| 41 | if __name__ == '__main__': |
| 42 | in_channles = 3 |