-
Notifications
You must be signed in to change notification settings - Fork 2
/
discriminator.py
64 lines (56 loc) · 1.78 KB
/
discriminator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import torch
import torch.nn as nn
import torchvision
class Blocks(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(Blocks, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU(0.2),
)
def forward(self, x):
return self.conv(x)
class Discriminator(nn.Module):
def __init__(self, in_channels, features):
super(Discriminator, self).__init__()
self.first_layer= nn.Sequential(
nn.Conv2d(in_channels, features, 3, 2 ,1),
nn.LeakyReLU(0.2),
)
self.Block1 = Blocks(features, features*2, stride=2)
self.Block2 = Blocks(features*2, features*2, stride=1)
self.Block3 = Blocks(features*2, features*4, stride=2)
self.Block4 = Blocks(features*4, features*4, stride=1)
self.Block5 = Blocks(features*4, features*8, stride=2)
self.Block6 = Blocks(features*8, features*8, stride=1)
self.Block7 = Blocks(features*8, features*8, stride=2)
self.Block8 = Blocks(features*8, features*8, stride=2)
self.Block9 = nn.Sequential(
nn.Conv2d(features*8, features*4, 3, 2, 1),
nn.LeakyReLU(0.2),
)
self.final_layer = nn.Sequential(
nn.Linear(features*4, 1),
nn.Sigmoid(),
)
def forward(self, x):
x = self.first_layer(x)
x = self.Block1(x)
x = self.Block2(x)
x = self.Block3(x)
x = self.Block4(x)
x = self.Block5(x)
x = self.Block6(x)
x = self.Block7(x)
x = self.Block8(x)
x = self.Block9(x)
x = x.view(x.size(0), -1)
return self.final_layer(x)
def test():
x = torch.randn(8,1,128,128)
disc = Discriminator(1,128)
out = disc(x)
print(out.shape)
if __name__ == "__main__":
test()