0

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.

2
  • what have you tried? post a minimal example Commented Nov 6, 2021 at 14:05
  • I have tried torch.where, and equals, but they only work for scalar values. Commented Nov 6, 2021 at 15:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ivan. I will check it, and let you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.