forked from sm1lla/ClassAwarePruning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
337 lines (293 loc) · 14.6 KB
/
main.py
File metadata and controls
337 lines (293 loc) · 14.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import time
import torch
from torch import nn
import hydra
from omegaconf import DictConfig, OmegaConf, ListConfig
from data_loader import dataloaderFactories
from pruner import DepGraphPruner, UnstructuredMagnitudePruner
from selection import get_selector
from models import get_model
import wandb
from fvcore.nn import FlopCountAnalysis
from distillation import KnowledgeDistillation
from metrics import (
get_parameter_ratio,
get_model_size,
)
from helpers import (
train,
filter_pruning_indices_for_resnet,
get_unstructured_sparsity,
run_pruner,
evaluate
)
@hydra.main(config_path="config", config_name="config", version_base=None)
def main(cfg: DictConfig):
# Hardware initialization
device = torch.device(
"cuda" if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available()
else "cpu"
)
if cfg.device:
device = torch.device(cfg.device)
print(f"%%%%%% Using device: {device}")
# WandB Setup
if cfg.log_results:
wandb_cfg = OmegaConf.to_container(cfg, resolve=True)
wandb_cfg["device"] = device
wandb.init(
project="ClassAwarePruning",
entity="sjoze",
config=wandb_cfg,
name=cfg.run_name,
)
print("%%%%%% Initialized WandB")
# Force pruning_ratio to always be a list (Hydra turns single item lists to floats)
if not isinstance(cfg.pruning.pruning_ratio, (list, ListConfig)):
cfg.pruning.pruning_ratio = [cfg.pruning.pruning_ratio]
###################################
# --------- DATALOADERS --------- #
###################################
dataloader_factory = dataloaderFactories[cfg.dataset.name](
train_batch_size=cfg.training.batch_size_train,
test_batch_size=cfg.training.batch_size_test,
selected_classes=cfg.selected_classes,
num_pruning_samples=cfg.num_pruning_samples,
use_data_augmentation=cfg.training.use_data_augmentation,
use_imagenet_labels=cfg.dataset.use_imagenet_labels if "use_imagenet_labels" in cfg.dataset else False,
subsample_ratio=cfg.dataset.subsample_ratio if "subsample_ratio" in cfg.dataset else None,
subsample_size_per_class=cfg.dataset.subsample_size_per_class if "subsample_size_per_class" in cfg.dataset else None,
)
train_loader, val_loader, test_loader = dataloader_factory.get_dataloaders()
print(f"\n%%%%%% {"=" * 80}")
print("%%%%%% DATALOADER INFO")
print(f"%%%%%% {"=" * 80}")
print(f"%%%%%% Train loader length: {len(train_loader)}")
print(f"%%%%%% Train dataset size: {len(train_loader.dataset)}")
print(f"%%%%%% Train batch size: {train_loader.batch_size}")
print(f"%%%%%% Val loader length: {len(val_loader)}")
print(f"%%%%%% Val dataset size: {len(val_loader.dataset)}")
print(f"%%%%%% Test loader length: {len(test_loader)}")
print(f"%%%%%% Test dataset size: {len(test_loader.dataset)}")
print(f"%%%%%% {"=" * 80}\n")
# Subset dataloader creation
subset_data_loader_train, subset_data_loader_val, subset_data_loader_test, pruning_dataloader = dataloader_factory.get_subset_dataloaders()
print(f"\n%%%%%% {"=" * 80}")
print("%%%%%% SUBSET DATALOADER INFO")
print(f"%%%%%% {"=" * 80}")
print(f"%%%%%% Subset train loader length: {len(subset_data_loader_train)}")
print(f"%%%%%% Subset train dataset size: {len(subset_data_loader_train.dataset)}")
print(f"%%%%%% Subset val dataset size: {len(subset_data_loader_val.dataset)}")
print(f"%%%%%% Subset test dataset size: {len(subset_data_loader_test.dataset)}")
print(f"%%%%%% {"=" * 80}\n")
model = get_model(
cfg.model.name, pretrained=cfg.training.use_pretrained_model, num_classes=cfg.dataset.num_classes,
dataset_name=cfg.dataset.name
)
model.to(device)
print(f"%%%%%% GPU memory after loading base model: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
print(f"%%%%%% Loaded Model - Pretrained: {cfg.training.use_pretrained_model}, "
f"Number of classes: {cfg.dataset.num_classes}, Dataset: {cfg.dataset.name}")
# Train the model or load pretrained weights
if cfg.training.train and not cfg.training.use_pretrained_model:
print("%%%%%% Training Model")
train(cfg, model, train_loader, test_loader, device)
print("%%%%%% Done Training")
elif cfg.training.use_pretrained_model and cfg.dataset.name == "imagenet":
print("%%%%%% Using torchvision ImageNet pretrained weights")
elif cfg.model.pretrained_weights_path:
print(f"%%%%%% Loading custom weights from {cfg.model.pretrained_weights_path}")
model.load_state_dict(torch.load(cfg.model.pretrained_weights_path, weights_only=True, map_location=device))
print("%%%%%% Loaded custom weights")
else:
print("%%%%%% Using randomly initialized model")
###################################
# ------- FILTER SELECTION ------ #
###################################
selection_time, removal_time = 0, 0
# Mapping indices (e.g. [105, 305, 402] to the new output nodes [0, 1, 2])
mapping = {new_idx: orig_class for new_idx, orig_class in enumerate(cfg.selected_classes)}
print(f"%%%%%% Mapping of classes: {mapping}")
# Specifically for OCAP where we need less pruning samples (~25 per class according to authors)
if pruning_dataloader is not None:
print(f"%%%%%% Smaller training dataloader in use - Originally: {len(subset_data_loader_train.dataset)}, "
f"Now using: {len(pruning_dataloader.dataset)}")
# ----- STRUCTURED FILTER SELECTION -----
if not cfg.pruning.name.startswith("unstructured_"):
selector = get_selector(
selector_config=cfg.pruning,
data_loader=pruning_dataloader if pruning_dataloader is not None else subset_data_loader_train,
device=device,
skip_first_layers=cfg.model.skip_first_layers
)
print(f"%%%%%% Loaded selector: {selector}")
start = time.perf_counter()
all_indices = selector.select(model)
selection_time = time.perf_counter() - start
print(f"%%%%%% Time spent on selecting indices: {selection_time}")
print(f"%%%%%% Global pruning ratio: {selector.global_pruning_ratio}")
if cfg.model.name.startswith("resnet"):
all_indices = filter_pruning_indices_for_resnet(all_indices, cfg.model.name)
for num, indices in enumerate(all_indices):
print(f"%%%%%% Pruning ratio number {num}: {cfg.pruning.pruning_ratio[num]}")
pruner = DepGraphPruner(
model=model,
indices=indices,
replace_last_layer=cfg.replace_last_layer,
selected_classes=cfg.selected_classes,
device=device,
)
# Filter Removal
pruned_model, removal_time = run_pruner(pruner, cfg.pruning.pruning_ratio[num])
# ----- UNSTRUCTURED FILTER SELECTION -----
else:
# Dont need to make a distinction between selection and pruning, since we don't need to remove filters.
# We can simply set to zero.
pruner = UnstructuredMagnitudePruner(
model=model,
sparsity=cfg.pruning.pruning_ratio[0],
replace_last_layer=cfg.replace_last_layer,
selected_classes=cfg.selected_classes,
device=device
)
# Filter Removal
pruned_model, removal_time = run_pruner(pruner, cfg.pruning.pruning_ratio[0])
pruning_time = selection_time + removal_time
print(f"%%%%%% Time spent on pruning: {pruning_time} = filter selection ({selection_time} + filter removal {removal_time})")
print(f"%%%%%% GPU memory after pruning model: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
print(f"%%%%%% Model architecture before:\n{model}")
print(f"%%%%%% Model architecture after:\n{pruned_model}")
model_size_before = get_model_size(model)
print(f"%%%%%% Model size before pruning: {model_size_before} MB")
model_size_after = get_model_size(pruned_model)
print(f"%%%%%% Model size after pruning: {model_size_after} MB")
###################################
# ---------- EVALUATION --------- #
###################################
torch.cuda.empty_cache()
model.to(device)
pruned_model.to(device)
print(f"%%%%%% GPU memory before starting evaluation: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
# ----- BEFORE PRUNING (BASE MODEL) -----
accuracy_before, inference_time_before, _ = evaluate(
model, subset_data_loader_test, cfg, device, 0, mapping, label="Before pruning", is_pruned=False
)
# ----- AFTER PRUNING -----
accuracy_after, inference_time_after, _ = evaluate(
pruned_model, subset_data_loader_test, cfg, device, inference_time_before, mapping,
label="After pruning"
)
# ----- KNOWLEDGE DISTILLATION SETUP -----
if cfg.use_knowledge_distillation:
cfg.pruning.name = "unstructured_magnitude"
cfg.pruning.pruning_ratio = [get_parameter_ratio(model, pruned_model)]
unstr_pruner = UnstructuredMagnitudePruner(
model=model,
sparsity=cfg.pruning.pruning_ratio[0],
replace_last_layer=cfg.replace_last_layer,
selected_classes=cfg.selected_classes,
device=device
)
# Structured pruned model becomes the student and unstructured pruned model becomes the teacher, which
# will get trained before it distills its knowledge into the student, hence the swap in
student_model = pruned_model
pruned_model, _ = run_pruner(unstr_pruner, cfg.pruning.pruning_ratio[0])
pruned_model = pruned_model.to(device)
# ----- RETRAINING -----
retraining_time = 0
best_accuracy, best_epoch, accuracy_after_retraining, inference_time_ratio_retraining = None, None, None, None
if cfg.training.retrain_after_pruning:
print("%%%%%% Retraining the pruned model...")
print(f"+++++ GPU memory right before training: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
start = time.perf_counter()
best_accuracy, best_epoch = train(
cfg, pruned_model,
subset_data_loader_train, subset_data_loader_val,
device, num_epochs=cfg.training.retrain_epochs, retrain=True
)
retraining_time = time.perf_counter() - start
accuracy_after_retraining, _, inference_time_ratio_retraining = evaluate(
pruned_model, subset_data_loader_test, cfg, device, inference_time_before, mapping,
label="After Retraining"
)
# ----- KNOWLEDGE DISTILLATION TRAINING -----
if cfg.use_knowledge_distillation:
kd = KnowledgeDistillation(
teacher_model=pruned_model, student_model=student_model,
selected_classes=cfg.selected_classes,
temperature=3.0, alpha=0.7, lr=1e-3
)
student_model, _, _ = kd.train(
train_loader=subset_data_loader_train,
val_loader=subset_data_loader_val,
epochs=100
)
accuracy_kd, _, _ = evaluate(
student_model, subset_data_loader_test, cfg, device, inference_time_before, mapping,
label="After Knowledge Distillation"
)
###################################
# ----------- LOGGING ----------- #
###################################
# Model parameters
model_size_before = get_model_size(model)
print(f"%%%%%% Model size before pruning: {model_size_before} MB")
model_size_after = get_model_size(pruned_model)
print(f"%%%%%% Model size after pruning: {model_size_after} MB")
parameter_ratio = get_parameter_ratio(model, pruned_model)
print(f"%%%%%% Pruned parameters ratio: {1 - parameter_ratio}")
global_pruning_ratio = None if cfg.pruning.name.startswith("unstructured_") else selector.global_pruning_ratio
# Flop Analysis
flops_before = FlopCountAnalysis(model, torch.randn(1, 3, 224, 224).to(device))
flops_after = FlopCountAnalysis(pruned_model, torch.randn(1, 3, 224, 224).to(device))
print(f"%%%%%% FLOPs before pruning: {flops_before.total() / 1e6} MFLOPs")
print(f"%%%%%% FLOPs after pruning: {flops_after.total() / 1e6} MFLOPs")
print(f"%%%%%% FLOPs reduction ratio: {flops_after.total() / flops_before.total()}")
print(f"%%%%%% Batch Inference time before pruning: {inference_time_before}")
print(f"%%%%%% Batch Inference time after pruning: {inference_time_after}")
inference_time_ratio = (inference_time_after / inference_time_before) if inference_time_before > 0 else 0
print(f"%%%%%% Inference time ratio: {inference_time_ratio}")
if cfg.pruning.name.startswith("unstructured_"):
sparsity_info = get_unstructured_sparsity(pruned_model)
print(f"%%%%%% Actual global sparsity: {sparsity_info["global"]:.4f}")
if cfg.log_results:
wandb.log({
"actual_sparsity": sparsity_info["global"],
"layer_sparsity": sparsity_info
})
if cfg.log_results:
wandb.log(
{
"accuracy_before": accuracy_before,
"accuracy_after": accuracy_after,
"model_size_before": model_size_before,
"model_size_after": model_size_after,
"model_size_ratio": model_size_after / model_size_before,
"parameter_ratio": parameter_ratio,
"gloabal_pruning_ratio": global_pruning_ratio,
"inference_time_batch_before": inference_time_before,
"inference_time_batch_after": inference_time_after,
"inference_time_ratio": inference_time_ratio,
"inference_time_per_sample_before": inference_time_before
/ cfg.training.batch_size_test,
"inference_time_per_sample_after": inference_time_after,
"pruned_parameters": 1 - parameter_ratio,
"flops_before": flops_before.total(),
"flops_after": flops_after.total(),
"flops_ratio": flops_after.total() / flops_before.total(),
"selection_time": selection_time,
"removal_time": removal_time,
"pruning_time": pruning_time,
"retraining_time": retraining_time,
"total_time": pruning_time + retraining_time,
"best_accuracy_retraining": best_accuracy,
"best_epoch_retraining": best_epoch,
"accuracy_after_retraining": accuracy_after_retraining,
"inference_time_ratio_retraining": inference_time_ratio_retraining,
}
)
if cfg.log_results:
wandb.finish()
if __name__ == "__main__":
main()