rogermt commited on
Commit
da24a99
Β·
verified Β·
1 Parent(s): 3931383

Task 319 ONNX Guide: full algorithm for 267/267, memory estimate ~100-200KB

Browse files
Files changed (1) hide show
  1. medal-solvers/TASK319_ONNX_GUIDE.md +64 -0
medal-solvers/TASK319_ONNX_GUIDE.md ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Task 319 β€” ONNX Build Guide
2
+
3
+ ## Rule (267/267 VERIFIED)
4
+
5
+ **CRITICAL**: Object binary = `(inp == color)` in bbox, NOT `(inp != bg)`!
6
+
7
+ ### Algorithm (2-pass, template = largest then second-largest):
8
+
9
+ ```
10
+ 1. bg = channel with most pixels (argmax of per-channel sums)
11
+ 2. Sort non-bg channels by pixel count β†’ [ch1_largest, ch2, ch3]
12
+ 3. For template_channel in [ch1_largest, ch2]:
13
+ a. Find largest uniform block (bs_r, bs_c) in template's binary
14
+ b. Downsample template by block β†’ pattern (ph Γ— pw)
15
+ c. For each other channel (candidate):
16
+ - Cross-correlate pattern with candidate binary at all positions
17
+ - If max correlation == sum(pattern) β†’ MATCH FOUND
18
+ - Output = candidate channel's binary (one-hot encoded)
19
+ d. If no match with uniform block, try row/col grouping:
20
+ - Group consecutive identical rows, group consecutive identical cols
21
+ - Take first row/col of each group β†’ downsampled pattern
22
+ - Cross-correlate with candidates
23
+ 4. Output = matched candidate in one-hot format
24
+ ```
25
+
26
+ ### Statistics:
27
+ - Largest template + block method: 248/267
28
+ - + row/col grouping: 250/267
29
+ - + second-largest template: 267/267 (100%!)
30
+
31
+ ### ONNX Implementation Notes:
32
+
33
+ **Input**: [1, 10, 30, 30] β€” channels ARE the per-color masks
34
+
35
+ **Key Operations Needed**:
36
+ 1. Per-channel pixel sum β†’ ReduceSum [10] β†’ find bg (ArgMax) and rank others
37
+ 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)
38
+ 3. Block uniformity check: reshape to [h/bs_r, bs_r, w/bs_c, bs_c], check min==max per block
39
+ 4. Cross-correlation: Conv2D with downsampled pattern as kernel (or MatMul approach)
40
+ 5. Match detection: max(correlation) == sum(pattern)
41
+ 6. Output construction: select matched channel
42
+
43
+ **Memory Estimate**:
44
+ - Correlation maps: ~[30,30] per block_size Γ— ~10 sizes = 36KB
45
+ - Template/candidate masks: [30,30] Γ— 3 = 11KB
46
+ - Auxiliaries: ~50KB
47
+ - **Total: ~100-200KB β†’ score 12-13 pts β†’ gain +4-5**
48
+
49
+ **Block Size Enumeration** (covers 248/267):
50
+ - Valid (bs_r, bs_c) where bs divides object dims (max 10Γ—10)
51
+ - 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)
52
+
53
+ **Row/Col Grouping in ONNX** (covers remaining 2 from 1st template):
54
+ - Row boundary: ReduceSum(abs(mask[r] - mask[r-1])) > 0
55
+ - Take rows AT boundaries β†’ downsampled rows
56
+ - Similarly for cols
57
+
58
+ **Second Template Pass** (covers final 17):
59
+ - Same algorithm but on second-largest channel
60
+ - If first template found a match, skip this pass
61
+
62
+ ### Files
63
+ - `task319_solver_267.py` β€” Complete Python solver (267/267)
64
+ - `build_task255_onnx.py` β€” Reference for ONNX building patterns