forked from CoinCheung/triplet-reid-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoencoders.py
88 lines (70 loc) · 3.15 KB
/
autoencoders.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
from torch.utils import data
import torch.nn as nn
from torch.autograd import Variable
from torch.nn import functional as F
from decoder import Decoder
class VAE(nn.Module):
def __init__(self, input_size=2048, fc_hidden1=1536, fc_hidden2=1024, drop_p=0.3, CNN_embed_dim=728, orig_height=256, orig_width=128):
super(VAE, self).__init__()
self.input_size = input_size
self.fc_hidden1 = fc_hidden1
self.fc_hidden2 = fc_hidden2
self.CNN_embed_dim = CNN_embed_dim
self.drop_p = drop_p
self.orig_height = orig_height
self.orig_width = orig_width
self.encoder = nn.Sequential(
nn.Linear(self.input_size, self.fc_hidden1),
nn.BatchNorm1d(self.fc_hidden1, momentum=0.01),
nn.ReLU(),
nn.Linear(self.fc_hidden1, self.fc_hidden2),
nn.BatchNorm1d(self.fc_hidden2, momentum=0.01),
nn.ReLU()
)
# Latent vectors mu and sigma
self.fc_mu = nn.Linear(self.fc_hidden2, self.CNN_embed_dim) # output = CNN embedding latent variables
self.fc_logvar = nn.Linear(self.fc_hidden2, self.CNN_embed_dim) # output = CNN embedding latent variables
self.decoder = Decoder(CNN_embed_dim, fc_hidden2)
def reparameterize(self, mu, logvar):
if self.training:
std = logvar.mul(0.5).exp_()
eps = Variable(std.data.new(std.size()).normal_())
return eps.mul(std).add_(mu)
else:
return mu
def forward(self, x):
x = self.encoder(x)
if self.drop_p and self.training:
x = F.dropout(x, p=self.drop_p, training=self.training)
mu, logvar = self.fc_mu(x), self.fc_logvar(x)
x = self.reparameterize(mu, logvar)
x_reconst = self.decoder(x, out_size=(self.orig_height, self.orig_width))
return x_reconst, x, mu, logvar
class AE(nn.Module):
def __init__(self, input_size=2048, fc_hidden1=1536, fc_hidden2=1024, drop_p=0.3, CNN_embed_dim=728, orig_height=256, orig_width=128):
super(AE, self).__init__()
self.input_size = input_size
self.fc_hidden1 = fc_hidden1
self.fc_hidden2 = fc_hidden2
self.CNN_embed_dim = CNN_embed_dim
self.drop_p = drop_p
self.orig_height = orig_height
self.orig_width = orig_width
self.encoder = nn.Sequential(
nn.Linear(self.input_size, self.fc_hidden1),
nn.BatchNorm1d(self.fc_hidden1, momentum=0.01),
nn.ReLU(),
nn.Linear(self.fc_hidden1, self.fc_hidden2),
nn.BatchNorm1d(self.fc_hidden2, momentum=0.01),
nn.ReLU(),
nn.Linear(self.fc_hidden2, self.CNN_embed_dim),
nn.BatchNorm1d(self.CNN_embed_dim, momentum=0.01),
nn.ReLU()
)
self.decoder = Decoder(CNN_embed_dim, fc_hidden2)
def forward(self, x):
x = self.encoder(x)
if self.drop_p and self.training:
x = F.dropout(x, p=self.drop_p, training=self.training)
x_reconst = self.decoder(x, out_size=(self.orig_height, self.orig_width))
return x_reconst, x