Spaces:
Runtime error
Runtime error
Download Text2Human/models/losses/segmentation_loss.py from radames/Text2Human-API: direct link, hf CLI and curl.
- Browser
- Download file 801 Bytes
-
https://huggingface.co/spaces/radames/Text2Human-API/resolve/52782af3e9bc9f498c984e704729e7f86f8c57ad/Text2Human/models/losses/segmentation_loss.py
- Command line
-
hf download hf://spaces/radames/Text2Human-API@52782af3e9bc9f498c984e704729e7f86f8c57ad/Text2Human/models/losses/segmentation_loss.py
-
curl -L -o segmentation_loss.py https://huggingface.co/spaces/radames/Text2Human-API/resolve/52782af3e9bc9f498c984e704729e7f86f8c57ad/Text2Human/models/losses/segmentation_loss.py
801 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() | |
| } | |