generated from xinntao/ProjectTemplate-Python
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import importlib | ||
from os import path as osp | ||
|
||
from basicsr.utils import scandir | ||
|
||
# automatically scan and import loss modules for registry | ||
# scan all the files that end with '_loss.py' under the loss folder | ||
loss_folder = osp.dirname(osp.abspath(__file__)) | ||
loss_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(loss_folder) if v.endswith('_loss.py')] | ||
# import all the loss modules | ||
_model_modules = [importlib.import_module(f'losses.{file_name}') for file_name in loss_filenames] |
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 @@ | ||
from torch import nn as nn | ||
from torch.nn import functional as F | ||
|
||
from basicsr.utils.registry import LOSS_REGISTRY | ||
|
||
|
||
@LOSS_REGISTRY.register() | ||
class ExampleLoss(nn.Module): | ||
"""Example Loss. | ||
Args: | ||
loss_weight (float): Loss weight for Example loss. Default: 1.0. | ||
""" | ||
|
||
def __init__(self, loss_weight=1.0): | ||
super(ExampleLoss, self).__init__() | ||
self.loss_weight = loss_weight | ||
|
||
def forward(self, pred, target, **kwargs): | ||
""" | ||
Args: | ||
pred (Tensor): of shape (N, C, H, W). Predicted tensor. | ||
target (Tensor): of shape (N, C, H, W). Ground truth tensor. | ||
weight (Tensor, optional): of shape (N, C, H, W). Element-wise weights. Default: None. | ||
""" | ||
return self.loss_weight * F.l1_loss(pred, target, reduction='mean') |
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