-
Notifications
You must be signed in to change notification settings - Fork 0
/
discriminator.py
32 lines (25 loc) · 1.24 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
import torch
import torch.nn as nn
class Discriminator(torch.nn.Module):
def __init__(self, channels):
super().__init__()
self.main_module = nn.Sequential(
nn.Conv2d(in_channels=channels, out_channels=256, kernel_size=4, stride=2, padding=1),
nn.InstanceNorm2d(256, affine=True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=4, stride=2, padding=1),
nn.InstanceNorm2d(512, affine=True),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=4, stride=2, padding=1),
nn.InstanceNorm2d(1024, affine=True),
nn.LeakyReLU(0.2, inplace=True))
self.output = nn.Sequential(
# The output of D is no longer a probability, we do not apply sigmoid at the output of D.
nn.Conv2d(in_channels=1024, out_channels=1, kernel_size=2, stride=1, padding=0, bias=False))
def forward(self, x):
x = self.main_module(x)
return self.output(x)
def feature_extraction(self, x):
# Use discriminator for feature extraction then flatten to vector of 16384
x = self.main_module(x)
return x.flatten(dim=1)