Convert a Batch Normalization layer.

    Parameters
    keras_layer: layer
        A keras layer object.

    builder: NeuralNetworkBuilder
        A neural network builder object.
439776
def convert_batchnorm(builder, layer, input_names, output_names, keras_layer):
    

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

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

    # Set parameters
    # Parameter arrangement in Keras: gamma, beta, mean, variance
    idx = 0
    gamma, beta = None, None
    if keras_layer.scale:
        gamma = keras_layer.get_weights()[idx]
        idx += 1
    if keras_layer.center:
        beta = keras_layer.get_weights()[idx]
        idx += 1
    mean = keras_layer.get_weights()[idx]
    std = keras_layer.get_weights()[idx+1]

    gamma = _np.ones(mean.shape) if gamma is None else gamma
    beta = _np.zeros(mean.shape) if beta is None else beta

    # 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)
672993
def batch_norm(self,
                   input_layer=None,
                   decay=0.999,
                   scale=False,
                   epsilon=0.001):
        """"""
        if input_layer is None:
            input_layer = self.top_layer
        else:
            self.top_size = None
        name = "batchnorm" + str(self.counts["batchnorm"])
        self.counts["batchnorm"] += 1

        with tf.variable_scope(name) as scope:
            if self.use_tf_layers:
                bn = tf.contrib.layers.batch_norm(
                    input_layer,
                    decay=decay,
                    scale=scale,
                    epsilon=epsilon,
                    is_training=self.phase_train,
                    fused=True,
                    data_format=self.data_format,
                    scope=scope)
            else:
                bn = self._batch_norm_without_layers(input_layer, decay, scale,
                                                     epsilon)
        self.top_layer = bn
        self.top_size = bn.shape[
            3] if self.data_format == "NHWC" else bn.shape[1]
        self.top_size = int(self.top_size)
        return bn
1354469
def convert_upsample(builder, layer, input_names, output_names, keras_layer):
    
    _check_data_format(keras_layer)
    # Get input and output names
    input_name, output_name = (input_names[0], output_names[0])

    is_1d = isinstance(keras_layer, _keras.layers.UpSampling1D)

    # Currently, we only support upsample of same dims
    fh = fw = 1
    if is_1d:
        if type(keras_layer.size) is tuple and len(keras_layer.size) == 1:
            fh, fw = 1, keras_layer.size[0]
        elif type(keras_layer.size) is int:
            fh, fw = 1, keras_layer.size
        else:
            raise ValueError("Unrecognized upsample factor format %s" % (str(keras_layer.size)))
    else:
        if type(keras_layer.size) is int:
            fh = fw = keras_layer.size
        elif len(keras_layer.size) == 2:
            if keras_layer.size[0] != keras_layer.size[1]:
                raise ValueError("Upsample with different rows and columns not supported.")
            else:
                fh = keras_layer.size[0]
                fw = keras_layer.size[1]
        else:
            raise ValueError("Unrecognized upsample factor format %s" % (str(keras_layer.size)))

    builder.add_upsample(name = layer,
             scaling_factor_h = fh,
             scaling_factor_w = fw,
             input_name = input_name,
             output_name = output_name)
569797
def convert_flatten(builder, layer, input_names, output_names, keras_layer):
    
    input_name, output_name = (input_names[0], output_names[0])

    # blob_order == 0 if the input blob needs not be rearranged
    # blob_order == 1 if the input blob needs to be rearranged
    blob_order = 0

    # using keras_layer.input.shape have a "?" (Dimension[None] at the front),
    # making a 3D tensor with unknown batch size 4D
    if len(keras_layer.input.shape) == 4:
        blob_order = 1

    builder.add_flatten(name=layer, mode=blob_order, input_name=input_name, output_name=output_name)
689430
def apply_norm(x, norm_type, depth, epsilon, layer_collection=None):
  """"""
  if layer_collection is not None:
    assert norm_type == "layer"
  if norm_type == "layer":
    return layer_norm(
        x, filters=depth, epsilon=epsilon, layer_collection=layer_collection)
  if norm_type == "group":
    return group_norm(x, filters=depth, epsilon=epsilon)
  if norm_type == "batch":
    return layers().BatchNormalization(epsilon=epsilon)(x)
  if norm_type == "noam":
    return noam_norm(x, epsilon)
  if norm_type == "l2":
    return l2_norm(x, filters=depth, epsilon=epsilon)
  if norm_type == "none":
    return x
  raise ValueError("Parameter normalizer_fn must be one of: 'layer', 'batch',"
                   "'noam', 'lr', 'none'.")
