-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
101 lines (93 loc) · 3.73 KB
/
models.py
File metadata and controls
101 lines (93 loc) · 3.73 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class AE(nn.Module):
def __init__(self, latent_size, multiplier=4, unc=False, img_size=64, vae=False):
super(AE, self).__init__()
out_channels = 2 if unc else 1
self.fm = img_size // 16
self.unc = unc
self.mp = multiplier
self.encoder = nn.Sequential(
nn.Conv2d(1, int(16 * multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(16 * multiplier)),
nn.ReLU(True),
nn.Conv2d(int(16 * multiplier),
int(32 * multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(32 * multiplier)),
nn.ReLU(True),
nn.Conv2d(int(32 * multiplier),
int(64 * multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(64 * multiplier)),
nn.ReLU(True),
nn.Conv2d(int(64 * multiplier),
int(64 * multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(64 * multiplier)),
nn.ReLU(True),
)
if not vae:
self.linear_enc = nn.Sequential(
nn.Linear(int(64 * multiplier) * self.fm*self.fm, 2048),
nn.BatchNorm1d(2048),
nn.ReLU(True),
nn.Linear(2048, latent_size),
)
else:
self.linear_enc = nn.Sequential(
nn.Linear(int(64 * multiplier) * self.fm*self.fm, 2048),
nn.BatchNorm1d(2048),
nn.ReLU(True),
nn.Linear(2048, latent_size * 2),
)
self.linear_dec = nn.Sequential(
nn.Linear(latent_size, 2048),
nn.BatchNorm1d(2048),
nn.ReLU(True),
nn.Linear(2048, int(64 * multiplier) * self.fm*self.fm),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(int(64*multiplier), int(64 *
multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(64*multiplier)),
nn.ReLU(True),
nn.ConvTranspose2d(int(64*multiplier), int(32 *
multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(32*multiplier)),
nn.ReLU(True),
nn.ConvTranspose2d(int(32*multiplier), int(16 *
multiplier), 4, 2, 1, bias=False),
nn.BatchNorm2d(int(16*multiplier)),
nn.ReLU(True),
nn.ConvTranspose2d(int(16*multiplier),
out_channels, 4, 2, 1, bias=False),
)
# self.initialize()
def initialize(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.LayerNorm):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
nn.init.constant_(m.bias, 0)
def forward(self, x):
lat_rep = self.feature(x)
out = self.decode(lat_rep)
return out
def feature(self, x):
lat_rep = self.encoder(x)
lat_rep = lat_rep.view(lat_rep.size(0), -1)
lat_rep = self.linear_enc(lat_rep)
return lat_rep
def decode(self, x):
out = self.linear_dec(x)
out = out.view(out.size(0), int(64 * self.mp), self.fm, self.fm)
out = self.decoder(out)
if not self.unc:
return out
else:
return out.chunk(2, 1)