コード例 #1
0
ファイル: nn_layers.py プロジェクト: tensorflow/models
    def build(self, input_shape):
        num_reduced_filters = nn_layers.make_divisible(
            max(1, int(self._in_filters * self._se_ratio)),
            divisor=self._divisible_by,
            round_down_protect=self._round_down_protect)

        self._se_reduce = helper.Conv2DQuantized(
            filters=num_reduced_filters,
            kernel_size=1,
            strides=1,
            padding='same',
            use_bias=True,
            kernel_initializer=tf_utils.clone_initializer(
                self._kernel_initializer),
            kernel_regularizer=self._kernel_regularizer,
            bias_regularizer=self._bias_regularizer,
            activation=helper.NoOpActivation())

        self._se_expand = helper.Conv2DOutputQuantized(
            filters=self._out_filters,
            kernel_size=1,
            strides=1,
            padding='same',
            use_bias=True,
            kernel_initializer=tf_utils.clone_initializer(
                self._kernel_initializer),
            kernel_regularizer=self._kernel_regularizer,
            bias_regularizer=self._bias_regularizer,
            activation=helper.NoOpActivation())

        self._multiply = tfmot.quantization.keras.QuantizeWrapperV2(
            tf.keras.layers.Multiply(),
            configs.Default8BitQuantizeConfig([], [], True))
        self._reduce_mean_quantizer = (
            tfmot.quantization.keras.quantizers.MovingAverageQuantizer(
                num_bits=8,
                per_axis=False,
                symmetric=False,
                narrow_range=False))
        self._reduce_mean_quantizer_vars = self._reduce_mean_quantizer.build(
            None, 'reduce_mean_quantizer_vars', self)

        self._activation_layer = tfmot.quantization.keras.QuantizeWrapperV2(
            tf_utils.get_activation(self._activation, use_keras_layer=True),
            configs.Default8BitActivationQuantizeConfig())
        self._create_gating_activation_layer()

        self._build_quantizer_vars()
        super().build(input_shape)
コード例 #2
0
ファイル: nn_layers.py プロジェクト: npfp/models
    def build(self, input_shape: Sequence[tf.TensorShape]):
        """Creates the variables of the segmentation head."""
        # When input_shape is a list/tuple, the first corresponds to backbone
        # features used for resizing the decoder features (the second) if feature
        # fusion type is `deeplabv3plus`.
        backbone_shape = input_shape[0]
        use_depthwise_convolution = self._config_dict[
            'use_depthwise_convolution']
        random_initializer = tf.keras.initializers.RandomNormal(stddev=0.01)
        conv_kwargs = {
            'kernel_size': 3 if not use_depthwise_convolution else 1,
            'padding': 'same',
            'use_bias': False,
            'kernel_initializer': random_initializer,
            'kernel_regularizer': self._config_dict['kernel_regularizer'],
        }

        norm_layer = (tf.keras.layers.experimental.SyncBatchNormalization
                      if self._config_dict['use_sync_bn'] else
                      tf.keras.layers.BatchNormalization)
        norm_with_quantize = helper.BatchNormalizationQuantized(norm_layer)
        norm_no_quantize = helper.BatchNormalizationNoQuantized(norm_layer)
        norm = helper.norm_by_activation(self._config_dict['activation'],
                                         norm_with_quantize, norm_no_quantize)

        bn_kwargs = {
            'axis': self._bn_axis,
            'momentum': self._config_dict['norm_momentum'],
            'epsilon': self._config_dict['norm_epsilon'],
        }

        if self._config_dict['feature_fusion'] == 'deeplabv3plus':
            # Deeplabv3+ feature fusion layers.
            self._dlv3p_conv = helper.Conv2DQuantized(
                kernel_size=1,
                padding='same',
                use_bias=False,
                kernel_initializer=tf.keras.initializers.RandomNormal(
                    stddev=0.01),
                kernel_regularizer=self._config_dict['kernel_regularizer'],
                name='segmentation_head_deeplabv3p_fusion_conv',
                filters=self._config_dict['low_level_num_filters'],
                activation=helper.NoOpActivation())

            self._dlv3p_norm = norm(
                name='segmentation_head_deeplabv3p_fusion_norm', **bn_kwargs)

        # Segmentation head layers.
        self._convs = []
        self._norms = []
        for i in range(self._config_dict['num_convs']):
            if use_depthwise_convolution:
                self._convs.append(
                    helper.DepthwiseConv2DQuantized(
                        name='segmentation_head_depthwise_conv_{}'.format(i),
                        kernel_size=3,
                        padding='same',
                        use_bias=False,
                        depthwise_initializer=random_initializer,
                        depthwise_regularizer=self.
                        _config_dict['kernel_regularizer'],
                        depth_multiplier=1,
                        activation=helper.NoOpActivation()))
                norm_name = 'segmentation_head_depthwise_norm_{}'.format(i)
                self._norms.append(norm(name=norm_name, **bn_kwargs))
            conv_name = 'segmentation_head_conv_{}'.format(i)
            self._convs.append(
                helper.Conv2DQuantized(
                    name=conv_name,
                    filters=self._config_dict['num_filters'],
                    activation=helper.NoOpActivation(),
                    **conv_kwargs))
            norm_name = 'segmentation_head_norm_{}'.format(i)
            self._norms.append(norm(name=norm_name, **bn_kwargs))

        self._classifier = helper.Conv2DOutputQuantized(
            name='segmentation_output',
            filters=self._config_dict['num_classes'],
            kernel_size=self._config_dict['prediction_kernel_size'],
            padding='same',
            bias_initializer=tf.zeros_initializer(),
            kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.01),
            kernel_regularizer=self._config_dict['kernel_regularizer'],
            bias_regularizer=self._config_dict['bias_regularizer'],
            activation=helper.NoOpActivation())

        self._upsampling_layer = helper.UpSampling2DQuantized(
            size=(self._config_dict['upsample_factor'],
                  self._config_dict['upsample_factor']),
            interpolation='nearest')
        self._resizing_layer = tf.keras.layers.Resizing(
            backbone_shape[1], backbone_shape[2], interpolation='bilinear')

        self._concat_layer = helper.ConcatenateQuantized(axis=self._bn_axis)

        super().build(input_shape)