def define_criterion(self, name):
     if name.lower() == 'bce+dice':
         self.criterion = Loss.BCE_Dice()
     elif name.lower() == 'dice':
         self.criterion = Loss.DiceLoss()
     elif name.lower() == 'bce':
         self.criterion = nn.BCEWithLogitsLoss()
     elif name.lower() == 'robustfocal':
         self.criterion = Loss.RobustFocalLoss2d()
     elif name.lower() == 'lovasz-hinge' or name.lower() == 'lovasz':
         self.criterion = Loss.Lovasz_Hinge(per_image=True)
     elif name.lower() == 'bce+lovasz':
         self.criterion = Loss.BCE_Lovasz(per_image=True)
     else:
         raise NotImplementedError(
             'Loss {} is not implemented'.format(name))
Exemplo n.º 2
0
 def define_criterion(self, name):
     if name.lower() == 'bce+dice':
         self.criterion = Loss.BCE_Dice()
     elif name.lower() == 'dice':
         self.criterion = Loss.DiceLoss()
     elif name.lower() == 'bce':
         # self.criterion = nn.BCEWithLogitsLoss()
         # self.criterion = nn.CrossEntropyLoss(size_average=False, reduction='sum')
         self.criterion = nn.CrossEntropyLoss()
         # self.criterion = Loss.CE_SOFT()
     elif name.lower() == 'robustfocal':
         self.criterion = Loss.RobustFocalLoss2d()
     elif name.lower() == 'lovasz-hinge' or name.lower() == 'lovasz':
         self.criterion = Loss.Lovasz_Hinge(per_image=True)
     elif name.lower() == 'bce+lovasz':
         self.criterion = Loss.BCE_Lovasz(per_image=True)
     else:
         raise NotImplementedError(
             'Loss {} is not implemented'.format(name))
Exemplo n.º 3
0
    def __init__(self, config, train_loader, valid_loader, test_loader):

        # Data loader
        self.train_loader = train_loader
        self.valid_loader = valid_loader
        self.test_loader = test_loader

        # Models
        self.unet = None
        self.optimizer = None
        self.scheduler = None
        self.img_ch = config.img_ch
        self.output_ch = config.output_ch

        # Losses
        self.criterion = Loss.DiceLoss()

        # Hyper-parameters
        self.lr = config.lr
        self.beta1 = config.beta1
        self.beta2 = config.beta2

        # Training settings
        self.num_epochs = config.num_epochs
        self.num_epochs_test = config.num_epochs_test
        self.batch_size = config.batch_size

        # Path
        self.model_path = config.model_path
        self.train_result_path = config.train_result_path
        self.val_result_path = config.val_result_path
        self.test_result_path = config.test_result_path
        self.mode = config.mode

        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')
        self.model_type = config.model_type
        self.build_model()