-
Notifications
You must be signed in to change notification settings - Fork 1
/
discriminator.py
61 lines (55 loc) · 1.91 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
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from unet_generator import DownSampleBlock
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.down1 = DownSampleBlock(6,64,False)
self.down2 = DownSampleBlock(64,128,True)
self.down3 = DownSampleBlock(128,256,True)
self.down4 = DownSampleBlock(256,512,True,stride=1, padding=1)
self.conv = nn.Conv2d(512, 1, 4,stride=1, padding=1, bias=False)
nn.init.normal_(self.conv.weight, mean=0.0, std=0.02)
# self.out = nn.Sigmoid()
def forward(self, input, target):
x = torch.cat([input,target],dim=1)
x =self.down1(x)
x= self.down2(x)
x = self.down3(x)
x = self.down4(x)
x = self.conv(x)
# x = self.out(x)
return x
class DownSampleBlockInstance(nn.Module):
def __init__(self, in_ch, out_ch, use_batchnorm=False, stride=2, padding=1):
super().__init__()
self.conv1 = nn.Conv2d(in_ch, out_ch, 4,stride=stride, padding=padding, bias=False)
nn.init.normal_(self.conv1.weight, mean=0.0, std=0.02)
self.bn = nn.InstanceNorm2d(out_ch) if use_batchnorm else None
self.relu = nn.LeakyReLU()
def forward(self, x):
x = self.conv1(x)
if self.bn:
x = self.bn(x)
x = self.relu(x)
return x
class DiscriminatorInstance(nn.Module):
def __init__(self):
super().__init__()
self.down1 = DownSampleBlockInstance(3,64,False)
self.down2 = DownSampleBlockInstance(64,128,True)
self.down3 = DownSampleBlockInstance(128,256,True)
self.down4 = DownSampleBlockInstance(256,512,True,stride=1, padding=1)
self.conv = nn.Conv2d(512, 1, 4,stride=1, padding=1, bias=False)
nn.init.normal_(self.conv.weight, mean=0.0, std=0.02)
# self.out = nn.Sigmoid()
def forward(self, input):
x =self.down1(input)
x= self.down2(x)
x = self.down3(x)
x = self.down4(x)
x = self.conv(x)
# x = self.out(x)
return x