def per_pixel_l2_dist(x, y, reduction='mean'):
    """ Calculate per-pixel averaged L2 distance.
    """
    n_pixels = x.size(1) * x.size(2) * x.size(3)
    l2_dist = (x - y).pow(2).sum(dim=(1, 2, 3)).pow(0.5).div(n_pixels)

    if reduction == 'mean':
        return l2_dist.mean().detach().clone()
    elif reduction == 'none':
        return l2_dist.detach().clone()