-
Notifications
You must be signed in to change notification settings - Fork 39
/
model.py
123 lines (115 loc) · 4.02 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import torch
import torch.nn as nn
from functools import reduce
from torch.autograd import Variable
class shave_block(nn.Module):
def __init__(self, s):
super(shave_block, self).__init__()
self.s=s
def forward(self,x):
return x[:,:,self.s:-self.s,self.s:-self.s]
class LambdaBase(nn.Sequential):
def __init__(self, fn, *args):
super(LambdaBase, self).__init__(*args)
self.lambda_func = fn
def forward_prepare(self, input):
output = []
for module in self._modules.values():
output.append(module(input))
return output if output else input
class Lambda(LambdaBase):
def forward(self, input):
return self.lambda_func(self.forward_prepare(input))
class LambdaMap(LambdaBase):
def forward(self, input):
return list(map(self.lambda_func,self.forward_prepare(input)))
class LambdaReduce(LambdaBase):
def forward(self, input):
return reduce(self.lambda_func,self.forward_prepare(input))
def generator():
G = nn.Sequential( # Sequential,
nn.ReflectionPad2d((40, 40, 40, 40)),
nn.Conv2d(1,32,(9, 9),(1, 1),(4, 4)),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,64,(3, 3),(2, 2),(1, 1)),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64,128,(3, 3),(2, 2),(1, 1)),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Sequential( # Sequential,
LambdaMap(lambda x: x, # ConcatTable,
nn.Sequential( # Sequential,
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
),
shave_block(2),
),
LambdaReduce(lambda x,y: x+y), # CAddTable,
),
nn.Sequential( # Sequential,
LambdaMap(lambda x: x, # ConcatTable,
nn.Sequential( # Sequential,
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
),
shave_block(2),
),
LambdaReduce(lambda x,y: x+y), # CAddTable,
),
nn.Sequential( # Sequential,
LambdaMap(lambda x: x, # ConcatTable,
nn.Sequential( # Sequential,
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
),
shave_block(2),
),
LambdaReduce(lambda x,y: x+y), # CAddTable,
),
nn.Sequential( # Sequential,
LambdaMap(lambda x: x, # ConcatTable,
nn.Sequential( # Sequential,
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
),
shave_block(2),
),
LambdaReduce(lambda x,y: x+y), # CAddTable,
),
nn.Sequential( # Sequential,
LambdaMap(lambda x: x, # ConcatTable,
nn.Sequential( # Sequential,
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3)),
nn.BatchNorm2d(128),
),
shave_block(2),
),
LambdaReduce(lambda x,y: x+y), # CAddTable,
),
nn.ConvTranspose2d(128,64,(3, 3),(2, 2),(1, 1),(1, 1)),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64,32,(3, 3),(2, 2),(1, 1),(1, 1)),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,2,(9, 9),(1, 1),(4, 4)),
nn.Tanh(),
)
return G