提交 9efa0289 编写于 作者: M michaelowenliu

re-design deeplab model

上级 ddc3d5cb
......@@ -28,7 +28,7 @@ class ANN(nn.Layer):
"""
The ANN implementation based on PaddlePaddle.
The orginal artile refers to
The original article refers to
Zhen, Zhu, et al. "Asymmetric Non-local Neural Networks for Semantic Segmentation."
(https://arxiv.org/pdf/1908.07678.pdf)
......@@ -37,8 +37,8 @@ class ANN(nn.Layer):
Args:
num_classes (int): the unique number of target classes.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
model_pretrained (str): the path of pretrained model. Defaullt to None.
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
model_pretrained (str): the path of pretrained model. Default to None.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
the first index will be taken as low-level features; the second one will be
taken as high-level features in AFNB module. Usually backbone consists of four
downsampling stage, and return an output of each stage, so we set default (2, 3),
......@@ -48,7 +48,7 @@ class ANN(nn.Layer):
Default to 256.
inter_channels (int): both input and output channels of APNB modules.
psp_size (tuple): the out size of pooled feature maps. Default to (1, 3, 6, 8).
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss. Default to True.
enable_auxiliary_loss (bool): a bool values indicates whether adding auxiliary loss. Default to True.
"""
def __init__(self,
......@@ -79,7 +79,7 @@ class ANN(nn.Layer):
psp_size=psp_size)
self.context = nn.Sequential(
layer_libs.ConvBnRelu(
layer_libs.ConvBNReLU(
in_channels=high_in_channels,
out_channels=inter_channels,
kernel_size=3,
......@@ -94,9 +94,7 @@ class ANN(nn.Layer):
psp_size=psp_size))
self.cls = nn.Conv2d(
in_channels=inter_channels,
out_channels=num_classes,
kernel_size=1)
in_channels=inter_channels, out_channels=num_classes, kernel_size=1)
self.auxlayer = layer_libs.AuxLayer(
in_channels=low_in_channels,
inter_channels=low_in_channels // 2,
......@@ -122,7 +120,8 @@ class ANN(nn.Layer):
if self.enable_auxiliary_loss:
auxiliary_logit = self.auxlayer(low_level_x)
auxiliary_logit = F.resize_bilinear(auxiliary_logit, input.shape[2:])
auxiliary_logit = F.resize_bilinear(auxiliary_logit,
input.shape[2:])
logit_list.append(auxiliary_logit)
return logit_list
......@@ -219,7 +218,7 @@ class APNB(nn.Layer):
SelfAttentionBlock_APNB(in_channels, out_channels, key_channels,
value_channels, size) for size in sizes
])
self.conv_bn = layer_libs.ConvBnRelu(
self.conv_bn = layer_libs.ConvBNReLU(
in_channels=in_channels * 2,
out_channels=out_channels,
kernel_size=1)
......@@ -280,11 +279,11 @@ class SelfAttentionBlock_AFNB(nn.Layer):
if out_channels == None:
self.out_channels = high_in_channels
self.pool = nn.Pool2D(pool_size=(scale, scale), pool_type="max")
self.f_key = layer_libs.ConvBnRelu(
self.f_key = layer_libs.ConvBNReLU(
in_channels=low_in_channels,
out_channels=key_channels,
kernel_size=1)
self.f_query = layer_libs.ConvBnRelu(
self.f_query = layer_libs.ConvBNReLU(
in_channels=high_in_channels,
out_channels=key_channels,
kernel_size=1)
......@@ -315,7 +314,7 @@ class SelfAttentionBlock_AFNB(nn.Layer):
key = _pp_module(key, self.psp_size)
sim_map = paddle.matmul(query, key)
sim_map = (self.key_channels ** -.5) * sim_map
sim_map = (self.key_channels**-.5) * sim_map
sim_map = F.softmax(sim_map, axis=-1)
context = paddle.matmul(sim_map, value)
......@@ -358,7 +357,7 @@ class SelfAttentionBlock_APNB(nn.Layer):
self.value_channels = value_channels
self.pool = nn.Pool2D(pool_size=(scale, scale), pool_type="max")
self.f_key = layer_libs.ConvBnRelu(
self.f_key = layer_libs.ConvBNReLU(
in_channels=self.in_channels,
out_channels=self.key_channels,
kernel_size=1)
......@@ -384,15 +383,14 @@ class SelfAttentionBlock_APNB(nn.Layer):
value = paddle.transpose(value, perm=(0, 2, 1))
query = self.f_query(x)
query = paddle.reshape(
query, shape=(batch_size, self.key_channels, -1))
query = paddle.reshape(query, shape=(batch_size, self.key_channels, -1))
query = paddle.transpose(query, perm=(0, 2, 1))
key = self.f_key(x)
key = _pp_module(key, self.psp_size)
sim_map = paddle.matmul(query, key)
sim_map = (self.key_channels ** -.5) * sim_map
sim_map = (self.key_channels**-.5) * sim_map
sim_map = F.softmax(sim_map, axis=-1)
context = paddle.matmul(sim_map, value)
......
......@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import paddle
from paddle import nn
import paddle.nn.functional as F
......@@ -34,11 +33,11 @@ class ASPPModule(nn.Layer):
image_pooling: if augmented with image-level features.
"""
def __init__(self,
aspp_ratios,
in_channels,
out_channels,
sep_conv=False,
def __init__(self,
aspp_ratios,
in_channels,
out_channels,
sep_conv=False,
image_pooling=False):
super(ASPPModule, self).__init__()
......@@ -47,42 +46,41 @@ class ASPPModule(nn.Layer):
for ratio in aspp_ratios:
if sep_conv and ratio > 1:
conv_func = layer_libs.DepthwiseConvBnRelu
conv_func = layer_libs.DepthwiseConvBNReLU
else:
conv_func = layer_libs.ConvBnRelu
conv_func = layer_libs.ConvBNReLU
block = conv_func(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1 if ratio == 1 else 3,
dilation=ratio,
padding=0 if ratio == 1 else ratio
)
padding=0 if ratio == 1 else ratio)
self.aspp_blocks.append(block)
out_size = len(self.aspp_blocks)
if image_pooling:
self.global_avg_pool = nn.Sequential(
nn.AdaptiveAvgPool2d(output_size=(1, 1)),
layer_libs.ConvBnRelu(in_channels, out_channels, kernel_size=1, bias_attr=False)
)
layer_libs.ConvBNReLU(
in_channels, out_channels, kernel_size=1, bias_attr=False))
out_size += 1
self.image_pooling = image_pooling
self.conv_bn_relu = layer_libs.ConvBnRelu(
in_channels=out_channels * out_size,
out_channels=out_channels,
self.conv_bn_relu = layer_libs.ConvBNReLU(
in_channels=out_channels * out_size,
out_channels=out_channels,
kernel_size=1)
self.dropout = nn.Dropout(p=0.1) # drop rate
self.dropout = nn.Dropout(p=0.1) # drop rate
def forward(self, x):
outputs = []
for block in self.aspp_blocks:
outputs.append(block(x))
if self.image_pooling:
img_avg = self.global_avg_pool(x)
img_avg = F.resize_bilinear(img_avg, out_shape=x.shape[2:])
......@@ -93,17 +91,17 @@ class ASPPModule(nn.Layer):
x = self.dropout(x)
return x
class PPModule(nn.Layer):
"""
Pyramid pooling module orginally in PSPNet
Pyramid pooling module originally in PSPNet
Args:
in_channels (int): the number of intput channels to pyramid pooling module.
out_channels (int): the number of output channels after pyramid pooling module.
bin_sizes (tuple): the out size of pooled feature maps. Default to (1,2,3,6).
dim_reduction (bool): a bool value represent if reduing dimention after pooling. Default to True.
dim_reduction (bool): a bool value represent if reducing dimension after pooling. Default to True.
"""
def __init__(self,
......@@ -125,7 +123,7 @@ class PPModule(nn.Layer):
for size in bin_sizes
])
self.conv_bn_relu2 = layer_libs.ConvBnRelu(
self.conv_bn_relu2 = layer_libs.ConvBNReLU(
in_channels=in_channels + inter_channels * len(bin_sizes),
out_channels=out_channels,
kernel_size=3,
......@@ -135,7 +133,7 @@ class PPModule(nn.Layer):
"""
Create one pooling layer.
In our implementation, we adopt the same dimention reduction as the original paper that might be
In our implementation, we adopt the same dimension reduction as the original paper that might be
slightly different with other implementations.
After pooling, the channels are reduced to 1/len(bin_sizes) immediately, while some other implementations
......@@ -151,7 +149,7 @@ class PPModule(nn.Layer):
"""
prior = nn.AdaptiveAvgPool2d(output_size=(size, size))
conv = layer_libs.ConvBnRelu(
conv = layer_libs.ConvBNReLU(
in_channels=in_channels, out_channels=out_channels, kernel_size=1)
return nn.Sequential(prior, conv)
......@@ -167,4 +165,4 @@ class PPModule(nn.Layer):
cat = paddle.concat(cat_layers, axis=1)
out = self.conv_bn_relu2(cat)
return out
\ No newline at end of file
return out
......@@ -29,140 +29,193 @@ class DeepLabV3P(nn.Layer):
"""
The DeepLabV3Plus implementation based on PaddlePaddle.
The orginal artile refers to
"Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation"
Liang-Chieh Chen, Yukun Zhu, George Papandreou, Florian Schroff, Hartwig Adam.
The original article refers to
Liang-Chieh Chen, et, al. "Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation"
(https://arxiv.org/abs/1802.02611)
The DeepLabV3P consists of three main components, Backbone, ASPP and Decoder.
Args:
num_classes (int): the unique number of target classes.
backbone (paddle.nn.Layer): backbone network, currently support Xception65, Resnet101_vd.
model_pretrained (str): the path of pretrained model.
aspp_ratios (tuple): the dilation rate using in ASSP module.
if output_stride=16, aspp_ratios should be set as (1, 6, 12, 18).
if output_stride=8, aspp_ratios is (1, 12, 24, 36).
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
the first index will be taken as a low-level feature in Deconder component;
backbone (paddle.nn.Layer): backbone network, currently support Resnet50_vd/Resnet101_vd/Xception65.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
the first index will be taken as a low-level feature in Decoder component;
the second one will be taken as input of ASPP component.
Usually backbone consists of four downsampling stage, and return an output of
each stage, so we set default (0, 3), which means taking feature map of the first
stage in backbone as low-level feature used in Decoder, and feature map of the fourth
stage as input of ASPP.
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
aspp_ratios (tuple): the dilation rate using in ASSP module.
if output_stride=16, aspp_ratios should be set as (1, 6, 12, 18).
if output_stride=8, aspp_ratios is (1, 12, 24, 36).
aspp_out_channels (int): the output channels of ASPP module.
pretrained (str): the path of pretrained model for fine tuning.
"""
def __init__(self,
num_classes,
backbone,
backbone_pretrained=None,
model_pretrained=None,
backbone_indices=(0, 3),
backbone_channels=(256, 2048),
aspp_ratios=(1, 6, 12, 18),
aspp_out_channels=256):
aspp_out_channels=256,
pretrained=None):
super(DeepLabV3P, self).__init__()
self.backbone = backbone
self.backbone_pretrained = backbone_pretrained
self.model_pretrained = model_pretrained
backbone_channels = backbone.backbone_channels
self.head = DeepLabV3PHead(
num_classes,
backbone_indices,
backbone_channels,
aspp_ratios,
aspp_out_channels)
utils.load_entire_model(self, pretrained)
def forward(self, input):
feat_list = self.backbone(input)
logit_list = self.head(feat_list)
return [
F.resize_bilinear(logit, input.shape[2:]) for logit in logit_list
]
class DeepLabV3PHead(nn.Layer):
"""
The DeepLabV3PHead implementation based on PaddlePaddle.
Args:
num_classes (int): the unique number of target classes.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
the first index will be taken as a low-level feature in Decoder component;
the second one will be taken as input of ASPP component.
Usually backbone consists of four downsampling stage, and return an output of
each stage, so we set default (0, 3), which means taking feature map of the first
stage in backbone as low-level feature used in Decoder, and feature map of the fourth
stage as input of ASPP.
backbone_channels (tuple): returned channels of backbone
aspp_ratios (tuple): the dilation rate using in ASSP module.
if output_stride=16, aspp_ratios should be set as (1, 6, 12, 18).
if output_stride=8, aspp_ratios is (1, 12, 24, 36).
aspp_out_channels (int): the output channels of ASPP module.
"""
def __init__(self,
num_classes,
backbone_indices,
backbone_channels,
aspp_ratios=(1, 6, 12, 18),
aspp_out_channels=256):
super(DeepLabV3PHead, self).__init__()
self.aspp = pyramid_pool.ASPPModule(
aspp_ratios, backbone_channels[1], aspp_out_channels, sep_conv=True, image_pooling=True)
self.decoder = Decoder(num_classes, backbone_channels[0])
aspp_ratios,
backbone_channels[backbone_indices[1]],
aspp_out_channels,
sep_conv=True,
image_pooling=True)
self.decoder = Decoder(num_classes, backbone_channels[backbone_indices[0]])
self.backbone_indices = backbone_indices
self.init_weight()
def forward(self, input, label=None):
def forward(self, feat_list):
logit_list = []
_, feat_list = self.backbone(input)
low_level_feat = feat_list[self.backbone_indices[0]]
x = feat_list[self.backbone_indices[1]]
x = self.aspp(x)
logit = self.decoder(x, low_level_feat)
logit = F.resize_bilinear(logit, input.shape[2:])
logit_list.append(logit)
return logit_list
def init_weight(self):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if self.model_pretrained is not None:
utils.load_pretrained_model(self, self.model_pretrained)
elif self.backbone_pretrained is not None:
utils.load_pretrained_model(self.backbone, self.backbone_pretrained)
pass
@manager.MODELS.add_component
class DeepLabV3(nn.Layer):
"""
The DeepLabV3 implementation based on PaddlePaddle.
The orginal article refers to
"Rethinking Atrous Convolution for Semantic Image Segmentation"
Liang-Chieh Chen, George Papandreou, Florian Schroff, Hartwig Adam.
The original article refers to
Liang-Chieh Chen, et, al. "Rethinking Atrous Convolution for Semantic Image Segmentation"
(https://arxiv.org/pdf/1706.05587.pdf)
Args:
Refer to DeepLabV3P above
"""
def __init__(self,
num_classes,
backbone,
backbone_pretrained=None,
model_pretrained=None,
backbone_indices=(3,),
backbone_channels=(2048,),
pretrained=None,
backbone_indices=(3, ),
aspp_ratios=(1, 6, 12, 18),
aspp_out_channels=256):
super(DeepLabV3, self).__init__()
self.backbone = backbone
backbone_channels = backbone.backbone_channels
self.head = DeepLabV3Head(
num_classes,
backbone_indices,
backbone_channels,
aspp_ratios,
aspp_out_channels)
utils.load_entire_model(self, pretrained)
def forward(self, input):
feat_list = self.backbone(input)
logit_list = self.head(feat_list)
return [
F.resize_bilinear(logit, input.shape[2:]) for logit in logit_list
]
class DeepLabV3Head(nn.Layer):
def __init__(self,
num_classes,
backbone_indices=(3, ),
backbone_channels=(2048, ),
aspp_ratios=(1, 6, 12, 18),
aspp_out_channels=256):
super(DeepLabV3Head, self).__init__()
self.aspp = pyramid_pool.ASPPModule(
aspp_ratios, backbone_channels[0], aspp_out_channels,
sep_conv=False, image_pooling=True)
aspp_ratios,
backbone_channels[backbone_indices[0]],
aspp_out_channels,
sep_conv=False,
image_pooling=True)
self.cls = nn.Conv2d(
in_channels=backbone_channels[0],
in_channels=backbone_channels[backbone_indices[0]],
out_channels=num_classes,
kernel_size=1)
self.backbone_indices = backbone_indices
self.init_weight(model_pretrained)
self.init_weight()
def forward(self, input, label=None):
def forward(self, feat_list):
logit_list = []
_, feat_list = self.backbone(input)
x = feat_list[self.backbone_indices[0]]
logit = self.cls(x)
logit = F.resize_bilinear(logit, input.shape[2:])
logit_list.append(logit)
return logit_list
def init_weight(self, pretrained_model=None):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if pretrained_model is not None:
if os.path.exists(pretrained_model):
utils.load_pretrained_model(self, pretrained_model)
else:
raise Exception('Pretrained model is not found: {}'.format(
pretrained_model))
def init_weight(self):
pass
class Decoder(nn.Layer):
......@@ -178,12 +231,12 @@ class Decoder(nn.Layer):
def __init__(self, num_classes, in_channels):
super(Decoder, self).__init__()
self.conv_bn_relu1 = layer_libs.ConvBnRelu(
self.conv_bn_relu1 = layer_libs.ConvBNReLU(
in_channels=in_channels, out_channels=48, kernel_size=1)
self.conv_bn_relu2 = layer_libs.DepthwiseConvBnRelu(
self.conv_bn_relu2 = layer_libs.DepthwiseConvBNReLU(
in_channels=304, out_channels=256, kernel_size=3, padding=1)
self.conv_bn_relu3 = layer_libs.DepthwiseConvBnRelu(
self.conv_bn_relu3 = layer_libs.DepthwiseConvBNReLU(
in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv = nn.Conv2d(
in_channels=256, out_channels=num_classes, kernel_size=1)
......
......@@ -26,15 +26,15 @@ class FastSCNN(nn.Layer):
As mentioned in the original paper, FastSCNN is a real-time segmentation algorithm (123.5fps)
even for high resolution images (1024x2048).
The orginal artile refers to
The original article refers to
Poudel, Rudra PK, et al. "Fast-scnn: Fast semantic segmentation network."
(https://arxiv.org/pdf/1902.04502.pdf)
Args:
num_classes (int): the unique number of target classes. Default to 2.
model_pretrained (str): the path of pretrained model. Defaullt to None.
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss.
model_pretrained (str): the path of pretrained model. Default to None.
enable_auxiliary_loss (bool): a bool values indicates whether adding auxiliary loss.
if true, auxiliary loss will be added after LearningToDownsample module, where the weight is 0.4. Default to False.
"""
......@@ -105,15 +105,15 @@ class LearningToDownsample(nn.Layer):
def __init__(self, dw_channels1=32, dw_channels2=48, out_channels=64):
super(LearningToDownsample, self).__init__()
self.conv_bn_relu = layer_libs.ConvBnRelu(
self.conv_bn_relu = layer_libs.ConvBNReLU(
in_channels=3, out_channels=dw_channels1, kernel_size=3, stride=2)
self.dsconv_bn_relu1 = layer_libs.DepthwiseConvBnRelu(
self.dsconv_bn_relu1 = layer_libs.DepthwiseConvBNReLU(
in_channels=dw_channels1,
out_channels=dw_channels2,
kernel_size=3,
stride=2,
padding=1)
self.dsconv_bn_relu2 = layer_libs.DepthwiseConvBnRelu(
self.dsconv_bn_relu2 = layer_libs.DepthwiseConvBNReLU(
in_channels=dw_channels2,
out_channels=out_channels,
kernel_size=3,
......@@ -208,13 +208,13 @@ class LinearBottleneck(nn.Layer):
expand_channels = in_channels * expansion
self.block = nn.Sequential(
# pw
layer_libs.ConvBnRelu(
layer_libs.ConvBNReLU(
in_channels=in_channels,
out_channels=expand_channels,
kernel_size=1,
bias_attr=False),
# dw
layer_libs.ConvBnRelu(
layer_libs.ConvBNReLU(
in_channels=expand_channels,
out_channels=expand_channels,
kernel_size=3,
......@@ -239,7 +239,7 @@ class LinearBottleneck(nn.Layer):
class FeatureFusionModule(nn.Layer):
"""
Feature Fusion Module Implememtation.
Feature Fusion Module Implementation.
This module fuses high-resolution feature and low-resolution feature.
......@@ -253,7 +253,7 @@ class FeatureFusionModule(nn.Layer):
super(FeatureFusionModule, self).__init__()
# There only depth-wise conv is used WITHOUT point-wise conv
self.dwconv = layer_libs.ConvBnRelu(
self.dwconv = layer_libs.ConvBNReLU(
in_channels=low_in_channels,
out_channels=out_channels,
kernel_size=3,
......@@ -301,13 +301,13 @@ class Classifier(nn.Layer):
def __init__(self, input_channels, num_classes):
super(Classifier, self).__init__()
self.dsconv1 = layer_libs.DepthwiseConvBnRelu(
self.dsconv1 = layer_libs.DepthwiseConvBNReLU(
in_channels=input_channels,
out_channels=input_channels,
kernel_size=3,
padding=1)
self.dsconv2 = layer_libs.DepthwiseConvBnRelu(
self.dsconv2 = layer_libs.DepthwiseConvBNReLU(
in_channels=input_channels,
out_channels=input_channels,
kernel_size=3,
......
......@@ -27,15 +27,15 @@ class GCNet(nn.Layer):
"""
The GCNet implementation based on PaddlePaddle.
The orginal artile refers to
The original article refers to
Cao, Yue, et al. "GCnet: Non-local networks meet squeeze-excitation networks and beyond."
(https://arxiv.org/pdf/1904.11492.pdf)
Args:
num_classes (int): the unique number of target classes.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
model_pretrained (str): the path of pretrained model. Defaullt to None.
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
model_pretrained (str): the path of pretrained model. Default to None.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
the first index will be taken as a deep-supervision feature in auxiliary layer;
the second one will be taken as input of GlobalContextBlock. Usually backbone
consists of four downsampling stage, and return an output of each stage, so we
......@@ -43,8 +43,8 @@ class GCNet(nn.Layer):
and the fourth stage (res5c) in backbone.
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
gc_channels (int): input channels to Global Context Block. Default to 512.
ratio (float): it indictes the ratio of attention channels and gc_channels. Default to 1/4.
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss. Default to True.
ratio (float): it indicates the ratio of attention channels and gc_channels. Default to 1/4.
enable_auxiliary_loss (bool): a bool values indicates whether adding auxiliary loss. Default to True.
"""
def __init__(self,
......@@ -63,7 +63,7 @@ class GCNet(nn.Layer):
self.backbone = backbone
in_channels = backbone_channels[1]
self.conv_bn_relu1 = layer_libs.ConvBnRelu(
self.conv_bn_relu1 = layer_libs.ConvBNReLU(
in_channels=in_channels,
out_channels=gc_channels,
kernel_size=3,
......@@ -71,13 +71,13 @@ class GCNet(nn.Layer):
self.gc_block = GlobalContextBlock(in_channels=gc_channels, ratio=ratio)
self.conv_bn_relu2 = layer_libs.ConvBnRelu(
self.conv_bn_relu2 = layer_libs.ConvBNReLU(
in_channels=gc_channels,
out_channels=gc_channels,
kernel_size=3,
padding=1)
self.conv_bn_relu3 = layer_libs.ConvBnRelu(
self.conv_bn_relu3 = layer_libs.ConvBNReLU(
in_channels=in_channels + gc_channels,
out_channels=gc_channels,
kernel_size=3,
......@@ -154,7 +154,7 @@ class GlobalContextBlock(nn.Layer):
in_channels=in_channels, out_channels=1, kernel_size=1)
self.softmax = nn.Softmax(axis=2)
inter_channels = int(in_channels * ratio)
self.channel_add_conv = nn.Sequential(
nn.Conv2d(
......
......@@ -18,7 +18,7 @@ import paddle.fluid as fluid
from paddle.fluid.dygraph import Sequential, Conv2D
from paddleseg.cvlibs import manager
from paddleseg.models.common.layer_libs import ConvBnRelu
from paddleseg.models.common.layer_libs import ConvBNReLU
from paddleseg import utils
......@@ -73,16 +73,16 @@ class ObjectAttentionBlock(fluid.dygraph.Layer):
self.key_channels = key_channels
self.f_pixel = Sequential(
ConvBnRelu(in_channels, key_channels, 1),
ConvBnRelu(key_channels, key_channels, 1))
ConvBNReLU(in_channels, key_channels, 1),
ConvBNReLU(key_channels, key_channels, 1))
self.f_object = Sequential(
ConvBnRelu(in_channels, key_channels, 1),
ConvBnRelu(key_channels, key_channels, 1))
ConvBNReLU(in_channels, key_channels, 1),
ConvBNReLU(key_channels, key_channels, 1))
self.f_down = ConvBnRelu(in_channels, key_channels, 1)
self.f_down = ConvBNReLU(in_channels, key_channels, 1)
self.f_up = ConvBnRelu(key_channels, in_channels, 1)
self.f_up = ConvBNReLU(key_channels, in_channels, 1)
def forward(self, x, proxy):
n, _, h, w = x.shape
......@@ -135,12 +135,12 @@ class OCRNet(fluid.dygraph.Layer):
self.spatial_gather = SpatialGatherBlock()
self.spatial_ocr = SpatialOCRModule(ocr_mid_channels, ocr_key_channels,
ocr_mid_channels)
self.conv3x3_ocr = ConvBnRelu(
self.conv3x3_ocr = ConvBNReLU(
in_channels, ocr_mid_channels, 3, padding=1)
self.cls_head = Conv2D(ocr_mid_channels, self.num_classes, 1)
self.aux_head = Sequential(
ConvBnRelu(in_channels, in_channels, 3, padding=1),
ConvBNReLU(in_channels, in_channels, 3, padding=1),
Conv2D(in_channels, self.num_classes, 1))
self.init_weight(model_pretrained)
......
......@@ -26,7 +26,7 @@ class PSPNet(nn.Layer):
"""
The PSPNet implementation based on PaddlePaddle.
The orginal artile refers to
The original article refers to
Zhao, Hengshuang, et al. "Pyramid scene parsing network."
Proceedings of the IEEE conference on computer vision and pattern recognition. 2017.
(https://openaccess.thecvf.com/content_cvpr_2017/papers/Zhao_Pyramid_Scene_Parsing_CVPR_2017_paper.pdf)
......@@ -34,8 +34,8 @@ class PSPNet(nn.Layer):
Args:
num_classes (int): the unique number of target classes.
backbone (Paddle.nn.Layer): backbone network, currently support Resnet50/101.
model_pretrained (str): the path of pretrained model. Defaullt to None.
backbone_indices (tuple): two values in the tuple indicte the indices of output of backbone.
model_pretrained (str): the path of pretrained model. Default to None.
backbone_indices (tuple): two values in the tuple indicate the indices of output of backbone.
the first index will be taken as a deep-supervision feature in auxiliary layer;
the second one will be taken as input of Pyramid Pooling Module (PPModule).
Usually backbone consists of four downsampling stage, and return an output of
......@@ -44,7 +44,7 @@ class PSPNet(nn.Layer):
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
pp_out_channels (int): output channels after Pyramid Pooling Module. Default to 1024.
bin_sizes (tuple): the out size of pooled feature maps. Default to (1,2,3,6).
enable_auxiliary_loss (bool): a bool values indictes whether adding auxiliary loss. Default to True.
enable_auxiliary_loss (bool): a bool values indicates whether adding auxiliary loss. Default to True.
"""
def __init__(self,
......@@ -107,6 +107,7 @@ class PSPNet(nn.Layer):
def init_weight(self, pretrained_model=None):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册