# Task 319 — ONNX Build Guide ## Rule (267/267 VERIFIED) **CRITICAL**: Object binary = `(inp == color)` in bbox, NOT `(inp != bg)`! ### Algorithm (2-pass, template = largest then second-largest): ``` 1. bg = channel with most pixels (argmax of per-channel sums) 2. Sort non-bg channels by pixel count → [ch1_largest, ch2, ch3] 3. For template_channel in [ch1_largest, ch2]: a. Find largest uniform block (bs_r, bs_c) in template's binary b. Downsample template by block → pattern (ph × pw) c. For each other channel (candidate): - Cross-correlate pattern with candidate binary at all positions - If max correlation == sum(pattern) → MATCH FOUND - Output = candidate channel's binary (one-hot encoded) d. If no match with uniform block, try row/col grouping: - Group consecutive identical rows, group consecutive identical cols - Take first row/col of each group → downsampled pattern - Cross-correlate with candidates 4. Output = matched candidate in one-hot format ``` ### Statistics: - Largest template + block method: 248/267 - + row/col grouping: 250/267 - + second-largest template: 267/267 (100%!) ### ONNX Implementation Notes: **Input**: [1, 10, 30, 30] — channels ARE the per-color masks **Key Operations Needed**: 1. Per-channel pixel sum → ReduceSum [10] → find bg (ArgMax) and rank others 2. For template channel: try block sizes (2,2), (2,3), (3,2), (3,3), (2,4), (4,2), (4,3), (3,4), (4,4), (5,5) 3. Block uniformity check: reshape to [h/bs_r, bs_r, w/bs_c, bs_c], check min==max per block 4. Cross-correlation: Conv2D with downsampled pattern as kernel (or MatMul approach) 5. Match detection: max(correlation) == sum(pattern) 6. Output construction: select matched channel **Memory Estimate**: - Correlation maps: ~[30,30] per block_size × ~10 sizes = 36KB - Template/candidate masks: [30,30] × 3 = 11KB - Auxiliaries: ~50KB - **Total: ~100-200KB → score 12-13 pts → gain +4-5** **Block Size Enumeration** (covers 248/267): - Valid (bs_r, bs_c) where bs divides object dims (max 10×10) - Try: (2,2), (2,1), (1,2), (3,3), (3,1), (1,3), (2,3), (3,2), (4,2), (2,4), (5,5), (4,4), (5,2), (2,5) **Row/Col Grouping in ONNX** (covers remaining 2 from 1st template): - Row boundary: ReduceSum(abs(mask[r] - mask[r-1])) > 0 - Take rows AT boundaries → downsampled rows - Similarly for cols **Second Template Pass** (covers final 17): - Same algorithm but on second-largest channel - If first template found a match, skip this pass ### Files - `task319_solver_267.py` — Complete Python solver (267/267) - `build_task255_onnx.py` — Reference for ONNX building patterns