def get_model( model_spec, kernel_initializer=tf.initializers.he_normal(), depthwise_initializer=depthwise_initializers.depthwise_he_normal(), dense_initializer=tf.initializers.random_normal(stddev=0.01), kernel_regularizer=_l2_regularizer(0.00004), batch_norm_epsilon=0.001, force_stateless_batch_norm=False, dropout_rate=0, num_classes=1001): """Get a new Layer representing an architecture or one-shot model. Args: model_spec: basic_specs.ConvTowerSpec namedtuple controlling the search space to use. kernel_initializer: TF initializer to use for the kernels of ordinary convolutions. depthwise_initializer: TF initializer to use for the kernels of depthwise convolutions. dense_initializer: TF initializer to use for the final dense (fully connected) layer of the network. kernel_regularizer: TF regularizer to apply to kernels. batch_norm_epsilon: Positive float, the value of `epsilon` to use in batch normalization to prevent division by zero. force_stateless_batch_norm: Boolean. If true, we will run all batch norm ops in 'training' mode, even at eval time. dropout_rate: Float between 0 and 1. The fraction of elements to drop immediately before the final 1x1 convolution. num_classes: An integer. The expected number of output classes. Returns: A layers.Sequential object whose aux_inputs are detection endpoints. """ if dropout_rate < 0 or dropout_rate > 1: raise ValueError( 'dropout_rate must be between 0 and 1: {:f}'.format(dropout_rate)) if model_spec.filters_base < 1: raise ValueError( 'filters_base must be a positive integer: {:s}'.format( str(model_spec.filters_base))) params = { 'filters_base': model_spec.filters_base, 'kernel_initializer': kernel_initializer, 'depthwise_initializer': depthwise_initializer, 'dense_initializer': dense_initializer, 'kernel_regularizer': kernel_regularizer, 'batch_norm_epsilon': batch_norm_epsilon, 'force_stateless_batch_norm': force_stateless_batch_norm, 'dropout_rate': dropout_rate, 'num_classes': num_classes, } return _build_model(params, model_spec)
def test_depthwise_he_normal_initializer_end_to_end(self): # This is an end-to-end test for the depthwise_he_normal() function. # We apply a depthwise_he_normal() to a tensor, and verify that the # distribution of outputs matches what we expect. input_tensor = tf.random.normal(shape=(32, 20, 20, 1024), mean=0.0, stddev=1) kernel_initializer = depthwise_initializers.depthwise_he_normal() kernel = tf.get_variable(name='kernel', initializer=kernel_initializer, shape=[5, 5, 1024, 1]) output_tensor = tf.nn.depthwise_conv2d(tf.nn.relu(input_tensor), kernel, strides=(1, 1, 1, 1), padding='VALID') self.evaluate(tf.global_variables_initializer()) result = self.evaluate(output_tensor) self.assertNear(np.mean(result), 0.0, 0.05) self.assertNear(np.std(result), 1.0, 0.05)