def _decoder_with_sum_merge(encode_data, decode_shortcut, param_attr): encode_data = fluid.layers.resize_bilinear(encode_data, decode_shortcut.shape[2:]) encode_data = conv( encode_data, #cfg.MODEL.DEEPLAB.DECODER.CONV_FILTERS, 256, 1, 1, groups=1, padding=0, param_attr=param_attr) with scope('merge'): decode_shortcut = conv( decode_shortcut, #cfg.MODEL.DEEPLAB.DECODER.CONV_FILTERS, 256, 1, 1, groups=1, padding=0, param_attr=param_attr) return encode_data + decode_shortcut
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 sub_net_1(input): with scope("conv1_sub1"): tmp = conv(input, 32, 3, 2, padding=1) tmp = bn(tmp, act='relu') with scope("conv2_sub1"): tmp = conv(tmp, 32, 3, 2, padding=1) tmp = bn(tmp, act='relu') with scope("conv3_sub1"): tmp = conv(tmp, 64, 3, 2, padding=1) tmp = bn(tmp, act='relu') with scope("conv3_sub1_proj"): tmp = conv(tmp, 128, 1, 1) tmp = bn(tmp) return tmp
def psp_module(input, out_features): cat_layers = [] sizes = (1, 2, 3, 6) for size in sizes: psp_name = "psp" + str(size) with scope(psp_name): pool = fluid.layers.adaptive_pool2d(input, pool_size=[size, size], pool_type='avg', name=psp_name + '_adapool') data = conv(pool, out_features, filter_size=1, bias_attr=False, name=psp_name + '_conv') data_bn = bn(data, act='relu') interp = fluid.layers.resize_bilinear(data_bn, out_shape=input.shape[2:], name=psp_name + '_interp', align_mode=0) cat_layers.append(interp) cat_layers = [input] + cat_layers out = fluid.layers.concat(cat_layers, axis=1, name='psp_cat') return out
def deeplabv3p_nas(img, num_classes, arch=None): data, decode_shortcut = nas_backbone(img, arch) # 编码器解码器设置 cfg.MODEL.DEFAULT_EPSILON = 1e-5 if cfg.MODEL.DEEPLAB.ENCODER_WITH_ASPP: data = encoder(data) if cfg.MODEL.DEEPLAB.ENABLE_DECODER: data = decoder(data, decode_shortcut) # 根据类别数设置最后一个卷积层输出,并resize到图片原始尺寸 param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.01)) with scope('logit'): logit = conv(data, num_classes, 1, stride=1, padding=0, bias_attr=True, param_attr=param_attr) logit = fluid.layers.resize_bilinear(logit, img.shape[2:]) return logit
def deeplabv3p(img, num_classes): # Backbone设置:xception 或 mobilenetv2 data, decode_shortcut = resnet_vd(img) # 编码器解码器设置 #cfg.MODEL.DEFAULT_EPSILON = 1e-5 #if cfg.MODEL.DEEPLAB.ENCODER_WITH_ASPP: data = encoder(data) #if cfg.MODEL.DEEPLAB.ENABLE_DECODER: data = decoder(data, decode_shortcut) # 根据类别数设置最后一个卷积层输出,并resize到图片原始尺寸 param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.01)) #if not cfg.MODEL.DEEPLAB.DECODER.OUTPUT_IS_LOGITS: with scope('logit'): with fluid.name_scope('last_conv'): logit = conv( data, num_classes, 1, stride=1, padding=0, bias_attr=True, param_attr=param_attr) #else: # logit = data logit = fluid.layers.resize_bilinear(logit, img.shape[2:]) return logit
def deeplabv3p(img, num_classes): # Backbone设置:xception 或 mobilenetv2 if 'xception' in cfg.MODEL.DEEPLAB.BACKBONE: data, decode_shortcut = xception(img) elif 'mobilenet' in cfg.MODEL.DEEPLAB.BACKBONE: data, decode_shortcut = mobilenetv2(img) else: raise Exception("deeplab only support xception and mobilenet backbone") # 编码器解码器设置 cfg.MODEL.DEFAULT_EPSILON = 1e-5 if cfg.MODEL.DEEPLAB.ENCODER_WITH_ASPP: data = encoder(data) if cfg.MODEL.DEEPLAB.ENABLE_DECODER: data = decoder(data, decode_shortcut) # 根据类别数设置最后一个卷积层输出,并resize到图片原始尺寸 param_attr = fluid.ParamAttr( name=name_scope + 'weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.01)) with scope('logit'): with fluid.name_scope('last_conv'): logit = conv(data, num_classes, 1, stride=1, padding=0, bias_attr=True, param_attr=param_attr) logit = fluid.layers.resize_bilinear(logit, img.shape[2:]) return logit
def sub_net_4(input, input_shape): tmp = pyramis_pooling(input, input_shape) with scope("conv5_4_k1"): tmp = conv(tmp, 256, 1, 1) tmp = bn(tmp, act='relu') tmp = interp(tmp, out_shape=np.ceil(input_shape / 16)) return tmp
def CCF24(sub2_out, sub4_out, input_shape): with scope("conv_sub4"): tmp = conv(sub4_out, 128, 3, dilation=2, padding=2) tmp = bn(tmp) tmp = tmp + sub2_out tmp = fluid.layers.relu(tmp) tmp = interp(tmp, np.ceil(input_shape / 8)) return tmp
def CCF124(sub1_out, sub24_out, input_shape): tmp = zero_padding(sub24_out, padding=2) with scope("conv_sub2"): tmp = conv(tmp, 128, 3, dilation=2) tmp = bn(tmp) tmp = tmp + sub1_out tmp = fluid.layers.relu(tmp) tmp = interp(tmp, input_shape // 4) return tmp
def net(self, higher_res_feature, lower_res_feature): h, w = higher_res_feature.shape[2:] lower_res_feature = fluid.layers.resize_bilinear(lower_res_feature, [h, w], align_mode=0) with scope('dwconv'): lower_res_feature = relu( bn(conv(lower_res_feature, self.out_channels, 1))) #(lower_res_feature) with scope('conv_lower_res'): lower_res_feature = bn( conv(lower_res_feature, self.out_channels, 1, bias_attr=True)) with scope('conv_higher_res'): higher_res_feature = bn( conv(higher_res_feature, self.out_channels, 1, bias_attr=True)) out = higher_res_feature + lower_res_feature return relu(out)
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 get_logit(data, num_classes): # 根据类别数设置最后一个卷积层输出 param_attr = fluid.ParamAttr( name='weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.01)) with scope("logit"): data = conv( data, num_classes, 3, stride=1, padding=1, param_attr=param_attr) return data
def psp_module(input, out_features): # Pyramid Scene Parsing 金字塔池化模块 # 输入:backbone输出的特征 # 输出:对输入进行不同尺度pooling, 卷积操作后插值回原始尺寸,并concat # 最后进行一个卷积及BN操作 cat_layers = [] sizes = (1, 2, 3, 6) for size in sizes: psp_name = "psp" + str(size) with scope(psp_name): pool = fluid.layers.adaptive_pool2d( input, pool_size=[size, size], pool_type='avg', name=psp_name + '_adapool') data = conv( pool, out_features, filter_size=1, bias_attr=True, name=psp_name + '_conv') data_bn = bn(data, act='relu') interp = fluid.layers.resize_bilinear( data_bn, out_shape=input.shape[2:], name=psp_name + '_interp') cat_layers.append(interp) cat_layers = [input] + cat_layers[::-1] cat = fluid.layers.concat(cat_layers, axis=1, name='psp_cat') psp_end_name = "psp_end" with scope(psp_end_name): data = conv( cat, out_features, filter_size=3, padding=1, bias_attr=True, name=psp_end_name) out = bn(data, act='relu') return out
def net(self, x): x, _ = inverted_blocks(x, self.in_channels, self.t, self.block_channels[0], self.num_blocks[0], 2, 'inverted_block_1') x, _ = inverted_blocks(x, self.block_channels[0], self.t, self.block_channels[1], self.num_blocks[1], 2, 'inverted_block_2') x, _ = inverted_blocks(x, self.block_channels[1], self.t, self.block_channels[2], self.num_blocks[2], 1, 'inverted_block_3') x = psp_module(x, self.block_channels[2] // 4) with scope('out'): x = relu(bn(conv(x, self.out_channels, 1))) return x
def get_logit(data, num_classes, name="logit"): param_attr = fluid.ParamAttr( name=name + 'weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.01)) with scope(name): data = conv(data, num_classes, 1, stride=1, padding=0, param_attr=param_attr, bias_attr=True) return data
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 get_logit_interp(input, num_classes, out_shape, name="logit"): # 根据类别数决定最后一层卷积输出, 并插值回原始尺寸 param_attr = fluid.ParamAttr( name=name + 'weights', regularizer=fluid.regularizer.L2DecayRegularizer( regularization_coeff=0.0), initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.01)) with scope(name): logit = conv( input, num_classes, filter_size=1, param_attr=param_attr, bias_attr=True, name=name + '_conv') logit_interp = fluid.layers.resize_bilinear( logit, out_shape=out_shape, name=name + '_interp') return logit_interp
def sub_net_2(input): with scope("conv3_1_sub2_proj"): tmp = conv(input, 128, 1, 1) tmp = bn(tmp) return tmp
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 aux_layer(x, num_classes): x = relu(bn(conv(x, 32, 3, padding=1))) x = dropout2d(x, 0.1, is_train=(cfg.PHASE == 'train')) with scope('logit'): x = conv(x, num_classes, 1, bias_attr=True) return x
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