-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
134 lines (117 loc) · 4.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
try:
import cv2
import torch
except:
pass
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable as V
from utils import initialize_weights
from preprocess import idols
class Encoder(nn.Module):
def __init__(self, gpu=False):
super(Encoder, self).__init__()
self.gpu = gpu
self.channel = 3
self.c_dim = len(idols)
self.width = 64
self.height = 64
self.flat = 512 * (self.width // 16) * (self.height // 16)
conv_seq = [
nn.Conv2d(self.channel + self.c_dim, 64, 4, 2, 1),
nn.LeakyReLU(0.2),
nn.Dropout2d(),
nn.Conv2d(64, 128, 4, 2, 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2),
nn.Dropout2d(),
nn.Conv2d(128, 256, 4, 2, 1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2),
nn.Dropout2d(),
nn.Conv2d(256, 512, 4, 2, 1),
]
fc_seq = [
nn.Linear(self.flat, 1024),
nn.BatchNorm1d(1024),
nn.Tanh(),
]
conv_seq = [m.cuda() for m in conv_seq] if self.gpu else conv_seq
fc_seq = [m.cuda() for m in fc_seq] if self.gpu else fc_seq
self.conv = nn.Sequential(*conv_seq)
self.fc = nn.Sequential(*fc_seq)
self.mu = nn.Linear(1024, 1024)
self.ls = nn.Linear(1024, 1024)
self.mu = self.mu.cuda() if self.gpu else self.mu
self.ls = self.ls.cuda() if self.gpu else self.ls
initialize_weights(self)
def forward(self, x, c):
c = c.repeat(1, self.width * self.height).view(-1, self.c_dim, self.width, self.height)
out = self.conv(torch.cat([x, c], 1))
out = out.view(-1, self.flat)
out = self.fc(out)
return self.mu(out), self.ls(out)
class Decoder(nn.Module):
def __init__(self, gpu=False):
super(Decoder, self).__init__()
self.gpu = gpu
self.channel = 3
self.c_dim = len(idols)
self.width = 64
self.height = 64
self.flat = 512 * (self.width // 16) * (self.height // 16)
fc_seq = [
nn.Linear(1024 + self.c_dim, self.flat),
nn.BatchNorm1d(self.flat),
nn.Tanh(),
]
deconv_seq = [
nn.ConvTranspose2d(512, 256, 4, 2, 1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2),
nn.Dropout2d(),
nn.ConvTranspose2d(256, 128, 4, 2, 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2),
nn.Dropout2d(),
nn.ConvTranspose2d(128, 64, 4, 2, 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2),
nn.Dropout2d(),
nn.ConvTranspose2d(64, self.channel, 4, 2, 1),
nn.Sigmoid(),
]
fc_seq = [m.cuda() for m in fc_seq] if self.gpu else fc_seq
deconv_seq = [m.cuda() for m in deconv_seq] if self.gpu else deconv_seq
self.fc = nn.Sequential(*fc_seq)
self.deconv = nn.Sequential(*deconv_seq)
initialize_weights(self)
def forward(self, z, c):
out = self.fc(torch.cat([z, c], 1)).view(-1, 512, self.width // 16, self.height // 16)
out = self.deconv(out)
return out
class Love2Live(nn.Module):
def __init__(self, gpu=False):
super(Love2Live, self).__init__()
self.gpu = gpu
self.encoder = Encoder(gpu=self.gpu)
self.decoder = Decoder(gpu=self.gpu)
def sample(self, mu, ls):
eps = V(torch.randn(mu.size()), requires_grad=False)
if self.gpu:
eps = eps.cuda()
return mu + (ls / 2).exp() * eps
def forward(self, x, c):
mu, ls = self.encoder(x, c)
z = self.sample(mu, ls)
return self.decoder(z, c)
def predict(self, c):
batch_size = c.size()[0]
mu = V(torch.zeros(batch_size, 1024), requires_grad=False)
ls = V(torch.zeros(batch_size, 1024), requires_grad=False)
mu = mu.cuda() if self.gpu else mu
ls = ls.cuda() if self.gpu else ls
z = self.sample(mu, ls)
return self.decoder(z, c)