fix: reduce peak memory usage for large images and batch runs - #16
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three targeted changes to address the OOM crash in issue #13:
det_sizereduction (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_DIMENSIONcap (pipeline.py) — images with a longest edge above 4096px are downscaled withINTER_AREAbefore any inference. Prevents unbounded memory growth from high-res inputs. Configurable via theREFACER_MAX_DIMENSIONenv 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_sizechange is the highest-leverage fix for the single-image case. Thegc.collect()call addresses batch accumulation. The dimension cap is a safety valve for unusually large inputs.Reviewer notes
REFACER_MAX_DIMENSIONdefault of 4096 is generous — most photojournalism images are well below this. Tune downward if more headroom is needed.Closes #13