Python keras.backend.spatial_2d_padding() Examples

The following are code examples for showing how to use keras.backend.spatial_2d_padding(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: deep-mil-for-whole-mammogram-classification   Author: wentaozhu   File: customlayers.py    MIT License 6 votes vote down vote up
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original
    Alexnet
    """
    def f(X):
        b, ch, r, c = X.shape
        half = n // 2
        square = K.square(X)
        extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1))
                                              , (0,half))
        extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
        scale = k
        for i in range(n):
            scale += alpha * extra_channels[:,i:i+ch,:,:]
        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs) 
Example 2
Project: deep-mil-for-whole-mammogram-classification   Author: wentaozhu   File: customlayers.py    MIT License 6 votes vote down vote up
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original
    Alexnet
    """
    def f(X):
        b, ch, r, c = X.shape
        half = n // 2
        square = K.square(X)
        extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1))
                                              , (0,half))
        extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
        scale = k
        for i in range(n):
            scale += alpha * extra_channels[:,i:i+ch,:,:]
        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs) 
Example 3
Project: onnx-keras   Author: leodestiny   File: backend.py    MIT License 6 votes vote down vote up
def get_keras_pad(cls, x, pads, dim, data_format=None):
        if sum(pads) == 0:
            return x
        if len(pads) == dim * 2:
            pads = list(np.transpose(np.array(pads).reshape([2, dim]).astype(np.int32)))
            pads = tuple(tuple(i) for i in pads)
        elif len(pads) == dim:
            pads = tuple((i, i) for i in pads)
        if dim == 1:
            return Lambda(lambda _x: K.temporal_padding(_x, pads))(x)
        elif dim == 2:
            return Lambda(lambda _x: K.spatial_2d_padding(_x, pads, data_format))(x)
        elif dim == 3:
            return Lambda(lambda _x: K.spatial_3d_padding(_x, pads, data_format))(x)
        else:
            raise NotImplementedError("padding with dim {} is not implemented.".format(dim)) 
Example 4
Project: cnn_evaluation_smoke   Author: filonenkoa   File: customlayers.py    GNU General Public License v3.0 6 votes vote down vote up
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original
    Alexnet
    """
    def f(X):
        b, ch, r, c = X.shape
        half = n // 2
        square = K.square(X)
        extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1)))
        extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
        scale = k
        for i in range(n):
            scale += alpha * extra_channels[:,i:i+ch,:,:]
        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs) 
Example 5
Project: deepdriving   Author: babraham123   File: customlayers.py    MIT License 6 votes vote down vote up
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original
    Alexnet
    """
    def f(X):
        b, ch, r, c = X.shape
        half = n // 2
        square = K.square(X)
        a = K.permute_dimensions(square, (0,2,3,1));
        extra_channels = K.spatial_2d_padding(a, (0,half))
        extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
        scale = k
        for i in range(n):
            scale += alpha * extra_channels[:,i:i+ch,:,:]
        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs) 
Example 6
Project: deepdriving   Author: babraham123   File: customlayers.py    MIT License 6 votes vote down vote up
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original
    Alexnet
    """
    def f(X):
        b, ch, r, c = X.shape
        half = n // 2
        square = K.square(X)
        a = K.permute_dimensions(square, (0,2,3,1));
        extra_channels = K.spatial_2d_padding(a, (0,half))
        extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
        scale = k
        for i in range(n):
            scale += alpha * extra_channels[:,i:i+ch,:,:]
        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs) 
Example 7
Project: rna_protein_binding   Author: wentaozhu   File: customlayers.py    MIT License 6 votes vote down vote up
def crosschannelnormalization(alpha = 1e-4, k=2, beta=0.75, n=5,**kwargs):
    """
    This is the function used for cross channel normalization in the original
    Alexnet
    """
    def f(X):
        b, ch, r, c = X.shape
        half = n // 2
        square = K.square(X)
        extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1))
                                              , (0,half))
        extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
        scale = k
        for i in range(n):
            scale += alpha * extra_channels[:,i:i+ch,:,:]
        scale = scale ** beta
        return X / scale

    return Lambda(f, output_shape=lambda input_shape:input_shape,**kwargs) 
Example 8
Project: gcnet_stereo   Author: hmarechal   File: cost_volume.py    MIT License 5 votes vote down vote up
def _concat_features(lf, states):
    b,f,h,w = lf.get_shape().as_list()
    rf = states[0]
    rfs = rf[:, :, :, :-1]
    disp_rfs = K.spatial_2d_padding(rfs, padding=((0, 0), (1, 0)), data_format='channels_first')
    concat = K.concatenate([lf, rf], axis=2)
    output = K.reshape(concat, (-1, 2*f, h, w))
    return output, [disp_rfs]