Skip to content

Anomalib image_threshold with recall=1 #2641

Closed

Description

@mohamed-s7

in anomalib PaDiM, model.image_threshold = 88.8 for the best F1 score.
but I want the recall = 1 even if I lose a little bit in precision, so I want recall = 1 and maximise the precision.
how can I find that threshold?

using just pred_scores that are between 0 and 1, and labels the threshold that I am getting is 0.4 (value between 0 and 1) but manually I found the threshold that makes the recall = 1 and maximaizes the precision is 77.8

Activity

  1. alfieroddan commented on Jun 30, 2025

    @alfieroddan
    Contributor

    Each model in anomalib generates a prediction by using the PostProcessor.

    So in your case, instead of calculating the threshold based on F1 (this is done internally by using F1AdaptiveThreshold), you could:

    Turn off post-processing

    from anomalib.models import Padim
    
    model = Padim(post_processor=False)

    This would generate an "anomaly map" which you can do any custom normalisation, thresholding or processing you want.

    Change PostProcessor Args

    The PostProcessor has some args you can change.

    As an example you could keep the normalisation of the PostProcessor but turn off the thresholding.

    from anomalib.models import Padim
    from anomalib.post_processing import PostProcessor
    
    post_processor = PostProcessor(enable_thresholding=False)
    model = Padim(post_processor=post_processor)

    Then you could manually set the threshold but keep the normalisation that anomalib does.

    Create your own PostProcessor

    See here in the docs

    Implement a new adaptive threshold

    I think this is probably the best way for longevity is to create your own PostProcessor with your own threshold selection. Internally for PaDiM the default PostProcessor is used.

    Instead of F1AdaptiveThreshold, you could write your own!

    This could adaptively set the threshold for optimal Recall whilst maintaining anomalibs PostProcessor features.

  2. samet-akcay commented on Jun 30, 2025

    @samet-akcay
    Contributor

    Thanks @alfieroddan for the detailed response. I'm converting this to a Q&A. We could continue there if needed

  3. locked and limited conversation to collaborators on Jun 30, 2025
  4. converted this issue into a discussion #2800 on Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions