-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
121 lines (98 loc) · 4.69 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
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
117
118
119
120
121
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 11 21:17:20 2021
@author: Shiyu
"""
import torch
from torch import nn, optim
from torch.nn import functional as F
from disvae.utils.initialization import weights_init
# from torch.distributions.gumbel import Gumbel
class ControlVAE(nn.Module):
def __init__(self, img_size, encoder, decoder, latent_dim, num_prop):
"""
Class which defines model and forward pass.
Parameters
----------
img_size : tuple of ints
Size of images. E.g. (1, 32, 32) or (3, 64, 64).
encoder: encoder
decoder: decoder
latent_dim: latent dimension
num_prop: number of properties
"""
super(ControlVAE, self).__init__()
if list(img_size[1:]) not in [[32, 32], [64, 64]]:
raise RuntimeError("{} sized images not supported. Only (None, 32, 32) and (None, 64, 64) supported. Build your own architecture or reshape images!".format(img_size))
# Number of properties
self.num_prop=num_prop
self.latent_dim = latent_dim
self.img_size = img_size
self.num_pixels = self.img_size[1] * self.img_size[2]
self.encoder = encoder(img_size, self.latent_dim, self.latent_dim)
self.decoder = decoder(img_size, self.latent_dim, self.latent_dim, self.num_prop)
self.reset_parameters()
self.w_mask = torch.nn.Parameter(torch.randn(self.num_prop, self.latent_dim, 2))
# self.w_mask = torch.eye(3).cuda()
def reparameterize(self, mean, logvar):
"""
Samples from a normal distribution using the reparameterization trick.
Parameters
----------
mean : torch.Tensor
Mean of the normal distribution. Shape (batch_size, latent_dim)
logvar : torch.Tensor
Diagonal log variance of the normal distribution. Shape (batch_size,
latent_dim)
"""
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mean + std * eps
def forward(self, x, tau, mask = None, w2=None, w_mask=None, label=None):
"""
Forward pass of model.
Parameters
----------
x : torch.Tensor
Batch of data. Shape (batch_size, n_chan, height, width)
"""
# if self.training:
latent_dist_z_mean,latent_dist_w_mean,latent_dist_z_std,latent_dist_w_std = self.encoder(x,label) #for training process
# else:
# latent_dist_z_mean,latent_dist_w_mean,latent_dist_z_std,latent_dist_w_std,p_pred = self.encoder(x,label) #for testing process
latent_sample_z = self.reparameterize(latent_dist_z_mean,latent_dist_z_std)
latent_sample_w = self.reparameterize(latent_dist_w_mean,latent_dist_w_std)
if w2 != None:
latent_sample_w = w2.repeat(latent_sample_z.shape[0], 1)
if mask == None:
logit = torch.sigmoid(self.w_mask) / (1 - torch.sigmoid(self.w_mask))
mask = F.gumbel_softmax(logit.cuda(), tau, hard=True)[:, :, 1]
reconstruct,y_reconstruct,wp = self.decoder(latent_sample_z,latent_sample_w,mask)
latent_dist_z=(latent_dist_z_mean,latent_dist_z_std)
latent_dist_w=(latent_dist_w_mean,latent_dist_w_std)
return (reconstruct,y_reconstruct), latent_dist_z, latent_dist_w, latent_sample_z,latent_sample_w, mask, self.w_mask
def reset_parameters(self):
self.apply(weights_init)
def sample_latent(self, x,p=None):
"""
Returns a sample from the latent distribution.
Parameters
----------
x : torch.Tensor
Batch of data. Shape (batch_size, n_chan, height, width)
"""
latent_dist_z_mean,latent_dist_w_mean,latent_dist_z_std,latent_dist_w_std,p_pred = self.encoder(x,p)
latent_dist_z=(latent_dist_z_mean,latent_dist_z_std)
latent_dist_w=(latent_dist_w_mean,latent_dist_w_std)
latent_sample_z = self.reparameterize(*latent_dist_z)
latent_sample_w = self.reparameterize(*latent_dist_w)
return latent_sample_z, latent_sample_w
def iterate_get_w(self,label,w_latent_idx, maxIter=20):
#get the w for a kind of given property
w_n=label.view(-1,1).to('cuda').float()#[N]
for iter_index in range(maxIter):
summand = self.decoder.property_lin_list[w_latent_idx](w_n)
w_n1 = label.view(-1,1).to('cuda').float() - summand
print('Iteration of difference:'+str(torch.abs(w_n-w_n1).mean().item()))
w_n=w_n1.clone()
return w_n1.view(-1)