{"record":{"id":"e043fbe9ff872a03","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"please-run-accumulate-first","errorCode":null,"errorMessage":"Please run accumulate() first","messagePattern":"Please run accumulate\\(\\) first","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"pytorch_keypoint/HRNet/validation.py","lineNumber":83,"sourceCode":"        print_string = iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, mean_s)\n        return mean_s, print_string\n\n    stats, print_list = [0] * 10, [\"\"] * 10\n    stats[0], print_list[0] = _summarize(1, maxDets=20)\n    stats[1], print_list[1] = _summarize(1, maxDets=20, iouThr=.5)\n    stats[2], print_list[2] = _summarize(1, maxDets=20, iouThr=.75)\n    stats[3], print_list[3] = _summarize(1, maxDets=20, areaRng='medium')\n    stats[4], print_list[4] = _summarize(1, maxDets=20, areaRng='large')\n    stats[5], print_list[5] = _summarize(0, maxDets=20)\n    stats[6], print_list[6] = _summarize(0, maxDets=20, iouThr=.5)\n    stats[7], print_list[7] = _summarize(0, maxDets=20, iouThr=.75)\n    stats[8], print_list[8] = _summarize(0, maxDets=20, areaRng='medium')\n    stats[9], print_list[9] = _summarize(0, maxDets=20, areaRng='large')\n\n    print_info = \"\\n\".join(print_list)\n\n    if not self.eval:\n        raise Exception('Please run accumulate() first')\n\n    return stats, print_info\n\n\ndef save_info(coco_evaluator,\n              save_name: str = \"record_mAP.txt\"):\n    # calculate COCO info for all keypoints\n    coco_stats, print_coco = summarize(coco_evaluator)\n\n    # 将验证结果保存至txt文件中\n    with open(save_name, \"w\") as f:\n        record_lines = [\"COCO results:\", print_coco]\n        f.write(\"\\n\".join(record_lines))\n\n\ndef main(args):\n    device = torch.device(args.device if torch.cuda.is_available() else \"cpu\")\n    print(\"Using {} device training.\".format(device.type))","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_keypoint/HRNet/validation.py#L65-L101","documentation":"The COCO-style summarizer requires self.eval to be populated by a prior accumulate() call (pycocotools COCOeval contract). If summarize() runs before accumulate(), self.eval is None and it raises Exception('Please run accumulate() first'). Here save_info triggers summarize on an evaluator that never completed evaluation.","triggerScenarios":"Calling summarize()/save_info(coco_evaluator) when evaluate() produced no results — e.g. empty validation set, evaluator constructed but update() never called, distributed run where a rank got no data so accumulate was skipped, or calling summarize manually before accumulate().","commonSituations":"Empty val dataset directory; all samples filtered out by transforms; saving mAP record on exception paths where evaluation partially ran; copying save_info into a custom loop that forgets to call evaluate/accumulate first.","solutions":["Ensure you call coco_evaluator.accumulate() (via the standard evaluate() helper) before summarize()/save_info.","Verify the validation dataset is non-empty and the DataLoader yields batches on every rank.","Guard the call: only invoke save_info if getattr(coco_evaluator, 'eval', None) is not None.","In DDP, make sure synchronize_results/merge succeeded so results exist before accumulation.","Fix the pipeline so exceptions during evaluate don't leave the evaluator half-initialized before save_info runs."],"exampleFix":"# before\nsave_info(coco_evaluator)  # Exception if eval is None\n# after\nif getattr(coco_evaluator, \"eval\", None) is not None:\n    save_info(coco_evaluator)","handlingStrategy":"type-guard","validationCode":"if getattr(coco_evaluator, \"eval\", None) is None or len(coco_evaluator) == 0:\n    logging.warning(\"No COCO eval results; skipping save_info\")\nelse:\n    save_info(coco_evaluator)","typeGuard":"def has_eval_results(coco_evaluator) -> bool:\n    return getattr(coco_evaluator, \"eval\", None) is not None","tryCatchPattern":"try:\n    save_info(coco_evaluator)\nexcept Exception as e:\n    if \"accumulate\" in str(e):\n        logging.warning(\"Evaluation never ran (empty val set?) — skipping mAP record\")\n    else:\n        raise","preventionTips":["Always call accumulate() before summarize().","Assert the validation DataLoader yields at least one batch per rank.","Check the empty-val-set case in distributed runs."],"tags":["pytorch","coco","evaluation","lifecycle","state-error"],"backgroundTag":"accumulate-not-called","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}