def convert_pooling(builder, layer, input_names, output_names, keras_layer):
    """
    Convert pooling 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])

    # Pooling layer type
    if isinstance(keras_layer, _keras.layers.convolutional.MaxPooling2D) or \
        isinstance(keras_layer, _keras.layers.convolutional.MaxPooling1D) or \
        isinstance(keras_layer, _keras.layers.pooling.GlobalMaxPooling2D) or \
        isinstance(keras_layer, _keras.layers.pooling.GlobalMaxPooling1D):
        layer_type_str = 'MAX'
    elif isinstance(keras_layer, _keras.layers.convolutional.AveragePooling2D) or \
        isinstance(keras_layer, _keras.layers.convolutional.AveragePooling1D) or \
        isinstance(keras_layer, _keras.layers.pooling.GlobalAveragePooling2D) or \
        isinstance(keras_layer, _keras.layers.pooling.GlobalAveragePooling1D):
        layer_type_str = 'AVERAGE'
    else:
        raise TypeError("Pooling type %s not supported" % keras_layer)

    # if it's global, set the global flag
    if isinstance(keras_layer, _keras.layers.pooling.GlobalMaxPooling2D) or \
        isinstance(keras_layer, _keras.layers.pooling.GlobalAveragePooling2D):
        # 2D global pooling
        global_pooling = True
        height, width = (0, 0)
        stride_height, stride_width = (0,0)
        padding_type = 'VALID'
    elif isinstance(keras_layer, _keras.layers.pooling.GlobalMaxPooling1D) or \
        isinstance(keras_layer, _keras.layers.pooling.GlobalAveragePooling1D):
        # 1D global pooling: 1D global pooling seems problematic in the backend,
        # use this work-around
        global_pooling = False
        _, width, channels = keras_layer.input_shape
        height = 1
        stride_height, stride_width = height, width
        padding_type = 'VALID'
    else:
        global_pooling = False
        # Set pool sizes and strides
        # 1D cases:
        if isinstance(keras_layer, _keras.layers.convolutional.MaxPooling1D) or \
            isinstance(keras_layer, _keras.layers.pooling.GlobalMaxPooling1D) or \
            isinstance(keras_layer, _keras.layers.convolutional.AveragePooling1D) or \
            isinstance(keras_layer, _keras.layers.pooling.GlobalAveragePooling1D):
            pool_size = keras_layer.pool_size if type(keras_layer.pool_size) is int else keras_layer.pool_size[0]
            height, width = 1, pool_size
            if keras_layer.strides is not None:
                strides = keras_layer.strides if type(keras_layer.strides) is int else keras_layer.strides[0]
                stride_height, stride_width = 1, strides
            else:
                stride_height, stride_width = 1, pool_size
        # 2D cases:
        else:
            height, width = keras_layer.pool_size
            if keras_layer.strides is None:
                stride_height, stride_width = height, width
            else:
                stride_height, stride_width = keras_layer.strides

        # Padding
        padding = keras_layer.padding
        if keras_layer.padding == 'valid':
            padding_type = 'VALID'
        elif keras_layer.padding == 'same':
            padding_type = 'SAME'
        else:
            raise TypeError("Border mode %s not supported" % padding)

    builder.add_pooling(name = layer,
        height = height,
        width = width,
        stride_height = stride_height,
        stride_width = stride_width,
        layer_type = layer_type_str,
        padding_type = padding_type,
        input_name = input_name,
        output_name = output_name,
        exclude_pad_area = True,
        is_global = global_pooling)
Convert pooling layer from keras to coreml.

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

    builder: NeuralNetworkBuilder
        A neural network builder object.