our_knp/cats-dogs/prepare_baseline_v2.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

19 lines
1 KiB
Python

"""Prepare the original model's exact train prefix plus the new held-out test."""
from pathlib import Path
import numpy as np
root = Path('/home/int/run/cats-dogs')
raw = np.load(root / 'data-v2/test-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
encoded = np.concatenate([np.clip(contrast, 0, 255).reshape(-1, 1024),
np.clip(-contrast, 0, 255).reshape(-1, 1024)], axis=1).astype(np.uint8)
with (root / 'features-v2/baseline_test.bin').open('wb') as out:
out.write((root / 'data/contrast_train.bin').read_bytes())
out.write(encoded.tobytes())
y = np.concatenate([np.loadtxt(root / 'data/train.target', dtype=int), raw['labels']])
np.savetxt(root / 'features-v2/baseline_test.target', y, fmt='%d')
print('Original train prefix and fresh test encoded', flush=True)