728586
def convert_convolution1d(builder, layer, input_names, output_names, keras_layer):
    
    # Get input and output names
    input_name, output_name = (input_names[0], output_names[0])

    has_bias = keras_layer.use_bias

    # Get the weights from _keras.
    # Keras stores convolution weights as list of numpy arrays
    weightList = keras_layer.get_weights()
    output_shape = list(filter(None, keras_layer.output_shape))[:-1]

    # Parameter
    filter_length, input_dim, n_filters = weightList[0].shape
    stride_width = keras_layer.strides if type(keras_layer.strides) is int \
            else keras_layer.strides[0]

    # Weights and bias terms
    W = _np.expand_dims(weightList[0],axis=0)
    b = weightList[1] if has_bias else None

    dilations = [1,1]
    if (type(keras_layer.dilation_rate) is list) or \
            (type(keras_layer.dilation_rate) is tuple):
        dilations = [1, keras_layer.dilation_rate[0]]
    else:
        dilations = [1, keras_layer.dilation_rate]

    keras_padding = keras_layer.padding
    if keras_padding == 'causal':
        builder.add_padding(name = layer + '__causal_pad__',
                left = filter_length-1, right=0, top=0, bottom=0, value= 0,
                input_name = input_name,
                output_name= input_name + '__causal_pad__')
        input_name = input_name + '__causal_pad__'
        keras_padding = 'valid'

    builder.add_convolution(name = layer,
             kernel_channels = input_dim,
             output_channels = n_filters,
             height = 1,
             width = filter_length,
             stride_height = 1,
             stride_width = stride_width,
             border_mode = keras_padding,
             groups = 1,
             W = W,
             b = b,
             has_bias = has_bias,
             is_deconv = False,
             output_shape = output_shape,
             input_name = input_name,
             output_name = output_name,
             dilation_factors = dilations)
987675
def make_basic_ngpu(nb_classes=10, input_shape=(None, 28, 28, 1), **kwargs):
  
  model = make_basic_cnn()
  layers = model.layers

  model = MLPnGPU(nb_classes, layers, input_shape)
  return model
1070033
def make_input_layers(self):
        
        self.input_layers = []
        if hasattr(self.model, 'input_layers'):
            input_keras_layers = self.model.input_layers[:]
            self.input_layers = [None] * len(input_keras_layers)
            for layer in self.layer_list:
                keras_layer = self.keras_layer_map[layer]
                if isinstance(keras_layer, _keras.engine.topology.InputLayer):
                    if keras_layer in input_keras_layers:
                        idx = input_keras_layers.index(keras_layer)
                        self.input_layers[idx] = layer
        elif len(self.model.inbound_nodes) <= 1:
            for ts in _to_list(self.model.input):
                # search for the InputLayer that matches this ts
                for l in self.layer_list:
                    kl = self.keras_layer_map[l]
                    if isinstance(kl, _keras.engine.topology.InputLayer) and kl.input == ts:
                        self.input_layers.append(l)
        else:
            raise ValueError("Input values cannot be identified.")
292958
def prepare_new_layer(self, input_layer):
        
        # create memory layer
        output_layer_name = os.path.splitext(input_layer.name())[0]
        output_layer_name = unique_filename(
            prefix=('%s_minimum_needs_' % output_layer_name),
            dir='minimum_needs_calculator')
        output_layer = create_memory_layer(
            output_layer_name,
            input_layer.geometryType(),
            input_layer.crs(),
            input_layer.fields())

        # monkey patching input layer to make it work with
        # prepare vector layer function
        temp_layer = input_layer
        temp_layer.keywords = {
            'layer_purpose': layer_purpose_aggregation['key']}

        # copy features to output layer
        copy_layer(temp_layer, output_layer)

        # Monkey patching output layer to make it work with
        # minimum needs calculator
        output_layer.keywords['layer_purpose'] = (
            layer_purpose_aggregation['key'])
        output_layer.keywords['inasafe_fields'] = {
            displaced_field['key']: self.displaced.currentField()
        }
        if self.aggregation_name.currentField():
            output_layer.keywords['inasafe_fields'][
                aggregation_name_field['key']] = (
                    self.aggregation_name.currentField())

        # remove unnecessary fields & rename inasafe fields
        clean_inasafe_fields(output_layer)

        return output_layer
