Spaces:
Running on Zero
Running on Zero
Download models/losses.py from Vignesh38/Map-Detect: direct link, hf CLI and curl.
- Browser
- Download file 709 Bytes
-
https://huggingface.co/spaces/Vignesh38/Map-Detect/resolve/main/models/losses.py
- Command line
-
hf download hf://spaces/Vignesh38/Map-Detect/models/losses.py
-
curl -L -o losses.py https://huggingface.co/spaces/Vignesh38/Map-Detect/resolve/main/models/losses.py
709 Bytes
| import torch | |
| import torch.nn.functional as F | |
| def cross_entropy(input, target, weight=None, reduction='mean',ignore_index=255): | |
| """ | |
| logSoftmax_with_loss | |
| :param input: torch.Tensor, N*C*H*W | |
| :param target: torch.Tensor, N*1*H*W,/ N*H*W | |
| :param weight: torch.Tensor, C | |
| :return: torch.Tensor [0] | |
| """ | |
| target = target.long() | |
| if target.dim() == 4: | |
| target = torch.squeeze(target, dim=1) | |
| if input.shape[-1] != target.shape[-1]: | |
| input = F.interpolate(input, size=target.shape[1:], mode='bilinear',align_corners=True) | |
| return F.cross_entropy(input=input, target=target, weight=weight, | |
| ignore_index=ignore_index, reduction=reduction) | |