Spaces:
Runtime error
Runtime error
Download model.py from joselobenitezg/Pytorch-Image-Segmentation: direct link, hf CLI and curl.
- Browser
- Download file 712 Bytes
-
https://huggingface.co/spaces/joselobenitezg/Pytorch-Image-Segmentation/resolve/main/model.py
- Command line
-
hf download hf://spaces/joselobenitezg/Pytorch-Image-Segmentation/model.py
-
curl -L -o model.py https://huggingface.co/spaces/joselobenitezg/Pytorch-Image-Segmentation/resolve/main/model.py
712 Bytes
| from torch import nn | |
| import segmentation_models_pytorch as smp | |
| from segmentation_models_pytorch.losses import DiceLoss | |
| ENCODER = 'timm-efficientnet-b0' | |
| WEIGHTS = 'imagenet' | |
| class SegmentationModel(nn.Module): | |
| def __init__(self): | |
| super(SegmentationModel, self).__init__() | |
| self.arc = smp.Unet( | |
| encoder_name = ENCODER, | |
| encoder_weights = WEIGHTS, | |
| in_channels = 3, | |
| classes = 1, | |
| activation = None | |
| ) | |
| def forward(self, images, masks = None): | |
| logits = self.arc(images) | |
| if masks != None: | |
| loss1 = DiceLoss(mode='binary')(logits, masks) | |
| loss2 = nn.BCEWithLogitsLoss()(logits, masks) | |
| return logits, loss1 + loss2 | |
| return logits | |