File size: 6,080 Bytes
2147e2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import math

import torch
import torch.nn as nn
import torch.nn.functional as F

from torch.autograd import Variable


def default_conv(in_channels, out_channels, kernel_size, bias=True):
    return nn.Conv2d(
        in_channels, out_channels, kernel_size,
        padding=(kernel_size//2), bias=bias)


class ResUnit(nn.Module):
    def __init__(self, dim):
        super(ResUnit, self).__init__()

        self.act = nn.ReLU(True)
        self.conv1 = default_conv(dim, dim, 3)
        self.conv2 = default_conv(dim, dim*2, 1)
        self.conv3 = default_conv(dim*2, dim, 1)

    def forward(self, x):
        shortcut = x

        x = self.conv1(x)
        x = self.conv2(x)
        x = self.act(x)
        x = self.conv3(x)

        return x + shortcut


class FusionBlock(nn.Module):
    def __init__(self, n_color, embed_dim):
        super(FusionBlock, self).__init__()

        self.act = nn.ReLU(True)

        self.conv_1 = default_conv(n_color, embed_dim, 3)
        self.conv_2 = default_conv(embed_dim, embed_dim, 3)

        self.conv_1_2 = default_conv(embed_dim, embed_dim, 3)
        self.conv_2_2 = default_conv(embed_dim, embed_dim, 3)

        self.ru_1 = ResUnit(embed_dim)
        self.ru_2 = ResUnit(embed_dim)

        self.ru_1_1 = ResUnit(embed_dim)
        self.ru_2_1 = ResUnit(embed_dim)

        self.ru = ResUnit(embed_dim)
        self.ru_ = ResUnit(embed_dim)

        self.conv_tail_1 = default_conv(embed_dim*2, embed_dim, 3)
        self.conv_tail_2 = default_conv(embed_dim, embed_dim, 3)

    def forward(self, img_snow, mask):

        img_snow = self.ru_1(self.conv_1(img_snow))
        mask = self.ru_2(self.conv_2(mask))

        img_1 = self.ru(self.conv_1_2((img_snow-mask)))

        img_snow = self.ru_1_1(img_snow)
        mask = self.ru_2_1(mask)

        img_2 = self.ru_(self.conv_2_2((img_snow-mask)))


        return self.conv_tail_2(self.act(self.conv_tail_1(torch.cat((img_1, img_2), dim=1))))


class MARB(nn.Module):
    def __init__(self, dim):
        super(MARB, self).__init__()

        self.act = nn.ReLU(True)

        self.conv_dl2 = default_conv(dim, dim, 1)
        self.conv_dl3 = default_conv(dim, dim, 3)
        self.conv_dl5 = default_conv(dim, dim, 5)

        self.conv1_1 = default_conv(dim, dim, 1)
        self.conv1_2 = default_conv(dim, dim, 1)
        self.conv1_3 = default_conv(dim, dim, 1)

        self.conv2_1 = default_conv(dim*2, dim, 1)
        self.conv2_2 = default_conv(dim*2, dim, 1)

        self.conv_tail = default_conv(dim*2, dim, 1)

    def forward(self, x):
        x1 = self.conv1_1(self.conv_dl2(x))
        x2 = self.conv1_2(self.conv_dl3(x))
        x3 = self.conv1_3(self.conv_dl5(x))

        x_cat_1 = self.conv2_1(torch.cat((x1, x2), dim=1))
        x_cat_2 = self.conv2_2(torch.cat((x2, x3), dim=1))

        return self.conv_tail(self.act(torch.cat((x_cat_1, x_cat_2), dim=1))) + x

# class MARB(nn.Module):
#     def __init__(self, dim):
#         super(MARB, self).__init__()
#
#         self.act = nn.ReLU(True)
#
#         self.conv_dl2 = default_conv(dim, dim, 1)
#         self.conv_dl3 = default_conv(dim, dim, 3)
#         self.conv_dl5 = default_conv(dim, dim, 5)
#
#         self.conv1_1 = default_conv(dim, dim, 3)
#         self.conv1_2 = default_conv(dim, dim, 3)
#         self.conv1_3 = default_conv(dim, dim, 3)
#
#         # self.conv2_1 = default_conv(dim*2, dim, 3)
#         # self.conv2_2 = default_conv(dim*2, dim, 3)
#
#         self.conv_tail = default_conv(dim*3, dim, 3)
#
#     def forward(self, x):
#         x1 = self.conv1_1(self.conv_dl2(x))
#         x2 = self.conv1_2(self.conv_dl3(x))
#         x3 = self.conv1_3(self.conv_dl5(x))
#
#         # x_cat_1 = self.conv2_1(torch.cat((x1, x2), dim=1))
#         # x_cat_2 = self.conv2_2(torch.cat((x2, x3), dim=1))
#
#         return self.conv_tail(self.act(torch.cat((x1, x2, x3), dim=1))) + x


class MaskBlock(nn.Module):
    def __init__(self, embed_dim):
        super(MaskBlock, self).__init__()
        self.act = nn.ReLU(True)
        self.conv_head = default_conv(embed_dim, embed_dim, 3)

        self.conv_self = default_conv(embed_dim, embed_dim, 1)

        self.conv1 = default_conv(embed_dim, embed_dim, 3)
        self.conv1_1 = default_conv(embed_dim, embed_dim, 1)
        self.conv1_2 = default_conv(embed_dim, embed_dim, 1)
        self.conv_tail = default_conv(embed_dim, embed_dim, 3)

    def forward(self, x):
        x = self.conv_head(x)
        x = self.conv_self(x)
        x = x.mul(x)
        x = self.act(self.conv1(x))
        x = self.conv1_1(x).mul(self.conv1_2(x))

        return self.conv_tail(x)

def dwt_init(x):
    x01 = x[:, :, 0::2, :] / 2
    x02 = x[:, :, 1::2, :] / 2
    x1 = x01[:, :, :, 0::2]
    x2 = x02[:, :, :, 0::2]
    x3 = x01[:, :, :, 1::2]
    x4 = x02[:, :, :, 1::2]
    x_LL = x1 + x2 + x3 + x4
    x_HL = -x1 - x2 + x3 + x4
    x_LH = -x1 + x2 - x3 + x4
    x_HH = x1 - x2 - x3 + x4

    return torch.cat((x_LL, x_HL, x_LH, x_HH), 1)


def iwt_init(x):
    r = 2
    in_batch, in_channel, in_height, in_width = x.size()
    # print([in_batch, in_channel, in_height, in_width])
    out_batch, out_channel, out_height, out_width = in_batch, int(
        in_channel / (r ** 2)), r * in_height, r * in_width
    x1 = x[:, 0:out_channel, :, :] / 2
    x2 = x[:, out_channel:out_channel * 2, :, :] / 2
    x3 = x[:, out_channel * 2:out_channel * 3, :, :] / 2
    x4 = x[:, out_channel * 3:out_channel * 4, :, :] / 2

    h = torch.zeros([out_batch, out_channel, out_height, out_width]).float().cuda()

    h[:, :, 0::2, 0::2] = x1 - x2 - x3 + x4
    h[:, :, 1::2, 0::2] = x1 - x2 + x3 - x4
    h[:, :, 0::2, 1::2] = x1 + x2 - x3 - x4
    h[:, :, 1::2, 1::2] = x1 + x2 + x3 + x4

    return h

class DWT(nn.Module):
    def __init__(self):
        super(DWT, self).__init__()
        self.requires_grad = False

    def forward(self, x):
        return dwt_init(x)


class IWT(nn.Module):
    def __init__(self):
        super(IWT, self).__init__()
        self.requires_grad = False

    def forward(self, x):
        return iwt_init(x)