losses.py 19.7 KB
Newer Older
Boris Bonev's avatar
Boris Bonev committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# coding=utf-8

# SPDX-FileCopyrightText: Copyright (c) 2025 The torch-harmonics Authors. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import torch
import torch.nn as nn
Andrea Paris's avatar
Andrea Paris committed
34
import torch.amp as amp
Boris Bonev's avatar
Boris Bonev committed
35
36
37
38
39
40
41
42
import torch.nn.functional as F
from typing import Optional
from abc import ABC, abstractmethod

from torch_harmonics.quadrature import _precompute_latitudes


def get_quadrature_weights(nlat: int, nlon: int, grid: str, tile: bool = False, normalized: bool = True) -> torch.Tensor:
apaaris's avatar
apaaris committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    """
    Get quadrature weights for spherical integration.
    
    Parameters
    -----------
    nlat : int
        Number of latitude points
    nlon : int
        Number of longitude points
    grid : str
        Grid type ("equiangular", "legendre-gauss", "lobatto")
    tile : bool, optional
        Whether to tile weights across longitude dimension, by default False
    normalized : bool, optional
        Whether to normalize weights to sum to 1, by default True
        
    Returns
    -------
    torch.Tensor
        Quadrature weights tensor
    """
Boris Bonev's avatar
Boris Bonev committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    # area weights
    _, q = _precompute_latitudes(nlat=nlat, grid=grid)
    q = q.reshape(-1, 1) * 2 * torch.pi / nlon

    # numerical precision can be an issue here, make sure it sums to 1:
    if normalized:
        q = q / torch.sum(q) / float(nlon)

    if tile:
        q = torch.tile(q, (1, nlon)).contiguous()

    return q.to(torch.float32)


class DiceLossS2(nn.Module):
apaaris's avatar
apaaris committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    """
    Dice loss for spherical segmentation tasks.
    
    Parameters
    -----------
    nlat : int
        Number of latitude points
    nlon : int
        Number of longitude points
    grid : str, optional
        Grid type, by default "equiangular"
    weight : torch.Tensor, optional
        Class weights, by default None
    smooth : float, optional
        Smoothing factor, by default 0
    ignore_index : int, optional
        Index to ignore in loss computation, by default -100
    mode : str, optional
        Aggregation mode ("micro" or "macro"), by default "micro"
    """
    
