Search code examples
pythonpython-3.ximagenumpynumpy-einsum

Python Numpy einsum klij->kijl and kijl->klij what do they mean?


I am trying to understand the following code from https://github.com/rezazad68/BCDU-Net/blob/master/Retina%20Blood%20Vessel%20Segmentation/evaluate.py:

patches_imgs_test = np.einsum('klij->kijl', patches_imgs_test)

and also the following:

predictions = np.einsum('kijl->klij', predictions)

I tried looking up the einsum operands klij->kijl and kijl->klij but lady luck has yet to be on my side. The closest I got (probably) are the following which do not explain cases with '4 charcters':

https://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html Understanding NumPy's einsum

My intuition is that its just rotations of the images based on how the characters are shifting. Am I right or close on this? Some insights will be appreciated!

P.S. The numpy einsum documentation is killing me..


Solution

  • The provided einsum statement is equivalent to (using np.moveaxis):

     patches_imgs_test  = np.moveaxis(patches_imgs_test, 1, -1)
    

    followed by:

    predictions = np.moveaxis(predictions, -1, 1)
    

    Basically, moving the second axis to the end, and then putting it back in the results.

    In this case, it's patches of pictures being dumped into a neural network. The second and fourth axes are the actual patches, so the code puts them at the end before passing to the NN, while the first and third axes are location data.