-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.py
71 lines (66 loc) · 2.17 KB
/
decoder.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
import torch.nn as nn
import torch
import copy
dec5_1 = nn.Sequential( # Sequential,
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512,512,(3, 3)),
nn.ReLU(),
nn.UpsamplingNearest2d(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512,512,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512,512,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512,512,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512,256,(3, 3)),
nn.ReLU(),
nn.UpsamplingNearest2d(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256,256,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256,256,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256,256,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256,128,(3, 3)),
nn.ReLU(),
nn.UpsamplingNearest2d(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128,128,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128,64,(3, 3)),
nn.ReLU(),
nn.UpsamplingNearest2d(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64,64,(3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64,3,(3, 3)),
)
class Decoder(nn.Module):
def __init__(self, level, pretrained_path=None):
super().__init__()
if level == 1:
self.net = nn.Sequential(*copy.deepcopy(list(dec5_1.children())[-2:]))
elif level == 2:
self.net = nn.Sequential(*copy.deepcopy(list(dec5_1.children())[-9:]))
elif level == 3:
self.net = nn.Sequential(*copy.deepcopy(list(dec5_1.children())[-16:]))
elif level == 4:
self.net = nn.Sequential(*copy.deepcopy(list(dec5_1.children())[-29:]))
elif level == 5:
self.net = dec5_1
else:
raise ValueError('level should be between 1~5')
if pretrained_path is not None:
self.net.load_state_dict(torch.load(pretrained_path, map_location=lambda storage, loc: storage))
def forward(self, x):
return self.net(x)