File size: 4,172 Bytes
8f5f7e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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()