-
Notifications
You must be signed in to change notification settings - Fork 164
/
discriminator.py
70 lines (60 loc) · 2.81 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
65
66
67
68
69
70
import os
import torch.nn as nn
import torch.optim as optim
from base_networks import *
from torchvision.transforms import *
class Discriminator(nn.Module):
def __init__(self, num_channels, base_filter, image_size):
super(Discriminator, self).__init__()
self.image_size = image_size
self.input_conv = ConvBlock(num_channels, base_filter, 3, 1, 1, activation='lrelu', norm=None)
self.conv_blocks = nn.Sequential(
ConvBlock(base_filter, base_filter, 3, 2, 1, activation='lrelu', norm ='batch'),
ConvBlock(base_filter, base_filter * 2, 3, 1, 1, activation='lrelu', norm ='batch'),
ConvBlock(base_filter * 2, base_filter * 2, 3, 2, 1, activation='lrelu', norm ='batch'),
ConvBlock(base_filter * 2, base_filter * 4, 3, 1, 1, activation='lrelu', norm ='batch'),
ConvBlock(base_filter * 4, base_filter * 4, 3, 2, 1, activation='lrelu', norm ='batch'),
ConvBlock(base_filter * 4, base_filter * 8, 3, 1, 1, activation='lrelu', norm ='batch'),
ConvBlock(base_filter * 8, base_filter * 8, 3, 2, 1, activation='lrelu', norm ='batch'),
)
self.dense_layers = nn.Sequential(
DenseBlock(base_filter * 8 * image_size // 16 * image_size // 16, base_filter * 16, activation='lrelu',
norm=None),
DenseBlock(base_filter * 16, 1, activation='sigmoid', norm=None)
)
for m in self.modules():
classname = m.__class__.__name__
if classname.find('Conv2d') != -1:
torch.nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('ConvTranspose2d') != -1:
torch.nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, x):
out = self.input_conv(x)
out = self.conv_blocks(out)
out = out.view(out.size()[0], -1)
out = self.dense_layers(out)
return out
class FeatureExtractor(nn.Module):
def __init__(self, netVGG, feature_layer=[9,18,27,36]):
super(FeatureExtractor, self).__init__()
self.features = nn.Sequential(*list(netVGG.features.children()))
self.feature_layer = feature_layer
def forward(self, x):
results = []
for ii,model in enumerate(self.features):
if ii in self.feature_layer:
x = model(x)
results.append(x)
return results
class FeatureExtractorResnet(nn.Module):
def __init__(self, resnet):
super(FeatureExtractorResnet, self).__init__()
self.features = nn.Sequential(*list(resnet.children())[:-1])
def forward(self, x):
results = []
results.append(self.features(x))
return results