pyosv.post.patch_extractor

  1import numpy as np
  2
  3
  4def get_patches(img : np.ndarray, kernel : tuple = (16, 16), stride : tuple = (16, 16)) -> np.ndarray:
  5    '''
  6        Split an image into patches
  7
  8        Parameters:
  9        -----------
 10            - img : np.ndarray 
 11                a WxHxB image, with width W, height H and B bands
 12            - kernel : tuple
 13                Tuple of two values used to define the size of the patch
 14            - stride :  tuple
 15                Tuple of two values representing the stride to extract patches
 16
 17        Returns:
 18        --------
 19            - patches : np.ndarray
 20                the Nx(kernel[0])x(kernel[1])xB vector containing the N patches extracted from img
 21        Usage:
 22        ------
 23        ```python
 24        import numpy as np  
 25
 26        img         = np.array(  
 27            [[
 28                [0.1, 0.2, 0.3],  
 29                [0.4, 0.5, 0.6],  
 30                [0.7, 0.8, 0.9]
 31                ],
 32            [
 33                [0.1, 0.2, 0.3],  
 34                [0.4, 0.5, 0.6],  
 35                [0.7, 0.8, 0.9]
 36                ],
 37            [
 38                [0.1, 0.2, 0.3],  
 39                [0.4, 0.5, 0.6],  
 40                [0.7, 0.8, 0.9]
 41                ]
 42            ]  
 43        ) 
 44
 45        # Making channels last
 46        img = np.moveaxis(img, 0, -1)
 47
 48        patches = get_patches(img, kernel=(2, 2), stride=(1,1))
 49        ```
 50
 51        Output:
 52        -------
 53        ```python
 54        [[[[0.1 0.1 0.1]
 55        [0.2 0.2 0.2]]
 56
 57        [[0.4 0.4 0.4]
 58        [0.5 0.5 0.5]]]
 59
 60
 61        [[[0.2 0.2 0.2]
 62        [0.3 0.3 0.3]]
 63
 64        [[0.5 0.5 0.5]
 65        [0.6 0.6 0.6]]]
 66
 67
 68        [[[0.3 0.3 0.3]
 69        [0.3 0.3 0.3]]
 70
 71        [[0.6 0.6 0.6]
 72        [0.6 0.6 0.6]]]
 73
 74
 75        [[[0.4 0.4 0.4]
 76        [0.5 0.5 0.5]]
 77
 78        [[0.7 0.7 0.7]
 79        [0.8 0.8 0.8]]]
 80
 81
 82        [[[0.5 0.5 0.5]
 83        [0.6 0.6 0.6]]
 84
 85        [[0.8 0.8 0.8]
 86        [0.9 0.9 0.9]]]
 87
 88
 89        [[[0.6 0.6 0.6]
 90        [0.6 0.6 0.6]]
 91
 92        [[0.9 0.9 0.9]
 93        [0.9 0.9 0.9]]]
 94
 95
 96        [[[0.7 0.7 0.7]
 97        [0.8 0.8 0.8]]
 98
 99        [[0.7 0.7 0.7]
100        [0.8 0.8 0.8]]]
101
102
103        [[[0.8 0.8 0.8]
104        [0.9 0.9 0.9]]
105
106        [[0.8 0.8 0.8]
107        [0.9 0.9 0.9]]]
108
109
110        [[[0.9 0.9 0.9]
111        [0.9 0.9 0.9]]
112
113        [[0.9 0.9 0.9]
114        [0.9 0.9 0.9]]]]
115        ```
116    '''
117
118    if len(img.shape) != 3:
119        raise Exception("Error: len of img.shape must be 3")
120    if kernel[0] < 1 or kernel[1] < 1:
121        raise Exception("Error: kernel must be grather than 1")
122    if stride[0] < 1 or stride[1] < 1:
123        raise Exception("Error: kernel must be grather than 1")
124    
125
126    w, h, c = img.shape
127
128
129    if kernel[0] > w or kernel[1] > h:
130        raise Exception("Error: kernel size cannot be grather than image size")
131    if stride[0] > w or stride[1] > h:
132        raise Exception("Error: stride cannot be grather than image size")
133    
134
135    
136    N = len(range(0, w, stride[0])) * len(range(0, h, stride[1]))
137    patches = np.zeros((N, kernel[0], kernel[1], c))
138
139    ct = 0
140    for ww in range(0, w, stride[0]):
141        for hh in range(0,  h, stride[1]):         
142            patches[ct, ...] = img[ww:ww+kernel[0],hh:hh+kernel[1],:]
143            ct += 1
144
145    return patches
146
147
148def patches_generator(img : np.ndarray, kernel : tuple = (16, 16), stride : tuple = (16, 16), batch_size : int = 1) -> np.ndarray:
149    '''
150        Split an image into patches
151
152        Parameters:
153        -----------
154            - img : np.ndarray 
155                a WxHxB image, with width W, height H and B bands
156            - kernel : tuple
157                Tuple of two values used to define the size of the patch
158            - stride :  tuple
159                Tuple of two values representing the stride to extract patches
160            - batch_size : int
161                integere representing how many patches yield at each iteration
162
163        Returns:
164        --------
165            - patches : np.ndarray
166                the (batch_size)x(kernel[0])x(kernel[1])xB vector containing the N patches extracted from img
167        Usage:
168        ------
169        ```python
170        import numpy as np  
171
172        img         = np.array(  
173            [[
174                [0.1, 0.2, 0.3],  
175                [0.4, 0.5, 0.6],  
176                [0.7, 0.8, 0.9]
177                ],
178            [
179                [0.1, 0.2, 0.3],  
180                [0.4, 0.5, 0.6],  
181                [0.7, 0.8, 0.9]
182                ],
183            [
184                [0.1, 0.2, 0.3],  
185                [0.4, 0.5, 0.6],  
186                [0.7, 0.8, 0.9]
187                ]
188            ]  
189        ) 
190
191        # Making channels last
192        img = np.moveaxis(img, 0, -1)
193
194        patches = patches_generator(img, kernel=(2, 2), stride=(1,1), batch_size = 1)
195        ```
196
197        Output:
198        -------
199        ```python
200        
201        ```
202    '''
203
204    if len(img.shape) != 3:
205        raise Exception("Error: len of img.shape must be 3")
206    if kernel[0] < 1 or kernel[1] < 1:
207        raise Exception("Error: kernel must be grather than 1")
208    if stride[0] < 1 or stride[1] < 1:
209        raise Exception("Error: kernel must be grather than 1")
210    if batch_size < 1:
211        raise Exception("Error: batch_size must be grather than 1")
212    
213
214    w, h, c = img.shape
215
216
217    if kernel[0] > w or kernel[1] > h:
218        raise Exception("Error: kernel size cannot be grather than image size")
219    if stride[0] > w or stride[1] > h:
220        raise Exception("Error: stride cannot be grather than image size")
221    
222    
223    patches = np.zeros((batch_size, kernel[0], kernel[1], c))   
224
225    
226    ct = 0 
227    for ww in range(0, w-stride[0], stride[0]):
228        for hh in range(0,  h-stride[1], stride[1]):
229
230            patches[ct, :kernel[0], :kernel[1], :] = img[ww:ww+kernel[0],hh:hh+kernel[1],:]
231
232            if ct == batch_size-1:
233                ct = 0
234                yield patches
235            else: ct += 1
def get_patches( img: numpy.ndarray, kernel: tuple = (16, 16), stride: tuple = (16, 16)) -> numpy.ndarray:
  5def get_patches(img : np.ndarray, kernel : tuple = (16, 16), stride : tuple = (16, 16)) -> np.ndarray:
  6    '''
  7        Split an image into patches
  8
  9        Parameters:
 10        -----------
 11            - img : np.ndarray 
 12                a WxHxB image, with width W, height H and B bands
 13            - kernel : tuple
 14                Tuple of two values used to define the size of the patch
 15            - stride :  tuple
 16                Tuple of two values representing the stride to extract patches
 17
 18        Returns:
 19        --------
 20            - patches : np.ndarray
 21                the Nx(kernel[0])x(kernel[1])xB vector containing the N patches extracted from img
 22        Usage:
 23        ------
 24        ```python
 25        import numpy as np  
 26
 27        img         = np.array(  
 28            [[
 29                [0.1, 0.2, 0.3],  
 30                [0.4, 0.5, 0.6],  
 31                [0.7, 0.8, 0.9]
 32                ],
 33            [
 34                [0.1, 0.2, 0.3],  
 35                [0.4, 0.5, 0.6],  
 36                [0.7, 0.8, 0.9]
 37                ],
 38            [
 39                [0.1, 0.2, 0.3],  
 40                [0.4, 0.5, 0.6],  
 41                [0.7, 0.8, 0.9]
 42                ]
 43            ]  
 44        ) 
 45
 46        # Making channels last
 47        img = np.moveaxis(img, 0, -1)
 48
 49        patches = get_patches(img, kernel=(2, 2), stride=(1,1))
 50        ```
 51
 52        Output:
 53        -------
 54        ```python
 55        [[[[0.1 0.1 0.1]
 56        [0.2 0.2 0.2]]
 57
 58        [[0.4 0.4 0.4]
 59        [0.5 0.5 0.5]]]
 60
 61
 62        [[[0.2 0.2 0.2]
 63        [0.3 0.3 0.3]]
 64
 65        [[0.5 0.5 0.5]
 66        [0.6 0.6 0.6]]]
 67
 68
 69        [[[0.3 0.3 0.3]
 70        [0.3 0.3 0.3]]
 71
 72        [[0.6 0.6 0.6]
 73        [0.6 0.6 0.6]]]
 74
 75
 76        [[[0.4 0.4 0.4]
 77        [0.5 0.5 0.5]]
 78
 79        [[0.7 0.7 0.7]
 80        [0.8 0.8 0.8]]]
 81
 82
 83        [[[0.5 0.5 0.5]
 84        [0.6 0.6 0.6]]
 85
 86        [[0.8 0.8 0.8]
 87        [0.9 0.9 0.9]]]
 88
 89
 90        [[[0.6 0.6 0.6]
 91        [0.6 0.6 0.6]]
 92
 93        [[0.9 0.9 0.9]
 94        [0.9 0.9 0.9]]]
 95
 96
 97        [[[0.7 0.7 0.7]
 98        [0.8 0.8 0.8]]
 99
100        [[0.7 0.7 0.7]
101        [0.8 0.8 0.8]]]
102
103
104        [[[0.8 0.8 0.8]
105        [0.9 0.9 0.9]]
106
107        [[0.8 0.8 0.8]
108        [0.9 0.9 0.9]]]
109
110
111        [[[0.9 0.9 0.9]
112        [0.9 0.9 0.9]]
113
114        [[0.9 0.9 0.9]
115        [0.9 0.9 0.9]]]]
116        ```
117    '''
118
119    if len(img.shape) != 3:
120        raise Exception("Error: len of img.shape must be 3")
121    if kernel[0] < 1 or kernel[1] < 1:
122        raise Exception("Error: kernel must be grather than 1")
123    if stride[0] < 1 or stride[1] < 1:
124        raise Exception("Error: kernel must be grather than 1")
125    
126
127    w, h, c = img.shape
128
129
130    if kernel[0] > w or kernel[1] > h:
131        raise Exception("Error: kernel size cannot be grather than image size")
132    if stride[0] > w or stride[1] > h:
133        raise Exception("Error: stride cannot be grather than image size")
134    
135
136    
137    N = len(range(0, w, stride[0])) * len(range(0, h, stride[1]))
138    patches = np.zeros((N, kernel[0], kernel[1], c))
139
140    ct = 0
141    for ww in range(0, w, stride[0]):
142        for hh in range(0,  h, stride[1]):         
143            patches[ct, ...] = img[ww:ww+kernel[0],hh:hh+kernel[1],:]
144            ct += 1
145
146    return patches

