remaster.py 9.4 KB
Newer Older
L
LielinJiang 已提交
1 2 3 4
import paddle
import paddle.nn as nn
import paddle.nn.functional as F

L
fix nan  
LielinJiang 已提交
5

L
LielinJiang 已提交
6
class TempConv(nn.Layer):
L
fix nan  
LielinJiang 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    def __init__(self,
                 in_planes,
                 out_planes,
                 kernel_size=(1, 3, 3),
                 stride=(1, 1, 1),
                 padding=(0, 1, 1)):
        super(TempConv, self).__init__()
        self.conv3d = nn.Conv3d(in_planes,
                                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 已提交
24 25

class Upsample(nn.Layer):
L
fix nan  
LielinJiang 已提交
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
    def __init__(self, in_planes, out_planes, scale_factor=(1, 2, 2)):
        super(Upsample, self).__init__()
        self.scale_factor = scale_factor
        self.conv3d = nn.Conv3d(in_planes,
                                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 已提交
51 52

class UpsampleConcat(nn.Layer):
L
fix nan  
LielinJiang 已提交
53 54 55 56 57 58 59 60 61 62
    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 已提交
63 64 65
        out_size = x1.shape[2:]
        for i in range(3):
            out_size[i] = scale_factor[i] * out_size[i]
L
fix nan  
LielinJiang 已提交
66 67 68 69 70 71 72

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

L
fix nan  
LielinJiang 已提交
76 77

class SourceReferenceAttention(nn.Layer):
L
LielinJiang 已提交
78 79 80 81 82 83 84 85 86 87 88 89
    """
    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 已提交
90
        super(SourceReferenceAttention, self).__init__()
L
LielinJiang 已提交
91
        self.query_conv = nn.Conv3d(in_channels=in_planes_s,
L
fix nan  
LielinJiang 已提交
92 93 94 95 96
                                    out_channels=in_planes_s // 8,
                                    kernel_size=1)
        self.key_conv = nn.Conv3d(in_channels=in_planes_r,
                                  out_channels=in_planes_r // 8,
                                  kernel_size=1)
L
LielinJiang 已提交
97
        self.value_conv = nn.Conv3d(in_channels=in_planes_r,
L
fix nan  
LielinJiang 已提交
98 99 100 101 102 103
                                    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 已提交
104 105 106 107

    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 已提交
108 109 110

        proj_query = paddle.reshape(self.query_conv(source),
                                    [s_batchsize, -1, sT * sH * sW])
L
LielinJiang 已提交
111
        proj_query = paddle.transpose(proj_query, [0, 2, 1])
L
fix nan  
LielinJiang 已提交
112 113 114 115 116 117 118 119 120 121 122
        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 已提交
123 124 125
        return out, attention


L
fix nan  
LielinJiang 已提交
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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
class NetworkR(nn.Layer):
    def __init__(self):
        super(NetworkR, self).__init__()

        self.layers = nn.Sequential(
            nn.ReplicationPad3d((1, 1, 1, 1, 1, 1)),
            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),
            nn.Conv3d(16,
                      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(
            nn.ReplicationPad3d((1, 1, 1, 1, 0, 0)),
            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
            nn.Conv3d(8,
                      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)