-
Notifications
You must be signed in to change notification settings - Fork 54
/
models.py
82 lines (69 loc) · 2.75 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class Generator(nn.Module):
def __init__(self, img_size, latent_dim, dim):
super(Generator, self).__init__()
self.dim = dim
self.latent_dim = latent_dim
self.img_size = img_size
self.feature_sizes = (self.img_size[0] / 16, self.img_size[1] / 16)
self.latent_to_features = nn.Sequential(
nn.Linear(latent_dim, 8 * dim * self.feature_sizes[0] * self.feature_sizes[1]),
nn.ReLU()
)
self.features_to_image = nn.Sequential(
nn.ConvTranspose2d(8 * dim, 4 * dim, 4, 2, 1),
nn.ReLU(),
nn.BatchNorm2d(4 * dim),
nn.ConvTranspose2d(4 * dim, 2 * dim, 4, 2, 1),
nn.ReLU(),
nn.BatchNorm2d(2 * dim),
nn.ConvTranspose2d(2 * dim, dim, 4, 2, 1),
nn.ReLU(),
nn.BatchNorm2d(dim),
nn.ConvTranspose2d(dim, self.img_size[2], 4, 2, 1),
nn.Sigmoid()
)
def forward(self, input_data):
# Map latent into appropriate size for transposed convolutions
x = self.latent_to_features(input_data)
# Reshape
x = x.view(-1, 8 * self.dim, self.feature_sizes[0], self.feature_sizes[1])
# Return generated image
return self.features_to_image(x)
def sample_latent(self, num_samples):
return torch.randn((num_samples, self.latent_dim))
class Discriminator(nn.Module):
def __init__(self, img_size, dim):
"""
img_size : (int, int, int)
Height and width must be powers of 2. E.g. (32, 32, 1) or
(64, 128, 3). Last number indicates number of channels, e.g. 1 for
grayscale or 3 for RGB
"""
super(Discriminator, self).__init__()
self.img_size = img_size
self.image_to_features = nn.Sequential(
nn.Conv2d(self.img_size[2], dim, 4, 2, 1),
nn.LeakyReLU(0.2),
nn.Conv2d(dim, 2 * dim, 4, 2, 1),
nn.LeakyReLU(0.2),
nn.Conv2d(2 * dim, 4 * dim, 4, 2, 1),
nn.LeakyReLU(0.2),
nn.Conv2d(4 * dim, 8 * dim, 4, 2, 1),
nn.Sigmoid()
)
# 4 convolutions of stride 2, i.e. halving of size everytime
# So output size will be 8 * (img_size / 2 ^ 4) * (img_size / 2 ^ 4)
output_size = 8 * dim * (img_size[0] / 16) * (img_size[1] / 16)
self.features_to_prob = nn.Sequential(
nn.Linear(output_size, 1),
nn.Sigmoid()
)
def forward(self, input_data):
batch_size = input_data.size()[0]
x = self.image_to_features(input_data)
x = x.view(batch_size, -1)
return self.features_to_prob(x)