-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
33 lines (23 loc) · 748 Bytes
/
model.py
File metadata and controls
33 lines (23 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from torch import nn
import segmentation_models_pytorch as smp
from segmentation_models_pytorch.losses import DiceLoss
import yaml
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
ENCODER = config['ENCODER']
WEIGHTS = config['WEIGHTS']
class SegmentationModel(nn.Module):
def __init__(self):
super(SegmentationModel,self).__init__()
self.backbone = smp.Unet(
encoder_name=ENCODER,
encoder_weights=WEIGHTS,
in_channels=3,
classes=1,
activation=None
)
def forward(self,images,masks=None):
logits = self.backbone(images)
if masks !=None:
return logits,DiceLoss(mode='binary')(logits,masks) + nn.BCEWithLogitsLoss()(logits,masks)
return logits