MCPcopy Create free account
hub / github.com/thygate/stable-diffusion-webui-depthmap-script / SILogLoss

Class SILogLoss

dzoedepth/trainers/loss.py:42–93  ·  view source on GitHub ↗

SILog loss (pixel-wise)

Source from the content-addressed store, hash-verified

40
41# Main loss function used for ZoeDepth. Copy/paste from AdaBins repo (https://github.com/shariqfarooq123/AdaBins/blob/0952d91e9e762be310bb4cd055cbfe2448c0ce20/loss.py#L7)
42class SILogLoss(nn.Module):
43 """SILog loss (pixel-wise)"""
44 def __init__(self, beta=0.15):
45 super(SILogLoss, self).__init__()
46 self.name = 'SILog'
47 self.beta = beta
48
49 def forward(self, input, target, mask=None, interpolate=True, return_interpolated=False):
50 input = extract_key(input, KEY_OUTPUT)
51 if input.shape[-1] != target.shape[-1] and interpolate:
52 input = nn.functional.interpolate(
53 input, target.shape[-2:], mode='bilinear', align_corners=True)
54 intr_input = input
55 else:
56 intr_input = input
57
58 if target.ndim == 3:
59 target = target.unsqueeze(1)
60
61 if mask is not None:
62 if mask.ndim == 3:
63 mask = mask.unsqueeze(1)
64
65 input = input[mask]
66 target = target[mask]
67
68 with amp.autocast(enabled=False): # amp causes NaNs in this loss function
69 alpha = 1e-7
70 g = torch.log(input + alpha) - torch.log(target + alpha)
71
72 # n, c, h, w = g.shape
73 # norm = 1/(h*w)
74 # Dg = norm * torch.sum(g**2) - (0.85/(norm**2)) * (torch.sum(g))**2
75
76 Dg = torch.var(g) + self.beta * torch.pow(torch.mean(g), 2)
77
78 loss = 10 * torch.sqrt(Dg)
79
80 if torch.isnan(loss):
81 print("Nan SILog loss")
82 print("input:", input.shape)
83 print("target:", target.shape)
84 print("G", torch.sum(torch.isnan(g)))
85 print("Input min max", torch.min(input), torch.max(input))
86 print("Target min max", torch.min(target), torch.max(target))
87 print("Dg", torch.isnan(Dg))
88 print("loss", torch.isnan(loss))
89
90 if not return_interpolated:
91 return loss
92
93 return loss, intr_input
94
95
96def grad(x):

Callers 2

__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected