our_knp/cats-dogs/prepare_contrast.py
int 8f43927575 Initial import: KNP experiments (examples, viz, cats-dogs, reports)
examples/ is based on KasperskyLab/knp at 55470ed6 with local changes
to the mnist-learn altai/blifat models, cuda-backend CMake and model_loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 18:17:15 +03:00

30 lines
1.7 KiB
Python

"""Fixed local-contrast ON/OFF encoding; no learned feature extractor."""
import hashlib
import json
from pathlib import Path
import numpy as np
root = Path('/home/int/run/cats-dogs/data')
arrays, labels = {}, {}
for split in ('train', 'val', 'test'):
raw = np.load(root / f'{split}-original.npz')
rgb = raw['rgb'].reshape(-1, 3, 32, 32).astype(np.float32)
gray = (299 * rgb[:, 0] + 587 * rgb[:, 1] + 114 * rgb[:, 2]) / 1000
padded = np.pad(gray, ((0, 0), (2, 2), (2, 2)), mode='edge')
blur = sum(padded[:, y:y+32, x:x+32] for y in range(5) for x in range(5)) / 25
contrast = (gray - blur) * 4
arrays[split] = np.concatenate([np.clip(contrast, 0, 255).reshape(-1, 1024),
np.clip(-contrast, 0, 255).reshape(-1, 1024)], axis=1).astype(np.uint8)
labels[split] = raw['labels']
arrays[split].tofile(root / f'contrast_{split}.bin')
np.savetxt(root / f'contrast_{split}.target', labels[split], fmt='%d')
for name, second in [('validation', 'val'), ('train_test', 'test')]:
np.concatenate([arrays['train'], arrays[second]]).tofile(root / f'contrast_{name}.bin')
np.savetxt(root / f'contrast_{name}.target', np.concatenate([labels['train'], labels[second]]), fmt='%d')
manifest = json.loads((root / 'manifest.json').read_text())
manifest['encoding'] = 'grayscale BT.601 float32; subtract 5x5 box blur (edge padding); gain 4; ON/OFF positive and negative parts, clip uint8'
manifest['encoded_sha256'] = {s: hashlib.sha256((root / f'contrast_{s}.bin').read_bytes()).hexdigest()
for s in arrays}
(root / 'contrast-manifest.json').write_text(json.dumps(manifest, indent=2) + '\n')
print('Contrast data ready', flush=True)