{"record":{"id":"1e5ea56e64fcaca2","repo":"open-mmlab/mmdetection","slug":"k-is-not-a-valid-recall-threshold","errorCode":null,"errorMessage":"{k} is not a valid recall threshold","messagePattern":"(.+?) is not a valid recall threshold","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"mmdet/evaluation/metrics/flickr30k_metric.py","lineNumber":35,"sourceCode":"        \"\"\"\n        Parameters:\n           - topk : tuple of ints corresponding to the recalls being\n           tracked (eg, recall@1, recall@10, ...)\n        \"\"\"\n\n        self.total_byk_bycat: Dict[int, Dict[str, int]] = {\n            k: defaultdict(int)\n            for k in topk\n        }\n        self.positives_byk_bycat: Dict[int, Dict[str, int]] = {\n            k: defaultdict(int)\n            for k in topk\n        }\n\n    def add_positive(self, k: int, category: str):\n        \"\"\"Log a positive hit @k for given category.\"\"\"\n        if k not in self.total_byk_bycat:\n            raise RuntimeError(f'{k} is not a valid recall threshold')\n        self.total_byk_bycat[k][category] += 1\n        self.positives_byk_bycat[k][category] += 1\n\n    def add_negative(self, k: int, category: str):\n        \"\"\"Log a negative hit @k for given category.\"\"\"\n        if k not in self.total_byk_bycat:\n            raise RuntimeError(f'{k} is not a valid recall threshold')\n        self.total_byk_bycat[k][category] += 1\n\n    def report(self) -> Dict[str, Dict[str, float]]:\n        \"\"\"Return a condensed report of the results as a dict of dict.\n\n        report[k][cat] is the recall@k for the given category\n        \"\"\"\n        report: Dict[str, Dict[str, float]] = {}\n        for k in self.total_byk_bycat:\n            assert k in self.positives_byk_bycat\n            report[str(k)] = {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/evaluation/metrics/flickr30k_metric.py#L17-L53","documentation":"Flickr30kRecall add_positive raises RuntimeError when the recall threshold `k` is not one of the thresholds the metric was initialized with (i.e. not a key in total_byk_bycat, which is built from the topk list given at construction). It guards the per-k/per-category counters from being silently created for unknown thresholds.","triggerScenarios":"Calling add_positive(k, category) where k is not in the topk list passed to the Flickr30k metric constructor (e.g. metric built with topk=[1,5,10] but add_positive called with k=3), typically inside compute_metrics when scoring text-to-image retrieval results.","commonSituations":"Changing the retrieval code to report a different k than the configured topk list; passing k as a string ('5' vs 5); mismatch between the topk config used at metric construction and the ks the scoring loop iterates over.","solutions":["Make the k values passed to add_positive exactly match the topk list given to the metric constructor","If you want additional thresholds, add them to the topk list when instantiating the metric","Ensure k is an int, not a string, before calling add_positive/add_negative"],"exampleFix":"// before\nmetric = Flickr30kMetric(topk=[1, 5, 10])\n... metric.recall.add_positive(k=3, cat)  # raises\n// after\nmetric = Flickr30kMetric(topk=[1, 3, 5, 10])\n... metric.recall.add_positive(k=3, cat)","handlingStrategy":"validation","validationCode":"valid_ks = set(metric.recall.total_byk_bycat.keys())\nif k not in valid_ks:\n    raise ValueError(f'{k} not in configured topk {sorted(valid_ks)}')\nmetric.recall.add_positive(k, category)","typeGuard":"def is_valid_recall_k(metric, k) -> bool:\n    return k in metric.recall.total_byk_bycat","tryCatchPattern":"try:\n    metric.recall.add_positive(k, category)\nexcept RuntimeError:\n    pass  # threshold not configured; skip or log","preventionTips":["Derive eval-loop k values from the same topk list used to build the metric","Keep topk in one config constant shared by constructor and scoring loop","Ensure k is an int, not a string, before calling"],"tags":["mmdetection","flickr30k","recall","argument-validation"],"backgroundTag":"value-not-in-allowed-set","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}