Deagin Claude Opus 5 (1M context) commited on
Commit
6991c35
·
1 Parent(s): 5d6a94e

Fix the actual OOM: np.linalg.svd was allocating an (N, N) matrix

Browse files

fit_planes refines each plane with an SVD of the (N, 3) inlier matrix:

_, _, vh = np.linalg.svd(inlier_pts - centroid)

NumPy defaults to full_matrices=True, which builds U at (N, N) — quadratic
in the inlier count for a matrix with three columns. Only vh[2] is used.

The instrumented log caught it exactly:

[rss=0.98G gpu=2.89G/peak2.89G] Building isolated: 820x921 px
[rss=0.98G gpu=2.89G/peak2.89G] Fitting 3D planes to DSM via RANSAC...
runtime error: Memory limit exceeded (32.0G)

RSS is under 1 GB one line before the kill, then one allocation blows 32 GB.
Reproduced offline with no API key: 644k inliers asks for 2.11 TiB, 3.8M asks
for 101 TiB. On Linux the allocator overcommits and the kernel OOM-kills the
container, which is why this surfaced as "Memory limit exceeded" rather than
a MemoryError traceback.

This scaled with roof area, which is what made it look like a hardware
shortfall: a 196x208 house passed, 820x921 did not, and t4-small -> t4-medium
only moved the ceiling from 16 GB to 32 GB. It is not fixable by buying RAM —
the ask grows as the square of the inlier count.

Measured on a 2000x2000 crop with 3,841,600 points, whole pipeline:
before: MemoryError, 101 TiB requested
after : RANSAC 27.8s, peak RSS 0.86 GB

Correctness checked against synthetic ground truth: two 25-degree faces at
90/270 azimuth recover as 25.00/25.05 degrees and 90.00/270.00.

Also in the same loop, np.random.choice(N, 3, replace=False) -> randint.
NumPy implements choice-without-replacement as a full permutation of the
population, so picking 3 points from a 644k cloud shuffled a 644k array on
every one of the 1000 iterations. Duplicate draws are harmless: they produce
a degenerate triangle, which the existing magnitude check discards.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Files changed (2) hide show
  1. AGENTS.md +19 -0
  2. ransac_planes.py +17 -3
AGENTS.md CHANGED
@@ -75,6 +75,25 @@ Use `model.get_nearest_supported_resolution()` to snap.
75
  C-RADIOv4-H expects pixel values in [0, 1] range. It normalizes internally.
76
  Do NOT apply ImageNet normalization.
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  ### RANSAC finds too few/many planes
79
  - Increase `max_planes` for complex roofs (hip, cross-gable).
80
  - Decrease `ransac_threshold` for tighter plane fits on clean DSM.
 
75
  C-RADIOv4-H expects pixel values in [0, 1] range. It normalizes internally.
76
  Do NOT apply ImageNet normalization.
77
 
78
+ ### Never call np.linalg.svd without full_matrices=False
79
+
80
+ `fit_planes` refines each plane with an SVD of the (N, 3) inlier matrix. NumPy
81
+ defaults to `full_matrices=True`, which builds U at **(N, N)** — quadratic in
82
+ the inlier count for a matrix with three columns. At 65k inliers that is one
83
+ 34 GB allocation; at 3.8M points it asks for 101 TiB. On Linux the allocator
84
+ overcommits and the kernel OOM-kills the container, so the Space reported
85
+ "Memory limit exceeded" with no traceback and RSS under 1 GB on the line
86
+ before. Only `vh[2]` is used and `vh` is identical either way.
87
+
88
+ This scaled with roof size, so it looked like a hardware problem: a small
89
+ house passed, an 820x921 px crop did not, and upgrading t4-small -> t4-medium
90
+ just moved the ceiling from 16 GB to 32 GB. Measured after the fix, the full
91
+ pipeline peaks at 0.86 GB on a 2000x2000 crop with 3.8M points.
92
+
93
+ Related, in the same loop: sample RANSAC triplets with `np.random.randint`,
94
+ never `np.random.choice(N, 3, replace=False)` — NumPy implements the latter as
95
+ a full permutation of N, so it shuffled a 500k array on every iteration.
96
+
97
  ### RANSAC finds too few/many planes
98
  - Increase `max_planes` for complex roofs (hip, cross-gable).
99
  - Decrease `ransac_threshold` for tighter plane fits on clean DSM.
ransac_planes.py CHANGED
@@ -108,7 +108,13 @@ def fit_planes(
108
  if len(remaining_pts) < 3:
109
  break
110
 
111
- idx = np.random.choice(len(remaining_pts), 3, replace=False)
 
 
 
 
 
 
112
  p1, p2, p3 = remaining_pts[idx]
113
 
114
  normal = np.cross(p2 - p1, p3 - p1)
@@ -130,10 +136,18 @@ def fit_planes(
130
  if best_count < min_points:
131
  break
132
 
133
- # SVD refinement
 
 
 
 
 
 
 
 
134
  inlier_pts = remaining_pts[best_inliers]
135
  centroid = inlier_pts.mean(axis=0)
136
- _, _, vh = np.linalg.svd(inlier_pts - centroid)
137
  refined_normal = vh[2]
138
  refined_d = -np.dot(refined_normal, centroid)
139
 
 
108
  if len(remaining_pts) < 3:
109
  break
110
 
111
+ # randint, not choice(..., replace=False): NumPy implements the
112
+ # latter as a full permutation of the population, so sampling 3
113
+ # points from a 500k point cloud shuffled a 500k array on every
114
+ # one of the 1000 iterations. Duplicate draws are harmless here —
115
+ # they give a degenerate triangle, which the `mag` check below
116
+ # already discards.
117
+ idx = np.random.randint(0, len(remaining_pts), 3)
118
  p1, p2, p3 = remaining_pts[idx]
119
 
120
  normal = np.cross(p2 - p1, p3 - p1)
 
136
  if best_count < min_points:
137
  break
138
 
139
+ # SVD refinement.
140
+ #
141
+ # full_matrices=False is load-bearing, not a micro-optimisation.
142
+ # inlier_pts is (N, 3); the default full_matrices=True builds U at
143
+ # (N, N), which is quadratic in the inlier count for a matrix with
144
+ # three columns. At 65k inliers that is a single 34 GB allocation —
145
+ # it OOM-killed the Space on any roof larger than a small house
146
+ # (820x921 px was enough) while RSS sat under 1 GB the line before.
147
+ # Only vh[2] is used, and vh is identical either way.
148
  inlier_pts = remaining_pts[best_inliers]
149
  centroid = inlier_pts.mean(axis=0)
150
+ _, _, vh = np.linalg.svd(inlier_pts - centroid, full_matrices=False)
151
  refined_normal = vh[2]
152
  refined_d = -np.dot(refined_normal, centroid)
153