WACA-UNet / tools /make_samples.py
ymin98's picture
Upload 10 files
8f5f7e0 verified
# tools/prepare_samples.py
"""
예제:
cd waca_unet_space
python tools/prepare_samples.py --root /data/ICCAD_2023/hidden-real-circuit-data \
--configs_path configs/began_iccad_fake \
--dataset iccad_hidden \
--indices 0 1 2 3 4
- build_dataset_iccad_hidden / build_dataset_iccad_real 을 μ΄μš©ν•΄μ„œ
(input, target, casename)을 κ°€μ Έμ˜¨ λ’€, input만 samples/ 폴더에 μ €μž₯.
- input은 IRDropDatasetμ—μ„œ λ°˜ν™˜ν•˜λŠ” (C,H,W) ν…μ„œ(μ •κ·œν™” μ™„λ£Œ)λ₯Ό κ·ΈλŒ€λ‘œ npy둜 μ €μž₯.
"""
import os
import argparse
import numpy as np
import torch
from config import get_config
from ir_dataset import (
build_dataset_iccad_hidden,
build_dataset_iccad_real,
)
def parse_args():
p = argparse.ArgumentParser()
p.add_argument(
"--root",
type=str,
help="ICCAD hidden/real root path. 예: /data/ICCAD_2023/hidden-real-circuit-data or /data/ICCAD_2023/real-circuit-data",
default='/data/ICCAD_2023/hidden-real-circuit-data',
)
p.add_argument(
"--dataset",
type=str,
choices=["iccad_hidden", "iccad_real"],
default="iccad_real",
)
p.add_argument(
"--img_size",
type=int,
default=384,
)
p.add_argument(
"--in_ch",
type=int,
default=25,
)
p.add_argument(
"--configs_path",
type=str,
default="/workspace/IR_Drop_prior_study/XICCAD/configs/cfirst/began_iccad_fake/stats_1um.json",
help="stats_1um.json 이 λ“€μ–΄μžˆλŠ” 폴더",
)
p.add_argument(
"--unit",
type=str,
default="1um",
)
p.add_argument(
"--indices",
type=int,
nargs="+",
default=[0, 1, 2, 3, 4],
help="예제 μƒ˜ν”Œλ‘œ 뽑을 dataset μΈλ±μŠ€λ“€",
)
p.add_argument(
"--out_dir",
type=str,
default="samples",
help="npyλ₯Ό μ €μž₯ν•  디렉토리",
)
return p.parse_args()
def main():
args = parse_args()
os.makedirs(args.out_dir, exist_ok=True)
# ν•™μŠ΅ λ•Œ μ“°λ˜ 톡계 config λ‘œλ“œ (λ‹Ήμ‹ μ˜ get_config μ‹œκ·Έλ‹ˆμ²˜μ— 맞게 μ‘°μ •)
norm_config = get_config(
args.unit,
configs_path=args.configs_path,
dataset_name="began_iccad_fake",
)
common_kwargs = dict(
img_size=args.img_size,
in_ch=args.in_ch,
train=False,
use_raw=False, # ν•™μŠ΅ λ•Œμ™€ λ™μΌν•˜κ²Œ z-score λ“±μœΌλ‘œ μ •κ·œν™”λœ μž…λ ₯ μ‚¬μš©
input_norm_type="z_score",
target_norm_type="raw",
target_layers=[], # 전체 μ’…ν•© IR-drop (이미 npy에 ν†΅ν•©λœ 경우)
use_pdn_density=True,
use_pad_distance=True,
use_comprehensive_feature=True,
norm_config=norm_config,
return_case=True, # (x, y, casename) λ°˜ν™˜
interpolation="lanczos",
)
if args.dataset == "iccad_hidden":
dataset = build_dataset_iccad_hidden(
root_path=args.root,
**common_kwargs,
)
else:
dataset = build_dataset_iccad_real(
root_path=args.root,
**common_kwargs,
)
# build_dataset_* κ°€ (train,val) νŠœν”Œμ„ λ°˜ν™˜ν•˜λŠ” 경우 λŒ€μ‘
if isinstance(dataset, (tuple, list)) and len(dataset) == 2:
dataset = dataset[1] # val set을 예제둜 μ‚¬μš©ν•˜κ±°λ‚˜, ν•„μš”μ— 따라 μˆ˜μ •
print(f"Dataset length: {len(dataset)}")
for idx in args.indices:
if idx >= len(dataset):
print(f"[WARN] index {idx} is out of range, skip.")
continue
sample = dataset[idx]
if len(sample) == 3:
x, y, casename = sample
else:
x, y = sample
casename = f"idx{idx}"
if isinstance(x, torch.Tensor):
x_np = x.detach().cpu().numpy()
else:
x_np = np.asarray(x)
out_name = f"{casename}_input.npy"
out_path = os.path.join(args.out_dir, out_name)
np.save(out_path, x_np)
print(f"Saved: {out_path} (shape={x_np.shape})")
print("Done.")
if __name__ == "__main__":
main()