Skip to content

fix: reduce peak memory usage for large images and batch runs - #16

Merged
strbck merged 1 commit into
mainfrom
fix/memory-oom
May 13, 2026
Merged

fix: reduce peak memory usage for large images and batch runs#16
strbck merged 1 commit into
mainfrom
fix/memory-oom

Conversation

@strbck

@strbck strbck commented May 13, 2026

Copy link
Copy Markdown
Owner

Summary

Three targeted changes to address the OOM crash in issue #13:

  • det_size reduction (models.py) — InsightFace was running at (1024, 1024), which is 2.6× the area of the standard (640, 640). Dropped to 640, which also fixes an inconsistency with the Dockerfile pre-warm (which already used 640).
  • REFACER_MAX_DIMENSION cap (pipeline.py) — images with a longest edge above 4096px are downscaled with INTER_AREA before any inference. Prevents unbounded memory growth from high-res inputs. Configurable via the REFACER_MAX_DIMENSION env var.
  • gc.collect() between images (pipeline.py) — forces Python to release large intermediate arrays (image buffers, ONNX activations) to the OS after each image rather than letting them accumulate across a batch run.

Why

Single-image runs were spiking to ~5.8 GB on a machine with a 4 GB Docker limit. The det_size change is the highest-leverage fix for the single-image case. The gc.collect() call addresses batch accumulation. The dimension cap is a safety valve for unusually large inputs.

Reviewer notes

  • No changes to the public API or Docker setup.
  • The REFACER_MAX_DIMENSION default of 4096 is generous — most photojournalism images are well below this. Tune downward if more headroom is needed.
  • A warning is logged whenever downscaling occurs so it's visible in the UI/CLI output.

Closes #13

Three changes targeting the OOM crash reported in issue #13:

1. Reduce InsightFace det_size from (1024, 1024) to (640, 640).
   The detection grid was 2.6× larger than needed — 640 is the
   standard size for this model family and matches the value already
   used in the Dockerfile pre-warm step.

2. Add REFACER_MAX_DIMENSION cap (default 4096px longest edge).
   Images larger than this are downscaled with INTER_AREA before
   any inference runs. Prevents unbounded memory growth from very
   high-resolution inputs. Override via the REFACER_MAX_DIMENSION
   env var.

3. Call gc.collect() after each image in the batch loop.
   Ensures large intermediate arrays (full-resolution image buffers,
   ONNX activations) are released to the OS between images rather
   than accumulating across the run.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@strbck
strbck merged commit 05246f9 into main May 13, 2026
1 check passed
@strbck
strbck deleted the fix/memory-oom branch May 13, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refacer will exit code 137 if presented with too many input images

1 participant