-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
84 lines (68 loc) · 2.52 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
import torch.nn as nn
import torch
class Autoencodermodel(nn.Module):
def __init__(self):
super(Autoencodermodel, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(256, 200, kernel_size=3),
nn.ReLU(),
GroupNorm(200),
nn.Conv2d(200, 150, kernel_size=3,stride=2),
nn.ReLU(),
GroupNorm(150,num_groups=30),
nn.Conv2d(150,100,kernel_size=3),
nn.ReLU(),
GroupNorm(100,num_groups=10),
nn.Conv2d(100,70,kernel_size=2),
nn.ReLU(),
GroupNorm(70,num_groups=10),
nn.Conv2d(70,60,kernel_size=2),
nn.ReLU(),
GroupNorm(60,num_groups=20),
nn.Conv2d(60, 50, kernel_size=1),
nn.Tanh()
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(50, 150, kernel_size=5),
nn.ReLU(),
nn.ConvTranspose2d(150, 200, kernel_size=4, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(200, 256, kernel_size=3),
nn.Tanh()
)
self.img_decoder = nn.Sequential(
nn.ConvTranspose2d(256, 200, kernel_size=5, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(200, 180, kernel_size=3, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(180, 150, kernel_size=3, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(150, 128, kernel_size=2),
nn.ReLU(),
nn.ConvTranspose2d(128, 3, kernel_size=1),
nn.Sigmoid(),
)
def forward(self, x):
## learns the data representation from input
z = self.encoder(x)
## reconstruct the data based on the learned data representation
y = self.decoder(z)
# # reconstruct the images based on the learned data representation
img = self.img_decoder(y)
return z,y,img
class GroupNorm(nn.Module):
def __init__(self, num_features, num_groups=32, eps=1e-5):
super(GroupNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(1,num_features,1,1))
self.beta = nn.Parameter(torch.zeros(1,num_features,1,1))
self.num_groups = num_groups
self.eps = eps
def forward(self, x):
N, C, H, W = x.size()
x = x.view(N, self.num_groups ,-1)
mean = x.mean(-1, keepdim=True)
var = x.var(-1, keepdim=True)
# normalize
x = (x-mean) / (var+self.eps).sqrt()
x = x.view(N,C,H,W)
return x * self.gamma + self.beta