def decoder(encode_data, decode_shortcut): # 解码器配置 # encode_data:编码器输出 # decode_shortcut: 从backbone引出的分支, resize后与encode_data concat # DECODER_USE_SEP_CONV: 默认为真,则concat后连接两个可分离卷积,否则为普通卷积 param_attr = fluid.ParamAttr(name=name_scope + 'weights', regularizer=None, initializer=fluid.initializer.TruncatedNormal( loc=0.0, scale=0.06)) with scope('decoder'): with scope('concat'): decode_shortcut = bn_relu( conv(decode_shortcut, 48, 1, 1, groups=1, padding=0, param_attr=param_attr)) encode_data = fluid.layers.resize_bilinear( encode_data, decode_shortcut.shape[2:]) encode_data = fluid.layers.concat([encode_data, decode_shortcut], axis=1) if cfg.MODEL.DEEPLAB.DECODER_USE_SEP_CONV: with scope("separable_conv1"): encode_data = separate_conv(encode_data, 256, 1, 3, dilation=1, act=relu) with scope("separable_conv2"): encode_data = separate_conv(encode_data, 256, 1, 3, dilation=1, act=relu) else: with scope("decoder_conv1"): encode_data = bn_relu( conv(encode_data, 256, stride=1, filter_size=3, dilation=1, padding=1, param_attr=param_attr)) with scope("decoder_conv2"): encode_data = bn_relu( conv(encode_data, 256, stride=1, filter_size=3, dilation=1, padding=1, param_attr=param_attr)) return encode_data
def _decoder_with_concat(encode_data, decode_shortcut, param_attr): with scope('concat'): decode_shortcut = bn_relu( conv( decode_shortcut, 48, 1, 1, groups=1, padding=0, param_attr=param_attr)) encode_data = fluid.layers.resize_bilinear(encode_data, decode_shortcut.shape[2:]) encode_data = fluid.layers.concat([encode_data, decode_shortcut], axis=1) if cfg.MODEL.DEEPLAB.DECODER_USE_SEP_CONV: with scope("separable_conv1"): encode_data = separate_conv( encode_data, cfg.MODEL.DEEPLAB.DECODER.CONV_FILTERS, 1, 3, dilation=1, act=relu) with scope("separable_conv2"): encode_data = separate_conv( encode_data, cfg.MODEL.DEEPLAB.DECODER.CONV_FILTERS, 1, 3, dilation=1, act=relu) else: with scope("decoder_conv1"): encode_data = bn_relu( conv( encode_data, cfg.MODEL.DEEPLAB.DECODER.CONV_FILTERS, stride=1, filter_size=3, dilation=1, padding=1, param_attr=param_attr)) with scope("decoder_conv2"): encode_data = bn_relu( conv( encode_data, cfg.MODEL.DEEPLAB.DECODER.CONV_FILTERS, stride=1, filter_size=3, dilation=1, padding=1, param_attr=param_attr)) return encode_data
def xception_block(self, input, channels, strides=1, filters=3, dilation=1, skip_conv=True, has_skip=True, activation_fn_in_separable_conv=False): repeat_number = 3 channels = check_data(channels, repeat_number) filters = check_data(filters, repeat_number) strides = check_data(strides, repeat_number) data = input results = [] for i in range(repeat_number): with scope('separable_conv' + str(i + 1)): if not activation_fn_in_separable_conv: data = relu(data) data = separate_conv( data, channels[i], strides[i], filters[i], dilation=dilation) else: data = separate_conv( data, channels[i], strides[i], filters[i], dilation=dilation, act=relu) results.append(data) if not has_skip: return data, results if skip_conv: param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=None, initializer=fluid.initializer.TruncatedNormal( loc=0.0, scale=0.09)) with scope('shortcut'): skip = bn( conv( input, channels[-1], 1, strides[-1], groups=1, padding=0, param_attr=param_attr)) else: skip = input return data + skip, results
def net(self, x): with scope('dsconv1'): x = separate_conv(x, self.dw_channels, stride=self.stride, filter=3, act=fluid.layers.relu) with scope('dsconv2'): x = separate_conv(x, self.dw_channels, stride=self.stride, filter=3, act=fluid.layers.relu) x = dropout2d(x, 0.1, is_train=cfg.PHASE == 'train') x = conv(x, self.num_classes, 1, bias_attr=True) return x
def learning_to_downsample(x, dw_channels1=32, dw_channels2=48, out_channels=64): x = relu(bn(conv(x, dw_channels1, 3, 2))) with scope('dsconv1'): x = separate_conv(x, dw_channels2, stride=2, filter=3, act=fluid.layers.relu) with scope('dsconv2'): x = separate_conv(x, out_channels, stride=2, filter=3, act=fluid.layers.relu) return x
def encoder(input): #优化方向:可考虑ASPP_WITH_SE模块 # 编码器配置,采用ASPP架构,pooling + 1x1_conv + 三个不同尺度的空洞卷积并行, concat后1x1conv # ASPP_WITH_SEP_CONV:默认为真,使用depthwise可分离卷积,否则使用普通卷积 # OUTPUT_STRIDE: 下采样倍数,8或16,决定aspp_ratios大小 # aspp_ratios:ASPP模块空洞卷积的采样率 aspp_ratios = [6, 12, 18] param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=None, initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.06)) concat_logits = [] with scope('encoder'): #channel = cfg.MODEL.DEEPLAB.ENCODER.ASPP_CONVS_FILTERS channel = 256 with scope("image_pool"): #if not cfg.MODEL.DEEPLAB.ENCODER.POOLING_CROP_SIZE: image_avg = fluid.layers.reduce_mean( input, [2, 3], keep_dim=True) #else: # pool_w = int((cfg.MODEL.DEEPLAB.ENCODER.POOLING_CROP_SIZE[0] - # 1.0) / cfg.MODEL.DEEPLAB.OUTPUT_STRIDE + 1.0) # pool_h = int((cfg.MODEL.DEEPLAB.ENCODER.POOLING_CROP_SIZE[1] - # 1.0) / cfg.MODEL.DEEPLAB.OUTPUT_STRIDE + 1.0) # image_avg = fluid.layers.pool2d( # input, # pool_size=(pool_h, pool_w), # pool_stride=cfg.MODEL.DEEPLAB.ENCODER.POOLING_STRIDE, # pool_type='avg', # pool_padding='VALID') #act = qsigmoid if cfg.MODEL.DEEPLAB.ENCODER.SE_USE_QSIGMOID else bn_relu act = bn_relu image_avg = act( conv( image_avg, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)) image_avg = fluid.layers.resize_bilinear(image_avg, input.shape[2:]) #if cfg.MODEL.DEEPLAB.ENCODER.ADD_IMAGE_LEVEL_FEATURE: concat_logits.append(image_avg) with scope("aspp0"): aspp0 = bn_relu( conv( input, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)) concat_logits.append(aspp0) if aspp_ratios: with scope("aspp1"): #if cfg.MODEL.DEEPLAB.ASPP_WITH_SEP_CONV: aspp1 = separate_conv( input, channel, 1, 3, dilation=aspp_ratios[0], act=relu) # else: # aspp1 = bn_relu( # conv( # input, # channel, # stride=1, # filter_size=3, # dilation=aspp_ratios[0], # padding=aspp_ratios[0], # param_attr=param_attr)) concat_logits.append(aspp1) with scope("aspp2"): #if cfg.MODEL.DEEPLAB.ASPP_WITH_SEP_CONV: aspp2 = separate_conv( input, channel, 1, 3, dilation=aspp_ratios[1], act=relu) # else: # aspp2 = bn_relu( # conv( # input, # channel, # stride=1, # filter_size=3, # dilation=aspp_ratios[1], # padding=aspp_ratios[1], # param_attr=param_attr)) concat_logits.append(aspp2) with scope("aspp3"): #if cfg.MODEL.DEEPLAB.ASPP_WITH_SEP_CONV: aspp3 = separate_conv( input, channel, 1, 3, dilation=aspp_ratios[2], act=relu) # else: # aspp3 = bn_relu( # conv( # input, # channel, # stride=1, # filter_size=3, # dilation=aspp_ratios[2], # padding=aspp_ratios[2], # param_attr=param_attr)) concat_logits.append(aspp3) with scope("concat"): data = fluid.layers.concat(concat_logits, axis=1) #if cfg.MODEL.DEEPLAB.ENCODER.ASPP_WITH_CONCAT_PROJECTION: data = bn_relu( conv( data, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)) data = fluid.layers.dropout(data, 0.9) #if cfg.MODEL.DEEPLAB.ENCODER.ASPP_WITH_SE: # data = data * image_avg return data
def encoder(input): # 编码器配置,采用ASPP架构,pooling + 1x1_conv + 三个不同尺度的空洞卷积并行, concat后1x1conv # ASPP_WITH_SEP_CONV:默认为真,使用depthwise可分离卷积,否则使用普通卷积 # OUTPUT_STRIDE: 下采样倍数,8或16,决定aspp_ratios大小 # aspp_ratios:ASPP模块空洞卷积的采样率 if cfg.MODEL.DEEPLAB.OUTPUT_STRIDE == 16: aspp_ratios = [6, 12, 18] elif cfg.MODEL.DEEPLAB.OUTPUT_STRIDE == 8: aspp_ratios = [12, 24, 36] else: raise Exception("deeplab only support stride 8 or 16") param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=None, initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.06)) with scope('encoder'): channel = 256 with scope("image_pool"): if cfg.MODEL.FP16: image_avg = fluid.layers.reduce_mean( fluid.layers.cast(input, 'float32'), [2, 3], keep_dim=True) image_avg = fluid.layers.cast(image_avg, 'float16') else: image_avg = fluid.layers.reduce_mean( input, [2, 3], keep_dim=True) image_avg = bn_relu( conv( image_avg, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)) if cfg.MODEL.FP16: image_avg = fluid.layers.cast(image_avg, 'float32') image_avg = fluid.layers.resize_bilinear(image_avg, input.shape[2:]) if cfg.MODEL.FP16: image_avg = fluid.layers.cast(image_avg, 'float16') with scope("aspp0"): aspp0 = bn_relu( conv( input, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)) with scope("aspp1"): if cfg.MODEL.DEEPLAB.ASPP_WITH_SEP_CONV: aspp1 = separate_conv( input, channel, 1, 3, dilation=aspp_ratios[0], act=relu) else: aspp1 = bn_relu( conv( input, channel, stride=1, filter_size=3, dilation=aspp_ratios[0], padding=aspp_ratios[0], param_attr=param_attr)) with scope("aspp2"): if cfg.MODEL.DEEPLAB.ASPP_WITH_SEP_CONV: aspp2 = separate_conv( input, channel, 1, 3, dilation=aspp_ratios[1], act=relu) else: aspp2 = bn_relu( conv( input, channel, stride=1, filter_size=3, dilation=aspp_ratios[1], padding=aspp_ratios[1], param_attr=param_attr)) with scope("aspp3"): if cfg.MODEL.DEEPLAB.ASPP_WITH_SEP_CONV: aspp3 = separate_conv( input, channel, 1, 3, dilation=aspp_ratios[2], act=relu) else: aspp3 = bn_relu( conv( input, channel, stride=1, filter_size=3, dilation=aspp_ratios[2], padding=aspp_ratios[2], param_attr=param_attr)) with scope("concat"): data = fluid.layers.concat([image_avg, aspp0, aspp1, aspp2, aspp3], axis=1) data = bn_relu( conv( data, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)) data = fluid.layers.dropout(data, 0.9) return data