Skip to content

adding normalization at inferece level for applying temp scaling in yolov8n #13597

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

Open
omidgh1379 opened this issue May 18, 2025 · 2 comments
Open
Labels
detect Object Detection issues, PR's question Further information is requested

Comments

@omidgh1379
Copy link

Im finetune yolov8n on my own dataset with the goal of getting the raw logits and apply temp scaling to reduce the uncertainty.

when I get the raw result from model.model(image) which normalization should I apply to match my traning normalization ?

since when I apply without normizaiton I have high overconfidect model and when I divide by 255 my model is underconfident.

def compare_with_original_model(self , valid_loader ):

    all_logits = []
    all_labels =[]

    

    with torch.no_grad():

        self.temperature.data = torch.tensor([1.0] , device = self.temperature.device , dtype = torch.float32)
        for batch in valid_loader:
            img = batch['img']
            img = img / 255
            
            outputs = self.forward(img)
            

            targets = {
                'cls' : batch['cls'] , 
                'bboxes' : batch['bboxes'] , 
                'batch_idx' : batch['batch_idx']
            }

            logits , labels = self._match_targets_to_outputs(outputs , targets)

            if logits is not None and labels is not None:
                all_logits.append(logits)
                all_labels.append(labels)

    all_logits = torch.cat(all_logits , dim = 0)
    all_labels = torch.cat(all_labels , dim = 0).long()


    nll_criterion = nn.CrossEntropyLoss().cuda()
    ece_criterion = _ECE_criterion().cuda()

    nll = nll_criterion(all_logits , all_labels.squeeze(1)).item()
    ece = ece_criterion(all_logits , all_labels.squeeze(1)).item()

    print('all_logits shape is : ',all_logits.shape)
    print('all_labels shape is : ',all_labels.squeeze(1).shape)

    print(f'NLL: {nll:.3f}, ECE: {ece:.3f}')
    print('entropy of the temperature scaled model is : ',self.confidence_entropy(all_logits))
    return nll , ece 

Originally posted by @omidgh1379 in #12063

@UltralyticsAssistant UltralyticsAssistant added detect Object Detection issues, PR's question Further information is requested labels May 18, 2025
@omidgh1379 omidgh1379 changed the title adding normalization at inferece layer for applying temp scaling in yolov8n adding normalization at inferece level for applying temp scaling in yolov8n May 18, 2025
@UltralyticsAssistant
Copy link
Member

👋 Hello @omidgh1379, thank you for reaching out and for your interest in YOLOv5 🚀! This is an automated response to help you get started—an Ultralytics engineer will assist you further soon.

For questions involving inference normalization, temperature scaling, or custom model modifications, please provide a minimum reproducible example (MRE) if possible. This helps us understand and debug your scenario more efficiently.

If your issue relates to 🐛 bugs, please share the smallest code snippet and dataset sample that reproduces the problem. For custom training or inference questions, include relevant code, logs, and any clarifying details about your dataset or model setup. You can also review our Tips for Best Training Results for guidance.

Requirements

Python>=3.8.0 with all requirements.txt installed, including PyTorch>=1.8. Quick install:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 can be run in these verified environments, all dependencies (including CUDA/CUDNN, Python, PyTorch) preinstalled:

Status

YOLOv5 CI
If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Thank you for your contribution! 🥳

@pderrenger
Copy link
Member

Hi @omidgh1379,

I notice you're asking about YOLOv8n normalization in the YOLOv5 repository. For consistency between training and inference, you should apply the same normalization in both stages. Since you trained with images normalized by dividing by 255, you should maintain this during inference.

If you're experiencing confidence calibration issues, temperature scaling should be applied to the model's output logits after normalization, not as a replacement for input normalization. The division by 255 is correct for input preprocessing.

For optimal temperature scaling:

  1. Keep your input normalization consistent (img = img / 255)
  2. Apply temperature scaling to the output logits (dividing logits by your learned temperature parameter)
  3. Fine-tune the temperature parameter on a validation set

If you're specifically working with YOLOv8n, you might want to check the Ultralytics YOLOv8 repository for architecture-specific details, as there could be slight differences in how normalization is handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
detect Object Detection issues, PR's question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants