code wiki / _hdl_build / nx_poseest_gate.nx

nx_poseest_gate.nx source

↩ module page · 123 lines · 5315 B

1// nx_poseest_gate.nx -- prove LEARNED pose localization (roadmap R2 v0): render the rig across arm poses, 2// classify the RIGHT-HAND region from the silhouette with a perceptron, prove it GENERALIZES to HELD-OUT arm 3// configs vs untrained at chance. image -> learned -> keypoint, the video->pose keystone middle at honest small 4// scale. (fresh name -- the nx_pose_estimate_gate target hit a name-keyed stale-.s cache.) expect_exit:0 ORIGINAL 5import "nx_syscalls.nx" 6import "nx_figure_render.nx" 7import "nx_pose_estimate.nx" 8 9func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 11 12const PW: i64 = 64 13const PH: i64 = 48 14const GW: i64 = 8 15const GH: i64 = 6 16const NCELL: i64 = 48 17const NFEAT: i64 = 49 18const NR: i64 = 9 19const NTR: i64 = 30 20const NHO: i64 = 9 21const NALL: i64 = 39 22 23func clamp3(v: i64) -> i64 { if v < 0 { return 0 } if v > 2 { return 2 } return v } 24func pe_sample(sk: i64, fb: *i64, zb: *i64, pitch: i64, elbow: i64, occ: *i64, wxy: *i64) -> i64 { 25 sk_pose(sk, RG_SHR, 0, md_deg(pitch)); sk_pose(sk, RG_ELR, md_deg(elbow), 0) 26 sk_update(sk) 27 rig_draw(sk, fb, zb, PW, PH, md_deg(15), 70) 28 pe_occupancy(zb, PW, PH, GW, GH, occ) 29 occ[NCELL] = 256 30 let bw: *i64 = sk_bone(sk, RG_WRR); wxy[0] = bw[15]; wxy[1] = bw[16] 31 return 0 32} 33 34func main() -> i64 { 35 hw("=== nx_poseest_gate -- learned hand-region from silhouette (R2 v0) ===\n" as *u8) 36 let sk: i64 = sys_mmap(sk_bytes()) as i64 37 let fb: *i64 = sys_mmap(PW * PH * 8) as *i64 38 let zb: *i64 = sys_mmap(PW * PH * 8) as *i64 39 let occs: *i64 = sys_mmap(NALL * NFEAT * 8) as *i64 40 let wx: *i64 = sys_mmap(NALL * 8) as *i64 41 let wy: *i64 = sys_mmap(NALL * 8) as *i64 42 let trp: *i64 = sys_mmap(6 * 8) as *i64 43 trp[0]=0-70; trp[1]=0-40; trp[2]=0-10; trp[3]=20; trp[4]=50; trp[5]=80 44 let tre: *i64 = sys_mmap(5 * 8) as *i64 45 tre[0]=0; tre[1]=25; tre[2]=50; tre[3]=75; tre[4]=100 46 let hop: *i64 = sys_mmap(3 * 8) as *i64 47 hop[0]=0-55; hop[1]=5; hop[2]=65 48 let hoe: *i64 = sys_mmap(3 * 8) as *i64 49 hoe[0]=12; hoe[1]=37; hoe[2]=62 50 51 rig_build(sk) 52 let wxy: *i64 = sys_mmap(2 * 8) as *i64 53 var idx: i64 = 0 54 var pi: i64 = 0 55 while pi < 6 { 56 var ei: i64 = 0 57 while ei < 5 { 58 let occp: *i64 = (occs as i64 + idx * NFEAT * 8) as *i64 59 pe_sample(sk, fb, zb, trp[pi], tre[ei], occp, wxy) 60 wx[idx] = wxy[0]; wy[idx] = wxy[1]; idx = idx + 1 61 ei = ei + 1 62 } 63 pi = pi + 1 64 } 65 pi = 0 66 while pi < 3 { 67 var ei: i64 = 0 68 while ei < 3 { 69 let occp: *i64 = (occs as i64 + idx * NFEAT * 8) as *i64 70 pe_sample(sk, fb, zb, hop[pi], hoe[ei], occp, wxy) 71 wx[idx] = wxy[0]; wy[idx] = wxy[1]; idx = idx + 1 72 ei = ei + 1 73 } 74 pi = pi + 1 75 } 76 77 var xmin: i64 = wx[0]; var xmax: i64 = wx[0]; var ymin: i64 = wy[0]; var ymax: i64 = wy[0] 78 var i: i64 = 1 79 while i < NALL { 80 if wx[i] < xmin { xmin = wx[i] } 81 if wx[i] > xmax { xmax = wx[i] } 82 if wy[i] < ymin { ymin = wy[i] } 83 if wy[i] > ymax { ymax = wy[i] } 84 i = i + 1 85 } 86 let labels: *i64 = sys_mmap(NALL * 8) as *i64 87 i = 0 88 while i < NALL { 89 let xr: i64 = clamp3(3 * (wx[i] - xmin) / (xmax - xmin + 1)) 90 let yr: i64 = clamp3(3 * (wy[i] - ymin) / (ymax - ymin + 1)) 91 labels[i] = yr * 3 + xr 92 i = i + 1 93 } 94 hw(" wrist range x["); pn(xmin); hw(","); pn(xmax); hw("] y["); pn(ymin); hw(","); pn(ymax); hw("]\n" as *u8) 95 96 let W: *i64 = sys_mmap(NR * NFEAT * 8) as *i64 97 let mist: i64 = pe_train(W, occs, labels, NTR, NFEAT, NR, 60) 98 hw(" perceptron trained on "); pn(NTR); hw(" poses, last-epoch mistakes="); pn(mist); hw("\n" as *u8) 99 100 let W0: *i64 = sys_mmap(NR * NFEAT * 8) as *i64 101 var z: i64 = 0 102 while z < NR * NFEAT { W0[z] = 0; z = z + 1 } 103 var corr: i64 = 0 104 var corr0: i64 = 0 105 i = NTR 106 while i < NALL { 107 let occ: *i64 = (occs as i64 + i * NFEAT * 8) as *i64 108 if pe_predict(W, occ, NFEAT, NR) == labels[i] { corr = corr + 1 } 109 if pe_predict(W0, occ, NFEAT, NR) == labels[i] { corr0 = corr0 + 1 } 110 i = i + 1 111 } 112 hw(" HELD-OUT: TRAINED "); pn(corr); hw("/"); pn(NHO); hw(" vs UNTRAINED "); pn(corr0); hw("/"); pn(NHO); hw("\n" as *u8) 113 114 var fails: i64 = 0 115 if corr > corr0 { 116 if corr * 100 >= NHO * 55 { hw("T PASS GENERALIZES (trained "); pn(corr); hw("/"); pn(NHO); hw(" >> untrained "); pn(corr0); hw(" = learned image->keypoint, not chance)\n" as *u8) } 117 else { fails = fails + 1; hw("T FAIL trained accuracy too low "); pn(corr); hw("/"); pn(NHO); hw("\n" as *u8) } 118 } else { fails = fails + 1; hw("T FAIL trained not > untrained\n" as *u8) } 119 120 if fails == 0 { hw("POSEEST-GATE GREEN -- learned pose localization generalizes (R2 v0; coarse region, our-render domain)\n" as *u8); sys_exit(0); return 0 } 121 hw("POSEEST-GATE RED fails="); pn(fails); hw("\n" as *u8) 122 sys_exit(1); return 1 123}