Esempio n. 1
0
 def __init__(self,in_c,out_c,output_shape,smooth_ratio=8):
     super().__init__()    
     self.output_shape=output_shape
     self.class_number=out_c
     self.smooth_ratio=smooth_ratio
     
     conv_layers=[]
     while in_c//smooth_ratio>self.class_number:
         conv_layers.append(conv_bn_relu(in_c,in_c//smooth_ratio))
         in_c=in_c//smooth_ratio
         
     conv_layers.append(conv_bn_relu(in_c,self.class_number))
     self.midnet=nn.Sequential(*conv_layers)
Esempio n. 2
0
 def __init__(self,in_channels, out_channels, output_shape,use_sigmoid):
     """
     in_channels
     out_channels
     sub_class_number=in_channels//out_channels
     mid_channels=out_channels*sub_class_number
     """
     super().__init__()
     self.output_shape=output_shape
     self.class_number=out_channels
     self.sub_class_number=in_channels//out_channels
     assert self.sub_class_number>1
     self.midnet=conv_bn_relu(in_channels,self.class_number*self.sub_class_number)
     
     self.use_sigmoid=use_sigmoid
     self.sigmoid=nn.Sigmoid()
Esempio n. 3
0
 def __init__(self,in_c,mid_c,config,ratio=10):
     """
     in_c --> mid_c --> class_number
     in_c --> mid_c*ratio --> mid_c --> class_number
     """
     super().__init__()
     self.config=config
     self.input_shape=config.input_shape
     self.class_number=config.class_number
     self.midnet=conv_bn_relu(in_c,mid_c*ratio)
     self.midnet_out_channels=mid_c
     self.decoder=motionnet_upsample_bilinear(in_channels=self.midnet_out_channels,
                                                  out_channels=self.class_number,
                                                  output_shape=self.input_shape[0:2])
     self.sigmoid=nn.Sigmoid()
     self.use_sigmoid=config.subclass_sigmoid
Esempio n. 4
0
    def __init__(self, in_channels, out_channels, upsample_ratio, eps=1e-5, momentum=0.1):
        """
        out_channels: class number
        upsample_ratio: 2**upsample_layer
        """
        super().__init__()

        self.conv_bn_relu = conv_bn_relu(in_channels=in_channels,
                                         out_channels=out_channels*upsample_ratio*upsample_ratio,
                                         kernel_size=1,
                                         padding=0,
                                         stride=1,
                                         eps=eps,
                                         momentum=momentum)

        self.duc = nn.PixelShuffle(upsample_ratio)
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(
                    m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
Esempio n. 5
0
    def __init__(self, backbones, config):
        super().__init__()
        self.deconv_layer = config.deconv_layer
        self.upsample_layer = config.upsample_layer
        self.fusion_type = config.fusion_type
        self.use_none_layer = config.use_none_layer
        self.decode_main_layer = config.decode_main_layer
        self.min_channel_number = config.min_channel_number
        self.backbones = backbones
        self.filter_type = config.filter_type
        self.filter_feature = config.filter_feature
        self.filter_relu = config.filter_relu

        if config.net_name.find('flow') >= 0:
            self.use_flow = True
        else:
            self.use_flow = False

        if self.filter_feature is None:
            self.filter_feature = 'aux' if self.use_flow else 'all'

        self.layers = []
        self.filter_layers = []
        self.keys = [
            'main_backbone', 'aux_backbone', 'main_panet', 'aux_panet'
        ]
        channels = {}
        inplace = True
        for idx in range(self.deconv_layer + 1):
            if idx < self.upsample_layer:
                self.layers.append(None)
                self.filter_layers.append(None)
                continue
            elif idx == self.deconv_layer:
                init_c = merge_c = 0
            else:
                init_c = merge_c = max(
                    backbones['main_backbone'].get_feature_map_channel(idx +
                                                                       1),
                    self.min_channel_number)

            out_c = max(
                backbones['main_backbone'].get_feature_map_channel(idx),
                self.min_channel_number)

            for key, value in backbones.items():
                assert key in self.keys
                channels[key] = value.get_feature_map_channel(idx)

                if self.fusion_type == 'all' or key.find('main') >= 0:
                    merge_c += channels[key]
                elif self.fusion_type in ['first', 'HR'
                                          ] and idx == self.upsample_layer:
                    merge_c += channels[key]
                elif self.fusion_type in ['last', 'LR'
                                          ] and idx == self.deconv_layer:
                    merge_c += channels[key]

            # add filter conv
            if (self.fusion_type in ['first','HR'] and idx==self.upsample_layer) or \
                (self.fusion_type in ['last','LR'] and idx==self.deconv_layer) or \
                self.fusion_type=='all':
                if self.filter_feature == 'aux':
                    filter_c = sum([
                        value for key, value in channels.items()
                        if key.find('aux') >= 0
                    ])
                else:
                    filter_c = merge_c

                self.filter_layers.append(
                    nn.Sequential(
                        conv_bn_relu(in_channels=filter_c,
                                     out_channels=32,
                                     inplace=inplace),
                        conv_bn_relu(in_channels=32,
                                     out_channels=1,
                                     inplace=inplace,
                                     use_relu=self.filter_relu,
                                     use_bn=self.filter_relu,
                                     bias=not self.filter_relu), nn.Sigmoid()))

                if self.filter_type == 'main':
                    merge_c = sum([
                        value for key, value in channels.items()
                        if key.find('main') >= 0
                    ]) + init_c
            else:
                self.filter_layers.append(None)

            current_layer = [
                conv_bn_relu(in_channels=merge_c,
                             out_channels=out_c,
                             kernel_size=3,
                             stride=1,
                             padding=1,
                             inplace=inplace)
            ]

            if idx == 0 or (self.use_none_layer
                            and idx > 3) or idx == self.upsample_layer:
                pass
            else:
                current_layer.append(
                    nn.ConvTranspose2d(out_c,
                                       out_c,
                                       kernel_size=4,
                                       stride=2,
                                       padding=1,
                                       bias=False))

            for _ in range(self.decode_main_layer):
                current_layer.append(
                    conv_bn_relu(in_channels=out_c,
                                 out_channels=out_c,
                                 kernel_size=3,
                                 stride=1,
                                 padding=1,
                                 inplace=inplace))
            self.layers.append(nn.Sequential(*current_layer))

        self.model_layers = nn.ModuleList(
            [layer for layer in self.layers if layer is not None])
        self.model_filter_layers = nn.ModuleList(
            [layer for layer in self.filter_layers if layer is not None])