def build_pspnet(inputs, label_size, num_classes, preset_model='PSPNet', frontend="ResNet101", pooling_type="MAX", weight_decay=1e-5, upscaling_method="conv", is_training=True, pretrained_dir="network_helpers/models"): """ Builds the PSPNet model. Arguments: inputs: The input tensor label_size: Size of the final label tensor. We need to know this for proper upscaling preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes pooling_type: Max or Average pooling Returns: PSPNet model """ logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend( inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) feature_map_shape = [int(x / 8.0) for x in label_size] print(feature_map_shape) psp = PyramidPoolingModule(end_points['pool3'], feature_map_shape=feature_map_shape, pooling_type=pooling_type) net = slim.conv2d(psp, 512, [3, 3], activation_fn=None) net = slim.batch_norm(net, fused=True) net = tf.nn.relu(net) if upscaling_method.lower() == "conv": net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 256) net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 128) net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 64) elif upscaling_method.lower() == "bilinear": net = Upsampling(net, label_size) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_dense_aspp(inputs, num_classes, preset_model='DenseASPP', frontend="ResNet101", weight_decay=1e-5, is_training=True, pretrained_dir="network_helpers/models"): logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend( inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) init_features = end_points['pool3'] ### First block, rate = 3 d_3_features = DilatedConvBlock(init_features, n_filters=256, kernel_size=[1, 1]) d_3 = DilatedConvBlock(d_3_features, n_filters=64, rate=3, kernel_size=[3, 3]) ### Second block, rate = 6 d_4 = tf.concat([init_features, d_3], axis=-1) d_4 = DilatedConvBlock(d_4, n_filters=256, kernel_size=[1, 1]) d_4 = DilatedConvBlock(d_4, n_filters=64, rate=6, kernel_size=[3, 3]) ### Third block, rate = 12 d_5 = tf.concat([init_features, d_3, d_4], axis=-1) d_5 = DilatedConvBlock(d_5, n_filters=256, kernel_size=[1, 1]) d_5 = DilatedConvBlock(d_5, n_filters=64, rate=12, kernel_size=[3, 3]) ### Fourth block, rate = 18 d_6 = tf.concat([init_features, d_3, d_4, d_5], axis=-1) d_6 = DilatedConvBlock(d_6, n_filters=256, kernel_size=[1, 1]) d_6 = DilatedConvBlock(d_6, n_filters=64, rate=18, kernel_size=[3, 3]) ### Fifth block, rate = 24 d_7 = tf.concat([init_features, d_3, d_4, d_5, d_6], axis=-1) d_7 = DilatedConvBlock(d_7, n_filters=256, kernel_size=[1, 1]) d_7 = DilatedConvBlock(d_7, n_filters=64, rate=24, kernel_size=[3, 3]) full_block = tf.concat([init_features, d_3, d_4, d_5, d_6, d_7], axis=-1) net = slim.conv2d(full_block, num_classes, [1, 1], activation_fn=None, scope='logits') net = Upsampling(net, scale=8) return net, init_fn
def build_refinenet(inputs, num_classes, preset_model='RefineNet', frontend="ResNet101", weight_decay=1e-5, upscaling_method="bilinear", pretrained_dir="models", is_training=True): """ Builds the RefineNet model. Arguments: inputs: The input tensor preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: RefineNet model """ logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) high = [end_points['pool5'], end_points['pool4'], end_points['pool3'], end_points['pool2']] low = [None, None, None, None] # Get the feature maps to the proper size with bottleneck high[0]=slim.conv2d(high[0], 512, 1) high[1]=slim.conv2d(high[1], 256, 1) high[2]=slim.conv2d(high[2], 256, 1) high[3]=slim.conv2d(high[3], 256, 1) # RefineNet low[0]=RefineBlock(high_inputs=high[0],low_inputs=None) # Only input ResNet 1/32 low[1]=RefineBlock(high[1],low[0]) # High input = ResNet 1/16, Low input = Previous 1/16 low[2]=RefineBlock(high[2],low[1]) # High input = ResNet 1/8, Low input = Previous 1/8 low[3]=RefineBlock(high[3],low[2]) # High input = ResNet 1/4, Low input = Previous 1/4 # g[3]=Upsampling(g[3],scale=4) net = low[3] net = ResidualConvUnit(net) net = ResidualConvUnit(net) if upscaling_method.lower() == "conv": net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 128) net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 64) elif upscaling_method.lower() == "bilinear": net = Upsampling(net, scale=4) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_bisenet(inputs, num_classes, preset_model='BiSeNet', frontend="ResNet101", weight_decay=1e-5, is_training=True, pretrained_dir="network_helpers/models"): """ Builds the BiSeNet model. Arguments: inputs: The input tensor= preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: BiSeNet model """ ### The spatial path ### The number of feature maps for each convolution is not specified in the paper ### It was chosen here to be equal to the number of feature maps of a classification ### model at each corresponding stage spatial_net = ConvBlock(inputs, n_filters=64, kernel_size=[3, 3], strides=2) spatial_net = ConvBlock(spatial_net, n_filters=128, kernel_size=[3, 3], strides=2) spatial_net = ConvBlock(spatial_net, n_filters=256, kernel_size=[3, 3], strides=2) ### Context path logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) net_4 = AttentionRefinementModule(end_points['pool4'], n_filters=512) net_5 = AttentionRefinementModule(end_points['pool5'], n_filters=2048) global_channels = tf.reduce_mean(net_5, [1, 2], keep_dims=True) net_5_scaled = tf.multiply(global_channels, net_5) ### Combining the paths net_4 = Upsampling(net_4, scale=2) net_5_scaled = Upsampling(net_5_scaled, scale=4) context_net = tf.concat([net_4, net_5_scaled], axis=-1) net = FeatureFusionModule(input_1=spatial_net, input_2=context_net, n_filters=num_classes) ### Final upscaling and finish net = Upsampling(net, scale=8) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_custom(inputs, num_classes, frontend="ResNet101", weight_decay=1e-5, is_training=True, pretrained_dir="network_helpers/models"): logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(inputs, frontend, is_training=is_training) up_1 = conv_transpose_block(end_points["pool2"], strides=4, n_filters=64) up_2 = conv_transpose_block(end_points["pool3"], strides=8, n_filters=64) up_3 = conv_transpose_block(end_points["pool4"], strides=16, n_filters=64) up_4 = conv_transpose_block(end_points["pool5"], strides=32, n_filters=64) features = tf.concat([up_1, up_2, up_3, up_4], axis=-1) features = conv_block(inputs=features, n_filters=256, filter_size=[1, 1]) features = conv_block(inputs=features, n_filters=64, filter_size=[3, 3]) features = conv_block(inputs=features, n_filters=64, filter_size=[3, 3]) features = conv_block(inputs=features, n_filters=64, filter_size=[3, 3]) net = slim.conv2d(features, num_classes, [1, 1], scope='logits') return net
def build_deeplabv3(inputs, num_classes, preset_model='DeepLabV3', frontend="ResNet101", weight_decay=1e-5, is_training=True, pretrained_dir="models"): """ Builds the DeepLabV3 model. Arguments: inputs: The input tensor= preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: DeepLabV3 model """ logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend( inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) label_size = tf.shape(inputs)[1:3] net = AtrousSpatialPyramidPoolingModule(end_points['pool4']) net = Upsampling(net, label_size) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_deeplabv3_plus(inputs, num_classes, preset_model='DeepLabV3+', frontend="ResNet101", weight_decay=1e-5, is_training=True, pretrained_dir="network_helpers/models"): """ Builds the DeepLabV3 model. Arguments: inputs: The input tensor= preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: DeepLabV3 model """ logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend(inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) label_size = tf.shape(inputs)[1:3] encoder_features = end_points['pool2'] net = AtrousSpatialPyramidPoolingModule(end_points['pool4']) net = slim.conv2d(net, 256, [1, 1], scope="conv_1x1_output", activation_fn=None) decoder_features = Upsampling(net, label_size / 4) encoder_features = slim.conv2d(encoder_features, 48, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=None) net = tf.concat((encoder_features, decoder_features), axis=3) net = slim.conv2d(net, 256, [3, 3], activation_fn=tf.nn.relu, normalizer_fn=None) net = slim.conv2d(net, 256, [3, 3], activation_fn=tf.nn.relu, normalizer_fn=None) net = Upsampling(net, label_size) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_ddsc(inputs, num_classes, preset_model='DDSC', frontend="ResNet101", weight_decay=1e-5, is_training=True, pretrained_dir="network_helpers/models"): """ Builds the Dense Decoder Shortcut Connections model. Arguments: inputs: The input tensor= preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: Dense Decoder Shortcut Connections model """ logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend( inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) ### Adapting features for all stages decoder_4 = EncoderAdaptionBlock(end_points['pool5'], n_filters=1024) decoder_3 = EncoderAdaptionBlock(end_points['pool4'], n_filters=512) decoder_2 = EncoderAdaptionBlock(end_points['pool3'], n_filters=256) decoder_1 = EncoderAdaptionBlock(end_points['pool2'], n_filters=128) decoder_4 = SemanticFeatureGenerationBlock(decoder_4, D_features=1024, D_prime_features=1024 / 4, O_features=1024) ### Fusing features from 3 and 4 decoder_4 = ConvBlock(decoder_4, n_filters=512, kernel_size=[3, 3]) decoder_4 = Upsampling(decoder_4, scale=2) decoder_3 = ConvBlock(decoder_3, n_filters=512, kernel_size=[3, 3]) decoder_3 = tf.add_n([decoder_4, decoder_3]) decoder_3 = SemanticFeatureGenerationBlock(decoder_3, D_features=512, D_prime_features=512 / 4, O_features=512) ### Fusing features from 2, 3, 4 decoder_4 = ConvBlock(decoder_4, n_filters=256, kernel_size=[3, 3]) decoder_4 = Upsampling(decoder_4, scale=4) decoder_3 = ConvBlock(decoder_3, n_filters=256, kernel_size=[3, 3]) decoder_3 = Upsampling(decoder_3, scale=2) decoder_2 = ConvBlock(decoder_2, n_filters=256, kernel_size=[3, 3]) decoder_2 = tf.add_n([decoder_4, decoder_3, decoder_2]) decoder_2 = SemanticFeatureGenerationBlock(decoder_2, D_features=256, D_prime_features=256 / 4, O_features=256) ### Fusing features from 1, 2, 3, 4 decoder_4 = ConvBlock(decoder_4, n_filters=128, kernel_size=[3, 3]) decoder_4 = Upsampling(decoder_4, scale=8) decoder_3 = ConvBlock(decoder_3, n_filters=128, kernel_size=[3, 3]) decoder_3 = Upsampling(decoder_3, scale=4) decoder_2 = ConvBlock(decoder_2, n_filters=128, kernel_size=[3, 3]) decoder_2 = Upsampling(decoder_2, scale=2) decoder_1 = ConvBlock(decoder_1, n_filters=128, kernel_size=[3, 3]) decoder_1 = tf.add_n([decoder_4, decoder_3, decoder_2, decoder_1]) decoder_1 = SemanticFeatureGenerationBlock(decoder_1, D_features=128, D_prime_features=128 / 4, O_features=num_classes) ### Final upscaling and finish net = Upsampling(decoder_1, scale=4) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_gcn(inputs, num_classes, preset_model='GCN', frontend="ResNet101", weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="network_helpers/models"): """ Builds the GCN model. Arguments: inputs: The input tensor preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: GCN model """ logits, end_points, frontend_scope, init_fn = frontend_builder.build_frontend( inputs, frontend, pretrained_dir=pretrained_dir, is_training=is_training) res = [ end_points['pool5'], end_points['pool4'], end_points['pool3'], end_points['pool2'] ] down_5 = GlobalConvBlock(res[0], n_filters=21, size=3) down_5 = BoundaryRefinementBlock(down_5, n_filters=21, kernel_size=[3, 3]) down_5 = ConvUpscaleBlock(down_5, n_filters=21, kernel_size=[3, 3], scale=2) down_4 = GlobalConvBlock(res[1], n_filters=21, size=3) down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3]) down_4 = tf.add(down_4, down_5) down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3]) down_4 = ConvUpscaleBlock(down_4, n_filters=21, kernel_size=[3, 3], scale=2) down_3 = GlobalConvBlock(res[2], n_filters=21, size=3) down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3]) down_3 = tf.add(down_3, down_4) down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3]) down_3 = ConvUpscaleBlock(down_3, n_filters=21, kernel_size=[3, 3], scale=2) down_2 = GlobalConvBlock(res[3], n_filters=21, size=3) down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3]) down_2 = tf.add(down_2, down_3) down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3]) down_2 = ConvUpscaleBlock(down_2, n_filters=21, kernel_size=[3, 3], scale=2) net = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3]) net = ConvUpscaleBlock(net, n_filters=21, kernel_size=[3, 3], scale=2) net = BoundaryRefinementBlock(net, n_filters=21, kernel_size=[3, 3]) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn