-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
112 lines (95 loc) · 3.88 KB
/
models.py
File metadata and controls
112 lines (95 loc) · 3.88 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
102
103
104
105
106
107
108
109
110
111
import torch
import torch.nn as nn
import torch.cuda
# Tensor resize
class View(nn.Module):
def __init__(self, size):
super(View, self).__init__()
self.size = size
def forward(self, tensor):
return tensor.view(self.size)
# Generator, including STI and UIR
class Generator(nn.Module):
def __init__(self, channelNum=3, zDim=256, classNum=10):
super(Generator, self).__init__()
self.cnn = nn.Sequential(
nn.Conv2d(channelNum, 128, 4, 2, 1, bias=False), # B, 128, 32, 32; stride=2, each side decrease by 4
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.Conv2d(128, 256, 4, 2, 1, bias=False), # B, 256, 16, 16
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.Conv2d(256, 512, 4, 2, 1, bias=False), # B, 512, 8, 8
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.Conv2d(512, 1024, 4, 2, 1, bias=False), # B, 1024, 4, 4
nn.BatchNorm2d(1024),
nn.ReLU(True),
View((-1, 1024 * 2 * 2)), # B, 1024*4*4
)
self.fc_mu = nn.Linear(1024 * 2 * 2, zDim) # B, z_dim
self.fc_logvar = nn.Linear(1024 * 2 * 2, zDim) # B, z_dim
# STI decoder is a classifier for classification, on CIFAR10 dataset
self.sti = nn.Sequential(
nn.Linear(zDim, 1024 * 4 * 4), # B, 1024*8*8
View((-1, 1024, 4, 4)), # B, 1024, 8, 8
nn.Flatten(), # Flatten all dimension except batch
nn.Linear(1024 * 4 * 4, 120),
nn.ReLU(True),
nn.Linear(120, 84),
nn.ReLU(True),
nn.Linear(84, classNum),
)
# UIR decoder reconstruct image from latent variables
self.uir = nn.Sequential(
nn.Linear(zDim, 1024 * 4 * 4), # B, 1024*8*8
View((-1, 1024, 4, 4)), # B, 1024, 8, 8
nn.ConvTranspose2d(1024, 512, 4, 2, 1, bias=False), # B, 512, 16, 16
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False), # B, 256, 32, 32
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False), # B, 128, 64, 64
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, channelNum, 1), # B, nc, 64, 64
)
def reparameterize(self, mu, logvar):
stds = (0.5 * logvar).exp()
epsilon = torch.randn(*mu.size())
if mu.is_cuda:
stds, epsilon = stds.cuda(), epsilon.cuda()
latents = epsilon * stds + mu
return latents
def forward(self, x):
z = self.cnn(x)
mu, logvar = self.fc_mu(z), self.fc_logvar(z)
z = self.reparameterize(mu, logvar) # latent variables, rich representation
pred = self.sti(z) # STI output predictions
recon = self.uir(z) # UIR output reconstructions
return pred, recon, z, mu, logvar
# Label state discriminator
class StateDiscriminator(nn.Module):
# Grab from Pytorch DCGAN example
# Discriminator learns to differentiate labeled/unlabeled data
def __init__(self, z_dim=10):
super(StateDiscriminator, self).__init__()
self.z_dim = z_dim
self.net = nn.Sequential(
nn.Linear(z_dim, 512),
nn.ReLU(True),
nn.Linear(512, 512),
nn.ReLU(True),
nn.Linear(512, 1),
nn.Sigmoid()
)
def forward(self, z):
return self.net(z) # output a single value for state loss, model state of samples
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)