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 entry_flow(self, data): param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=None, initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.09)) with scope("entry_flow"): with scope("conv1"): data = bn_relu( conv(data, 32, 3, stride=2, padding=1, param_attr=param_attr)) with scope("conv2"): data = bn_relu( conv(data, 64, 3, stride=1, padding=1, param_attr=param_attr)) # get entry flow params block_num = self.bottleneck_params["entry_flow"][0] strides = self.bottleneck_params["entry_flow"][1] chns = self.bottleneck_params["entry_flow"][2] strides = check_data(strides, block_num) chns = check_data(chns, block_num) #print("entry:", block_num, strides, chns) # params to control your flow s = self.stride block_point = self.block_point output_stride = self.output_stride #print("entry:", s, block_point, output_stride) with scope("entry_flow"): for i in range(block_num): block_point = block_point + 1 with scope("block" + str(i + 1)): stride = strides[i] if check_stride( s * strides[i], output_stride) else 1 data, short_cuts = self.xception_block( data, chns[i], [1, 1, stride]) s = s * stride if check_points(block_point, self.decode_points): #print("decode shortcut:", block_point) self.short_cuts[block_point] = short_cuts[1] #print("entry:", i, data.shape) self.stride = s self.block_point = block_point #print("entry:", s, block_point, output_stride) return data
def double_conv(data, out_ch): param_attr = fluid.ParamAttr( name='weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.33)) with scope("conv0"): data = bn_relu( conv(data, out_ch, 3, stride=1, padding=1, param_attr=param_attr)) with scope("conv1"): data = bn_relu( conv(data, out_ch, 3, stride=1, padding=1, param_attr=param_attr)) return data
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