unet.py 8.7 KB
Newer Older
C
chenguowei01 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
C
chenguowei01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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.

from __future__ import absolute_import
C
chenguowei01 已提交
16 17 18
from __future__ import division
from __future__ import print_function

C
chenguowei01 已提交
19 20
from collections import OrderedDict

C
chenguowei01 已提交
21 22 23 24 25 26 27
import paddle.fluid as fluid
from paddle.fluid.dygraph import Conv2D, BatchNorm, Pool2D
import contextlib

regularizer = fluid.regularizer.L2DecayRegularizer(regularization_coeff=0.0)
name_scope = ""

C
chenguowei01 已提交
28

C
chenguowei01 已提交
29 30 31 32 33 34 35
@contextlib.contextmanager
def scope(name):
    global name_scope
    bk = name_scope
    name_scope = name_scope + name + '/'
    yield
    name_scope = bk
C
chenguowei01 已提交
36

C
chenguowei01 已提交
37 38 39 40 41 42 43 44 45

class UNet(fluid.dygraph.Layer):
    def __init__(self, num_classes, upsample_mode='bilinear', ignore_index=255):
        super().__init__()
        self.encode = Encoder()
        self.decode = Decode(upsample_mode=upsample_mode)
        self.get_logit = GetLogit(64, num_classes)
        self.ignore_index = ignore_index

C
chenguowei01 已提交
46
    def forward(self, x, label=None, mode='train'):
C
chenguowei01 已提交
47 48 49 50 51
        encode_data, short_cuts = self.encode(x)
        decode_data = self.decode(encode_data, short_cuts)
        logit = self.get_logit(decode_data)
        if mode == 'train':
            return self._get_loss(logit, label)
C
chenguowei01 已提交
52
        else:
C
chenguowei01 已提交
53 54 55
            score_map = fluid.layers.softmax(logit, axis=1)
            score_map = fluid.layers.transpose(score_map, [0, 2, 3, 1])
            pred = fluid.layers.argmax(score_map, axis=3)
C
chenguowei01 已提交
56
            pred = fluid.layers.unsqueeze(pred, axes=[3])
C
chenguowei01 已提交
57
            return pred, score_map
C
chenguowei01 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    def _get_loss(self, logit, label):
        mask = label != self.ignore_index
        mask = fluid.layers.cast(mask, 'float32')
        loss, probs = fluid.layers.softmax_with_cross_entropy(
            logit,
            label,
            ignore_index=self.ignore_index,
            return_softmax=True,
            axis=1)

        loss = loss * mask
        avg_loss = fluid.layers.mean(loss) / (fluid.layers.mean(mask) + 0.00001)

        label.stop_gradient = True
        mask.stop_gradient = True
        return avg_loss

C
chenguowei01 已提交
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

class Encoder(fluid.dygraph.Layer):
    def __init__(self):
        super().__init__()
        with scope('encode'):
            with scope('block1'):
                self.double_conv = DoubleConv(3, 64)
            with scope('block1'):
                self.down1 = Down(64, 128)
            with scope('block2'):
                self.down2 = Down(128, 256)
            with scope('block3'):
                self.down3 = Down(256, 512)
            with scope('block4'):
                self.down4 = Down(512, 512)

    def forward(self, x):
        short_cuts = []
        x = self.double_conv(x)
        short_cuts.append(x)
        x = self.down1(x)
        short_cuts.append(x)
        x = self.down2(x)
        short_cuts.append(x)
        x = self.down3(x)
        short_cuts.append(x)
        x = self.down4(x)
        return x, short_cuts


class Decode(fluid.dygraph.Layer):
    def __init__(self, upsample_mode='bilinear'):
        super().__init__()
        with scope('decode'):
            with scope('decode1'):
                self.up1 = Up(512, 256, upsample_mode)
            with scope('decode2'):
                self.up2 = Up(256, 128, upsample_mode)
            with scope('decode3'):
                self.up3 = Up(128, 64, upsample_mode)
            with scope('decode4'):
                self.up4 = Up(64, 64, upsample_mode)

    def forward(self, x, short_cuts):
        x = self.up1(x, short_cuts[3])
        x = self.up2(x, short_cuts[2])
        x = self.up3(x, short_cuts[1])
        x = self.up4(x, short_cuts[0])
        return x


