Instructions to use AndreCosta/resnet-unet-segmentation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AndreCosta/resnet-unet-segmentation with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="AndreCosta/resnet-unet-segmentation")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("AndreCosta/resnet-unet-segmentation", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download scripts/Segmentation/diceLossCriterion.py from AndreCosta/resnet-unet-segmentation: direct link, hf CLI and curl.
- Browser
- Download file 730 Bytes
-
https://huggingface.co/AndreCosta/resnet-unet-segmentation/resolve/main/scripts/Segmentation/diceLossCriterion.py
- Command line
-
hf download hf://AndreCosta/resnet-unet-segmentation/scripts/Segmentation/diceLossCriterion.py
-
curl -L -o diceLossCriterion.py https://huggingface.co/AndreCosta/resnet-unet-segmentation/resolve/main/scripts/Segmentation/diceLossCriterion.py
730 Bytes
| import torch | |
| import torch.nn as nn | |
| class DiceLoss(nn.Module): | |
| def __init__(self, smooth=1e-6): | |
| super(DiceLoss, self).__init__() | |
| self.smooth = smooth | |
| def forward(self, logits, targets): | |
| logits = logits.float() # [B, C, H, W] | |
| probs = torch.softmax(logits, dim=1) | |
| preds = probs[:, 1, :, :] # [B, H, W] | |
| if targets.ndim == 4: | |
| targets = targets.squeeze(1) # [B, H, W] | |
| targets = (targets == 1).float() # binariza se necessário | |
| intersection = (preds * targets).sum(dim=(1, 2)) | |
| union = preds.sum(dim=(1, 2)) + targets.sum(dim=(1, 2)) | |
| dice = (2 * intersection + self.smooth) / (union + self.smooth) | |
| return 1 - dice.mean() | |