def __init__(self, feat_level, target_num_channels, apply_bn=False, is_training_bn=None, conv_after_downsample=False, strategy=None, data_format=None, pooling_type=None, upsampling_type=None, name='resample_p0'): super().__init__(name=name) self.apply_bn = apply_bn self.is_training_bn = is_training_bn self.data_format = data_format self.target_num_channels = target_num_channels self.feat_level = feat_level self.strategy = strategy self.conv_after_downsample = conv_after_downsample self.pooling_type = pooling_type or 'max' self.upsampling_type = upsampling_type or 'nearest' self.conv2d = tf.keras.layers.Conv2D( self.target_num_channels, (1, 1), padding='same', data_format=self.data_format, name='conv2d') self.bn = util_keras.build_batch_norm( is_training_bn=self.is_training_bn, data_format=self.data_format, strategy=self.strategy, name='bn')
def __init__(self, is_training_bn, conv_bn_act_pattern, separable_conv, fpn_num_filters, act_type, data_format, strategy, name='op_after_combine'): super().__init__(name=name) self.conv_bn_act_pattern = conv_bn_act_pattern self.separable_conv = separable_conv self.fpn_num_filters = fpn_num_filters self.act_type = act_type self.data_format = data_format self.strategy = strategy self.is_training_bn = is_training_bn if self.separable_conv: conv2d_layer = functools.partial( tf.keras.layers.SeparableConv2D, depth_multiplier=1) else: conv2d_layer = tf.keras.layers.Conv2D self.conv_op = conv2d_layer( filters=fpn_num_filters, kernel_size=(3, 3), padding='same', use_bias=not self.conv_bn_act_pattern, data_format=self.data_format, name='conv') self.bn = util_keras.build_batch_norm( is_training_bn=self.is_training_bn, data_format=self.data_format, strategy=self.strategy, name='bn')
def test_batch_norm(self, is_training, strategy): inputs = tf.random.uniform([8, 40, 40, 3]) expect_results = utils.batch_norm_act(inputs, is_training, None, strategy=strategy) # Call batch norm layer with is_training parameter. bn_layer = util_keras.build_batch_norm(is_training, strategy=strategy) self.assertAllClose(expect_results, bn_layer(inputs, is_training))
def __init__(self, num_classes, num_filters, min_level, max_level, data_format, is_training_bn, act_type, strategy, name='segmentation_head', **kwargs): """Initialize SegmentationHead. Args: num_classes: number of classes. num_filters: number of filters for "intermediate" layers. min_level: minimum level for features. max_level: maximum level for features. data_format: string of 'channel_first' or 'channels_last'. is_training_bn: True if we train the BatchNorm. act_type: String of the activation used. strategy: string to specify training strategy for TPU/GPU/CPU. name: string of name. **kwargs: other parameters. """ super().__init__(name=name, **kwargs) self.act_type = act_type self.con2d_ts = [] self.con2d_t_bns = [] for level in range(max_level - min_level): self.con2d_ts.append( tf.keras.layers.Conv2DTranspose( num_filters, 3, strides=2, padding='same', data_format=data_format, use_bias=False)) self.con2d_t_bns.append( util_keras.build_batch_norm( is_training_bn=is_training_bn, data_format=data_format, strategy=strategy, name='bn_' + str(level))) self.head_transpose = tf.keras.layers.Conv2DTranspose( num_classes, 3, strides=2, padding='same')
def __init__(self, num_anchors=9, num_filters=32, min_level=3, max_level=7, is_training_bn=False, act_type='swish', repeats=4, separable_conv=True, survival_prob=None, strategy=None, data_format='channels_last', grad_checkpoint=False, name='box_net', feature_only=False, **kwargs): """Initialize BoxNet. Args: num_anchors: number of anchors used. num_filters: number of filters for "intermediate" layers. min_level: minimum level for features. max_level: maximum level for features. is_training_bn: True if we train the BatchNorm. act_type: String of the activation used. repeats: number of "intermediate" layers. separable_conv: True to use separable_conv instead of conv2D. survival_prob: if a value is set then drop connect will be used. strategy: string to specify training strategy for TPU/GPU/CPU. data_format: string of 'channel_first' or 'channels_last'. grad_checkpoint: bool, If true, apply grad checkpoint for saving memory. name: Name of the layer. feature_only: build the base feature network only (excluding box class head). **kwargs: other parameters. """ super().__init__(name=name, **kwargs) self.num_anchors = num_anchors self.num_filters = num_filters self.min_level = min_level self.max_level = max_level self.repeats = repeats self.separable_conv = separable_conv self.is_training_bn = is_training_bn self.survival_prob = survival_prob self.act_type = act_type self.strategy = strategy self.data_format = data_format self.grad_checkpoint = grad_checkpoint self.feature_only = feature_only self.conv_ops = [] self.bns = [] for i in range(self.repeats): # If using SeparableConv2D if self.separable_conv: self.conv_ops.append( tf.keras.layers.SeparableConv2D( filters=self.num_filters, depth_multiplier=1, pointwise_initializer=tf.initializers.variance_scaling(), depthwise_initializer=tf.initializers.variance_scaling(), data_format=self.data_format, kernel_size=3, activation=None, bias_initializer=tf.zeros_initializer(), padding='same', name='box-%d' % i)) # If using Conv2d else: self.conv_ops.append( tf.keras.layers.Conv2D( filters=self.num_filters, kernel_initializer=tf.random_normal_initializer(stddev=0.01), data_format=self.data_format, kernel_size=3, activation=None, bias_initializer=tf.zeros_initializer(), padding='same', name='box-%d' % i)) bn_per_level = [] for level in range(self.min_level, self.max_level + 1): bn_per_level.append( util_keras.build_batch_norm( is_training_bn=self.is_training_bn, strategy=self.strategy, data_format=self.data_format, name='box-%d-bn-%d' % (i, level))) self.bns.append(bn_per_level) self.boxes = self.boxes_layer( separable_conv, num_anchors, data_format, name='box-predict')