code wiki / (root) / nx_pose_keypoints_gate.nx

nx_pose_keypoints_gate.nx source

↩ module page · 63 lines · 3433 B

1// nx_pose_keypoints_gate.nx -- gate for the weights-free pose back-half (heatmap -> keypoint). Synthetic heatmaps 2// prove argmax + quarter-pixel subpixel + occlusion-reject + symmetric-no-shift + multi-joint plumbing. 3import "nx_syscalls.nx" 4import "nx_pose_keypoints.nx" 5 6func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return sys_write(1, s, n) } 7func gn(v: i64) -> i64 { 8 let bb: *u8 = sys_mmap(28); var m: i64 = v 9 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 10 let t: *u8 = sys_mmap(28); var k: i64 = 0 11 if m == 0 { t[0] = 48 as u8; k = 1 } 12 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 13 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 14 return sys_write(1, bb, k) 15} 16 17func main(argc: i64, argv: *i64) -> i64 { 18 var pass: i64 = 0 19 let W: i64 = 8; let H: i64 = 8 20 let hm: *i64 = sys_mmap(8 * 128) as *i64 21 let qx: *i64 = sys_mmap(8) as *i64; let qy: *i64 = sys_mmap(8) as *i64 22 let px: *i64 = sys_mmap(8) as *i64; let py: *i64 = sys_mmap(8) as *i64 23 var i: i64 = 0 24 25 // P1 argmax: flat 10, a 100 peak at (x=3,y=2) 26 i = 0; while i < 64 { hm[i] = 10; i = i + 1 } 27 hm[2*8+3] = 100 28 var c1: i64 = pose_argmax(hm, W, H, px, py) 29 if c1 == 100 { if px[0] == 3 { if py[0] == 2 { pass = pass + 1; gp("P1 argmax peak (3,2) OK\n" as *u8) } } } 30 if px[0] != 3 { gp("P1 FAIL x=" as *u8); gn(px[0]); gp(" y=" as *u8); gn(py[0]); gp("\n" as *u8) } 31 32 // P2 subpixel: right neighbor higher -> sx=+1 -> qx=13; y symmetric -> qy=8 33 hm[2*8+4] = 60; hm[2*8+2] = 20 34 var c2: i64 = pose_keypoint(hm, W, H, 50, qx, qy) 35 if c2 == 100 { if qx[0] == 13 { if qy[0] == 8 { pass = pass + 1; gp("P2 subpixel qx=13 qy=8 OK\n" as *u8) } } } 36 if qx[0] != 13 { gp("P2 FAIL qx=" as *u8); gn(qx[0]); gp(" qy=" as *u8); gn(qy[0]); gp("\n" as *u8) } 37 38 // P3 occlusion: flat 5, thresh 50 -> peak below thresh -> (-1,-1) conf 0 39 i = 0; while i < 64 { hm[i] = 5; i = i + 1 } 40 var c3: i64 = pose_keypoint(hm, W, H, 50, qx, qy) 41 if c3 == 0 { if qx[0] == 0-1 { pass = pass + 1; gp("P3 below-thresh -> occluded (-1) OK\n" as *u8) } } else { gp("P3 FAIL conf=" as *u8); gn(c3); gp("\n" as *u8) } 42 43 // P5 symmetric peak (3,3), equal neighbors -> NO shift -> qx=12 qy=12 44 i = 0; while i < 64 { hm[i] = 10; i = i + 1 } 45 hm[3*8+3] = 100 46 var c5: i64 = pose_keypoint(hm, W, H, 50, qx, qy) 47 if c5 == 100 { if qx[0] == 12 { if qy[0] == 12 { pass = pass + 1; gp("P5 symmetric -> no subpixel shift OK\n" as *u8) } } } 48 if qx[0] != 12 { gp("P5 FAIL qx=" as *u8); gn(qx[0]); gp("\n" as *u8) } 49 50 // P4 multi-joint: 2 heatmaps, peaks at (1,1) and (5,5) -> nvis=2, coords 4 and 20 51 i = 0; while i < 128 { hm[i] = 10; i = i + 1 } 52 hm[1*8+1] = 90 53 hm[64 + 5*8+5] = 90 54 let oqx: *i64 = sys_mmap(8*4) as *i64; let oqy: *i64 = sys_mmap(8*4) as *i64; let oc: *i64 = sys_mmap(8*4) as *i64 55 var nv: i64 = pose_extract(hm, 2, W, H, 50, oqx, oqy, oc) 56 if nv == 2 { if oqx[0] == 4 { if oqx[1] == 20 { pass = pass + 1; gp("P4 multi-joint 2 keypoints (4,20) OK\n" as *u8) } } } 57 if nv != 2 { gp("P4 FAIL nvis=" as *u8); gn(nv); gp("\n" as *u8) } 58 59 gp("POSE-KEYPOINTS-GATE pass=" as *u8); gn(pass); gp("/5\n" as *u8) 60 if pass == 5 { gp("POSE-KEYPOINTS-GATE GREEN 5/5 (argmax + quarter-pixel subpixel + occlusion + symmetric + multi-joint)\n" as *u8); sys_exit(0) } 61 sys_exit(1) 62 return 0 63}