forked from open-mmlab/mmsegmentation
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from amitklinger/Hailo-2.0
Hailo 2.0: Pruning Support + Best pruned checkpoint save
- Loading branch information
Showing
10 changed files
with
297 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from mmengine.hooks import CheckpointHook | ||
from mmseg.registry import HOOKS | ||
|
||
|
||
@HOOKS.register_module() | ||
class ExtCheckpointHook(CheckpointHook): | ||
|
||
def after_val_epoch(self, runner, metrics): | ||
if runner.iter == self.save_begin: | ||
runner.logger.info('Resetting best_score to 0.0') | ||
runner.message_hub.update_info('best_score', 0.0) | ||
runner.message_hub.pop_info('best_ckpt', None) | ||
if (runner.iter + 1 >= self.save_begin): | ||
runner.logger.info( | ||
f'Saving checkpoint at iter {runner.iter}') | ||
super().after_val_epoch(runner, metrics) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
--- | ||
|
||
version: 1.1.0 | ||
|
||
# General Hyperparams | ||
start_epoch: 50 | ||
num_epochs: 120 | ||
init_lr: 0.00001 | ||
final_lr: 0.00001 | ||
weights_warmup_lr: 0 | ||
biases_warmup_lr: 0 | ||
|
||
# Pruning Hyperparams | ||
init_sparsity: 0.01 | ||
final_sparsity: 0.60 | ||
pruning_start_epoch: 60 | ||
pruning_end_epoch: 110 | ||
pruning_update_frequency: 2.0 | ||
|
||
#Modifiers | ||
training_modifiers: | ||
- !LearningRateFunctionModifier | ||
start_epoch: eval(start_epoch) | ||
end_epoch: eval(num_epochs) | ||
lr_func: linear | ||
init_lr: eval(init_lr) | ||
final_lr: eval(init_lr) | ||
|
||
pruning_modifiers: | ||
- !GMPruningModifier | ||
params: | ||
- re:backbone.backbone.*.*.rbr_dense.conv.weight | ||
- re:backbone.neck.*.*.rbr_dense.conv.weight | ||
init_sparsity: eval(init_sparsity) | ||
final_sparsity: eval(final_sparsity) | ||
start_epoch: eval(pruning_start_epoch) | ||
end_epoch: eval(pruning_end_epoch) | ||
update_frequency: eval(pruning_update_frequency) | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from mmseg.registry import HOOKS | ||
from mmseg.utils.misc import calc_sparsity | ||
from mmengine.hooks import Hook | ||
from sparseml.pytorch.optim import ScheduledModifierManager | ||
|
||
|
||
@HOOKS.register_module() | ||
class SparseMLHook(Hook): | ||
def __init__(self, steps_per_epoch=1488, start_epoch=50, prune_interval_epoch=2): | ||
self.steps_per_epoch = steps_per_epoch | ||
self.start_epoch = start_epoch | ||
self.prune_interval_epoch = prune_interval_epoch | ||
|
||
def before_train(self, runner) -> None: | ||
self.manager = ScheduledModifierManager.from_yaml(runner.cfg.recipe) | ||
|
||
optimizer = runner.optim_wrapper.optimizer | ||
optimizer = self.manager.modify(runner.model.module, | ||
optimizer, | ||
steps_per_epoch=self.steps_per_epoch, | ||
epoch=self.start_epoch) | ||
runner.optim_wrapper.optimizer = optimizer | ||
|
||
def after_train(self, runner) -> None: | ||
self.manager.finalize(runner.model.module) | ||
|
||
def after_train_iter(self, runner, batch_idx, data_batch, outputs): | ||
if batch_idx % (self.steps_per_epoch * self.prune_interval_epoch) == 0: # 2 Epochs | ||
calc_sparsity(runner.model.state_dict(), runner.logger) | ||
runner.logger.info(f"Epoch #{batch_idx // self.steps_per_epoch} End") | ||
|
||
def after_test_epoch(self, runner, metrics): | ||
runner.logger.info("Switching to deployment model") | ||
# if repvgg style -> deploy | ||
for module in runner.model.modules(): | ||
if hasattr(module, 'switch_to_deploy'): | ||
module.switch_to_deploy() | ||
calc_sparsity(runner.model.state_dict(), runner.logger, True) |
Oops, something went wrong.