WZMIAOMIAO/deep-learning-for-image-processing · error · Exception

Please run accumulate() first

Error message

Please run accumulate() first

What it means

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.

Source

Thrown at pytorch_keypoint/HRNet/validation.py:83

        print_string = iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, mean_s)
        return mean_s, print_string

    stats, print_list = [0] * 10, [""] * 10
    stats[0], print_list[0] = _summarize(1, maxDets=20)
    stats[1], print_list[1] = _summarize(1, maxDets=20, iouThr=.5)
    stats[2], print_list[2] = _summarize(1, maxDets=20, iouThr=.75)
    stats[3], print_list[3] = _summarize(1, maxDets=20, areaRng='medium')
    stats[4], print_list[4] = _summarize(1, maxDets=20, areaRng='large')
    stats[5], print_list[5] = _summarize(0, maxDets=20)
    stats[6], print_list[6] = _summarize(0, maxDets=20, iouThr=.5)
    stats[7], print_list[7] = _summarize(0, maxDets=20, iouThr=.75)
    stats[8], print_list[8] = _summarize(0, maxDets=20, areaRng='medium')
    stats[9], print_list[9] = _summarize(0, maxDets=20, areaRng='large')

    print_info = "\n".join(print_list)

    if not self.eval:
        raise Exception('Please run accumulate() first')

    return stats, print_info


def save_info(coco_evaluator,
              save_name: str = "record_mAP.txt"):
    # calculate COCO info for all keypoints
    coco_stats, print_coco = summarize(coco_evaluator)

    # 将验证结果保存至txt文件中
    with open(save_name, "w") as f:
        record_lines = ["COCO results:", print_coco]
        f.write("\n".join(record_lines))


def main(args):
    device = torch.device(args.device if torch.cuda.is_available() else "cpu")
    print("Using {} device training.".format(device.type))

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Ensure you call coco_evaluator.accumulate() (via the standard evaluate() helper) before summarize()/save_info.
  2. Verify the validation dataset is non-empty and the DataLoader yields batches on every rank.
  3. Guard the call: only invoke save_info if getattr(coco_evaluator, 'eval', None) is not None.
  4. In DDP, make sure synchronize_results/merge succeeded so results exist before accumulation.
  5. Fix the pipeline so exceptions during evaluate don't leave the evaluator half-initialized before save_info runs.

Example fix

# before
save_info(coco_evaluator)  # Exception if eval is None
# after
if getattr(coco_evaluator, "eval", None) is not None:
    save_info(coco_evaluator)
Defensive patterns

Strategy: type-guard

Validate before calling

if getattr(coco_evaluator, "eval", None) is None or len(coco_evaluator) == 0:
    logging.warning("No COCO eval results; skipping save_info")
else:
    save_info(coco_evaluator)

Type guard

def has_eval_results(coco_evaluator) -> bool:
    return getattr(coco_evaluator, "eval", None) is not None

Try / catch

try:
    save_info(coco_evaluator)
except Exception as e:
    if "accumulate" in str(e):
        logging.warning("Evaluation never ran (empty val set?) — skipping mAP record")
    else:
        raise

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/e043fbe9ff872a03. Report an issue: GitHub.