remaster.py 10.0 KB
Newer Older
Q
qingqing01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

L
LielinJiang 已提交
15 16 17 18
import paddle
import paddle.nn as nn
import paddle.nn.functional as F

L
fix nan  
LielinJiang 已提交
19

L
LielinJiang 已提交
20
class TempConv(nn.Layer):
L
fix nan  
LielinJiang 已提交
21 22 23 24 25 26 27
    def __init__(self,
                 in_planes,
                 out_planes,
                 kernel_size=(1, 3, 3),
                 stride=(1, 1, 1),
                 padding=(0, 1, 1)):
        super(TempConv, self).__init__()
L
LielinJiang 已提交
28
        self.conv3d = nn.Conv3D(in_planes,
L
fix nan  
LielinJiang 已提交
29 30 31 32 33 34 35 36 37
                                out_planes,
                                kernel_size=kernel_size,
                                stride=stride,
                                padding=padding)
        self.bn = nn.BatchNorm(out_planes)

    def forward(self, x):
        return F.elu(self.bn(self.conv3d(x)))

L
LielinJiang 已提交
38 39

class Upsample(nn.Layer):
L
fix nan  
LielinJiang 已提交
40 41 42
    def __init__(self, in_planes, out_planes, scale_factor=(1, 2, 2)):
        super(Upsample, self).__init__()
        self.scale_factor = scale_factor
L
LielinJiang 已提交
43
        self.conv3d = nn.Conv3D(in_planes,
L
fix nan  
LielinJiang 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                                out_planes,
                                kernel_size=(3, 3, 3),
                                stride=(1, 1, 1),
                                padding=(1, 1, 1))
        self.bn = nn.BatchNorm(out_planes)

    def forward(self, x):
        out_size = x.shape[2:]
        for i in range(3):
            out_size[i] = self.scale_factor[i] * out_size[i]

        return F.elu(
            self.bn(
                self.conv3d(
                    F.interpolate(x,
                                  size=out_size,
                                  mode='trilinear',
                                  align_corners=False,
                                  data_format='NCDHW',
                                  align_mode=0))))

L
LielinJiang 已提交
65 66

class UpsampleConcat(nn.Layer):
L
fix nan  
LielinJiang 已提交
67 68 69 70 71 72 73 74 75 76
    def __init__(self, in_planes_up, in_planes_flat, out_planes):
        super(UpsampleConcat, self).__init__()
        self.conv3d = TempConv(in_planes_up + in_planes_flat,
                               out_planes,
                               kernel_size=(3, 3, 3),
                               stride=(1, 1, 1),
                               padding=(1, 1, 1))

    def forward(self, x1, x2):
        scale_factor = (1, 2, 2)
L
LielinJiang 已提交
77 78 79
        out_size = x1.shape[2:]
        for i in range(3):
            out_size[i] = scale_factor[i] * out_size[i]
L
fix nan  
LielinJiang 已提交
80 81 82 83 84 85 86

        x1 = F.interpolate(x1,
                           size=out_size,
                           mode='trilinear',
                           align_corners=False,
                           data_format='NCDHW',
                           align_mode=0)
L
LielinJiang 已提交
87 88 89
        x = paddle.concat([x1, x2], axis=1)
        return self.conv3d(x)

L
fix nan  
LielinJiang 已提交
90 91

class SourceReferenceAttention(nn.Layer):
L
LielinJiang 已提交
92 93 94 95 96 97 98 99 100 101 102 103
    """
    Source-Reference Attention Layer
    """
    def __init__(self, in_planes_s, in_planes_r):
        """
        Parameters
        ----------
            in_planes_s: int
                Number of input source feature vector channels.
            in_planes_r: int
                Number of input reference feature vector channels.
        """
L
fix nan  
LielinJiang 已提交
104
        super(SourceReferenceAttention, self).__init__()
L
LielinJiang 已提交
105
        self.query_conv = nn.Conv3D(in_channels=in_planes_s,
L
fix nan  
LielinJiang 已提交
106 107
                                    out_channels=in_planes_s // 8,
                                    kernel_size=1)
L
LielinJiang 已提交
108
        self.key_conv = nn.Conv3D(in_channels=in_planes_r,
L
fix nan  
LielinJiang 已提交
109 110
                                  out_channels=in_planes_r // 8,
                                  kernel_size=1)
L
LielinJiang 已提交
111
        self.value_conv = nn.Conv3D(in_channels=in_planes_r,
L
fix nan  
LielinJiang 已提交
112 113 114 115 116 117
                                    out_channels=in_planes_r,
                                    kernel_size=1)
        self.gamma = self.create_parameter(
            shape=[1],
            dtype=self.query_conv.weight.dtype,
            default_initializer=nn.initializer.Constant(0.0))
L
LielinJiang 已提交
118 119 120 121

    def forward(self, source, reference):
        s_batchsize, sC, sT, sH, sW = source.shape
        r_batchsize, rC, rT, rH, rW = reference.shape
L
fix nan  
LielinJiang 已提交
122 123 124

        proj_query = paddle.reshape(self.query_conv(source),
                                    [s_batchsize, -1, sT * sH * sW])
L
LielinJiang 已提交
125
        proj_query = paddle.transpose(proj_query, [0, 2, 1])
L
fix nan  
LielinJiang 已提交
126 127 128 129 130 131 132 133 134 135 136
        proj_key = paddle.reshape(self.key_conv(reference),
                                  [r_batchsize, -1, rT * rW * rH])
        energy = paddle.bmm(proj_query, proj_key)
        attention = F.softmax(energy)

        proj_value = paddle.reshape(self.value_conv(reference),
                                    [r_batchsize, -1, rT * rH * rW])

        out = paddle.bmm(proj_value, paddle.transpose(attention, [0, 2, 1]))
        out = paddle.reshape(out, [s_batchsize, sC, sT, sH, sW])
        out = self.gamma * out + source
