File size: 3,142 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const fs = require('fs');
const path = require('path');
const {pathToFileURL} = require('url');
const {chromium} = require('playwright');

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

(async () => {
  const images = fs.readFileSync('train-images-idx3-ubyte');
  const labels = fs.readFileSync('train-labels-idx1-ubyte');
  const browser = await chromium.launch({headless: true});
  try {
    const page = await browser.newPage({viewport: {width: 1100, height: 760}});
    const errors = [];
    page.on('pageerror', error => errors.push(error.message));
    await page.goto(pathToFileURL(path.resolve('report.html')).href);
    const previews = await page.locator('figure').evaluateAll(figures => figures.map(figure => {
      const visible = element => {
        for (let node = element; node; node = node.parentElement) {
          const style = getComputedStyle(node);
          if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0) return false;
        }
        const rect = element.getBoundingClientRect();
        return rect.width > 0 && rect.height > 0;
      };
      return {
        text: figure.innerText,
        visible: visible(figure),
        pixels: [...figure.querySelectorAll('svg rect')].map(rect => {
          const box = rect.getBoundingClientRect();
          return {x: box.x, y: box.y, width: box.width, height: box.height,
            fill: getComputedStyle(rect).fill, visible: visible(rect)};
        }),
      };
    }));
    assert(previews.length >= 2, 'show at least two sample figures');
    for (const figure of previews.slice(0, 2)) {
      const match = /index\s+(\d+)\s*,\s*label\s+(\d+)/i.exec(figure.text);
      assert(match && figure.visible, 'sample index and label must be visible');
      const index = Number(match[1]);
      assert(Number(match[2]) === labels[8 + index], 'visible sample label disagrees with IDX');
      assert(figure.pixels.length === 64, 'sample needs all 64 pixels');
      const first = figure.pixels[0];
      for (const [offset, pixel] of figure.pixels.entries()) {
        const shade = 255 - images[16 + index * 64 + offset];
        assert(pixel.visible && pixel.fill === `rgb(${shade}, ${shade}, ${shade})`, 'actual rendered sample color disagrees with IDX');
        const near = (actual, expected) => Math.abs(actual - expected) <= 0.75;
        assert(near(pixel.width, first.width) && near(pixel.height, first.height)
          && near(pixel.x, first.x + offset % 8 * first.width)
          && near(pixel.y, first.y + Math.floor(offset / 8) * first.height),
        'sample pixels must form the claimed row-major 8x8 image without overlaps or gaps');
      }
    }
    assert(errors.length === 0, 'browser errors: ' + errors.join(' | '));
    fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
    await page.screenshot({path: '/logs/verifier/visual-audit/tiny-mlp-report.png', fullPage: true});
  } finally {
    await browser.close();
  }
  console.log('Rendered IDX sample images and report passed');
})().catch(error => {console.error(error); process.exit(1);});