From 5977ae402ce3ee3c511402064f1bbcd6e1c38410 Mon Sep 17 00:00:00 2001 From: michaelowenliu Date: Tue, 22 Sep 2020 19:40:31 +0800 Subject: [PATCH] add feat_channels in backbone --- .../paddleseg/models/backbones/resnet_vd.py | 10 ++--- dygraph/paddleseg/models/danet.py | 2 +- dygraph/paddleseg/models/deeplab.py | 42 +++++++++---------- dygraph/paddleseg/models/ocrnet.py | 2 +- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/dygraph/paddleseg/models/backbones/resnet_vd.py b/dygraph/paddleseg/models/backbones/resnet_vd.py index 38e2cf55..4f7e5589 100644 --- a/dygraph/paddleseg/models/backbones/resnet_vd.py +++ b/dygraph/paddleseg/models/backbones/resnet_vd.py @@ -224,9 +224,9 @@ class ResNet_vd(nn.Layer): ] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] - # for channels of returned stage - self.backbone_channels = [c * 4 for c in num_filters - ] if layers >= 50 else num_filters + # for channels of four returned stages + self.feat_channels = [c * 4 for c in num_filters + ] if layers >= 50 else num_filters dilation_dict = None if output_stride == 8: @@ -319,7 +319,7 @@ class ResNet_vd(nn.Layer): block_list.append(basic_block) shortcut = True self.stage_list.append(block_list) - + utils.load_pretrained_model(self, pretrained) def forward(self, inputs): @@ -336,8 +336,6 @@ class ResNet_vd(nn.Layer): feat_list.append(y) return feat_list - - @manager.BACKBONES.add_component diff --git a/dygraph/paddleseg/models/danet.py b/dygraph/paddleseg/models/danet.py index 5af7668e..cc9e463c 100644 --- a/dygraph/paddleseg/models/danet.py +++ b/dygraph/paddleseg/models/danet.py @@ -190,7 +190,7 @@ class DANet(nn.Layer): self.backbone = backbone self.backbone_indices = backbone_indices - in_channels = [self.backbone.channels[i] for i in backbone_indices] + in_channels = [self.backbone.feat_channels[i] for i in backbone_indices] self.head = DAHead(num_classes=num_classes, in_channels=in_channels) diff --git a/dygraph/paddleseg/models/deeplab.py b/dygraph/paddleseg/models/deeplab.py index 920990dd..3f85dff4 100644 --- a/dygraph/paddleseg/models/deeplab.py +++ b/dygraph/paddleseg/models/deeplab.py @@ -62,14 +62,13 @@ class DeepLabV3P(nn.Layer): super(DeepLabV3P, self).__init__() self.backbone = backbone - backbone_channels = backbone.backbone_channels + backbone_channels = [ + backbone.feat_channels[i] for i in backbone_indices + ] - self.head = DeepLabV3PHead( - num_classes, - backbone_indices, - backbone_channels, - aspp_ratios, - aspp_out_channels) + self.head = DeepLabV3PHead(num_classes, backbone_indices, + backbone_channels, aspp_ratios, + aspp_out_channels) utils.load_entire_model(self, pretrained) @@ -81,6 +80,7 @@ class DeepLabV3P(nn.Layer): F.resize_bilinear(logit, input.shape[2:]) for logit in logit_list ] + class DeepLabV3PHead(nn.Layer): """ The DeepLabV3PHead implementation based on PaddlePaddle. @@ -110,14 +110,14 @@ class DeepLabV3PHead(nn.Layer): aspp_out_channels=256): super(DeepLabV3PHead, self).__init__() - + self.aspp = pyramid_pool.ASPPModule( aspp_ratios, - backbone_channels[backbone_indices[1]], + backbone_channels[1], aspp_out_channels, sep_conv=True, image_pooling=True) - self.decoder = Decoder(num_classes, backbone_channels[backbone_indices[0]]) + self.decoder = Decoder(num_classes, backbone_channels[0]) self.backbone_indices = backbone_indices self.init_weight() @@ -135,6 +135,7 @@ class DeepLabV3PHead(nn.Layer): def init_weight(self): pass + @manager.MODELS.add_component class DeepLabV3(nn.Layer): """ @@ -147,7 +148,7 @@ class DeepLabV3(nn.Layer): Args: Refer to DeepLabV3P above """ - + def __init__(self, num_classes, backbone, @@ -159,15 +160,14 @@ class DeepLabV3(nn.Layer): super(DeepLabV3, self).__init__() self.backbone = backbone - backbone_channels = backbone.backbone_channels + backbone_channels = [ + backbone.feat_channels[i] for i in backbone_indices + ] + + self.head = DeepLabV3Head(num_classes, backbone_indices, + backbone_channels, aspp_ratios, + aspp_out_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): @@ -191,13 +191,13 @@ class DeepLabV3Head(nn.Layer): self.aspp = pyramid_pool.ASPPModule( aspp_ratios, - backbone_channels[backbone_indices[0]], + backbone_channels[0], aspp_out_channels, sep_conv=False, image_pooling=True) self.cls = nn.Conv2d( - in_channels=backbone_channels[backbone_indices[0]], + in_channels=backbone_channels[0], out_channels=num_classes, kernel_size=1) diff --git a/dygraph/paddleseg/models/ocrnet.py b/dygraph/paddleseg/models/ocrnet.py index 01f7214b..01ce6aca 100644 --- a/dygraph/paddleseg/models/ocrnet.py +++ b/dygraph/paddleseg/models/ocrnet.py @@ -203,7 +203,7 @@ class OCRNet(nn.Layer): self.backbone = backbone self.backbone_indices = backbone_indices - in_channels = [self.backbone.channels[i] for i in backbone_indices] + in_channels = [self.backbone.feat_channels[i] for i in backbone_indices] self.head = OCRHead( num_classes=num_classes, -- GitLab