-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodel.py
executable file
·99 lines (87 loc) · 2.99 KB
/
model.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
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
class Classifier(nn.Module):
def __init__(self, nc, ndf, nz):
super(Classifier, self).__init__()
self.nc = nc
self.ndf = ndf
self.nz = nz
self.encoder = nn.Sequential(
# input is (nc) x 64 x 64
nn.Conv2d(nc, ndf, 3, 1, 1),
nn.BatchNorm2d(ndf),
nn.MaxPool2d(2, 2, 0),
nn.ReLU(True),
# state size. (ndf) x 32 x 32
nn.Conv2d(ndf, ndf * 2, 3, 1, 1),
nn.BatchNorm2d(ndf * 2),
nn.MaxPool2d(2, 2, 0),
nn.ReLU(True),
# state size. (ndf*2) x 16 x 16
nn.Conv2d(ndf * 2, ndf * 4, 3, 1, 1),
nn.BatchNorm2d(ndf * 4),
nn.MaxPool2d(2, 2, 0),
nn.ReLU(True),
# state size. (ndf*4) x 8 x 8
nn.Conv2d(ndf * 4, ndf * 8, 3, 1, 1),
nn.BatchNorm2d(ndf * 8),
nn.MaxPool2d(2, 2, 0),
nn.ReLU(True),
# state size. (ndf*8) x 4 x 4
)
self.fc = nn.Sequential(
nn.Linear(ndf * 8 * 4 * 4, nz * 5),
nn.Dropout(0.5),
nn.Linear(nz * 5, nz),
)
def forward(self, x, release=False):
x = x.view(-1, 1, 64, 64)
x = self.encoder(x)
x = x.view(-1, self.ndf * 8 * 4 * 4)
x = self.fc(x)
if release:
return F.softmax(x, dim=1)
else:
return F.log_softmax(x, dim=1)
class Inversion(nn.Module):
def __init__(self, nc, ngf, nz, truncation, c):
super(Inversion, self).__init__()
self.nc = nc
self.ngf = ngf
self.nz = nz
self.truncation = truncation
self.c = c
self.decoder = nn.Sequential(
# input is Z
nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0),
nn.BatchNorm2d(ngf * 8),
nn.Tanh(),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1),
nn.BatchNorm2d(ngf * 4),
nn.Tanh(),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1),
nn.BatchNorm2d(ngf * 2),
nn.Tanh(),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1),
nn.BatchNorm2d(ngf),
nn.Tanh(),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d(ngf, nc, 4, 2, 1),
nn.Sigmoid()
# state size. (nc) x 64 x 64
)
def forward(self, x):
topk, indices = torch.topk(x, self.truncation)
topk = torch.clamp(torch.log(topk), min=-1000) + self.c
topk_min = topk.min(1, keepdim=True)[0]
topk = topk + F.relu(-topk_min)
x = torch.zeros(len(x), self.nz).cuda().scatter_(1, indices, topk)
x = x.view(-1, self.nz, 1, 1)
x = self.decoder(x)
x = x.view(-1, 1, 64, 64)
return x