fcn.py 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2020 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 math
import os

import paddle
C
chenguowei01 已提交
19 20
import paddle.nn as nn
import paddle.nn.functional as F
C
chenguowei01 已提交
21
from paddle.nn import Conv2d
22 23 24 25 26 27
from paddle.nn import SyncBatchNorm as BatchNorm

from paddleseg.cvlibs import manager
from paddleseg import utils
from paddleseg.cvlibs import param_init
from paddleseg.utils import logger
C
chenguowei01 已提交
28
from paddleseg.models.common import layer_utils
29 30 31 32 33 34 35 36 37

__all__ = [
    "fcn_hrnet_w18_small_v1", "fcn_hrnet_w18_small_v2", "fcn_hrnet_w18",
    "fcn_hrnet_w30", "fcn_hrnet_w32", "fcn_hrnet_w40", "fcn_hrnet_w44",
    "fcn_hrnet_w48", "fcn_hrnet_w60", "fcn_hrnet_w64"
]


@manager.MODELS.add_component
C
chenguowei01 已提交
38
class FCN(nn.Layer):
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
    """
    Fully Convolutional Networks for Semantic Segmentation.
    https://arxiv.org/abs/1411.4038

    Args:
        num_classes (int): the unique number of target classes.

        backbone (paddle.nn.Layer): backbone networks.

        model_pretrained (str): the path of pretrained model.

        backbone_indices (tuple): one values in the tuple indicte the indices of output of backbone.Default -1.

        backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.

        channels (int): channels after conv layer before the last one.
    """

    def __init__(self,
                 num_classes,
                 backbone,
                 backbone_pretrained=None,
                 model_pretrained=None,
                 backbone_indices=(-1, ),
                 backbone_channels=(270, ),
                 channels=None):
        super(FCN, self).__init__()

        self.num_classes = num_classes
        self.backbone_pretrained = backbone_pretrained
        self.model_pretrained = model_pretrained
        self.backbone_indices = backbone_indices
        if channels is None:
C
chenguowei01 已提交
72
            channels = backbone_channels[0]
73 74 75

        self.backbone = backbone
        self.conv_last_2 = ConvBNLayer(
C
chenguowei01 已提交
76 77 78
            in_channels=backbone_channels[0],
            out_channels=channels,
            kernel_size=1,
79
            stride=1)
C
chenguowei01 已提交
80 81 82 83
        self.conv_last_1 = Conv2d(
            in_channels=channels,
            out_channels=self.num_classes,
            kernel_size=1,
84 85 86 87 88 89 90 91 92 93 94
            stride=1,
            padding=0)
        if self.training:
            self.init_weight()

    def forward(self, x):
        input_shape = x.shape[2:]
        fea_list = self.backbone(x)
        x = fea_list[self.backbone_indices[0]]
        x = self.conv_last_2(x)
        logit = self.conv_last_1(x)
C
chenguowei01 已提交
95
        logit = F.resize_bilinear(logit, input_shape)
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
        return [logit]

    def init_weight(self):
        params = self.parameters()
        for param in params:
            param_name = param.name
            if 'batch_norm' in param_name:
                if 'w_0' in param_name:
                    param_init.constant_init(param, value=1.0)
                elif 'b_0' in param_name:
                    param_init.constant_init(param, value=0.0)
            if 'conv' in param_name and 'w_0' in param_name:
                param_init.normal_init(param, scale=0.001)

        if self.model_pretrained is not None:
            if os.path.exists(self.model_pretrained):
                utils.load_pretrained_model(self, self.model_pretrained)
            else:
                raise Exception('Pretrained model is not found: {}'.format(
                    self.model_pretrained))
        elif self.backbone_pretrained is not None:
            if os.path.exists(self.backbone_pretrained):
                utils.load_pretrained_model(self.backbone,
                                            self.backbone_pretrained)
            else:
                raise Exception('Pretrained model is not found: {}'.format(
                    self.backbone_pretrained))
        else:
            logger.warning('No pretrained model to load, train from scratch')


C
chenguowei01 已提交
127
class ConvBNLayer(nn.Layer):
128
    def __init__(self,
C
chenguowei01 已提交
129 130 131
                 in_channels,
                 out_channels,
                 kernel_size,
132 133 134 135 136
                 stride=1,
                 groups=1,
                 act="relu"):
        super(ConvBNLayer, self).__init__()

C
chenguowei01 已提交
137 138 139 140
        self._conv = Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=kernel_size,
141
            stride=stride,
C
chenguowei01 已提交
142
            padding=(kernel_size - 1) // 2,
143 144
            groups=groups,
            bias_attr=False)
C
chenguowei01 已提交
145 146
        self._batch_norm = BatchNorm(out_channels)
        self.act = layer_utils.Activation(act=act)
147 148 149 150

    def forward(self, input):
        y = self._conv(input)
        y = self._batch_norm(y)
C
chenguowei01 已提交
151
        y = self.act(y)
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
        return y


@manager.MODELS.add_component
def fcn_hrnet_w18_small_v1(*args, **kwargs):
    return FCN(backbone='HRNet_W18_Small_V1', backbone_channels=(240), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w18_small_v2(*args, **kwargs):
    return FCN(backbone='HRNet_W18_Small_V2', backbone_channels=(270), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w18(*args, **kwargs):
    return FCN(backbone='HRNet_W18', backbone_channels=(270), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w30(*args, **kwargs):
    return FCN(backbone='HRNet_W30', backbone_channels=(450), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w32(*args, **kwargs):
    return FCN(backbone='HRNet_W32', backbone_channels=(480), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w40(*args, **kwargs):
    return FCN(backbone='HRNet_W40', backbone_channels=(600), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w44(*args, **kwargs):
    return FCN(backbone='HRNet_W44', backbone_channels=(660), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w48(*args, **kwargs):
    return FCN(backbone='HRNet_W48', backbone_channels=(720), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w60(*args, **kwargs):
    return FCN(backbone='HRNet_W60', backbone_channels=(900), **kwargs)


@manager.MODELS.add_component
def fcn_hrnet_w64(*args, **kwargs):
    return FCN(backbone='HRNet_W64', backbone_channels=(960), **kwargs)