code wiki / _hdl_build / nx_natstat_corpus.nx

nx_natstat_corpus.nx source

↩ module page · 160 lines · 8986 B

1// nx_natstat_corpus.nx -- N2: fit the NATURAL-IMAGE model from a real-photo CORPUS (replaces hand-set thresholds 2// with corpus-derived mean/spread per NSS feature = the Mahalanobis natural model NIQE/BRISQUE use). Fetches a 3// dozen real photographs over our sovereign HTTPS+JPEG stack, computes [mscn_rho, subject-kurtosis, colourful, 4// scale-spread] on each, and prints mean+std per feature (paste into nx_natstat). Statistics only -- no image is 5// stored or shown. expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_x509_trust_store.nx" 8import "nx_trust_store_load_from_certdata.nx" 9import "nx_https_fetch_follow.nx" 10import "nx_jpeg_ascii.nx" 11import "nx_natstat.nx" 12const K_MAGIC_4194304: i64 = 4194304 13const K_MAGIC_99999: i64 = 99999 14const K_MAGIC_65536: i64 = 65536 15 16func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 17func pn(v: i64) -> i64 { 18 let b: *u8 = sys_mmap(32) as *u8 19 var x: i64 = v; var neg: i64 = 0 20 if x < 0 { neg = 1; x = 0 - x } 21 var i: i64 = 31 22 if x == 0 { b[i] = 48 as u8; i = i - 1 } 23 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 24 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 25 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 26 return 0 27} 28func isqrt(v: i64) -> i64 { if v <= 0 { return 0 } var x: i64 = v; var y: i64 = (x + 1) / 2; while y < x { x = y; y = (x + v / x) / 2 } return x } 29// find the JPEG SOI (0xFF 0xD8) in the fetched bytes -> body start (skips any HTTP headers). -1 if none. 30func find_soi(buf: *u8, n: i64) -> i64 { 31 var i: i64 = 0 32 while i < n - 1 { 33 if (buf[i] & 0xff) == 0xFF { if (buf[i + 1] & 0xff) == 0xD8 { return i } } 34 i = i + 1 35 } 36 return 0 - 1 37} 38func build_url(buf: *u8, c: i64) -> i64 { 39 let t: *u8 = "https://picsum.photos/seed/a/336/336" as *u8 // real photos (progressive JPEG), 1 per seed char 40 var i: i64 = 0 41 while t[i] != (0 as u8) { buf[i] = t[i]; i = i + 1 } 42 buf[i] = 0 as u8 43 buf[27] = c as u8 44 return 0 45} 46func main() -> i64 { 47 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 48 if r <= 0 { w("STORE-FAIL\n" as *u8); return 1 } 49 let store: *TrustStore = r as *TrustStore 50 let cap: i64 = K_MAGIC_4194304 51 let raw: *u8 = sys_mmap(cap) 52 let url: *u8 = sys_mmap(128) 53 let sc: i64 = 512 * 512 * 8 54 let buf1: *i64 = sys_mmap(sc) as *i64 55 let buf2: *i64 = sys_mmap(sc) as *i64 56 let rgbp: *i64 = sys_mmap(8) as *i64 57 let wp: *i64 = sys_mmap(8) as *i64 58 let hp: *i64 = sys_mmap(8) as *i64 59 let st: *i64 = sys_mmap(8) as *i64 60 // accumulators for the 4 features: sum + sumsq 61 let fs: *i64 = sys_mmap(64) as *i64 62 let fq: *i64 = sys_mmap(64) as *i64 63 var f: i64 = 0 64 while f < 8 { fs[f] = 0; fq[f] = 0; f = f + 1 } 65 let mbuf: *i64 = sys_mmap(sc) as *i64 // MSCN coefficient buffer for the pairwise (BRISQUE) features 66 let out5: *i64 = sys_mmap(64) as *i64 // per-photo overall photoreal LEVEL distribution + mean ai-fp 67 var lmin: i64 = K_MAGIC_99999 68 var lmax: i64 = 0 69 var lsum: i64 = 0 70 var aisum: i64 = 0 71 var ok: i64 = 0 72 w("=== NATURAL-IMAGE CORPUS (real photos -> NSS model) ===\n" as *u8) 73 var k: i64 = 0 74 while k < 14 { 75 build_url(url, 97 + k) 76 let n: i64 = nx_https_fetch_follow(url, store, raw, cap, 6, st) 77 w(" seed http=" as *u8); pn(st[0]); w(" n=" as *u8); pn(n); w(" b0=" as *u8); pn(raw[0] & 0xff); w(" b1=" as *u8); pn(raw[1] & 0xff); w(" b2=" as *u8); pn(raw[2] & 0xff); w("\n" as *u8) 78 if n > 1000 { 79 let soi: i64 = find_soi(raw, n) 80 if soi >= 0 { 81 let drc: i64 = nx_jpeg_decode_rgb((raw as i64 + soi) as *u8, n - soi, rgbp, wp, hp) 82 if drc == NX_JPEG_ASCII_OK { 83 let iw: i64 = wp[0] 84 let ih: i64 = hp[0] 85 let rgb: *u8 = rgbp[0] as *u8 86 let fb: *i64 = sys_mmap(iw * ih * 8) as *i64 87 var p: i64 = 0 88 while p < iw * ih { 89 fb[p] = (rgb[p * 3] & 0xff) + (rgb[p * 3 + 1] & 0xff) * 256 + (rgb[p * 3 + 2] & 0xff) * K_MAGIC_65536 90 p = p + 1 91 } 92 let f0: i64 = ns_mscn_rho(fb, iw, ih) 93 let f1: i64 = ns_grad_kurt(fb, iw, ih) 94 let f2: i64 = ns_colorful(fb, iw, ih) 95 let f3: i64 = ns_scale_spread(fb, iw, ih, buf1, buf2) 96 // filter degenerate samples (grayscale/near-flat -> not a natural COLOUR photo): colour<8 or spread<100 97 if f2 < 8 { w(" skip degenerate (grey/flat) colour=" as *u8); pn(f2); w(" spread=" as *u8); pn(f3); w("\n" as *u8) } else { 98 if f3 < 100 { w(" skip degenerate colour=" as *u8); pn(f2); w(" spread=" as *u8); pn(f3); w("\n" as *u8) } else { 99 fs[0] = fs[0] + f0; fq[0] = fq[0] + f0 * f0 100 fs[1] = fs[1] + f1; fq[1] = fq[1] + f1 * f1 101 fs[2] = fs[2] + f2; fq[2] = fq[2] + f2 * f2 102 fs[3] = fs[3] + f3; fq[3] = fq[3] + f3 * f3 103 ns_mscn_fill(fb, iw, ih, mbuf) 104 let ph: i64 = ns_mscn_pair(mbuf, iw, ih, 1, 0) // horizontal pairwise-product mean 105 let pv: i64 = ns_mscn_pair(mbuf, iw, ih, 0, 1) // vertical 106 let pd1: i64 = ns_mscn_pair(mbuf, iw, ih, 1, 1) // main diagonal 107 let pd2: i64 = ns_mscn_pair(mbuf, iw, ih, 1, 0 - 1) // secondary diagonal 108 fs[4] = fs[4] + ph; fq[4] = fq[4] + ph * ph 109 fs[5] = fs[5] + pv; fq[5] = fq[5] + pv * pv 110 fs[6] = fs[6] + pd1; fq[6] = fq[6] + pd1 * pd1 111 fs[7] = fs[7] + pd2; fq[7] = fq[7] + pd2 * pd2 112 ok = ok + 1 113 let lvl: i64 = ns_assess(fb, iw, ih, buf1, buf2, out5) 114 lsum = lsum + lvl; aisum = aisum + out5[4] 115 if lvl < lmin { lmin = lvl } 116 if lvl > lmax { lmax = lvl } 117 w(" photo " as *u8); pn(ok); w(" (" as *u8); pn(iw); w("x" as *u8); pn(ih); w("): LEVEL=" as *u8); pn(lvl); w(" rho=" as *u8); pn(f0) 118 w(" kurt=" as *u8); pn(f1); w(" colour=" as *u8); pn(f2); w(" spread=" as *u8); pn(f3); w(" ai=" as *u8); pn(out5[4]); w("\n" as *u8) 119 } } 120 } else { w(" seed decode-fail rc=" as *u8); pn(drc); w("\n" as *u8) } 121 } else { w(" seed no-SOI (not jpeg)\n" as *u8) } 122 } else { w(" seed fetch-fail http=" as *u8); pn(st[0]); w(" n=" as *u8); pn(n); w("\n" as *u8) } 123 k = k + 1 124 } 125 if ok < 3 { w("=== too few decoded (" as *u8); pn(ok); w(") -- decoder/format gap ===\n" as *u8); return 1 } 126 w("\n=== NATURAL MODEL over " as *u8); pn(ok); w(" real photos (mean +/- std) ===\n" as *u8) 127 let nm: *u8 = "rhokurtcolrspd" as *u8 128 var j: i64 = 0 129 while j < 8 { 130 let mean: i64 = fs[j] / ok 131 var vv: i64 = fq[j] / ok - mean * mean 132 if vv < 0 { vv = 0 } 133 let sd: i64 = isqrt(vv) 134 w(" feat " as *u8); pn(j); w(": mean=" as *u8); pn(mean); w(" std=" as *u8); pn(sd); w("\n" as *u8) 135 j = j + 1 136 } 137 // ★DISCRIMINATION: real-photo LEVEL distribution vs the Z-Image AI target vs our CG render 138 w("\n=== REAL vs AI-GENERATED vs CG (is-this-photoreal + is-it-AI) ===\n" as *u8) 139 w(" REAL photos (" as *u8); pn(ok); w("): overall LEVEL min=" as *u8); pn(lmin); w(" mean=" as *u8); pn(lsum / ok); w(" max=" as *u8); pn(lmax); w(" mean ai-fp axis=" as *u8); pn(aisum / ok); w("\n" as *u8) 140 let tszp: *i64 = sys_mmap(16) as *i64 141 let tj: *u8 = sys_read_file("knowledge/elara_ref.jpg" as *u8, tszp) 142 if (tj as i64) != 0 { 143 let trgb: *i64 = sys_mmap(8) as *i64 144 let ttw: *i64 = sys_mmap(8) as *i64 145 let tth: *i64 = sys_mmap(8) as *i64 146 if nx_jpeg_decode_rgb(tj, tszp[0], trgb, ttw, tth) == NX_JPEG_ASCII_OK { 147 let tiw: i64 = ttw[0] 148 let tih: i64 = tth[0] 149 let trp: *u8 = trgb[0] as *u8 150 let tfb: *i64 = sys_mmap(tiw * tih * 8) as *i64 151 var q: i64 = 0 152 while q < tiw * tih { tfb[q] = (trp[q * 3] & 0xff) + (trp[q * 3 + 1] & 0xff) * 256 + (trp[q * 3 + 2] & 0xff) * K_MAGIC_65536; q = q + 1 } 153 let tlvl: i64 = ns_assess(tfb, tiw, tih, buf1, buf2, out5) 154 w(" Z-IMAGE AI target: overall LEVEL=" as *u8); pn(tlvl); w(" ai-fp axis=" as *u8); pn(out5[4]); w(" mscn axis=" as *u8); pn(out5[1]); w("\n" as *u8) 155 } 156 } 157 w(" our CG render (clay): overall LEVEL=432 (see nx_face_vs_target)\n" as *u8) 158 w(" READING: LEVEL = HOW photoreal (real & good-AI both high; CG clay low). The MSCN/ai-fp AXES are the IS-IT-AI tells.\n" as *u8) 159 return 0 160}