forked from sm1lla/ClassAwarePruning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
272 lines (231 loc) · 9.57 KB
/
metrics.py
File metadata and controls
272 lines (231 loc) · 9.57 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import os
import time
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.profiler import profile, record_function, ProfilerActivity
from datetime import datetime
from statistics import mean
import onnxruntime as ort
import numpy as np
import io
def calculate_model_accuracy(
model: nn.Module,
device: str,
test_loader: DataLoader,
print_results: bool = True,
all_classes: bool = False,
num_classes: int = 10,
selected_classes: list = None,
):
"""
Evaluates the model's accuracy.
Args:
model (nn.Module): The model to use.
device (torch.device): Device to run on.
test_loader (DataLoader): The dataloader to use.
print_results (boolean): Print results to console.
all_classes (boolean): If false, only compute and return the overall accuracy and ignore per-class info.
selected_classes (List[int]): List of selected classes to prune for.
Returns:
Tuple(float, List[float]): The overall accuracy and the accuracies per class
"""
model.eval()
correct = 0
total = 0
if selected_classes:
# When the last layer is replaced to only predict the selected classes we
# need to map the model's output to the correct classes
selected_classes = torch.tensor(selected_classes).to(device)
num_classes = len(selected_classes)
class_correct = [0] * num_classes
class_total = [0] * num_classes
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
c = (predicted == labels).squeeze()
for i in range(labels.size(0)):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
accuracy = 100 * correct / total
if print_results:
print(f"%%%%% Accuracy of the model on the test set: {accuracy:.2f}%")
class_accuracies = {}
if all_classes and print_results:
for i in range(num_classes):
if class_total[i] > 0:
accuracy_i = 100 * class_correct[i] / class_total[i]
class_accuracies[i] = accuracy_i
print(f"%%%%%% Accuracy of class {i}: {accuracy_i:.2f}%")
return accuracy, class_accuracies
def calculate_accuracy_for_selected_classes(class_accuracies, selected_classes):
"""Calculate accuracy for selected classes."""
# Classes get mapped, hence [2,4,6] would become [0,1,2]
accuracies = [class_accuracies[i] for i in range(len(selected_classes))]
accuracy = sum(accuracies) / len(selected_classes)
return accuracy
def get_parameter_ratio(model: nn.Module, pruned_model: nn.Module):
"""Calculate the ratio of parameters in the pruned model to the original model."""
original_params = sum(p.numel() for p in model.parameters())
pruned_params = sum(p.numel() for p in pruned_model.parameters())
return pruned_params / original_params if original_params > 0 else 0
def get_model_size(model):
"""Get the size of the model in MB."""
name = datetime.now().strftime("%m:%d:%Y_%H:%M:%S")
torch.save(model.state_dict(), name + ".pt")
size = os.path.getsize(name + ".pt") / 1e6
os.remove(name + ".pt")
return size
def export_model_to_onnx(model: nn.Module, input_shape: tuple, device: str):
"""Export the model to ONNX format."""
dummy_input = torch.randn(1, *input_shape).to(device)
f = io.BytesIO()
torch.onnx.export(
model,
dummy_input,
f,
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names=["input"],
output_names=["output"],
dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}}
)
onnx_model = f.getvalue()
return onnx_model
def measure_inference_time_and_accuracy(
data_loader: DataLoader,
model: nn.Module,
device: str,
batch_size: int,
num_classes: int,
all_classes: bool,
print_results: bool,
selected_classes=None,
with_onnx=False,
mapping=None
):
"""
Measures the accuracy and the batch inference time of a model.
Args:
data_loader (DataLoader): The dataloader to use.
model (nn.Module): The model to use.
device (torch.device): Device to run on.
batch_size (int): The batch size to use for inference.
num_classes (int): The number of classes in the dataset.
all_classes (boolean): If false, only compute and return the overall accuracy and ignore per-class info.
print_results (boolean): Print results to console.
selected_classes (List[int]): List of selected classes to prune for.
with_onnx (boolean): Whether to use ONNX.
mapping (dict): Mapping for the indices on a subset.
Returns:
Tuple(float, List[float], float, List[float]): The overall accuracy
and the accuracies per class + The mean time for inference and individual times per batch
"""
model.eval()
correct = 0
total = 0
# Handle selected_classes conversion
if selected_classes:
selected_classes = torch.tensor(selected_classes).to(device)
num_classes = len(selected_classes)
# Create mapping tensor once before loop
mapping_tensor = None
if mapping is not None:
max_idx = max(mapping.keys()) + 1
mapping_tensor = torch.zeros(max_idx, dtype=torch.long, device=device)
for new_idx, orig_class in mapping.items():
mapping_tensor[new_idx] = orig_class
class_correct = [0] * num_classes
class_total = [0] * num_classes
times = []
C, W, H = data_loader.dataset.__getitem__(0)[0].shape
# Setup model function (ONNX or PyTorch)
if with_onnx:
model = export_model_to_onnx(model, (C, W, H), device)
session_options = ort.SessionOptions()
session_options.intra_op_num_threads = 1
session_options.inter_op_num_threads = 1
session = ort.InferenceSession(
model,
providers=["CUDAExecutionProvider"] if device.type == "cuda" else ["CPUExecutionProvider"],
sess_options=session_options
)
model_func = lambda x: session.run(None, {"input": x.cpu().numpy()})[0]
else:
model.to(device)
model_func = lambda x: model(x)
# Warmup
torch.cuda.empty_cache() if device.type == "cuda" else None
warmup_data = torch.randn(batch_size, C, H, W).to(device)
with torch.no_grad():
for _ in range(10):
_ = model_func(warmup_data)
# Main evaluation loop
for input, labels in data_loader:
input, labels = input.to(device), labels.to(device)
# Time inference
if device.type == "mps":
start_time = time.time()
torch.mps.synchronize()
output = model_func(input)
torch.mps.synchronize()
end_time = time.time()
times.append((end_time - start_time) * 1000) # Convert to ms
else:
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
acc_events=True
) as prof:
with record_function("model_inference"):
torch.cuda.synchronize() if device.type == "cuda" else None
output = model_func(input)
torch.cuda.synchronize() if device.type == "cuda" else None
for event in prof.key_averages():
if event.key == "model_inference":
if device.type == "cuda":
times.append(event.device_time_total / 1000) # Convert to ms
else:
times.append(event.cpu_time_total / 1000) # Convert to ms
# Get predictions
_, predicted = torch.max(output.data, 1)
# Apply selected_classes mapping if needed
if selected_classes is not None:
predicted = selected_classes[predicted]
# Map labels from [0,1,2] to original classes [4,6,8] if needed
if mapping_tensor is not None:
labels_mapped = mapping_tensor[labels]
else:
labels_mapped = labels
# Calculate accuracy
total += labels.size(0)
correct += (predicted == labels_mapped).sum().item()
c = (predicted == labels_mapped).squeeze()
# Per-class accuracy tracking
for i in range(labels.size(0)):
label = labels[i].item()
class_correct[label] += c[i].item()
class_total[label] += 1
# Calculate final metrics
accuracy = 100 * correct / total if total > 0 else 0
class_accuracies = {}
if all_classes and print_results:
for i in range(num_classes):
if class_total[i] > 0:
accuracy_i = 100 * class_correct[i] / class_total[i]
class_accuracies[i] = accuracy_i
print(f"%%%%% Accuracy of class {mapping[i] if mapping is not None else i}: {accuracy_i:.2f}%")
inference_time = mean(times) if times else 0
return accuracy, class_accuracies, inference_time, times
def measure_execution_time(selector, model):
"""Measures execution time a pruning method needs for selecting the indices."""
start = time.perf_counter()
indices = selector.select(model)
elapsed_time = time.perf_counter() - start
print("%%%%% Execution time:", elapsed_time)
return indices, elapsed_time