| 34 | return x + self.block(x) |
| 35 | |
| 36 | class Generator(torch.nn.Module): |
| 37 | def __init__(self,img_channels,num_features = 64,num_residual=9): |
| 38 | super(Generator, self).__init__() |
| 39 | self.initial = torch.nn.Sequential( |
| 40 | torch.nn.Conv2d(in_channels=img_channels,out_channels=num_features,kernel_size=(7,7),stride=(1,1),padding=3,padding_mode='reflect'), |
| 41 | torch.nn.ReLU(inplace=True) |
| 42 | ) |
| 43 | self.down_blocks = torch.nn.ModuleList( |
| 44 | [ |
| 45 | ConvBlock(in_channels=num_features,out_channels=num_features*2,kernel_size=(3,3),stride=(2,2),padding=1), |
| 46 | ConvBlock(in_channels=num_features*2, out_channels=num_features * 4, kernel_size=(3, 3), stride=(2, 2), padding=1) |
| 47 | ] |
| 48 | ) |
| 49 | self.residual_blocks = torch.nn.Sequential( |
| 50 | *[ResidualBlock(num_features*4) for _ in range(num_residual)] |
| 51 | ) |
| 52 | self.up_blocks = torch.nn.ModuleList( |
| 53 | [ |
| 54 | ConvBlock(in_channels=num_features*4,out_channels=num_features*2,down=False,kernel_size=3,stride = 2,padding=1,output_padding=1), |
| 55 | ConvBlock(in_channels=num_features * 2, out_channels=num_features * 1, down=False, kernel_size=3, stride=2, padding=1,output_padding=1) |
| 56 | ] |
| 57 | ) |
| 58 | self.last = torch.nn.Sequential( |
| 59 | torch.nn.Conv2d(in_channels=num_features, out_channels=img_channels, kernel_size=(7, 7), stride=(1, 1), |
| 60 | padding=3, padding_mode='reflect'), |
| 61 | torch.nn.Tanh() |
| 62 | ) |
| 63 | def forward(self,x): |
| 64 | x = self.initial(x) |
| 65 | for layer in self.down_blocks: |
| 66 | x = layer(x) |
| 67 | x = self.residual_blocks(x) |
| 68 | for layer in self.up_blocks: |
| 69 | x = layer(x) |
| 70 | out = self.last(x) |
| 71 | return out |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | img_channels = 3 |
no outgoing calls
no test coverage detected