How to compute the Surface Dice-Sørensen Coefficient in pytorch?
I would like to compute the Surface Dice-Sørensen Coefficient from this  paper (page 19)in python3/pytorch.

I have to point out, that I do not try to implement the simple standard volumetric Dice-Sørensen Coefficient! This one would look as follows in my implementation:

import torch

def volumetric_DSC(M1, M2):
    M1 = M1.view(-1)
    M2 = M2.view(-1)
    dividend = 2 * (M1 * M2).sum()
    divisor = (M1 * M1).sum() + (M2 * M2).sum()
    return dividend / divisor

if __name__ == "__main__":
    m1 = torch.empty(5, 5, 5).uniform_(0, 1)
    m1 = torch.bernoulli(m1)
    m2 = torch.empty(5, 5, 5).uniform_(0, 1)
    m2 = torch.bernoulli(m2)
    loss = volumetric_DSC(m1, m2)
    print("loss = {0}".format(loss))


How can I extend this code to a Surface Dice-Sørensen Coefficient loss?

A surface dice implementation was provided here as part of this study. You can use it as an evaluation metric but not as a loss function as it contains non-differentiable ops. You will need to provide a "tolerance" distance i.e. a surface dice of 0.9 means that 90% of surfaces lie within the tolerance (which is better calculated from the data itself, such as the inter-observer variation of the task you are solving)
