File size: 5,921 Bytes
1ab1a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import paddle
import paddle.nn as nn
import paddle.nn.functional as F

from paddleseg.models import layers


class NonLocal2D(nn.Layer):
    """Basic Non-local module.
    This model is the implementation of "Non-local Neural Networks"
    (https://arxiv.org/abs/1711.07971)

    Args:
        in_channels (int): Channels of the input feature map.
        reduction (int): Channel reduction ratio. Default: 2.
        use_scale (bool): Whether to scale pairwise_weight by `1/sqrt(inter_channels)` when the mode is `embedded_gaussian`. Default: True.
        sub_sample (bool): Whether to utilize max pooling after pairwise function. Default: False.
        mode (str): Options are `gaussian`, `concatenation`, `embedded_gaussian` and `dot_product`. Default: embedded_gaussian.
    """

    def __init__(self,
                 in_channels,
                 reduction=2,
                 use_scale=True,
                 sub_sample=False,
                 mode='embedded_gaussian'):
        super(NonLocal2D, self).__init__()
        self.in_channels = in_channels
        self.reduction = reduction
        self.use_scale = use_scale
        self.sub_sample = sub_sample
        self.mode = mode
        if mode not in [
                'gaussian', 'embedded_gaussian', 'dot_product', 'concatenation'
        ]:
            raise ValueError(
                "Mode should be in 'gaussian', 'concatenation','embedded_gaussian' or 'dot_product'."
            )

        self.inter_channels = max(in_channels // reduction, 1)

        self.g = nn.Conv2D(
            in_channels=self.in_channels,
            out_channels=self.inter_channels,
            kernel_size=1)
        self.conv_out = layers.ConvBNReLU(
            in_channels=self.inter_channels,
            out_channels=self.in_channels,
            kernel_size=1,
            bias_attr=False)

        if self.mode != "gaussian":
            self.theta = nn.Conv2D(
                in_channels=self.in_channels,
                out_channels=self.inter_channels,
                kernel_size=1)
            self.phi = nn.Conv2D(
                in_channels=self.in_channels,
                out_channels=self.inter_channels,
                kernel_size=1)

        if self.mode == "concatenation":
            self.concat_project = layers.ConvBNReLU(
                in_channels=self.inter_channels * 2,
                out_channels=1,
                kernel_size=1,
                bias_attr=False)

        if self.sub_sample:
            max_pool_layer = nn.MaxPool2D(kernel_size=(2, 2))
            self.g = nn.Sequential(self.g, max_pool_layer)
            if self.mode != 'gaussian':
                self.phi = nn.Sequential(self.phi, max_pool_layer)
            else:
                self.phi = max_pool_layer

    def gaussian(self, theta_x, phi_x):
        pairwise_weight = paddle.matmul(theta_x, phi_x)
        pairwise_weight = F.softmax(pairwise_weight, axis=-1)
        return pairwise_weight

    def embedded_gaussian(self, theta_x, phi_x):
        pairwise_weight = paddle.matmul(theta_x, phi_x)
        if self.use_scale:
            pairwise_weight /= theta_x.shape[-1]**0.5
        pairwise_weight = F.softmax(pairwise_weight, -1)
        return pairwise_weight

    def dot_product(self, theta_x, phi_x):
        pairwise_weight = paddle.matmul(theta_x, phi_x)
        pairwise_weight /= pairwise_weight.shape[-1]
        return pairwise_weight

    def concatenation(self, theta_x, phi_x):
        h = theta_x.shape[2]
        w = phi_x.shape[3]
        theta_x = paddle.tile(theta_x, [1, 1, 1, w])
        phi_x = paddle.tile(phi_x, [1, 1, h, 1])

        concat_feature = paddle.concat([theta_x, phi_x], axis=1)
        pairwise_weight = self.concat_project(concat_feature)
        n, _, h, w = pairwise_weight.shape
        pairwise_weight = paddle.reshape(pairwise_weight, [n, h, w])
        pairwise_weight /= pairwise_weight.shape[-1]
        return pairwise_weight

    def forward(self, x):
        n, c, h, w = x.shape
        g_x = paddle.reshape(self.g(x), [n, self.inter_channels, -1])
        g_x = paddle.transpose(g_x, [0, 2, 1])

        if self.mode == 'gaussian':
            theta_x = paddle.reshape(x, [n, self.inter_channels, -1])
            theta_x = paddle.transpose(theta_x, [0, 2, 1])
            if self.sub_sample:
                phi_x = paddle.reshape(
                    self.phi(x), [n, self.inter_channels, -1])
            else:
                phi_x = paddle.reshape(x, [n, self.in_channels, -1])

        elif self.mode == 'concatenation':
            theta_x = paddle.reshape(
                self.theta(x), [n, self.inter_channels, -1, 1])
            phi_x = paddle.reshape(self.phi(x), [n, self.inter_channels, 1, -1])

        else:
            theta_x = paddle.reshape(
                self.theta(x), [n, self.inter_channels, -1])
            theta_x = paddle.transpose(theta_x, [0, 2, 1])
            phi_x = paddle.reshape(self.phi(x), [n, self.inter_channels, -1])

        pairwise_func = getattr(self, self.mode)
        pairwise_weight = pairwise_func(theta_x, phi_x)
        y = paddle.matmul(pairwise_weight, g_x)
        y = paddle.transpose(y, [0, 2, 1])
        y = paddle.reshape(y, [n, self.inter_channels, h, w])

        output = x + self.conv_out(y)

        return output