-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_memory.py
More file actions
133 lines (110 loc) · 4.39 KB
/
Copy patheval_memory.py
File metadata and controls
133 lines (110 loc) · 4.39 KB
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
# Third-party imports
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import v2 as transforms
from transformers import MobileViTV2ForImageClassification, MobileViTV2Config
from timm import create_model
from GMR_Conv import gmr_resnet18
# Local imports
from model_hybrid import HybridCNNViT
from SingleModels.e2cnn_model import E2WRN16_8_Star_D8D4D1_Cls
from utils import load_config
from cli_args import get_opt
args = get_opt()
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
cfg = load_config("config/train.yaml")
IMG_SIZE = args.img_size
transform = transforms.Compose([
transforms.ToImage(),
transforms.Resize(IMG_SIZE),
transforms.ToDtype(torch.float32, scale=True),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010]),
])
test_data = datasets.CIFAR10(
root=cfg["SAVE_PATH"]["DATA"],
train=False,
download=True,
transform=transform
)
test_loader = DataLoader(test_data, batch_size=cfg["BATCH_SIZE"]["TEST"])
def build_model(model_name, ckpt):
if model_name == "gmr":
model = gmr_resnet18(
num_classes=cfg["NUM_CLASSES"],
inplanes=64,
in_channels=3,
gmr_conv_size=[9, 9, 5, 5],
num_rings=None,
)
# for pcam
model.conv1 = nn.Conv2d(
3,
64,
kernel_size=5,
stride=1,
padding=2,
bias=False,
)
model = model.to(device)
# model.load_state_dict(torch.load("models/gmr.pt", weights_only=True))
checkpoint = torch.load("models/gmr_pcam_no_rot.ckpt", map_location=device)
model.load_state_dict(checkpoint["model"])
return model.eval()
if model_name == "e2cnn":
model = E2WRN16_8_Star_D8D4D1_Cls(
num_classes=cfg["NUM_CLASSES"],
).to(device)
# model.load_state_dict(torch.load("models/e2cnn.pt", weights_only=True))
checkpoint = torch.load("models/e2cnn_pcam.ckpt", map_location=device)
model.load_state_dict(checkpoint["model"])
return model.eval()
if model_name == "mobile":
hf_model = "models/mobilevitv2-1.0"
config = MobileViTV2Config.from_pretrained(hf_model, local_files_only=True)
config.num_labels = cfg["NUM_CLASSES"]
model = MobileViTV2ForImageClassification(config).to(device)
model.load_state_dict(torch.load("models/mobile.pt", weights_only=True))
return model.eval()
if model_name == "timm":
ckpt = "models/efficientvit_m2_pcam_no_rot.pt" #"models/shvits1.pt" #"models/efficientvit_m2.pt"
timm_model = "efficientvit_m2" #"shvit_s1" #"efficientvit_m2"
model = create_model(timm_model, pretrained=False, num_classes=cfg["NUM_CLASSES"]).to(device)
model.load_state_dict(torch.load(ckpt, weights_only=True))
return model.eval()
if model_name == "hybrid":
model = HybridCNNViT(num_classes=cfg["NUM_CLASSES"]).to(device)
model.load_state_dict(
torch.load(ckpt, weights_only=True)) # cfg["SAVE_PATH"]["MODEL"]
# checkpoint = torch.load(cfg["SAVE_PATH"]["CHECKPOINTS"], map_location=device)
# model.load_state_dict(checkpoint["model"])
return model.eval()
raise ValueError(f"Unknown model: {model_name}")
torch.manual_seed(cfg["SEED"])
model = build_model(args.model, args.ckpt)
dummy = torch.randn(cfg["BATCH_SIZE"]["TEST"], 3, IMG_SIZE, IMG_SIZE).to(device)
warmup, iters = 30, 200
with torch.inference_mode():
# Warmup: trigger kernel selection / lazy init
for _ in range(warmup):
_ = model(dummy) #.logits
torch.cuda.synchronize()
# Start measurement window
torch.cuda.reset_peak_memory_stats(device)
for _ in range(iters):
_ = model(dummy) #.logits
torch.cuda.synchronize()
peak_alloc_mib = torch.cuda.max_memory_allocated(device) / (1024 ** 2)
peak_resv_mib = torch.cuda.max_memory_reserved(device) / (1024 ** 2)
#print(f"Peak allocated (MB): {peak_alloc_mib:.2f} MiB")
print(f"Peak reserved (MB): {peak_resv_mib:.2f} MiB")
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Total parameters:", total_params)
print("Trainable parameters:", trainable_params)