예제 #1
0
 def extract_image_patches(self,
                           x,
                           ksizes,
                           ssizes,
                           padding='same',
                           data_format='channels_last'):
     """
     Extract the patches from an image
     # Parameters
         x : The input image
         ksizes : 2-d tuple with the kernel size
         ssizes : 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)
     """
     kernel = [1, ksizes[0], ksizes[1], 1]
     strides = [1, ssizes[0], ssizes[1], 1]
     padding = self._preprocess_padding(padding)
     if data_format == 'channels_first':
         x = K.permute_dimensions(x, (0, 2, 3, 1))
     patches = extract_image_patches(x, kernel, strides, [1, 1, 1, 1],
                                     padding)
     return patches
예제 #2
0
    def extract_image_patches(self, input_tensor, k_sizes, s_sizes,
                              padding='same', data_format='channels_last'):
        """ Extract the patches from an image.

        Parameters
        ----------
        input_tensor: tensor
            The input image
        k_sizes: tuple
            2-d tuple with the kernel size
        s_sizes: tuple
            2-d tuple with the strides size
        padding: str, optional
            `"same"` or `"valid"`. Default: `"same"`
        data_format: str, optional.
            `"channels_last"` or `"channels_first"`. Default: `"channels_last"`

        Returns
        -------
        The (k_w, k_h) patches extracted
            Tensorflow ==> (batch_size, w, h, k_w, k_h, c)
            Theano ==> (batch_size, w, h, c, k_w, k_h)
        """
        kernel = [1, k_sizes[0], k_sizes[1], 1]
        strides = [1, s_sizes[0], s_sizes[1], 1]
        padding = self._preprocess_padding(padding)
        if data_format == 'channels_first':
            input_tensor = K.permute_dimensions(input_tensor, (0, 2, 3, 1))
        patches = extract_image_patches(input_tensor, kernel, strides, [1, 1, 1, 1], padding)
        return patches