Spaces:
Runtime error
Runtime error
Download taming/modules/losses/segmentation.py from UPstud/ColorCtrl: direct link, hf CLI and curl.
- Browser
- Download file 816 Bytes
-
https://huggingface.co/spaces/UPstud/ColorCtrl/resolve/main/taming/modules/losses/segmentation.py
- Command line
-
hf download hf://spaces/UPstud/ColorCtrl/taming/modules/losses/segmentation.py
-
curl -L -o segmentation.py https://huggingface.co/spaces/UPstud/ColorCtrl/resolve/main/taming/modules/losses/segmentation.py
816 Bytes
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class BCELoss(nn.Module): | |
| def forward(self, prediction, target): | |
| loss = F.binary_cross_entropy_with_logits(prediction,target) | |
| return loss, {} | |
| class BCELossWithQuant(nn.Module): | |
| def __init__(self, codebook_weight=1.): | |
| super().__init__() | |
| self.codebook_weight = codebook_weight | |
| def forward(self, qloss, target, prediction, split): | |
| bce_loss = F.binary_cross_entropy_with_logits(prediction,target) | |
| loss = bce_loss + self.codebook_weight*qloss | |
| return loss, {"{}/total_loss".format(split): loss.clone().detach().mean(), | |
| "{}/bce_loss".format(split): bce_loss.detach().mean(), | |
| "{}/quant_loss".format(split): qloss.detach().mean() | |
| } | |