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

Can I use this function (temperature_scaling) for other classification network?? #24

Open
edwardcho opened this issue Apr 20, 2021 · 2 comments

Comments

@edwardcho
Copy link

Hello Sir.

I tested your code (temperature scaling) in my code.
my code is based on inception_v3.

But I met some error.

# Code
    # This function probably should live outside of this class, but whatever
    #def set_temperature(self, valid_loader):
    def set_temperature(self, valid_loader):
        """
        Tune the tempearature of the model (using the validation set).
        We're going to set it to optimize NLL.
        valid_loader (DataLoader): validation set loader
        """
        self.cuda()
        nll_criterion = nn.CrossEntropyLoss().cuda()
        ece_criterion = _ECELoss().cuda()

        # First: collect all the logits and labels for the validation set
        logits_list = []
        labels_list = []
        with torch.no_grad():
            for input, label in valid_loader:
                input = input.cuda()
                logits = self.model(input)
                logits_list.append(logits)
                labels_list.append(label)
            logits = torch.cat(logits_list).cuda()
            labels = torch.cat(labels_list).cuda()

        # Calculate NLL and ECE before temperature scaling
        before_temperature_nll = nll_criterion(logits, labels).item()
        before_temperature_ece = ece_criterion(logits, labels).item()
        print('Before temperature - NLL: %.3f, ECE: %.3f' % (before_temperature_nll, before_temperature_ece))

        # Next: optimize the temperature w.r.t. NLL
        optimizer = optim.LBFGS([self.temperature], lr=0.01, max_iter=50)
        ...
# Code
'''
RuntimeError: Calculated padded input size per channel: (4 x 4). Kernel size: (5 x 5). Kernel size can't be greater than actual input size
'''

I checked error code in my code.
The part of error code is
logits = self.model(input)

So I think that this code is for DenseNet only. ??

Thanks,

@edwardcho
Copy link
Author

Hello Sir,

I add error message...

ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 768, 1, 1])

This error was occurred in this part.

logits = self.model(input)

What can I do for solving this problem??

Thanks..

@shaktikshri
Copy link

hi there, this is an issue in your model. Try debugging your classification network without the set temperature function first.

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

2 participants