| |
| """Prepare the Marmousi-derived 256 x 256 training NPZ for SAII-CLDM.""" |
|
|
| import argparse |
| from datetime import datetime |
| from pathlib import Path |
|
|
| import numpy as np |
| import scipy.io |
|
|
|
|
| def build_patch_indices(shape, size=256, interval=300, border=5): |
| height, width = shape |
| indices = set() |
|
|
| for offset in (border, 80, 160, 230): |
| for row in range(offset, height - size, interval): |
| for col in range(offset, width - size, interval): |
| indices.add((row, col)) |
|
|
| for row in range(height - size - border, 0, -interval): |
| for col in range(width - size - border, 0, -interval): |
| indices.add((row, col)) |
|
|
| return sorted(indices) |
|
|
|
|
| def elastic_patches(data, indices, size=256, border=5, seed=1234): |
| try: |
| import imgaug as ia |
| from imgaug import augmenters as iaa |
| except ImportError as exc: |
| raise SystemExit( |
| "imgaug is required for elastic deformation. Install it with `pip install imgaug`." |
| ) from exc |
|
|
| if seed is not None: |
| ia.seed(seed) |
| np.random.seed(seed) |
|
|
| seq = iaa.Sequential( |
| [iaa.ElasticTransformation(alpha=(30, 40), sigma=10)], |
| random_order=True, |
| ) |
|
|
| patches = [] |
| for row, col in indices: |
| padded = data[row - border : row + size + border, col - border : col + size + border] |
| patches.append(seq.augment_image(padded)[border : border + size, border : border + size]) |
| return patches |
|
|
|
|
| def prepare(input_mat, output_npz, key="A", size=256, seed=1234, dry_run=False): |
| mat = scipy.io.loadmat(input_mat) |
| if key not in mat: |
| keys = ", ".join(k for k in mat if not k.startswith("__")) |
| raise KeyError(f"Key {key!r} was not found in {input_mat}. Available keys: {keys}") |
|
|
| marmousi = np.log(mat[key][500:][::4]) |
| indices = build_patch_indices(marmousi.shape, size=size) |
| originals = [marmousi[row : row + size, col : col + size].reshape(size, size) for row, col in indices] |
|
|
| if dry_run: |
| print(f"input_shape={mat[key].shape}") |
| print(f"working_shape={marmousi.shape}") |
| print(f"base_patches={len(originals)}") |
| print(f"output_shape=({len(originals) * 5}, {size}, {size})") |
| return |
|
|
| left_right = [np.flip(patch, axis=1) for patch in originals] |
| up_down = [np.flip(patch, axis=0) for patch in originals] |
| both = [np.flip(patch, axis=0) for patch in left_right] |
| elastic = elastic_patches(marmousi, indices, size=size, seed=seed) |
|
|
| data = np.stack(originals + left_right + up_down + both + elastic, axis=0) |
| description = ( |
| "Derived from the official Marmousi model; contains 256 x 256 log-velocity " |
| "patches with left-right flip, up-down flip, both-direction flip, and elastic " |
| "deformation augmentations." |
| ) |
|
|
| output_npz = Path(output_npz) |
| output_npz.parent.mkdir(parents=True, exist_ok=True) |
| np.savez( |
| output_npz, |
| data=data, |
| creation_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
| description=description, |
| ) |
| print(f"saved {output_npz} with data shape {data.shape}") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description=__doc__) |
| parser.add_argument("--input-mat", required=True, help="Official Marmousi MATLAB file.") |
| parser.add_argument("--output", default="marmousi_256_train.npz", help="Output NPZ path.") |
| parser.add_argument("--key", default="A", help="MATLAB matrix key to read.") |
| parser.add_argument("--size", type=int, default=256, help="Patch size.") |
| parser.add_argument("--seed", type=int, default=1234, help="Random seed for elastic deformation.") |
| parser.add_argument("--dry-run", action="store_true", help="Print the expected output shape without writing the NPZ.") |
| args = parser.parse_args() |
| prepare(args.input_mat, args.output, key=args.key, size=args.size, seed=args.seed, dry_run=args.dry_run) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|