-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_digit.py
133 lines (102 loc) · 3.59 KB
/
models_digit.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import torch
import torch.nn as nn
from torch.autograd import grad
class Extractor_Digit(nn.Module):
def __init__(self, in_channels=128, lrelu_slope=0.02):
super(Extractor_Digit, self).__init__()
self.lrelu_slope = lrelu_slope
self.in_channels = in_channels
self.extract = nn.Sequential(
nn.Conv2d(3, self.in_channels//16, 3),
nn.BatchNorm2d(self.in_channels//16),
nn.MaxPool2d(2),
nn.LeakyReLU(self.lrelu_slope),
nn.Conv2d(self.in_channels//16, self.in_channels//8, 3),
nn.BatchNorm2d(self.in_channels//8),
nn.MaxPool2d(2),
nn.LeakyReLU(self.lrelu_slope),
nn.Conv2d(self.in_channels//8, self.in_channels//4, 3),
nn.BatchNorm2d(self.in_channels//4),
nn.LeakyReLU(self.lrelu_slope)
)
def forward(self, x):
z = self.extract(x)
# print(z.shape)
z = z.view(-1, 32*3*3)
return z
class Classifier_Digit(nn.Module):
def __init__(self, class_num):
super(Classifier_Digit, self).__init__()
self.class_num = class_num
self.classify = nn.Sequential(
#nn.Linear(32*9*9, 100),
nn.Linear(32*3*3, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Linear(256, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Dropout(),
nn.Linear(64, self.class_num),
nn.Softmax(1)
)
def forward(self, x):
return self.classify(x)
class Discriminator_Digit(nn.Module):
''' Domain Discriminator '''
def __init__(self):
super(Discriminator_Digit, self).__init__()
self.classify = nn.Sequential(
#nn.Linear(32*9*9, 64),
nn.Linear(32*3*3, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Linear(64, 32),
nn.BatchNorm1d(32),
nn.ReLU(),
nn.Linear(32, 1)
)
def forward(self, x):
return self.classify(x)
class Relater_Digit(nn.Module):
''' Relater network used in WADA model '''
def __init__(self):
super(Relater_Digit, self).__init__()
self.distinguish = nn.Sequential(
#nn.Linear(32*9*9, 100),
nn.Linear(32*3*3, 100),
nn.BatchNorm1d(100),
nn.ReLU(),
nn.Linear(100, 32),
nn.Dropout(),
nn.ReLU(),
nn.Linear(32, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.distinguish(x)
def gradient_penalty(critic, h_s, h_t):
''' Gradeitnt penalty for Wasserstein GAN'''
alpha = torch.rand(h_s.size(0), 1).cuda()
differences = h_t - h_s
interpolates = h_s + (alpha * differences)
interpolates = torch.cat([interpolates, h_s, h_t]).requires_grad_()
# interpolates.requires_grad_()
preds = critic(interpolates)
gradients = grad(preds, interpolates,
grad_outputs=torch.ones_like(preds),
retain_graph=True, create_graph=True)[0]
gradient_norm = gradients.norm(2, dim=1)
gradient_penalty = ((gradient_norm - 1)**2).mean()
return gradient_penalty
def set_requires_grad(model, requires_grad=True):
for param in model.parameters():
param.requires_grad = requires_grad
def EntropyLoss(input_):
mask = input_.ge(0.000001)
mask_out = torch.masked_select(input_, mask)
entropy = -(torch.sum(mask_out * torch.log(mask_out)))
return entropy / float(input_.size(0))