Boris Bonev's avatar
Boris Bonev committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    def __init__(self, nlat: int, nlon: int, grid: str = "equiangular", weight: torch.Tensor = None, smooth: float = 0, ignore_index: int = -100, mode: str = "micro"):

        super().__init__()

        self.smooth = smooth
        self.ignore_index = ignore_index
        self.mode = mode

        # area weights
        q = get_quadrature_weights(nlat=nlat, nlon=nlon, grid=grid)
        self.register_buffer("quad_weights", q)

        if weight is None:
            self.weight = None
        else:
            self.register_buffer("weight", weight.unsqueeze(0))

    def forward(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
apaaris's avatar
apaaris committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        """
        Forward pass of the Dice loss.
        
        Parameters
        -----------
        prd : torch.Tensor
            Prediction tensor with shape (batch, classes, nlat, nlon)
        tar : torch.Tensor
            Target tensor with shape (batch, nlat, nlon)
            
        Returns
        -------
        torch.Tensor
            Dice loss value
        """
Boris Bonev's avatar
Boris Bonev committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        prd = nn.functional.softmax(prd, dim=1)

        # mask values
        if self.ignore_index is not None:
            mask = torch.where(tar == self.ignore_index, 0, 1)
            prd = prd * mask.unsqueeze(1)
            tar = tar * mask

        # one hot encode
        taroh = nn.functional.one_hot(tar, num_classes=prd.shape[1]).permute(0, 3, 1, 2)

        # compute numerator and denominator
        intersection = torch.sum((prd * taroh) * self.quad_weights, dim=(-2, -1))
        union = torch.sum((prd + taroh) * self.quad_weights, dim=(-2, -1))

        if self.mode == "micro":
            if self.weight is not None:
                intersection = torch.sum(intersection * self.weight, dim=1)
                union = torch.sum(union * self.weight, dim=1)
            else:
                intersection = torch.mean(intersection, dim=1)
                union = torch.mean(union, dim=1)

        # compute score
        dice = (2 * intersection + self.smooth) / (union + self.smooth)

        # compute average over classes
        if self.mode == "macro":
            if self.weight is not None:
                dice = torch.sum(dice * self.weight, dim=1)
            else:
                dice = torch.mean(dice, dim=1)

        # average over batch
        dice = torch.mean(dice)

        return 1 - dice


class CrossEntropyLossS2(nn.Module):
apaaris's avatar
apaaris committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    """
    Cross-entropy loss for spherical classification tasks.
    
    Parameters
    -----------
    nlat : int
        Number of latitude points
    nlon : int
        Number of longitude points
    grid : str, optional
        Grid type, by default "equiangular"
    weight : torch.Tensor, optional
        Class weights, by default None
    smooth : float, optional
        Label smoothing factor, by default 0
    ignore_index : int, optional
        Index to ignore in loss computation, by default -100
    """
Boris Bonev's avatar
Boris Bonev committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

    def __init__(self, nlat: int, nlon: int, grid: str = "equiangular", weight: torch.Tensor = None, smooth: float = 0, ignore_index: int = -100):

        super().__init__()

        self.smooth = smooth
        self.ignore_index = ignore_index

        if weight is None:
            self.weight = None
        else:
            self.register_buffer("weight", weight)

        q = get_quadrature_weights(nlat=nlat, nlon=nlon, grid=grid)
        self.register_buffer("quad_weights", q)

    def forward(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
apaaris's avatar
apaaris committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        """
        Forward pass of the cross-entropy loss.
        
        Parameters
        -----------
        prd : torch.Tensor
            Prediction tensor with shape (batch, classes, nlat, nlon)
        tar : torch.Tensor
            Target tensor with shape (batch, nlat, nlon)
            
        Returns
        -------
        torch.Tensor
            Cross-entropy loss value
        """
Boris Bonev's avatar
Boris Bonev committed
223
224
225
226
227
228
229
230
231
232
233

        # compute log softmax
        logits = nn.functional.log_softmax(prd, dim=1)
        ce = nn.functional.cross_entropy(logits, tar, weight=self.weight, reduction="none", ignore_index=self.ignore_index, label_smoothing=self.smooth)
        ce = (ce * self.quad_weights).sum(dim=(-1, -2))
        ce = torch.mean(ce)

        return ce


class FocalLossS2(nn.Module):
apaaris's avatar
apaaris committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    """
    Focal loss for spherical classification tasks.
    
    Parameters
    -----------
    nlat : int
        Number of latitude points
    nlon : int
        Number of longitude points
    grid : str, optional
        Grid type, by default "equiangular"
    weight : torch.Tensor, optional
        Class weights, by default None
    smooth : float, optional
        Label smoothing factor, by default 0
    ignore_index : int, optional
        Index to ignore in loss computation, by default -100
    """
Boris Bonev's avatar
Boris Bonev committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

    def __init__(self, nlat: int, nlon: int, grid: str = "equiangular", weight: torch.Tensor = None, smooth: float = 0, ignore_index: int = -100):

        super().__init__()

        self.smooth = smooth
        self.ignore_index = ignore_index

        if weight is None:
            self.weight = None
        else:
            self.register_buffer("weight", weight)

        q = get_quadrature_weights(nlat=nlat, nlon=nlon, grid=grid)
        self.register_buffer("quad_weights", q)

    def forward(self, prd: torch.Tensor, tar: torch.Tensor, alpha: float = 0.25, gamma: float = 2):
apaaris's avatar
apaaris committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        """
        Forward pass of the focal loss.
        
        Parameters
        -----------
        prd : torch.Tensor
            Prediction tensor with shape (batch, classes, nlat, nlon)
        tar : torch.Tensor
            Target tensor with shape (batch, nlat, nlon)
        alpha : float, optional
            Alpha parameter for focal loss, by default 0.25
        gamma : float, optional
            Gamma parameter for focal loss, by default 2
            
        Returns
        -------
        torch.Tensor
            Focal loss value
        """
Boris Bonev's avatar
Boris Bonev committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

        # compute logits
        logits = nn.functional.log_softmax(prd, dim=1)

        # w = (1.0 - nn.functional.softmax(prd, dim=-3)).pow(gamma)
        # w = torch.where(tar == self.ignore_index, 0.0, w.gather(-3, tar.unsqueeze(-3)).squeeze(-3))
        ce = nn.functional.cross_entropy(logits, tar, weight=self.weight, reduction="none", ignore_index=self.ignore_index, label_smoothing=self.smooth)
        fl = alpha * (1 - torch.exp(-ce)) ** gamma * ce
        # fl = w * ce
        fl = (fl * self.quad_weights).sum(dim=(-1, -2))
        fl = fl.mean()

        return fl


class SphericalLossBase(nn.Module, ABC):
    """Abstract base class for spherical losses that handles common initialization and integration."""

    def __init__(self, nlat: int, nlon: int, grid: str = "equiangular", normalized: bool = True):
        super().__init__()

        self.nlat = nlat
        self.nlon = nlon
        self.grid = grid

        # get quadrature weights - these sum to 1!
        q = get_quadrature_weights(nlat=nlat, nlon=nlon, grid=grid, normalized=normalized)
        self.register_buffer("quad_weights", q)

    def _integrate_sphere(self, ugrid, mask=None):
        if mask is None:
            out = torch.sum(ugrid * self.quad_weights, dim=(-2, -1))
        elif mask is not None:
            out = torch.sum(mask * ugrid * self.quad_weights, dim=(-2, -1)) / torch.sum(mask * self.quad_weights, dim=(-2, -1))
        return out

    @abstractmethod
    def _compute_loss_term(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
        """Abstract method that must be implemented by child classes to compute loss terms.

        Args:
            prd (torch.Tensor): Prediction tensor
            tar (torch.Tensor): Target tensor

        Returns:
            torch.Tensor: Computed loss term before integration
        """
        pass

    def _post_integration_hook(self, loss: torch.Tensor) -> torch.Tensor:
        """Post-integration hook. Commonly used for the roots in Lp norms"""
        return loss

    def forward(self, prd: torch.Tensor, tar: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor:
        """Common forward pass that handles masking and reduction.

        Args:
            prd (torch.Tensor): Prediction tensor
            tar (torch.Tensor): Target tensor
            mask (Optional[torch.Tensor], optional): Mask tensor. Defaults to None.

        Returns:
            torch.Tensor: Final loss value
        """
        loss_term = self._compute_loss_term(prd, tar)
        # Integrate over the sphere for each item in the batch
        loss = self._integrate_sphere(loss_term, mask)
        # potentially call root
        loss = self._post_integration_hook(loss)
        # Average the loss over the batch dimension
        return torch.mean(loss)


class SquaredL2LossS2(SphericalLossBase):
apaaris's avatar
apaaris committed
362
363
364
365
366
367
    """
    Squared L2 loss for spherical regression tasks.
    
    Computes the squared difference between prediction and target tensors.
    """
    
Boris Bonev's avatar
Boris Bonev committed
368
    def _compute_loss_term(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
apaaris's avatar
apaaris committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
        """
        Compute squared L2 loss term.
        
        Parameters
        -----------
        prd : torch.Tensor
            Prediction tensor
        tar : torch.Tensor
            Target tensor
            
        Returns
        -------
        torch.Tensor
            Squared difference between prediction and target
        """
Boris Bonev's avatar
Boris Bonev committed
384
385
386
387
        return torch.square(prd - tar)


class L1LossS2(SphericalLossBase):
apaaris's avatar
apaaris committed
388
389
390
391
392
393
    """
    L1 loss for spherical regression tasks.
    
    Computes the absolute difference between prediction and target tensors.
    """
    
Boris Bonev's avatar
Boris Bonev committed
394
    def _compute_loss_term(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
apaaris's avatar
apaaris committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
        """
        Compute L1 loss term.
        
        Parameters
        -----------
        prd : torch.Tensor
            Prediction tensor
        tar : torch.Tensor
            Target tensor
            
        Returns
        -------
        torch.Tensor
            Absolute difference between prediction and target
        """
Boris Bonev's avatar
Boris Bonev committed
410
411
412
413
        return torch.abs(prd - tar)


class L2LossS2(SquaredL2LossS2):
apaaris's avatar
apaaris committed
414
415
416
417
418
419
    """
    L2 loss for spherical regression tasks.
    
    Computes the square root of the squared L2 loss.
    """
    
Boris Bonev's avatar
Boris Bonev committed
420
    def _post_integration_hook(self, loss: torch.Tensor) -> torch.Tensor:
apaaris's avatar
apaaris committed
421
422
423
424
425
426
427
428
429
430
431
432
433
        """
        Apply square root to get L2 norm.
        
        Parameters
        -----------
        loss : torch.Tensor
            Integrated squared loss
            
        Returns
        -------
        torch.Tensor
            Square root of the loss (L2 norm)
        """
Boris Bonev's avatar
Boris Bonev committed
434
435
436
437
        return torch.sqrt(loss)


class W11LossS2(SphericalLossBase):
apaaris's avatar
apaaris committed
438
439
440
441
442
443
    """
    W11 loss for spherical regression tasks.
    
    Computes the L1 norm of the gradient differences between prediction and target.
    """
    
Boris Bonev's avatar
Boris Bonev committed
444
    def __init__(self, nlat: int, nlon: int, grid: str = "equiangular"):
apaaris's avatar
apaaris committed
445
446
447
448
449
450
451
452
453
454
455
456
        """
        Initialize W11 loss.
        
        Parameters
        -----------
        nlat : int
            Number of latitude points
        nlon : int
            Number of longitude points
        grid : str, optional
            Grid type, by default "equiangular"
        """
Boris Bonev's avatar
Boris Bonev committed
457
458
459
460
461
462
463
464
465
466
467
468
        super().__init__(nlat=nlat, nlon=nlon, grid=grid)
        # Set up grid and domain for FFT
        l_phi = 2 * torch.pi  # domain size
        l_theta = torch.pi  # domain size

        k_phi = torch.fft.fftfreq(nlon, d=l_phi / (2 * torch.pi * nlon))
        k_theta = torch.fft.fftfreq(nlat, d=l_theta / (2 * torch.pi * nlat))
        k_theta_mesh, k_phi_mesh = torch.meshgrid(k_theta, k_phi, indexing="ij")
        self.register_buffer("k_phi_mesh", k_phi_mesh)
        self.register_buffer("k_theta_mesh", k_theta_mesh)

    def _compute_loss_term(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
Andrea Paris's avatar
Andrea Paris committed
469
470
471
472
473
474
475
476
        prdtype = prd.dtype
        with amp.autocast(device_type="cuda", enabled=False):
            prd = prd.to(torch.float32)
            prd_prime_fft2_phi_h = torch.fft.ifft2(1j * self.k_phi_mesh * torch.fft.fft2(prd)).real
            prd_prime_fft2_theta_h = torch.fft.ifft2(1j * self.k_theta_mesh * torch.fft.fft2(prd)).real

            tar_prime_fft2_phi_h = torch.fft.ifft2(1j * self.k_phi_mesh * torch.fft.fft2(tar)).real
            tar_prime_fft2_theta_h = torch.fft.ifft2(1j * self.k_theta_mesh * torch.fft.fft2(tar)).real
Boris Bonev's avatar
Boris Bonev committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

        # Return the element-wise loss term
        return torch.abs(prd_prime_fft2_phi_h - tar_prime_fft2_phi_h) + torch.abs(prd_prime_fft2_theta_h - tar_prime_fft2_theta_h)


class NormalLossS2(SphericalLossBase):
    """Combined L1 and Surface Normal Consistency Loss for spherical data.

    This loss function combines an L1 loss term with a surface normal alignment term.

    The loss consists of:
    1. L1 Loss: Absolute difference between predicted and target values
    2. Normal Consistency Loss: 1 - cosine similarity between surface normals
       (equivalent to cosine distance between normal vectors)

    Surface normals are computed by calculating gradients in latitude and longitude
    directions using FFT, then constructing 3D normal vectors that are normalized.

495
496
497
498
499
500
501
502
503
504
505
506
507
    Parameters
    ----------
    nlat : int
        Number of latitude points
    nlon : int
        Number of longitude points
    grid : str, optional
        Grid type, by default "equiangular"

    Returns
    -------
    torch.Tensor
        Combined loss term
Boris Bonev's avatar
Boris Bonev committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    """

    def __init__(self, nlat: int, nlon: int, grid: str = "equiangular"):
        super().__init__(nlat=nlat, nlon=nlon, grid=grid)
        # Set up grid and domain for FFT
        l_phi = 2 * torch.pi  # domain size
        l_theta = torch.pi  # domain size

        k_phi = torch.fft.fftfreq(nlon, d=l_phi / (2 * torch.pi * nlon))
        k_theta = torch.fft.fftfreq(nlat, d=l_theta / (2 * torch.pi * nlat))
        k_theta_mesh, k_phi_mesh = torch.meshgrid(k_theta, k_phi, indexing="ij")
        self.register_buffer("k_phi_mesh", k_phi_mesh)
        self.register_buffer("k_theta_mesh", k_theta_mesh)

    def compute_gradients(self, x):
apaaris's avatar
apaaris committed
523
524
525
526
527
528
529
530
531
532
533
534
535
        """
        Compute gradients of the input tensor using FFT.
        
        Parameters
        -----------
        x : torch.Tensor
            Input tensor with shape (batch, nlat, nlon) or (nlat, nlon)
            
        Returns
        -------
        tuple
            Tuple of (grad_phi, grad_theta) gradients
        """
Boris Bonev's avatar
Boris Bonev committed
536
537
538
539
        # Make sure x is reshaped to have a batch dimension if it's missing
        if x.dim() == 2:
            x = x.unsqueeze(0)  # Add batch dimension

apaaris's avatar
apaaris committed
540
541
542
        # Compute gradients using FFT
        grad_phi = torch.fft.ifft2(1j * self.k_phi_mesh * torch.fft.fft2(x)).real
        grad_theta = torch.fft.ifft2(1j * self.k_theta_mesh * torch.fft.fft2(x)).real
Boris Bonev's avatar
Boris Bonev committed
543

apaaris's avatar
apaaris committed
544
        return grad_phi, grad_theta
Boris Bonev's avatar
Boris Bonev committed
545

apaaris's avatar
apaaris committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
    def compute_normals(self, x):
        """
        Compute surface normals from the input tensor.
        
        Parameters
        -----------
        x : torch.Tensor
            Input tensor with shape (batch, nlat, nlon) or (nlat, nlon)
            
        Returns
        -------
        torch.Tensor
            Normal vectors with shape (batch, 3, nlat, nlon)
        """
        grad_phi, grad_theta = self.compute_gradients(x)
        
        # Construct normal vectors: (-grad_theta, -grad_phi, 1)
        normals = torch.stack([-grad_theta, -grad_phi, torch.ones_like(x)], dim=1)
        
        # Normalize
        norm = torch.norm(normals, dim=1, keepdim=True)
        normals = normals / (norm + 1e-8)
        
Boris Bonev's avatar
Boris Bonev committed
569
570
571
        return normals

    def _compute_loss_term(self, prd: torch.Tensor, tar: torch.Tensor) -> torch.Tensor:
apaaris's avatar
apaaris committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
        """
        Compute combined L1 and normal consistency loss.
        
        Parameters
        -----------
        prd : torch.Tensor
            Prediction tensor
        tar : torch.Tensor
            Target tensor
            
        Returns
        -------
        torch.Tensor
            Combined loss term
        """
Boris Bonev's avatar
Boris Bonev committed
587
588
589
590
591
592
593
        # Handle dimensions for both prediction and target
        # Ensure we have at least a batch dimension
        if prd.dim() == 2:
            prd = prd.unsqueeze(0)
        if tar.dim() == 2:
            tar = tar.unsqueeze(0)

apaaris's avatar
apaaris committed
594
595
        # L1 loss term
        l1_loss = torch.abs(prd - tar)
Boris Bonev's avatar
Boris Bonev committed
596

apaaris's avatar
apaaris committed
597
598
        # Normal consistency loss
        prd_normals = self.compute_normals(prd)
Boris Bonev's avatar
Boris Bonev committed
599
        tar_normals = self.compute_normals(tar)
apaaris's avatar
apaaris committed
600
601
602
603
604
605
606
        
        # Cosine similarity between normals
        cos_sim = torch.sum(prd_normals * tar_normals, dim=1)
        normal_loss = 1 - cos_sim

        # Combine losses (equal weighting)
        combined_loss = l1_loss + normal_loss.unsqueeze(1)
Boris Bonev's avatar
Boris Bonev committed
607

apaaris's avatar
apaaris committed
608
        return combined_loss