| import time
|
|
|
| import timm
|
| import torch
|
| from datasets import load_dataset
|
| from torch.utils.data import DataLoader
|
|
|
|
|
|
|
| MODEL_NAME = "convnextv2_nano.fcmae_ft_in1k"
|
| BATCH_SIZE = 64
|
| NUM_WORKERS = 8
|
| DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
| _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
|
|
|
|
|
|
|
| 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}")
|
|
|
|
|
|
|
| model = timm.create_model(MODEL_NAME, pretrained=True)
|
| model = model.half()
|
| 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")
|
|
|
|
|
| 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
|
|
|
|
|
| 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()
|
|
|
|
|
| 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()
|
|
|