L
LielinJiang 已提交
137 138 139
        return out, attention


L
fix nan  
LielinJiang 已提交
140 141 142 143 144
class NetworkR(nn.Layer):
    def __init__(self):
        super(NetworkR, self).__init__()

        self.layers = nn.Sequential(
L
LielinJiang 已提交
145
            nn.Pad3D((1, 1, 1, 1, 1, 1), mode='replicate'),
L
fix nan  
LielinJiang 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            TempConv(1,
                     64,
                     kernel_size=(3, 3, 3),
                     stride=(1, 2, 2),
                     padding=(0, 0, 0)),
            TempConv(64, 128, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            TempConv(128, 128, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            TempConv(128,
                     256,
                     kernel_size=(3, 3, 3),
                     stride=(1, 2, 2),
                     padding=(1, 1, 1)),
            TempConv(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            TempConv(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            TempConv(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            TempConv(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            Upsample(256, 128),
            TempConv(128, 64, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            TempConv(64, 64, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
            Upsample(64, 16),
L
LielinJiang 已提交
166
            nn.Conv3D(16,
L
fix nan  
LielinJiang 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
                      1,
                      kernel_size=(3, 3, 3),
                      stride=(1, 1, 1),
                      padding=(1, 1, 1)))

    def forward(self, x):
        return paddle.clip(
            (x + F.tanh(self.layers(((x * 1).detach()) - 0.4462414))), 0.0, 1.0)


class NetworkC(nn.Layer):
    def __init__(self):
        super(NetworkC, self).__init__()

        self.down1 = nn.Sequential(
L
LielinJiang 已提交
182
            nn.Pad3D((1, 1, 1, 1, 0, 0), mode='replicate'),
L
fix nan  
LielinJiang 已提交
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            TempConv(1, 64, stride=(1, 2, 2), padding=(0, 0, 0)),
            TempConv(64, 128), TempConv(128, 128),
            TempConv(128, 256, stride=(1, 2, 2)), TempConv(256, 256),
            TempConv(256, 256), TempConv(256, 512, stride=(1, 2, 2)),
            TempConv(512, 512), TempConv(512, 512))
        self.flat = nn.Sequential(TempConv(512, 512), TempConv(512, 512))
        self.down2 = nn.Sequential(
            TempConv(512, 512, stride=(1, 2, 2)),
            TempConv(512, 512),
        )
        self.stattn1 = SourceReferenceAttention(
            512, 512)  # Source-Reference Attention
        self.stattn2 = SourceReferenceAttention(
            512, 512)  # Source-Reference Attention
        self.selfattn1 = SourceReferenceAttention(512, 512)  # Self Attention
        self.conv1 = TempConv(512, 512)
        self.up1 = UpsampleConcat(512, 512, 512)  # 1/8
        self.selfattn2 = SourceReferenceAttention(512, 512)  # Self Attention
        self.conv2 = TempConv(512,
                              256,
                              kernel_size=(3, 3, 3),
                              stride=(1, 1, 1),
                              padding=(1, 1, 1))
        self.up2 = nn.Sequential(
            Upsample(256, 128),  # 1/4
            TempConv(128,
                     64,
                     kernel_size=(3, 3, 3),
                     stride=(1, 1, 1),
                     padding=(1, 1, 1)))
        self.up3 = nn.Sequential(
            Upsample(64, 32),  # 1/2
            TempConv(32,
                     16,
                     kernel_size=(3, 3, 3),
                     stride=(1, 1, 1),
                     padding=(1, 1, 1)))
        self.up4 = nn.Sequential(
            Upsample(16, 8),  # 1/1
L
LielinJiang 已提交
222
            nn.Conv3D(8,
L
fix nan  
LielinJiang 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                      2,
                      kernel_size=(3, 3, 3),
                      stride=(1, 1, 1),
                      padding=(1, 1, 1)))
        self.reffeatnet1 = nn.Sequential(
            TempConv(3, 64, stride=(1, 2, 2)),
            TempConv(64, 128),
            TempConv(128, 128),
            TempConv(128, 256, stride=(1, 2, 2)),
            TempConv(256, 256),
            TempConv(256, 256),
            TempConv(256, 512, stride=(1, 2, 2)),
            TempConv(512, 512),
            TempConv(512, 512),
        )
        self.reffeatnet2 = nn.Sequential(
            TempConv(512, 512, stride=(1, 2, 2)),
            TempConv(512, 512),
            TempConv(512, 512),
        )

    def forward(self, x, x_refs=None):
        x1 = self.down1(x - 0.4462414)
        if x_refs is not None:
            x_refs = paddle.transpose(
                x_refs, [0, 2, 1, 3, 4])  # [B,T,C,H,W] --> [B,C,T,H,W]
            reffeat = self.reffeatnet1(x_refs - 0.48)
            x1, _ = self.stattn1(x1, reffeat)

        x2 = self.flat(x1)
        out = self.down2(x1)
        if x_refs is not None:
            reffeat2 = self.reffeatnet2(reffeat)
            out, _ = self.stattn2(out, reffeat2)
        out = self.conv1(out)
        out, _ = self.selfattn1(out, out)
        out = self.up1(out, x2)
        out, _ = self.selfattn2(out, out)
        out = self.conv2(out)
        out = self.up2(out)
        out = self.up3(out)
        out = self.up4(out)

        return F.sigmoid(out)