1

I need to create stochastic boolean masks for a set of images. Each mask is an array of 1s with 6 random squares, in which the values are 0. The length of the squares are 56 pixels. The mask can be created with the following code:

mask = np.ones(shape=(3, h, w))
for _ in range(6):
        x_coordinate = np.random.randint(0, w)
        y_coordinate = np.random.randint(0, h)
        mask[:, x_coordinate: x_coordinate + 56, y_coordinate: y_coordinate + 56] = 0

Now the tricky thing that I want to do is vectorize this process for a batch of images. Is this even possible? Right now, I'm just calling this function using a simple for-loop for each image in my batch, but I was wondering if there was a way to avoid the for-loop altogether.

ALSO: the mask must be different for each image in the batch (can't use the same mask for each image)

Divakar
  • 218,885
  • 19
  • 262
  • 358
JavaNewb
  • 155
  • 1
  • 7

1 Answers1

1

We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windows and hence solve our case here. More info on use of as_strided based view_as_windows.

Being views-based, it would be as efficient as it can get!

from skimage.util.shape import view_as_windows

N = 3 # number of images in the batch
image_H,image_W = 5,7 # edit to image height, width
bbox_H,bbox_W = 2,3  # edit to window height, width to be set as 0s

w_off = image_W-bbox_W+1
h_off = image_H-bbox_H+1
M = w_off*h_off

R,C = np.unravel_index(np.random.choice(M, size=N, replace=False), (h_off, w_off))

mask_out = np.ones(shape=(N, image_H, image_W), dtype=bool)
win = view_as_windows(mask_out, (1, bbox_H,bbox_W))[...,0,:,:]
win[np.arange(len(R)),R,C] = 0

If you don't mind duplicate masks, simply use replace=True in the code.

Sample output with given input parameters -

In [6]: mask_out
Out[6]: 
array([[[ True,  True,  True,  True,  True,  True,  True],
        [ True,  True, False, False, False,  True,  True],
        [ True,  True, False, False, False,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True]],

       [[ True,  True,  True,  True,  True,  True,  True],
        [ True, False, False, False,  True,  True,  True],
        [ True, False, False, False,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True]],

       [[ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [False, False, False,  True,  True,  True,  True],
        [False, False, False,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True]]])
Divakar
  • 218,885
  • 19
  • 262
  • 358