def convert_convolution(builder, layer, input_names, output_names, keras_layer):
    """
    Convert convolution layer from keras to coreml.

    Parameters
    ----------
    keras_layer: layer
        A keras layer object.

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

    _check_data_format(keras_layer)

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

    has_bias = keras_layer.use_bias
    is_deconv = isinstance(keras_layer,
            _keras.layers.convolutional.Conv2DTranspose)

    # Get the weights from _keras.
    weightList = keras_layer.get_weights()

    # Dimensions and weights
    if is_deconv:
        height, width, n_filters, channels = weightList[0].shape
        W = weightList[0].transpose([0,1,3,2])
        try:
            output_blob_shape = list(filter(None, keras_layer.output_shape))
            output_shape = output_blob_shape[:-1]
        except:
            output_shape = None
    else:
        height, width, channels, n_filters = weightList[0].shape
        W = weightList[0]
        output_shape = None
    b = weightList[1] if has_bias else None

    output_channels = n_filters

    stride_height, stride_width = keras_layer.strides

    # Dilations
    dilations = [1,1]
    if (type(keras_layer.dilation_rate) is list) or (type(keras_layer.dilation_rate) is tuple):
        dilations = [keras_layer.dilation_rate[0], keras_layer.dilation_rate[1]]
    else:
        dilations = [keras_layer.dilation_rate, keras_layer.dilation_rate]
    if is_deconv and not dilations == [1,1]:
        raise ValueError("Unsupported non-unity dilation for Deconvolution layer")

    groups = 1
    kernel_channels = channels
    # depth-wise convolution
    if isinstance(keras_layer, DepthwiseConv2D):
        groups = channels
        kernel_channels = 1
        depth_multiplier = keras_layer.depth_multiplier
        W = _np.reshape(W,(height, width,1,channels * depth_multiplier))
        output_channels = channels * depth_multiplier

    builder.add_convolution(name = layer,
             kernel_channels = kernel_channels,
             output_channels = output_channels,
             height = height,
             width = width,
             stride_height = stride_height,
             stride_width = stride_width,
             border_mode = keras_layer.padding,
             groups = groups,
             W = W,
             b = b,
             has_bias = has_bias,
             is_deconv = is_deconv,
             output_shape = output_shape,
             input_name = input_name,
             output_name = output_name,
             dilation_factors = dilations)
def convert_convolution ( builder , layer , input_names , output_names , keras_layer ) : _check_data_format ( keras_layer ) input_name , output_name = ( input_names [ 0 ] , output_names [ 0 ] ) has_bias = keras_layer . use_bias is_deconv = isinstance ( keras_layer , _keras . layers . convolutional . Conv2DTranspose ) weightList = keras_layer . get_weights ( ) if is_deconv : height , width , n_filters , channels = weightList [ 0 ] . shape W = weightList [ 0 ] . transpose ( [ 0 , 1 , 3 , 2 ] ) try : output_blob_shape = list ( filter ( None , keras_layer . output_shape ) ) output_shape = output_blob_shape [ : - 1 ] except : output_shape = None else : height , width , channels , n_filters = weightList [ 0 ] . shape W = weightList [ 0 ] output_shape = None b = weightList [ 1 ] if has_bias else None output_channels = n_filters stride_height , stride_width = keras_layer . strides dilations = [ 1 , 1 ] if ( type ( keras_layer . dilation_rate ) is list ) or ( type ( keras_layer . dilation_rate ) is tuple ) : dilations = [ keras_layer . dilation_rate [ 0 ] , keras_layer . dilation_rate [ 1 ] ] else : dilations = [ keras_layer . dilation_rate , keras_layer . dilation_rate ] if is_deconv and not dilations == [ 1 , 1 ] : raise ValueError ( "Unsupported non-unity dilation for Deconvolution layer" ) groups = 1 kernel_channels = channels if isinstance ( keras_layer , DepthwiseConv2D ) : groups = channels kernel_channels = 1 depth_multiplier = keras_layer . depth_multiplier W = _np . reshape ( W , ( height , width , 1 , channels * depth_multiplier ) ) output_channels = channels * depth_multiplier builder . add_convolution ( name = layer , kernel_channels = kernel_channels , output_channels = output_channels , height = height , width = width , stride_height = stride_height , stride_width = stride_width , border_mode = keras_layer . padding , groups = groups , W = W , b = b , has_bias = has_bias , is_deconv = is_deconv , output_shape = output_shape , input_name = input_name , output_name = output_name , dilation_factors = dilations )
Convert convolution layer from keras to coreml.

    Parameters
    ----------
    keras_layer: layer
        A keras layer object.

    builder: NeuralNetworkBuilder
        A neural network builder object.