File size: 1,898 Bytes
6e33b3b | 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 | """One-off data preparation / audit for MTLCC (run after the download finishes).
- validity cache for pretrain / test tile lists (exists on disk, >=30 dates)
- per-band mean/std (cache/band_stats.json) from 600 train_fold0 tiles
- label-class audit of the hand-picked fine-tune sets and the test list
"""
import json, random, sys
import numpy as np
import config as C
from dataset import (pretrain_patch_ids, test_patch_ids, train_patch_ids, val_patch_ids,
MTLCCTileDataset, read_tileids, tile_path)
from tfrecord_io import load_tile
out = C.EXP_ROOT / "cache"; out.mkdir(parents=True, exist_ok=True)
pre = pretrain_patch_ids(); test = test_patch_ids()
print("pretrain tiles", len(pre), "of", len(read_tileids(C.PRETRAIN_TILEIDS)), "| test tiles", len(test), "of", len(read_tileids(C.TEST_TILEIDS)))
rng = random.Random(0); pick = rng.sample(pre, min(600, len(pre)))
s = np.zeros(C.N_CHANNELS); q = np.zeros(C.N_CHANNELS); n = 0
for tid in pick:
x, _ = MTLCCTileDataset([tid], load_target=False, train=False)[0][0], None
x = x[0].numpy().astype(np.float64) # (T,C,H,W)
s += x.sum((0, 2, 3)); q += (x ** 2).sum((0, 2, 3)); n += x.shape[0] * x.shape[2] * x.shape[3]
mean = s / n; std = np.sqrt(q / n - mean ** 2)
(out / "band_stats.json").write_text(json.dumps({"mean": mean.tolist(), "std": std.tolist(), "tiles": len(pick)}))
print("band mean", np.round(mean, 4)); print("band std ", np.round(std, 4))
audit = {}
for name, ids in (("train78", train_patch_ids()), ("val78", val_patch_ids())):
h = np.zeros(C.N_CLASSES, np.int64)
for tid in ids:
_, y = MTLCCTileDataset([tid], train=False)[0]; h += np.bincount(y.numpy().ravel(), minlength=C.N_CLASSES)
audit[name] = h.tolist(); print(name, "classes present", [i for i, v in enumerate(h) if v], "px", int(h.sum()))
(out / "label_audit.json").write_text(json.dumps(audit))
|