-
Notifications
You must be signed in to change notification settings - Fork 2
/
HidingRes.py
136 lines (112 loc) · 4.85 KB
/
HidingRes.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class ResidualBlock(nn.Module):
def __init__(self, channel_num, dilation=1, group=1):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(channel_num, channel_num, 3, 1, padding=dilation, dilation=dilation, groups=group, bias=False)
self.norm1 = nn.InstanceNorm2d(channel_num, affine=True)
self.conv2 = nn.Conv2d(channel_num, channel_num, 3, 1, padding=dilation, dilation=dilation, groups=group, bias=False)
self.norm2 = nn.InstanceNorm2d(channel_num, affine=True)
def forward(self, x):
y = F.relu(self.norm1(self.conv1(x)))
y = self.norm2(self.conv2(y))
return F.relu(x+y)
class HidingRes(nn.Module):
def __init__(self, in_c=4, out_c=3, only_residual=False,requires_grad=True):
super(HidingRes, self).__init__()
self.conv1 = nn.Conv2d(in_c, 128, 3, 1, 1, bias=False)
self.norm1 = nn.InstanceNorm2d(128, affine=True)
self.conv2 = nn.Conv2d(128, 128, 3, 1, 1, bias=False)
self.norm2 = nn.InstanceNorm2d(128, affine=True)
self.conv3 = nn.Conv2d(128, 128, 3, 2, 1, bias=False)
self.norm3 = nn.InstanceNorm2d(128, affine=True)
self.res1 = ResidualBlock(128, dilation=2)
self.res2 = ResidualBlock(128, dilation=2)
self.res3 = ResidualBlock(128, dilation=2)
self.res4 = ResidualBlock(128, dilation=2)
self.res5 = ResidualBlock(128, dilation=4)
self.res6 = ResidualBlock(128, dilation=4)
self.res7 = ResidualBlock(128, dilation=4)
self.res8 = ResidualBlock(128, dilation=4)
self.res9 = ResidualBlock(128, dilation=1)
self.deconv3 = nn.ConvTranspose2d(128, 128, 4, 2, 1)
self.norm4 = nn.InstanceNorm2d(128, affine=True)
self.deconv2 = nn.Conv2d(128, 128, 3, 1, 1)
self.norm5 = nn.InstanceNorm2d(128, affine=True)
self.deconv1 = nn.Conv2d(128, out_c, 1)
self.only_residual = only_residual
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
def forward(self, x, c):
c = c.view(c.size(0), c.size(1), 1, 1)
c = c.repeat(1, 1, x.size(2), x.size(3))
x = torch.cat([x, c], dim=1)
y = F.relu(self.norm1(self.conv1(x)))
y = F.relu(self.norm2(self.conv2(y)))
y = F.relu(self.norm3(self.conv3(y)))
y = self.res1(y)
y = self.res2(y)
y = self.res3(y)
y = self.res4(y)
y = self.res5(y)
y = self.res6(y)
y = self.res7(y)
y = self.res8(y)
y = self.res9(y)
y = F.relu(self.norm4(self.deconv3(y)))
y = F.relu(self.norm5(self.deconv2(y)))
if self.only_residual:
y = self.deconv1(y)
else:
y = F.tanh(self.deconv1(y))
return y
class HidingRes11(nn.Module):
def __init__(self, in_c=4, out_c=3, only_residual=False):
super(HidingRes11, self).__init__()
self.conv1 = nn.Conv2d(in_c, 128, 3, 1, 1, bias=False)
self.norm1 = nn.InstanceNorm2d(128, affine=True)
self.conv2 = nn.Conv2d(128, 128, 3, 1, 1, bias=False)
self.norm2 = nn.InstanceNorm2d(128, affine=True)
self.conv3 = nn.Conv2d(128, 128, 3, 2, 1, bias=False)
self.norm3 = nn.InstanceNorm2d(128, affine=True)
self.res1 = ResidualBlock(128, dilation=2)
self.res2 = ResidualBlock(128, dilation=2)
self.res3 = ResidualBlock(128, dilation=2)
self.res4 = ResidualBlock(128, dilation=2)
self.res5 = ResidualBlock(128, dilation=2)
self.res6 = ResidualBlock(128, dilation=4)
self.res7 = ResidualBlock(128, dilation=4)
self.res8 = ResidualBlock(128, dilation=4)
self.res9 = ResidualBlock(128, dilation=4)
self.res10 = ResidualBlock(128, dilation=4)
self.res11 = ResidualBlock(128, dilation=1)
self.deconv3 = nn.ConvTranspose2d(128, 128, 4, 2, 1)
self.norm4 = nn.InstanceNorm2d(128, affine=True)
self.deconv2 = nn.Conv2d(128, 128, 3, 1, 1)
self.norm5 = nn.InstanceNorm2d(128, affine=True)
self.deconv1 = nn.Conv2d(128, out_c, 1)
self.only_residual = only_residual
def forward(self, x):
y = F.relu(self.norm1(self.conv1(x)))
y = F.relu(self.norm2(self.conv2(y)))
y = F.relu(self.norm3(self.conv3(y)))
y = self.res1(y)
y = self.res2(y)
y = self.res3(y)
y = self.res4(y)
y = self.res5(y)
y = self.res6(y)
y = self.res7(y)
y = self.res8(y)
y = self.res9(y)
y = self.res10(y)
y = self.res11(y)
y = F.relu(self.norm4(self.deconv3(y)))
y = F.relu(self.norm5(self.deconv2(y)))
if self.only_residual:
y = self.deconv1(y)
else:
y = F.relu(self.deconv1(y))
return y