-
Notifications
You must be signed in to change notification settings - Fork 42
/
dni.py
67 lines (60 loc) · 2.41 KB
/
dni.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
import torch
import torch.nn as nn
import numpy as np
class dni_linear(nn.Module):
def __init__(self, input_dims, num_classes, dni_hidden_size=1024, conditioned=False):
super(dni_linear, self).__init__()
self.conditioned = conditioned
if self.conditioned:
dni_input_dims = input_dims+num_classes
else:
dni_input_dims = input_dims
self.layer1 = nn.Sequential(
nn.Linear(dni_input_dims, dni_hidden_size),
nn.BatchNorm1d(dni_hidden_size),
nn.ReLU()
)
self.layer2 = nn.Sequential(
nn.Linear(dni_hidden_size, dni_hidden_size),
nn.BatchNorm1d(dni_hidden_size),
nn.ReLU()
)
self.layer3 = nn.Linear(dni_hidden_size, input_dims)
def forward(self, x, y):
if self.conditioned:
assert y is not None
x = torch.cat((x, y), 1)
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
return out
class dni_Conv2d(nn.Module):
def __init__(self, input_dims, input_size, num_classes, dni_hidden_size=64, conditioned=False):
super(dni_Conv2d, self).__init__()
self.conditioned = conditioned
if self.conditioned:
dni_input_dims = input_dims+1
else:
dni_input_dims = input_dims
self.input_size = list(input_size)
self.label_emb = nn.Linear(num_classes, np.prod(np.array(input_size)))
self.layer1 = nn.Sequential(
nn.Conv2d(dni_input_dims, dni_hidden_size, kernel_size=5, padding=2),
nn.BatchNorm2d(dni_hidden_size),
nn.ReLU())
self.layer2 = nn.Sequential(
nn.Conv2d(dni_hidden_size, dni_hidden_size, kernel_size=5, padding=2),
nn.BatchNorm2d(dni_hidden_size),
nn.ReLU())
self.layer3 = nn.Sequential(
nn.Conv2d(dni_hidden_size, input_dims, kernel_size=5, padding=2))
def forward(self, x, y):
if self.conditioned:
assert y is not None
y = self.label_emb(y)
y = y.view([-1, 1]+self.input_size)
x = torch.cat((x, y), 1)
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
return out