forked from z65451/SVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archs.py
116 lines (100 loc) · 4.05 KB
/
archs.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
import torch
from torch import nn
from torch.nn import functional as F
from torch.autograd import Variable
import numpy as np
class FC_Encoder(nn.Module):
def __init__(self, output_size):
super(FC_Encoder, self).__init__()
self.fc1 = nn.Linear(784, output_size)
def forward(self, x):
h1 = F.relu(self.fc1(x))
return h1
class FC_Decoder(nn.Module):
def __init__(self, embedding_size):
super(FC_Decoder, self).__init__()
self.fc3 = nn.Linear(embedding_size, 1024)
self.fc4 = nn.Linear(1024, 784)
def forward(self, z):
h3 = F.relu(self.fc3(z))
return torch.sigmoid(self.fc4(h3))
class CNN_Encoder(nn.Module):
def __init__(self, output_size, input_size=(1, 28, 28)):
super(CNN_Encoder, self).__init__()
self.input_size = input_size
self.channel_mult = 16
#convolutions
self.conv = nn.Sequential(
nn.Conv2d(in_channels=1,
out_channels=self.channel_mult*1,
kernel_size=4,
stride=1,
padding=1),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.channel_mult*1, self.channel_mult*2, 4, 2, 1),
nn.BatchNorm2d(self.channel_mult*2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.channel_mult*2, self.channel_mult*4, 4, 2, 1),
nn.BatchNorm2d(self.channel_mult*4),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.channel_mult*4, self.channel_mult*8, 4, 2, 1),
nn.BatchNorm2d(self.channel_mult*8),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(self.channel_mult*8, self.channel_mult*16, 3, 2, 1),
nn.BatchNorm2d(self.channel_mult*16),
nn.LeakyReLU(0.2, inplace=True)
)
self.flat_fts = self.get_flat_fts(self.conv)
self.linear = nn.Sequential(
nn.Linear(self.flat_fts, output_size),
nn.BatchNorm1d(output_size),
nn.LeakyReLU(0.2),
)
def get_flat_fts(self, fts):
f = fts(Variable(torch.ones(1, *self.input_size)))
return int(np.prod(f.size()[1:]))
def forward(self, x):
x = self.conv(x.view(-1, *self.input_size))
x = x.view(-1, self.flat_fts)
return self.linear(x)
class CNN_Decoder(nn.Module):
def __init__(self, embedding_size, input_size=(1, 28, 28)):
super(CNN_Decoder, self).__init__()
self.input_height = 224
self.input_width = 224
self.input_dim = embedding_size
self.channel_mult = 16
self.output_channels = 1
self.fc_output_dim = 512
self.fc = nn.Sequential(
nn.Linear(self.input_dim, self.fc_output_dim),
nn.BatchNorm1d(self.fc_output_dim),
nn.ReLU(True)
)
self.deconv = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(self.fc_output_dim, self.channel_mult*4,
4, 1, 0, bias=False),
nn.BatchNorm2d(self.channel_mult*4),
nn.ReLU(True),
# state size. self.channel_mult*32 x 4 x 4
nn.ConvTranspose2d(self.channel_mult*4, self.channel_mult*2,
3, 2, 1, bias=False),
nn.BatchNorm2d(self.channel_mult*2),
nn.ReLU(True),
# state size. self.channel_mult*16 x 7 x 7
nn.ConvTranspose2d(self.channel_mult*2, self.channel_mult*1,
4, 2, 1, bias=False),
nn.BatchNorm2d(self.channel_mult*1),
nn.ReLU(True),
# state size. self.channel_mult*8 x 14 x 14
nn.ConvTranspose2d(self.channel_mult*1, self.output_channels, 4, 2, 1, bias=False),
nn.Sigmoid()
# state size. self.output_channels x 28 x 28
)
def forward(self, x):
x = self.fc(x)
x = x.view(-1, self.fc_output_dim, 1, 1)
x = self.deconv(x)
return x.view(5,3,224,224)
# return x.view(-1, self.input_width*self.input_height)