class GetLogit(fluid.dygraph.Layer):
    def __init__(self):
        super().__init__()


class DoubleConv(fluid.dygraph.Layer):
    def __init__(self, num_channels, num_filters):
        super().__init__()
        with scope('conv0'):
            param_attr = fluid.ParamAttr(
                name=name_scope + 'weights',
                regularizer=regularizer,
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=0.33))
            self.conv0 = Conv2D(
                num_channels=num_channels,
                num_filters=num_filters,
                filter_size=3,
                stride=1,
                padding=1,
                param_attr=param_attr)
            self.bn0 = BatchNorm(
                num_channels=num_filters,
                param_attr=fluid.ParamAttr(
                    name=name_scope + 'gamma', regularizer=regularizer),
                bias_attr=fluid.ParamAttr(
                    name=name_scope + 'beta', regularizer=regularizer),
                moving_mean_name=name_scope + 'moving_mean',
                moving_variance_name=name_scope + 'moving_variance')
        with scope('conv1'):
            param_attr = fluid.ParamAttr(
                name=name_scope + 'weights',
                regularizer=regularizer,
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=0.33))
            self.conv1 = Conv2D(
                num_channels=num_filters,
                num_filters=num_filters,
                filter_size=3,
                stride=1,
                padding=1,
                param_attr=param_attr)
            self.bn1 = BatchNorm(
                num_channels=num_filters,
                param_attr=fluid.ParamAttr(
                    name=name_scope + 'gamma', regularizer=regularizer),
                bias_attr=fluid.ParamAttr(
                    name=name_scope + 'beta', regularizer=regularizer),
                moving_mean_name=name_scope + 'moving_mean',
                moving_variance_name=name_scope + 'moving_variance')

    def forward(self, x):
        x = self.conv0(x)
        x = self.bn0(x)
        x = fluid.layers.relu(x)
        x = self.conv1(x)
        x = self.bn1(x)
        x = fluid.layers.relu(x)
        return x


class Down(fluid.dygraph.Layer):
    def __init__(self, num_channels, num_filters):
        super().__init__()
        with scope("down"):
            self.max_pool = Pool2D(
                pool_size=2, pool_type='max', pool_stride=2, pool_padding=0)
            self.double_conv = DoubleConv(num_channels, num_filters)

    def forward(self, x):
        x = self.max_pool(x)
        x = self.double_conv(x)
        return x


class Up(fluid.dygraph.Layer):
    def __init__(self, num_channels, num_filters, upsample_mode):
        super().__init__()
        self.upsample_mode = upsample_mode
        with scope('up'):
            if upsample_mode == 'bilinear':
                self.double_conv = DoubleConv(2 * num_channels, num_filters)
            if not upsample_mode == 'bilinear':
                param_attr = fluid.ParamAttr(
                    name=name_scope + 'weights',
                    regularizer=regularizer,
                    initializer=fluid.initializer.XavierInitializer(),
                )
                self.deconv = fluid.dygraph.Conv2DTranspose(
                    num_channels=num_channels,
                    num_filters=num_filters // 2,
                    filter_size=2,
                    stride=2,
                    padding=0,
                    param_attr=param_attr)
                self.double_conv = DoubleConv(num_channels + num_filters // 2,
                                              num_filters)

    def forward(self, x, short_cut):
        if self.upsample_mode == 'bilinear':
            short_cut_shape = fluid.layers.shape(short_cut)
            x = fluid.layers.resize_bilinear(x, short_cut_shape[2:])
C
chenguowei01 已提交
229
        else:
C
chenguowei01 已提交
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
            x = self.deconv(x)
        x = fluid.layers.concat([x, short_cut], axis=1)
        x = self.double_conv(x)
        return x


class GetLogit(fluid.dygraph.Layer):
    def __init__(self, num_channels, num_classes):
        super().__init__()
        with scope('logit'):
            param_attr = fluid.ParamAttr(
                name=name_scope + 'weights',
                regularizer=regularizer,
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=0.01))
            self.conv = Conv2D(
                num_channels=num_channels,
                num_filters=num_classes,
                filter_size=3,
                stride=1,
                padding=1,
                param_attr=param_attr)

    def forward(self, x):
        x = self.conv(x)
        return x