Ejemplo n.º 1
0
def extract_image_patches(X,
                          ksizes,
                          strides,
                          padding='valid',
                          data_format='channels_first'):
    patch_size = ksizes[1]
    if padding == 'same':
        padding = 'ignore_borders'
    if data_format == 'channels_last':
        X = KTH.permute_dimensions(X, [0, 3, 1, 2])
    # Thanks to https://github.com/awentzonline for the help!
    batch, c, w, h = KTH.shape(X)
    xs = KTH.shape(X)
    num_rows = 1 + (xs[-2] - patch_size) // strides[1]
    num_cols = 1 + (xs[-1] - patch_size) // strides[1]
    num_channels = xs[-3]
    patches = images2neibs(X, ksizes, strides, padding)
    # Theano is sorting by channel
    patches = KTH.reshape(
        patches,
        (batch, num_channels, num_rows * num_cols, patch_size, patch_size))
    patches = KTH.permute_dimensions(patches, (0, 2, 1, 3, 4))
    # arrange in a 2d-grid (rows, cols, channels, px, py)
    patches = KTH.reshape(
        patches,
        (batch, num_rows, num_cols, num_channels, patch_size, patch_size))
    if data_format == 'channels_last':
        patches = KTH.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
    return patches
Ejemplo n.º 2
0
def extract_image_patches(X,
                          ksizes,
                          strides,
                          padding="valid",
                          data_format="channels_first"):
    """
    Extract the patches from an image
    Parameters
    ----------
    X : The input image
    ksizes : 2-d tuple with the kernel size
    strides : 2-d tuple with the strides size
    padding : 'same' or 'valid'
    data_format : 'channels_last' or 'channels_first'
    Returns
    -------
    The (k_w,k_h) patches extracted
    TF ==> (batch_size,w,h,k_w,k_h,c)
    TH ==> (batch_size,w,h,c,k_w,k_h)
    """
    patch_size = ksizes[1]
    if padding == "same":
        padding = "ignore_borders"
    if data_format == "channels_last":
        X = KTH.permute_dimensions(X, [0, 3, 1, 2])
    # Thanks to https://github.com/awentzonline for the help!
    batch, c, w, h = KTH.shape(X)
    xs = KTH.shape(X)
    num_rows = 1 + (xs[-2] - patch_size) // strides[1]
    num_cols = 1 + (xs[-1] - patch_size) // strides[1]
    num_channels = xs[-3]
    patches = images2neibs(X, ksizes, strides, padding)
    # Theano is sorting by channel
    new_shape = (batch, num_channels, num_rows * num_cols, patch_size,
                 patch_size)
    patches = KTH.reshape(patches, new_shape)
    patches = KTH.permute_dimensions(patches, (0, 2, 1, 3, 4))
    # arrange in a 2d-grid (rows, cols, channels, px, py)
    new_shape = (batch, num_rows, num_cols, num_channels, patch_size,
                 patch_size)
    patches = KTH.reshape(patches, new_shape)
    if data_format == "channels_last":
        patches = KTH.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
    return patches
Ejemplo n.º 3
0
def extract_image_patches(X,
                          ksizes,
                          strides,
                          border_mode="valid",
                          dim_ordering="th"):
    '''
    Extract the patches from an image
    Parameters
    ----------
    X : The input image
    ksizes : 2-d tuple with the kernel size
    strides : 2-d tuple with the strides size
    border_mode : 'same' or 'valid'
    dim_ordering : 'tf' or 'th'
    Returns
    -------
    The (k_w,k_h) patches extracted
    TF ==> (batch_size,w,h,k_w,k_h,c)
    TH ==> (batch_size,w,h,c,k_w,k_h)
    '''
    patch_size = ksizes[1]
    if border_mode == "same":
        border_mode = "ignore_borders"
    if dim_ordering == "tf":
        X = KTH.permute_dimensions(X, [0, 3, 1, 2])
    # Thanks to https://github.com/awentzonline for the help!
    batch, c, w, h = KTH.shape(X)
    xs = KTH.shape(X)
    num_rows = 1 + (xs[-2] - patch_size) // strides[1]
    num_cols = 1 + (xs[-1] - patch_size) // strides[1]
    num_channels = xs[-3]
    patches = images2neibs(X, ksizes, strides, border_mode)
    # Theano is sorting by channel
    patches = KTH.reshape(patches,
                          (batch, num_channels, KTH.shape(patches)[0] //
                           num_channels, patch_size, patch_size))
    patches = KTH.permute_dimensions(patches, (0, 2, 1, 3, 4))
    # arrange in a 2d-grid (rows, cols, channels, px, py)
    patches = KTH.reshape(
        patches,
        (batch, num_rows, num_cols, num_channels, patch_size, patch_size))
    if dim_ordering == "tf":
        patches = KTH.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
    return patches