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()
|