Split an image into patches

Parameters:

- img : np.ndarray 
    a WxHxB image, with width W, height H and B bands
- kernel : tuple
    Tuple of two values used to define the size of the patch
- stride :  tuple
    Tuple of two values representing the stride to extract patches

Returns:

- patches : np.ndarray
    the Nx(kernel[0])x(kernel[1])xB vector containing the N patches extracted from img

Usage:

import numpy as np  

img         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ],
    [
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ],
    [
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ]
    ]  
) 

# Making channels last
img = np.moveaxis(img, 0, -1)

patches = get_patches(img, kernel=(2, 2), stride=(1,1))

Output:

[[[[0.1 0.1 0.1]
[0.2 0.2 0.2]]

[[0.4 0.4 0.4]
[0.5 0.5 0.5]]]


[[[0.2 0.2 0.2]
[0.3 0.3 0.3]]

[[0.5 0.5 0.5]
[0.6 0.6 0.6]]]


[[[0.3 0.3 0.3]
[0.3 0.3 0.3]]

[[0.6 0.6 0.6]
[0.6 0.6 0.6]]]


[[[0.4 0.4 0.4]
[0.5 0.5 0.5]]

[[0.7 0.7 0.7]
[0.8 0.8 0.8]]]


[[[0.5 0.5 0.5]
[0.6 0.6 0.6]]

[[0.8 0.8 0.8]
[0.9 0.9 0.9]]]


[[[0.6 0.6 0.6]
[0.6 0.6 0.6]]

[[0.9 0.9 0.9]
[0.9 0.9 0.9]]]


[[[0.7 0.7 0.7]
[0.8 0.8 0.8]]

[[0.7 0.7 0.7]
[0.8 0.8 0.8]]]


[[[0.8 0.8 0.8]
[0.9 0.9 0.9]]

[[0.8 0.8 0.8]
[0.9 0.9 0.9]]]


[[[0.9 0.9 0.9]
[0.9 0.9 0.9]]

[[0.9 0.9 0.9]
[0.9 0.9 0.9]]]]
def patches_generator( img: numpy.ndarray, kernel: tuple = (16, 16), stride: tuple = (16, 16), batch_size: int = 1) -> numpy.ndarray:
149def patches_generator(img : np.ndarray, kernel : tuple = (16, 16), stride : tuple = (16, 16), batch_size : int = 1) -> np.ndarray:
150    '''
151        Split an image into patches
152
153        Parameters:
154        -----------
155            - img : np.ndarray 
156                a WxHxB image, with width W, height H and B bands
157            - kernel : tuple
158                Tuple of two values used to define the size of the patch
159            - stride :  tuple
160                Tuple of two values representing the stride to extract patches
161            - batch_size : int
162                integere representing how many patches yield at each iteration
163
164        Returns:
165        --------
166            - patches : np.ndarray
167                the (batch_size)x(kernel[0])x(kernel[1])xB vector containing the N patches extracted from img
168        Usage:
169        ------
170        ```python
171        import numpy as np  
172
173        img         = np.array(  
174            [[
175                [0.1, 0.2, 0.3],  
176                [0.4, 0.5, 0.6],  
177                [0.7, 0.8, 0.9]
178                ],
179            [
180                [0.1, 0.2, 0.3],  
181                [0.4, 0.5, 0.6],  
182                [0.7, 0.8, 0.9]
183                ],
184            [
185                [0.1, 0.2, 0.3],  
186                [0.4, 0.5, 0.6],  
187                [0.7, 0.8, 0.9]
188                ]
189            ]  
190        ) 
191
192        # Making channels last
193        img = np.moveaxis(img, 0, -1)
194
195        patches = patches_generator(img, kernel=(2, 2), stride=(1,1), batch_size = 1)
196        ```
197
198        Output:
199        -------
200        ```python
201        
202        ```
203    '''
204
205    if len(img.shape) != 3:
206        raise Exception("Error: len of img.shape must be 3")
207    if kernel[0] < 1 or kernel[1] < 1:
208        raise Exception("Error: kernel must be grather than 1")
209    if stride[0] < 1 or stride[1] < 1:
210        raise Exception("Error: kernel must be grather than 1")
211    if batch_size < 1:
212        raise Exception("Error: batch_size must be grather than 1")
213    
214
215    w, h, c = img.shape
216
217
218    if kernel[0] > w or kernel[1] > h:
219        raise Exception("Error: kernel size cannot be grather than image size")
220    if stride[0] > w or stride[1] > h:
221        raise Exception("Error: stride cannot be grather than image size")
222    
223    
224    patches = np.zeros((batch_size, kernel[0], kernel[1], c))   
225
226    
227    ct = 0 
228    for ww in range(0, w-stride[0], stride[0]):
229        for hh in range(0,  h-stride[1], stride[1]):
230
231            patches[ct, :kernel[0], :kernel[1], :] = img[ww:ww+kernel[0],hh:hh+kernel[1],:]
232
233            if ct == batch_size-1:
234                ct = 0
235                yield patches
236            else: ct += 1

Split an image into patches

Parameters:

- img : np.ndarray 
    a WxHxB image, with width W, height H and B bands
- kernel : tuple
    Tuple of two values used to define the size of the patch
- stride :  tuple
    Tuple of two values representing the stride to extract patches
- batch_size : int
    integere representing how many patches yield at each iteration

Returns:

- patches : np.ndarray
    the (batch_size)x(kernel[0])x(kernel[1])xB vector containing the N patches extracted from img

Usage:

import numpy as np  

img         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ],
    [
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ],
    [
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ]
    ]  
) 

# Making channels last
img = np.moveaxis(img, 0, -1)

patches = patches_generator(img, kernel=(2, 2), stride=(1,1), batch_size = 1)

Output: