-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
73 lines (57 loc) · 2.11 KB
/
models.py
File metadata and controls
73 lines (57 loc) · 2.11 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
from mimetypes import init
import torch
import numpy as np
import torch.nn as nn
from config import *
class Generator(nn.Module):
def __init__(self, args):
super(Generator, self).__init__()
self.args = args
self.model = nn.Sequential(
nn.ConvTranspose2d(self.args.latent_dimension, 128, kernel_size=4, stride=1, padding=0),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.ConvTranspose2d(32, 1, kernel_size=4, stride=2, padding=1),
nn.Tanh()
)
def forward(self, z):
output = self.model(z)
return output
class Discriminator(nn.Module):
def __init__(self, args):
super(Discriminator, self).__init__()
self.args = args
self.model = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 1, kernel_size=4, stride=1, padding=0),
nn.Sigmoid()
)
def forward(self, x):
output = self.model(x)
output = output.view(-1, 1).squeeze(1)
return output
def main(args):
g = Generator(args)
print(torch.randn((4, args.latent_dimension)).shape)
tmp = torch.randn((4, args.latent_dimension)).view(-1, args.latent_dimension, 1, 1)
print(tmp.shape)
g_out = g(tmp)
d = Discriminator(args)
d_out = d(g_out)
if __name__ == '__main__':
parser = argparse.ArgumentParser('Parameters parser', parents=[get_args_parser()])
args = parser.parse_args()
main(args)