Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loss #11

Open
PeterLuoCoder opened this issue Jun 23, 2023 · 0 comments
Open

Loss #11

PeterLuoCoder opened this issue Jun 23, 2023 · 0 comments

Comments

@PeterLuoCoder
Copy link

"Hello, I'm studying your code and have a few questions. I apologize for the interruption, but could you please explain the following to me? In the train.py file, do setting use_mixup and use_edge to 0 or 1 represent false and true, respectively? When both use_mixup and use_edge are set to 0 by default, is the loss calculated as CrossEntropyLoss() + Edge_loss()? If I want to calculate the loss using only CrossEntropyLoss, how should I modify it? Thank you."

class FullModel(nn.Module):

def __init__(self, model, args2):
    super(FullModel, self).__init__()
    self.model = model
    self.use_mixup = args2.use_mixup
    self.use_edge = args2.use_edge

    # self.ce_loss = Edge_weak_loss()
    self.ce_loss = CrossEntropyLoss()

    self.edge_loss = Edge_loss()

    if self.use_mixup:
        self.mixup = Mixup(use_edge=args2.use_edge)

def forward(self, input, label=None, train=True):

    if train and self.use_mixup and label is not None:
        if self.use_edge:
            loss = self.mixup(input, label, [self.ce_loss, self.edge_loss], self.model)
        else:
            loss = self.mixup(input, label, self.ce_loss, self.model)
        return loss

    output = self.model(input)
    if train:
        losses = 0
        if isinstance(output, (list, tuple)):
            if self.use_edge:
                for i in range(len(output) - 1):
                    loss = self.ce_loss(output[i], label)
                    losses += loss
                losses += self.edge_loss(output[-1], edge_contour(label).long())
            else:
                for i in range(len(output)):
                    loss = self.ce_loss(output[i], label)
                    losses += loss
        else:
            losses = self.ce_loss(output, label)
        return losses
    else:
        if isinstance(output, (list, tuple)):
            return output[0]
        else:
            return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant