| 26 | return out |
| 27 | |
| 28 | class Discriminator(torch.nn.Module): |
| 29 | def __init__(self, in_channels=3,features=(64,128,256,512)): |
| 30 | super(Discriminator, self).__init__() |
| 31 | self.features = features |
| 32 | self.initial = torch.nn.Sequential( |
| 33 | torch.nn.Conv2d(in_channels=in_channels, out_channels=features[0], kernel_size=(4, 4), |
| 34 | stride=(2,2), padding=1, bias=True, padding_mode='reflect'), |
| 35 | torch.nn.BatchNorm2d(num_features=features[0]), |
| 36 | torch.nn.LeakyReLU(negative_slope=0.2, inplace=True) |
| 37 | ) |
| 38 | layers = [] |
| 39 | in_channels=features[0] |
| 40 | for feature in features[1:]: |
| 41 | layers.append( |
| 42 | Block(in_channels,feature,stride=1 if feature == features[-1] else 2) |
| 43 | ) |
| 44 | in_channels=feature |
| 45 | layers.append(torch.nn.Conv2d(in_channels=in_channels,out_channels=1,kernel_size=(4,4), |
| 46 | stride=(1,1),padding=1,padding_mode='reflect')) |
| 47 | #将值归一化到[0-1] |
| 48 | layers.append(torch.nn.Sigmoid()) |
| 49 | #对layers进行解序列 |
| 50 | self.model = torch.nn.Sequential( |
| 51 | *layers |
| 52 | ) |
| 53 | def forward(self,x): |
| 54 | x = self.initial(x) |
| 55 | out= self.model(x) |
| 56 | return out |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | x = torch.randn(size = (5,3,256,256),device='cpu') |
no outgoing calls
no test coverage detected