forked from yunjey/mnist-svhn-transfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
107 lines (88 loc) · 4.04 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
import torch.nn as nn
import torch.nn.functional as F
def deconv(c_in, c_out, k_size, stride=2, pad=1, bn=True):
"""Custom deconvolutional layer for simplicity."""
layers = []
layers.append(nn.ConvTranspose2d(c_in, c_out, k_size, stride, pad, bias=False))
if bn:
layers.append(nn.BatchNorm2d(c_out))
return nn.Sequential(*layers)
def conv(c_in, c_out, k_size, stride=2, pad=1, bn=True):
"""Custom convolutional layer for simplicity."""
layers = []
layers.append(nn.Conv2d(c_in, c_out, k_size, stride, pad, bias=False))
if bn:
layers.append(nn.BatchNorm2d(c_out))
return nn.Sequential(*layers)
class G12(nn.Module):
"""Generator for transfering from mnist to svhn"""
def __init__(self, conv_dim=64):
super(G12, self).__init__()
# encoding blocks
self.conv1 = conv(1, conv_dim, 4)
self.conv2 = conv(conv_dim, conv_dim*2, 4)
# residual blocks
self.conv3 = conv(conv_dim*2, conv_dim*2, 3, 1, 1)
self.conv4 = conv(conv_dim*2, conv_dim*2, 3, 1, 1)
# decoding blocks
self.deconv1 = deconv(conv_dim*2, conv_dim, 4)
self.deconv2 = deconv(conv_dim, 3, 4, bn=False)
def forward(self, x):
out = F.leaky_relu(self.conv1(x), 0.05) # (?, 64, 16, 16)
out = F.leaky_relu(self.conv2(out), 0.05) # (?, 128, 8, 8)
out = F.leaky_relu(self.conv3(out), 0.05) # ( " )
out = F.leaky_relu(self.conv4(out), 0.05) # ( " )
out = F.leaky_relu(self.deconv1(out), 0.05) # (?, 64, 16, 16)
out = F.tanh(self.deconv2(out)) # (?, 3, 32, 32)
return out
class G21(nn.Module):
"""Generator for transfering from svhn to mnist"""
def __init__(self, conv_dim=64):
super(G21, self).__init__()
# encoding blocks
self.conv1 = conv(3, conv_dim, 4)
self.conv2 = conv(conv_dim, conv_dim*2, 4)
# residual blocks
self.conv3 = conv(conv_dim*2, conv_dim*2, 3, 1, 1)
self.conv4 = conv(conv_dim*2, conv_dim*2, 3, 1, 1)
# decoding blocks
self.deconv1 = deconv(conv_dim*2, conv_dim, 4)
self.deconv2 = deconv(conv_dim, 1, 4, bn=False)
def forward(self, x):
out = F.leaky_relu(self.conv1(x), 0.05) # (?, 64, 16, 16)
out = F.leaky_relu(self.conv2(out), 0.05) # (?, 128, 8, 8)
out = F.leaky_relu(self.conv3(out), 0.05) # ( " )
out = F.leaky_relu(self.conv4(out), 0.05) # ( " )
out = F.leaky_relu(self.deconv1(out), 0.05) # (?, 64, 16, 16)
out = F.tanh(self.deconv2(out)) # (?, 1, 32, 32)
return out
class D1(nn.Module):
"""Discriminator for mnist."""
def __init__(self, conv_dim=64, use_labels=False):
super(D1, self).__init__()
self.conv1 = conv(1, conv_dim, 4, bn=False)
self.conv2 = conv(conv_dim, conv_dim*2, 4)
self.conv3 = conv(conv_dim*2, conv_dim*4, 4)
n_out = 11 if use_labels else 1
self.fc = conv(conv_dim*4, n_out, 4, 1, 0, False)
def forward(self, x):
out = F.leaky_relu(self.conv1(x), 0.05) # (?, 64, 16, 16)
out = F.leaky_relu(self.conv2(out), 0.05) # (?, 128, 8, 8)
out = F.leaky_relu(self.conv3(out), 0.05) # (?, 256, 4, 4)
out = self.fc(out).squeeze()
return out
class D2(nn.Module):
"""Discriminator for svhn."""
def __init__(self, conv_dim=64, use_labels=False):
super(D2, self).__init__()
self.conv1 = conv(3, conv_dim, 4, bn=False)
self.conv2 = conv(conv_dim, conv_dim*2, 4)
self.conv3 = conv(conv_dim*2, conv_dim*4, 4)
n_out = 11 if use_labels else 1
self.fc = conv(conv_dim*4, n_out, 4, 1, 0, False)
def forward(self, x):
out = F.leaky_relu(self.conv1(x), 0.05) # (?, 64, 16, 16)
out = F.leaky_relu(self.conv2(out), 0.05) # (?, 128, 8, 8)
out = F.leaky_relu(self.conv3(out), 0.05) # (?, 256, 4, 4)
out = self.fc(out).squeeze()
return out