-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- normalize per image
- Loading branch information
Showing
4 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from catalyst.dl.core import Callback, RunnerState | ||
import torch.nn as nn | ||
|
||
|
||
class LabelSmoothCriterionCallback(Callback): | ||
def __init__( | ||
self, | ||
input_key: str = "targets", | ||
output_key: str = "logits", | ||
prefix: str = "loss", | ||
criterion_key: str = None, | ||
loss_key: str = None, | ||
multiplier: float = 1.0 | ||
): | ||
self.input_key = input_key | ||
self.output_key = output_key | ||
self.prefix = prefix | ||
self.criterion_key = criterion_key | ||
self.loss_key = loss_key | ||
self.multiplier = multiplier | ||
|
||
def _add_loss_to_state(self, state: RunnerState, loss): | ||
if self.loss_key is None: | ||
if state.loss is not None: | ||
if isinstance(state.loss, list): | ||
state.loss.append(loss) | ||
else: | ||
state.loss = [state.loss, loss] | ||
else: | ||
state.loss = loss | ||
else: | ||
if state.loss is not None: | ||
assert isinstance(state.loss, dict) | ||
state.loss[self.loss_key] = loss | ||
else: | ||
state.loss = {self.loss_key: loss} | ||
|
||
def _compute_loss(self, state: RunnerState, criterion): | ||
loss = criterion( | ||
state.output[self.output_key], | ||
state.input[self.input_key] | ||
) | ||
return loss | ||
|
||
def on_stage_start(self, state: RunnerState): | ||
assert state.criterion is not None | ||
|
||
def on_batch_end(self, state: RunnerState): | ||
if state.loader_name.startswith("train"): | ||
criterion = state.get_key( | ||
key="criterion", inner_key=self.criterion_key | ||
) | ||
else: | ||
criterion = nn.CrossEntropyLoss() | ||
|
||
loss = self._compute_loss(state, criterion) * self.multiplier | ||
|
||
state.metrics.add_batch_value(metrics_dict={ | ||
self.prefix: loss.item(), | ||
}) | ||
|
||
self._add_loss_to_state(state, loss) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
|
||
class LabelSmoothingCrossEntropy(nn.Module): | ||
""" | ||
NLL loss with label smoothing. | ||
""" | ||
def __init__(self, smoothing=0.1): | ||
""" | ||
Constructor for the LabelSmoothing module. | ||
:param smoothing: label smoothing factor | ||
""" | ||
super(LabelSmoothingCrossEntropy, self).__init__() | ||
assert smoothing < 1.0 | ||
self.smoothing = smoothing | ||
self.confidence = 1. - smoothing | ||
|
||
def forward(self, x, target): | ||
logprobs = F.log_softmax(x, dim=-1) | ||
nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1)) | ||
nll_loss = nll_loss.squeeze(1) | ||
smooth_loss = -logprobs.mean(dim=-1) | ||
loss = self.confidence * nll_loss + self.smoothing * smooth_loss | ||
return loss.mean() |