Home > Net >  Finding 2D boolean patterns in larger boolean tensors/arrays
Finding 2D boolean patterns in larger boolean tensors/arrays

Time:11-07

I am looking for a way to find a 2D pattern in a MxNxR tensor/array with pytorch or numpy. For instance, to see if a dictionary of tensor of boolean pattern (e.g. {6x6 : freq}) exist in a larger boolean tensor (e.g. 3x256x256).

Then I want to update my patterns and frequencies of the dictionary.

I was hoping that there was a pytorchi way of doing it, instead of having loops over it, or have an optimized loop for doing it.

As far as I know, torch.where works when we have a scalar value. I’m not sure how should I do, if I have a tensor of 6x6 instead of a value. I looked into Finding Patterns in a Numpy Array , but I don't think that it's feasible to follow it for a 2D pattern.

CodePudding user response:

I'm thinking maybe you can pull this off using convolutions. Let's imagine you have an input made up of 0 and 1. Here we will take a minimal example with an u=input of 3x3 and a 2x2 pattern:

>>> x = torch.tensor([[1., 0., 0.],
                      [0., 1., 0.],
                      [1., 0., 0.]])

And the pattern would be:

>>> pattern = torch.tensor([[1., 0.],
                            [0., 1.]])

Here the pattern can be found in the upper left corner of the input.

We perform a convolution with nn.functional.conv2d with 1 - pattern as the kernel.

>>> img, mask = x[None, None], pattern[None, None]
>>> M = F.conv2d(img, 1 - mask)
tensor([[[[0., 1.],
          [2., 0.]]]])

There is a match if and only if the result is equal to the number of 1s in the pattern:

>>> M == mask.sum(dim=(2,3)))
tensor([[[[ True, False],
          [False, False]]]])

You can deduce the frequencies from this final boolean mask. You can extend this method to multiple patterns by adding in kernels in your convolution.

  • Related