def convert_batchnorm(builder, layer, input_names, output_names, keras_layer):
    """
    Parameters
    keras_layer: layer
        A keras layer object.

    builder: NeuralNetworkBuilder
        A neural network builder object.
    """

    # Get input and output names
    input_name, output_name = (input_names[0], output_names[0])

    # Currently CoreML supports only per-channel batch-norm
    if keras_layer.mode != 0:
        raise NotImplementedError(
            'Currently supports only per-feature normalization')

    axis = keras_layer.axis
    nb_channels = keras_layer.input_shape[axis]


    # Set parameters
    # Parameter arrangement in Keras: gamma, beta, mean, variance
    gamma = keras_layer.get_weights()[0]
    beta = keras_layer.get_weights()[1]
    mean = keras_layer.get_weights()[2]
    std = keras_layer.get_weights()[3]
    # compute adjusted parameters
    variance = std * std
    f = 1.0 / np.sqrt(std + keras_layer.epsilon)
    gamma1 = gamma*f
    beta1 = beta - gamma*mean*f
    mean[:] = 0.0 #mean
    variance[:] = 1.0 - .00001 #stddev

    builder.add_batchnorm(
        name = layer,
        channels = nb_channels,
        gamma = gamma1,
        beta = beta1,
        mean = mean,
        variance = variance,
        input_name = input_name,
        output_name = output_name)
def convert_batchnorm ( builder , layer , input_names , output_names , keras_layer ) : input_name , output_name = ( input_names [ 0 ] , output_names [ 0 ] ) if keras_layer . mode != 0 : raise NotImplementedError ( 'Currently supports only per-feature normalization' ) axis = keras_layer . axis nb_channels = keras_layer . input_shape [ axis ] gamma = keras_layer . get_weights ( ) [ 0 ] beta = keras_layer . get_weights ( ) [ 1 ] mean = keras_layer . get_weights ( ) [ 2 ] std = keras_layer . get_weights ( ) [ 3 ] variance = std * std f = 1.0 / np . sqrt ( std + keras_layer . epsilon ) gamma1 = gamma * f beta1 = beta - gamma * mean * f mean [ : ] = 0.0 variance [ : ] = 1.0 - .00001 builder . add_batchnorm ( name = layer , channels = nb_channels , gamma = gamma1 , beta = beta1 , mean = mean , variance = variance , input_name = input_name , output_name = output_name )
Parameters
    keras_layer: layer
        A keras layer object.

    builder: NeuralNetworkBuilder
        A neural network builder object.