119885
def _get_elementwise_name_from_keras_layer(keras_layer):
    
    if isinstance(keras_layer, _keras.layers.Add):
        return 'ADD'
    elif isinstance(keras_layer, _keras.layers.Multiply):
        return 'MULTIPLY'
    elif isinstance(keras_layer, _keras.layers.Concatenate):
        if len(keras_layer.input_shape[0]) == 3 and (keras_layer.axis == 1 or keras_layer.axis == -2):
            return 'SEQUENCE_CONCAT'
        elif len(keras_layer.input_shape[0]) == 4 and (keras_layer.axis == 3 or keras_layer.axis == -1):
            return 'CONCAT'
        elif len(keras_layer.input_shape[0]) == 2 and (keras_layer.axis == 1 or keras_layer.axis == -1):
            return 'CONCAT'
        else:
            raise ValueError('Only channel and sequence concatenation are supported.')
    elif isinstance(keras_layer, _keras.layers.Dot):
        if len(keras_layer.input_shape[0]) == 2:
            if type(keras_layer.axes) is list or type(keras_layer.axes) is tuple:
                if len(keras_layer.axes) > 1:
                    raise ValueError('Only vector dot-product is supported.')
                else:
                    axis = keras_layer.axes[0]
            else:
                axis = keras_layer.axes
            if axis != -1 and axis != 1:
                 raise ValueError('Only vector dot-product is supported.')

            if keras_layer.normalize:
                return 'COS'
            else:
                return 'DOT'
        else:
            raise ValueError('Only vector dot-product is supported.')
    elif isinstance(keras_layer, _keras.layers.Maximum):
        return 'MAX'
    elif isinstance(keras_layer, _keras.layers.Average):
        return 'AVE'
    else:
        _utils.raise_error_unsupported_option(str(type(keras_layer)), 'merge',
                keras_layer.name)
270963
def convert_simple_rnn(builder, layer, input_names, output_names, keras_layer):
    
    # Get input and output names
    hidden_size = keras_layer.output_dim
    input_size = keras_layer.input_shape[-1]

    output_all = keras_layer.return_sequences
    reverse_input = keras_layer.go_backwards

    if keras_layer.consume_less not in ['cpu', 'gpu']:
        raise ValueError('Cannot convert Keras layer with consume_less = %s' % keras_layer.consume_less)

    W_h = np.zeros((hidden_size, hidden_size))
    W_x = np.zeros((hidden_size, input_size))
    b = np.zeros((hidden_size,))

    if keras_layer.consume_less == 'cpu':
        W_h = keras_layer.get_weights()[1].T
        W_x = keras_layer.get_weights()[0].T
        b = keras_layer.get_weights()[2]
    else:
        W_h = keras_layer.get_weights()[1].T
        W_x = keras_layer.get_weights()[0].T
        b = keras_layer.get_weights()[2]

    # Set actication type
    activation_str = _get_recurrent_activation_name_from_keras(keras_layer.activation)

    # Add to the network
    builder.add_simple_rnn(
        name = layer,
        W_h = W_h, W_x = W_x, b = b,
        hidden_size = hidden_size,
        input_size = input_size,
        activation = activation_str,
        input_names = input_names,
        output_names = output_names,
        output_all=output_all,
        reverse_input=reverse_input)
898188
def convert_convolution1d(builder, layer, input_names, output_names, keras_layer):
    
    # Get input and output names
    input_name, output_name = (input_names[0], output_names[0])

    has_bias = keras_layer.bias

    # Get the weights from keras.
    # Keras stores convolution weights as list of numpy arrays
    weightList = keras_layer.get_weights()
    output_shape = list(filter(None, keras_layer.output_shape))[:-1]

    # Parameter
    # weightList[0].shape = [kernel_length, input_length(time_step), input_dim, num_kernels]
    filter_length, input_length, input_dim, n_filters = weightList[0].shape
    stride_width = keras_layer.subsample[0]

    # Weights and bias terms
    W = weightList[0]
    b = weightList[1] if has_bias else None

    dilation_factors = [1,1]
    if isinstance(keras_layer, keras.layers.convolutional.AtrousConvolution1D):
        dilation_factors[-1] = keras_layer.atrous_rate

    builder.add_convolution(name = layer,
             kernel_channels = input_dim,
             output_channels = n_filters,
             height = 1,
             width = filter_length,
             stride_height = 1,
             stride_width = stride_width,
             border_mode = keras_layer.border_mode,
             groups = 1,
             W = W,
             b = b,
             has_bias = has_bias,
             is_deconv = False,
             output_shape = output_shape,
             input_name = input_name,
             output_name = output_name,
             dilation_factors = dilation_factors)
