23 lines
1.1 KiB
Python
23 lines
1.1 KiB
Python
"""Export vehicles data for altai_objects: train+val images (uint8 3×32×32, train first), labels, random 12×12 crops.
|
||
|
||
usage: ~/anvenv/bin/python export_knp.py [--crops 200000] → /home/int/run/cifar/vehicles/knp_{images.bin,labels.txt,crops.bin}
|
||
"""
|
||
import argparse
|
||
|
||
import numpy as np
|
||
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--crops", type=int, default=200000)
|
||
a = ap.parse_args()
|
||
d = "/home/int/run/cifar/vehicles"
|
||
tr, va = np.load(f"{d}/train.npz"), np.load(f"{d}/val.npz")
|
||
xt, xv = tr["rgb"], va["rgb"] # load once: indexing an npz entry re-reads the whole array
|
||
x = np.concatenate([xt, xv]).astype(np.uint8)
|
||
y = np.concatenate([tr["labels"], va["labels"]])
|
||
x.tofile(f"{d}/knp_images.bin")
|
||
open(f"{d}/knp_labels.txt", "w").write("\n".join(map(str, y)) + "\n")
|
||
rng = np.random.default_rng(7)
|
||
i, oy, ox = rng.integers(0, len(xt), a.crops), rng.integers(0, 21, a.crops), rng.integers(0, 21, a.crops)
|
||
crops = np.stack([xt[j, :, b:b + 12, c:c + 12] for j, b, c in zip(i, oy, ox)]).astype(np.uint8)
|
||
crops.tofile(f"{d}/knp_crops.bin")
|
||
print("images", x.shape, "crops", crops.shape)
|