forked from clvrai/ACGAN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resblocks.py
106 lines (82 loc) · 3.2 KB
/
resblocks.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
from torch.nn import utils
import functools
# Identity mapping
class IdentityMapping(nn.Module):
def __init__(self, *args):
super(IdentityMapping, self).__init__()
def forward(self, x):
return x
class Block(nn.Module):
def __init__(self, in_ch, out_ch, h_ch=None, ksize=3, pad=1,
activation=F.relu, downsample=False, dropout=0.):
super(Block, self).__init__()
self.activation = activation
self.downsample = downsample
if dropout > 0:
drop_layer = functools.partial(nn.Dropout2d, p=dropout)
else:
drop_layer = IdentityMapping
self.learnable_sc = (in_ch != out_ch) or downsample
if h_ch is None:
h_ch = in_ch
else:
h_ch = out_ch
self.c1 = utils.spectral_norm(nn.Conv2d(in_ch, h_ch, ksize, 1, pad))
self.c2 = utils.spectral_norm(nn.Conv2d(h_ch, out_ch, ksize, 1, pad))
self.drop1 = drop_layer()
self.drop2 = drop_layer()
if self.learnable_sc:
self.c_sc = utils.spectral_norm(nn.Conv2d(in_ch, out_ch, 1, 1, 0))
self.drop_sc = drop_layer()
self._initialize()
def _initialize(self):
init.xavier_uniform_(self.c1.weight.data, math.sqrt(2))
init.xavier_uniform_(self.c2.weight.data, math.sqrt(2))
if self.learnable_sc:
init.xavier_uniform_(self.c_sc.weight.data)
def forward(self, x):
return self.shortcut(x) + self.residual(x)
def shortcut(self, x):
if self.learnable_sc:
x = self.drop_sc(self.c_sc(x))
if self.downsample:
return F.avg_pool2d(x, 2)
return x
def residual(self, x):
# conv(activation(x))?
h = self.drop1(self.c1(self.activation(x)))
h = self.drop2(self.c2(self.activation(h)))
if self.downsample:
h = F.avg_pool2d(h, 2)
return h
class OptimizedBlock(nn.Module):
def __init__(self, in_ch, out_ch, ksize=3, pad=1, activation=F.relu, dropout=0.):
super(OptimizedBlock, self).__init__()
self.activation = activation
if dropout > 0:
drop_layer = functools.partial(nn.Dropout2d, p=dropout)
else:
drop_layer = IdentityMapping
self.c1 = utils.spectral_norm(nn.Conv2d(in_ch, out_ch, ksize, 1, pad))
self.c2 = utils.spectral_norm(nn.Conv2d(out_ch, out_ch, ksize, 1, pad))
self.c_sc = utils.spectral_norm(nn.Conv2d(in_ch, out_ch, 1, 1, 0))
self.drop1 = drop_layer()
self.drop2 = drop_layer()
self.drop_sc = drop_layer()
self._initialize()
def _initialize(self):
init.xavier_uniform_(self.c1.weight.data, math.sqrt(2))
init.xavier_uniform_(self.c2.weight.data, math.sqrt(2))
init.xavier_uniform_(self.c_sc.weight.data)
def forward(self, x):
return self.shortcut(x) + self.residual(x)
def shortcut(self, x):
return self.drop_sc(self.c_sc(F.avg_pool2d(x, 2)))
def residual(self, x):
h = self.activation(self.drop1(self.c1(x)))
return F.avg_pool2d(self.drop2(self.c2(h)), 2)