545329
def gated_linear_unit_layer(x, name=None):
  
  with tf.variable_scope(name, default_name="glu_layer", values=[x]):
    depth = shape_list(x)[-1]
    x = layers().Dense(depth * 2, activation=None)(x)
    x, gating_x = tf.split(x, 2, axis=-1)
    return x * tf.nn.sigmoid(gating_x)
234964
def convert_lstm(builder, layer, input_names, output_names, keras_layer):
    

    hidden_size = keras_layer.units
    input_size = keras_layer.input_shape[-1]
    output_all = keras_layer.return_sequences
    reverse_input = keras_layer.go_backwards

    # Keras: [W_x, W_h, b] each in I F C O
    # CoreML: I F O G; W_h and W_x are separated
    W_h, W_x, b = ([], [], [])
    keras_W_h = keras_layer.get_weights()[1].T
    W_h.append(keras_W_h[0 * hidden_size:][:hidden_size])
    W_h.append(keras_W_h[1 * hidden_size:][:hidden_size])
    W_h.append(keras_W_h[3 * hidden_size:][:hidden_size])
    W_h.append(keras_W_h[2 * hidden_size:][:hidden_size])

    keras_W_x = keras_layer.get_weights()[0].T
    W_x.append(keras_W_x[0 * hidden_size:][:hidden_size])
    W_x.append(keras_W_x[1 * hidden_size:][:hidden_size])
    W_x.append(keras_W_x[3 * hidden_size:][:hidden_size])
    W_x.append(keras_W_x[2 * hidden_size:][:hidden_size])
    if keras_layer.use_bias:
        keras_b = keras_layer.get_weights()[2]
        b.append(keras_b[0 * hidden_size:][:hidden_size])
        b.append(keras_b[1 * hidden_size:][:hidden_size])
        b.append(keras_b[3 * hidden_size:][:hidden_size])
        b.append(keras_b[2 * hidden_size:][:hidden_size])
    if len(b) == 0:
        b = None

    # Set activation type
    inner_activation_str = _get_recurrent_activation_name_from_keras(keras_layer.recurrent_activation)
    activation_str = _get_recurrent_activation_name_from_keras(keras_layer.activation)

    # Add to the network
    builder.add_unilstm(
        name = layer,
        W_h = W_h, W_x = W_x, b = b,
        hidden_size = hidden_size,
        input_size = input_size,
        input_names = input_names,
        output_names = output_names,
        inner_activation = inner_activation_str,
        cell_state_update_activation = activation_str,
        output_activation = activation_str,
        output_all = output_all,
        forget_bias = keras_layer.unit_forget_bias,
        reverse_input = reverse_input)
586292
def layer(self, layer_name):
        
        uri = self.layer_uri(layer_name)
        layer = QgsVectorLayer(uri, layer_name, 'ogr')
        if not layer.isValid():
            layer = QgsRasterLayer(uri, layer_name)
            if not layer.isValid():
                return False

        monkey_patch_keywords(layer)

        return layer
315637
def convert_permute(builder, layer, input_names, output_names, keras_layer):
    
    input_name, output_name = (input_names[0], output_names[0])

    keras_dims = keras_layer.dims
    # Keras permute layer index begins at 1
    if len(keras_dims) == 3:
        # Keras input tensor interpret as (H,W,C)
        x = list(np.array(keras_dims))
        i1, i2, i3 = x.index(1), x.index(2), x.index(3)
        x[i1], x[i2], x[i3] = 2, 3, 1
        # add a sequence axis
        x = [0] + x
        dim = tuple(x)
    elif len(keras_dims) == 4:
        # Here we use Keras converter as a place holder for inserting
        # permutations - the values here are not valid Keras dim parameters
        # but parameters we need to use to convert to CoreML model
        dim = keras_dims
    else:
        raise NotImplementedError('Supports only 3d permutation.')

    builder.add_permute(name = layer, dim=dim, input_name = input_name,
            output_name = output_name)
860681
public InputType getOutputType(InputType... inputType) throws InvalidKerasConfigurationException {
        if (inputType.length > 1)
            throw new InvalidKerasConfigurationException(
                    "Keras BatchNorm layer accepts only one input (received " + inputType.length + ")");
        return this.getBatchNormalizationLayer().getOutputType(-1, inputType[0]);
    }