How to compute L1 distance matrix on tensors?

username: anil_batra
cooked: <p>I am trying to calculate L1 Distance matrix on images and neural network features. I wrote a naive to calculate this using scipy operations on 2d arrays. If any one can suggest fast and efficient approach to calculate the same if I have 4d Tensor (N x C x H x W). I don’t want to iterate over N and C to calculate L1 Distance matrix, as it will slow down the training process.</p>
<pre><code class="lang-auto">import numpy as np
from scipy.ndimage import iterate_structure, generate_binary_structure
import scipy.ndimage as ndimage
from itertools import repeat

def nearestPixelDifference(values,a,idx,offset,size):
        current_value = values[len(values)/2]
        current = np.unravel_index(idx[0],size)
        for i, pair in enumerate(offset):
            ii = np.add(current, pair)
            if ii[0] &lt; 0 or ii[1] &lt; 0:
                continue
            if ii[0] &gt;= size[0] or ii[1] &gt;= size[1]:
                continue
            a[idx[0],np.ravel_multi_index(ii,size)] = np.abs(current_value - values[i])
        idx[0] += 1
        return 0

def getSimilarityMatrix(gt,radius, func):
    index = [0]
    h,w = gt.shape
    foot = np.array(generate_binary_structure(2, 1),dtype=int)
    footprint = np.array(iterate_structure(foot , radius),dtype=int)
    shape = footprint.shape
    num_cols = np.where(footprint==1)[1].shape[0] #int(0.5 * radius*(4 + 2*(radius-1)))# n/2(2a+(n-1)d)
    x,y = np.where(footprint==1)
    offsets = zip(x-shape[0]/2,y-shape[1]/2)
    affinity = np.zeros((h**2,w**2),dtype=np.uint8)

    results = ndimage.generic_filter(gt, func,footprint=footprint,mode='constant',
                                     extra_arguments=(affinity,index,offsets,gt.shape))
    return affinity,footprint
</code></pre>
<p>Given an array x, algorithm computes L1 distance between nearest pixels within radius 2. Following is simple code for the same.</p>
<pre><code class="lang-auto">x = np.array([[1., 1., 1., 0.],
                 [1., 0., 0., 0.],
                 [0., 0., 0., 0.],
                 [0., 1., 1., 1.]])
pixel_affinity, struct = getSimilarityMatrix(x,2,nearestPixelDifference)
</code></pre>
<p>Thanks</p>

username: richard
cooked: <p>What do you mean by “L1 Distance Matrix”? Does <code>nn.L1Loss</code> do what you’re looking for? <a href="https://pytorch.org/docs/master/nn.html?highlight=l1loss#torch.nn.L1Loss">https://pytorch.org/docs/master/nn.html?highlight=l1loss#torch.nn.L1Loss</a></p>

username: anil_batra
cooked: <p>Sorry <a class="mention" href="/u/richard">@richard</a> for not good explanation. I was looking for L1 Pairwise Distance matrix of images. e.g. I have 2x2 image and its L1 Pairwise Distance matrix (within radius distance) will be 4x4<br>
I found one way as follows</p>
<pre><code class="lang-auto">X = [[2,3],
       [-1,5]]
</code></pre>
<pre><code class="lang-auto">L1 = (X - X.t()) * indexMat
L1 = [[0,1,3,0],
[1,0,0,2],
[3,0,0,6],
[0,2,6,0]]
</code></pre>
<p>The same I would like to calculate for 4D CNN features of network. Any efficient way? One I think of calculate full distance matrix and multiply with nearest neighbor indices? But that matrix will be sparse and how to reduce computations?</p>

username: YaduKini
cooked: <p>Did you find a solution for your problem? I have the same question. Please let me know. thanks in advance.</p>