-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcnv_model.py
executable file
·168 lines (132 loc) · 4.05 KB
/
cnv_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import torch
import torch.nn as nn
import torch.nn.functional as F
from chk import checkpoint_sequential_step, checkpoint
import math
import numpy as np
from torchvision.utils import save_image
import gin
def ginM(n): return gin.query_parameter(f'%{n}')
gin.external_configurable(nn.MaxPool2d, module='nn')
gin.external_configurable(nn.Upsample, module='nn')
class LN(nn.Module):
def forward(self, x):
return F.layer_norm(x, x.size()[1:], weight=None, bias=None, eps=1e-05)
@gin.configurable
class PadPool(nn.Module):
def forward(self, x):
x = F.pad(x, [0, 0, 0, 1])
x = F.max_pool2d(x,(2, 2), stride=(1, 2))
return x
def pCnv(inp,out,groups=1):
return nn.Sequential(
nn.Conv2d(inp,out,1,bias=False,groups=groups),
nn.InstanceNorm2d(out,affine=True)
)
#regarding same padding in PT https://github.com/pytorch/pytorch/issues/3867
def dsCnv(inp,k):
return nn.Sequential(
nn.Conv2d(inp,inp,k,groups=inp,bias=False,padding=(k - 1) // 2),
nn.InstanceNorm2d(inp,affine=True)
)
ngates = 2
class Gate(nn.Module):
def __init__(self,ifsz):
super().__init__()
self.ln = LN()
def forward(self, x):
t0,t1 = torch.chunk(x, ngates, dim=1)
t0 = torch.tanh_(t0)
t1.sub_(2)
t1 = torch.sigmoid_(t1)
return t1*t0
def customGC(module):
def custom_forward(*inputs):
inputs = module(inputs[0])
return inputs
return custom_forward
@gin.configurable
class GateBlock(nn.Module):
def __init__(self, ifsz, ofsz, gt = True, ksz = 3, GradCheck=gin.REQUIRED):
super().__init__()
cfsz = int( math.floor(ifsz/2) )
ifsz2 = ifsz + ifsz%2
self.sq = nn.Sequential(
pCnv(ifsz, cfsz),
dsCnv(cfsz,ksz),
nn.ELU(),
###########
pCnv(cfsz, cfsz*ngates),
dsCnv(cfsz*ngates,ksz),
Gate(cfsz),
###########
pCnv(cfsz, ifsz),
dsCnv(ifsz,ksz),
nn.ELU(),
)
self.gt = gt
self.gc = GradCheck
def forward(self, x):
if self.gc >= 1:
y = checkpoint(customGC(self.sq), x)
else:
y = self.sq(x)
out = x + y
return out
@gin.configurable
class InitBlock(nn.Module):
def __init__(self, fup, n_channels):
super().__init__()
self.n1 = LN()
self.Initsq = nn.Sequential(
pCnv(n_channels, fup),
nn.Softmax(dim=1),
dsCnv(fup,11),
LN()
)
def forward(self, x):
x = self.n1(x)
xt = x
x = self.Initsq(x)
x = torch.cat([x,xt],1)
return x
@gin.configurable
class OrigamiNet(nn.Module):
def __init__(self, n_channels, o_classes, wmul, lreszs, lszs, nlyrs, fup, GradCheck, reduceAxis=3):
super().__init__()
self.lreszs = lreszs
self.Initsq = InitBlock(fup)
layers = []
isz = fup + n_channels
osz = isz
for i in range(nlyrs):
osz = int( math.floor(lszs[i] * wmul) ) if i in lszs else isz
layers.append( GateBlock(isz, osz, True, 3) )
if isz != osz:
layers.append( pCnv(isz, osz) )
layers.append( nn.ELU() )
isz = osz
if i in lreszs:
layers.append( lreszs[i] )
layers.append( LN() )
self.Gatesq = nn.Sequential(*layers)
self.Finsq = nn.Sequential(
pCnv(osz, o_classes),
nn.ELU(),
)
self.n1 = LN()
self.it=0
self.gc = GradCheck
self.reduceAxis = reduceAxis
def forward(self, x, t=[]):
x = self.Initsq(x)
if self.gc >=2:
x = checkpoint_sequential_step(self.Gatesq,4,x) #slower, more memory save
# x = checkpoint_sequential_step(self.Gatesq,8,x) #faster, less memory save
else:
x = self.Gatesq(x)
x = self.Finsq(x)
x = torch.mean(x, self.reduceAxis, keepdim=False)
x = self.n1(x)
x = x.permute(0,2,1)
return x