File size: 4,068 Bytes
76ec265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time

import timm
import torch
from datasets import load_dataset
from torch.utils.data import DataLoader

# 1. ํ™˜๊ฒฝ ๋ฐ ์„ค์ • (์ „์—ญ ๋ณ€์ˆ˜)

MODEL_NAME = "convnextv2_nano.fcmae_ft_in1k"
BATCH_SIZE = 64
NUM_WORKERS = 8
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")


# ๋ชจ๋ธ ๊ตฌ์กฐ๋งŒ ๋น ๋ฅด๊ฒŒ ๋ถˆ๋Ÿฌ์™€์„œ ์ „์ฒ˜๋ฆฌ ๊ทœ๊ฒฉ(Transform) ์ถ”์ถœ
_dummy_model = timm.create_model(MODEL_NAME, pretrained=False)
data_config = timm.data.resolve_model_data_config(_dummy_model)
transform = timm.data.create_transform(**data_config, is_training=False)
del _dummy_model  # ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ์„ ์œ„ํ•ด ๋”๋ฏธ ๋ชจ๋ธ ์‚ญ์ œ


# main() ํ•จ์ˆ˜ ๋ฐ”๊นฅ์œผ๋กœ ๋นผ๋‚ด์–ด Windows ์›Œ์ปค๋“ค์ด ์ •์ƒ์ ์œผ๋กœ ๋ณต์‚ฌ(Pickle)ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
def collate_fn(examples):
    images = [transform(example["image"].convert("RGB")) for example in examples]
    labels = [example["label"] for example in examples]
    return torch.stack(images), torch.tensor(labels)


def main():
    print(f"๐Ÿš€ [Phase 1.1] Baseline ํ‰๊ฐ€ ์‹œ์ž‘: {MODEL_NAME}")
    print(f"๐Ÿ–ฅ๏ธ  Target Device: {DEVICE}")

    # 2. ๋ชจ๋ธ ๋กœ๋“œ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ(M) ์ธก์ •

    model = timm.create_model(MODEL_NAME, pretrained=True)
    model = model.half()  # FP16(Half Precision)์œผ๋กœ ๋ณ€ํ™˜
    model = model.to(DEVICE)
    model.eval()

    param_size = 0
    for param in model.parameters():
        param_size += param.nelement() * param.element_size()
    buffer_size = 0
    for buffer in model.buffers():
        buffer_size += buffer.nelement() * buffer.element_size()

    size_all_mb = (param_size + buffer_size) / 1024**2
    print(f"๐Ÿ“Š [์ง€ํ‘œ 1] ๋ชจ๋ธ ๋ฉ”๋ชจ๋ฆฌ(M): {size_all_mb:.2f} MB")

    # 3. ๋ฐ์ดํ„ฐ์…‹ ์ค€๋น„ (๋‹ค์šด๋กœ๋“œ๋œ ๋กœ์ปฌ ์บ์‹œ ์‚ฌ์šฉ)
    print("Hugging Face์—์„œ ImageNet-1K Validation ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ ์ค‘...")

    try:
        hf_val_dataset = load_dataset("ILSVRC/imagenet-1k", split="validation")

        val_loader = DataLoader(
            hf_val_dataset,
            batch_size=BATCH_SIZE,
            shuffle=False,
            num_workers=NUM_WORKERS,
            pin_memory=True,
            collate_fn=collate_fn,
        )
        print(f"๐Ÿ“ ๋ฐ์ดํ„ฐ์…‹ ์ค€๋น„ ์™„๋ฃŒ: ์ด {len(hf_val_dataset)}์žฅ")

    except Exception as e:
        print(f"โš ๏ธ ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ ์‹คํŒจ: {e}")
        return

    # 4. ์„ฑ๋Šฅ(P) ๋ฐ ์†๋„(S) ํ‰๊ฐ€ (Inference Loop)
    correct_top1 = 0
    total_samples = 0

    print("๐Ÿ”ฅ GPU ์›œ์—… ์ง„ํ–‰ ์ค‘...")
    dummy_input = torch.randn(BATCH_SIZE, 3, 224, 224, dtype=torch.float16, device=DEVICE)
    with torch.no_grad():
        for _ in range(10):
            _ = model(dummy_input)
    torch.cuda.synchronize()

    print("๐Ÿƒโ€โ™‚๏ธ ๋ณธ๊ฒฉ์ ์ธ ํ‰๊ฐ€ ์‹œ์ž‘...")
    start_time = time.time()

    with torch.no_grad():
        for images, labels in val_loader:
            images = images.to(DEVICE, dtype=torch.float16)
            labels = labels.to(DEVICE)

            outputs = model(images)

            _, predicted = outputs.max(1)
            total_samples += labels.size(0)
            correct_top1 += predicted.eq(labels).sum().item()

            # ์ง„ํ–‰ ์ƒํ™ฉ ๋ชจ๋‹ˆํ„ฐ๋ง (๋ฐฐ์น˜ 100๋ฒˆ๋งˆ๋‹ค ์ถœ๋ ฅ)
            if (total_samples // BATCH_SIZE) % 100 == 0:
                print(f"   ... ์ง„ํ–‰ ์ค‘: {total_samples}์žฅ ์ฒ˜๋ฆฌ ์™„๋ฃŒ")

    torch.cuda.synchronize()
    end_time = time.time()

    # ๊ฒฐ๊ณผ ๊ณ„์‚ฐ
    total_time = end_time - start_time
    fps = total_samples / total_time
    top1_acc = (correct_top1 / total_samples) * 100

    print("\n" + "=" * 50)
    print("[Phase 1.1 Baseline ๊ฒฐ๊ณผ ๋ฆฌํฌํŠธ]")
    print("=" * 50)
    print(f"์„ฑ๋Šฅ(P) - Top-1 Accuracy: {top1_acc:.2f} %")
    print(f"์†๋„(S) - Throughput: {fps:.2f} FPS")
    print(f"๋ฉ”๋ชจ๋ฆฌ(M) - Model Size: {size_all_mb:.2f} MB")
    print("=" * 50)


if __name__ == "__main__":
    import multiprocessing

    multiprocessing.freeze_support()
    main()