WACA-UNet / tools /make_samples.py
ymin98's picture
Upload 10 files
8f5f7e0 verified
Raw History Blame Contribute Delete
4.17 kB
# 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()