Upload folder using huggingface_hub
Browse files- __init__.py +7 -0
- __pycache__/__init__.cpython-312.pyc +0 -0
- __pycache__/contrastive.cpython-312.pyc +0 -0
- __pycache__/cosine.cpython-312.pyc +0 -0
- __pycache__/dice.cpython-312.pyc +0 -0
- __pycache__/focal.cpython-312.pyc +0 -0
- __pycache__/huber.cpython-312.pyc +0 -0
- __pycache__/kldiv.cpython-312.pyc +0 -0
- __pycache__/triplet.cpython-312.pyc +0 -0
- contrastive.py +14 -0
- cosine.py +9 -0
- dice.py +15 -0
- focal.py +17 -0
- huber.py +14 -0
- kldiv.py +11 -0
- setup.py +18 -0
- triplet.py +9 -0
__init__.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .focal import FocalLoss
|
2 |
+
from .dice import DiceLoss
|
3 |
+
from .contrastive import ContrastiveLoss
|
4 |
+
from .triplet import TripletLoss
|
5 |
+
from .cosine import CosineEmbeddingLoss
|
6 |
+
from .huber import HuberLoss
|
7 |
+
from .kldiv import KLDivLoss
|
__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (448 Bytes). View file
|
|
__pycache__/contrastive.cpython-312.pyc
ADDED
Binary file (1.37 kB). View file
|
|
__pycache__/cosine.cpython-312.pyc
ADDED
Binary file (902 Bytes). View file
|
|
__pycache__/dice.cpython-312.pyc
ADDED
Binary file (1.34 kB). View file
|
|
__pycache__/focal.cpython-312.pyc
ADDED
Binary file (1.45 kB). View file
|
|
__pycache__/huber.cpython-312.pyc
ADDED
Binary file (1.35 kB). View file
|
|
__pycache__/kldiv.cpython-312.pyc
ADDED
Binary file (1 kB). View file
|
|
__pycache__/triplet.cpython-312.pyc
ADDED
Binary file (888 Bytes). View file
|
|
contrastive.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
class ContrastiveLoss(nn.Module):
|
6 |
+
"""Contrastive Loss for Siamese networks"""
|
7 |
+
def __init__(self, margin=1.0):
|
8 |
+
super().__init__()
|
9 |
+
self.margin = margin
|
10 |
+
|
11 |
+
def forward(self, x1, x2, label):
|
12 |
+
dist = F.pairwise_distance(x1, x2)
|
13 |
+
loss = (1 - label) * torch.pow(dist, 2) + label * torch.pow(torch.clamp(self.margin - dist, min=0.0), 2)
|
14 |
+
return loss.mean()
|
cosine.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn.functional as F
|
2 |
+
|
3 |
+
class CosineEmbeddingLoss:
|
4 |
+
"""Cosine Embedding Loss for similarity learning"""
|
5 |
+
def __init__(self, margin=0.0):
|
6 |
+
self.margin = margin
|
7 |
+
|
8 |
+
def __call__(self, x1, x2, label):
|
9 |
+
return F.cosine_embedding_loss(x1, x2, label, margin=self.margin)
|
dice.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
class DiceLoss(nn.Module):
|
5 |
+
"""Dice Loss for segmentation"""
|
6 |
+
def __init__(self, smooth=1.0):
|
7 |
+
super().__init__()
|
8 |
+
self.smooth = smooth
|
9 |
+
|
10 |
+
def forward(self, inputs, targets):
|
11 |
+
inputs = torch.sigmoid(inputs).view(-1)
|
12 |
+
targets = targets.view(-1).float()
|
13 |
+
intersection = (inputs * targets).sum()
|
14 |
+
dice = (2. * intersection + self.smooth) / (inputs.sum() + targets.sum() + self.smooth)
|
15 |
+
return 1 - dice
|
focal.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
class FocalLoss(nn.Module):
|
6 |
+
"""Focal Loss for imbalanced classification"""
|
7 |
+
def __init__(self, alpha=1.0, gamma=2.0, reduction='mean'):
|
8 |
+
super().__init__()
|
9 |
+
self.alpha = alpha
|
10 |
+
self.gamma = gamma
|
11 |
+
self.reduction = reduction
|
12 |
+
|
13 |
+
def forward(self, inputs, targets):
|
14 |
+
ce_loss = F.cross_entropy(inputs, targets, reduction='none')
|
15 |
+
pt = torch.exp(-ce_loss)
|
16 |
+
loss = self.alpha * (1 - pt) ** self.gamma * ce_loss
|
17 |
+
return loss.mean() if self.reduction == 'mean' else loss.sum()
|
huber.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
class HuberLoss(nn.Module):
|
5 |
+
"""Huber Loss (a.k.a. Smooth L1)"""
|
6 |
+
def __init__(self, delta=1.0, reduction='mean'):
|
7 |
+
super().__init__()
|
8 |
+
self.delta = delta
|
9 |
+
self.reduction = reduction
|
10 |
+
|
11 |
+
def forward(self, inputs, targets):
|
12 |
+
diff = torch.abs(inputs - targets)
|
13 |
+
loss = torch.where(diff < self.delta, 0.5 * diff**2, self.delta * (diff - 0.5 * self.delta))
|
14 |
+
return loss.mean() if self.reduction == 'mean' else loss.sum()
|
kldiv.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
import torch.nn.functional as F
|
3 |
+
|
4 |
+
class KLDivLoss(nn.Module):
|
5 |
+
"""Kullback-Leibler Divergence Loss"""
|
6 |
+
def __init__(self, reduction='batchmean'):
|
7 |
+
super().__init__()
|
8 |
+
self.reduction = reduction
|
9 |
+
|
10 |
+
def forward(self, inputs, targets):
|
11 |
+
return F.kl_div(inputs, targets, reduction=self.reduction)
|
setup.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from setuptools import setup, find_packages
|
2 |
+
|
3 |
+
setup(
|
4 |
+
name="torchlosses",
|
5 |
+
version="0.1.0",
|
6 |
+
description="A collection of advanced PyTorch loss functions (Focal, Dice, Contrastive, Triplet, Cosine, Huber, KLDiv).",
|
7 |
+
author="Naga Adithya Kaushik (GenAIDevTOProd)",
|
8 |
+
author_email="[email protected]",
|
9 |
+
url="https://huggingface.co/GenAIDevTOProd",
|
10 |
+
packages=find_packages(),
|
11 |
+
install_requires=["torch>=1.10"],
|
12 |
+
classifiers=[
|
13 |
+
"Programming Language :: Python :: 3",
|
14 |
+
"License :: OSI Approved :: Apache Software License",
|
15 |
+
"Operating System :: OS Independent",
|
16 |
+
],
|
17 |
+
python_requires=">=3.7",
|
18 |
+
)
|
triplet.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn.functional as F
|
2 |
+
|
3 |
+
class TripletLoss:
|
4 |
+
"""Triplet Margin Loss"""
|
5 |
+
def __init__(self, margin=1.0):
|
6 |
+
self.margin = margin
|
7 |
+
|
8 |
+
def __call__(self, anchor, positive, negative):
|
9 |
+
return F.triplet_margin_loss(anchor, positive, negative, margin=self.margin, reduction='mean')
|