-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix]Dcff Deploy Revision #383
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3fd9c9b
dcff deploy revision
spynccat a7e5336
Merge branch 'dev-1.x' into dcff_deploy_revision
spynccat fb70f78
tempsave
spynccat 69c7d98
Merge branch 'dev-1.x' into dcff_deploy_revision
spynccat f99de08
update fix_subnet
spynccat 5206866
update mutator load
spynccat 8213b33
export/load_fix_subnet revision for mutator
spynccat 7c77ae1
Merge branch 'dev-1.x' into dcff_deploy_revision
spynccat a957099
update fix_subnet with dev-1.x
spynccat 9821668
update comments
spynccat e4c683b
update docs
spynccat afb68a0
update registry
spynccat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,46 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import os.path as osp | ||
|
||
from mmengine import fileio | ||
from mmengine.runner import ValLoop | ||
|
||
from mmrazor.registry import LOOPS | ||
from mmrazor.structures import export_fix_subnet | ||
|
||
|
||
@LOOPS.register_module() | ||
class ItePruneValLoop(ValLoop): | ||
"""Pruning loop for validation. Export fixed subnet configs. | ||
|
||
Args: | ||
runner (Runner): A reference of runner. | ||
dataloader (Dataloader or dict): A dataloader object or a dict to | ||
build a dataloader. | ||
evaluator (Evaluator or dict or list): Used for computing metrics. | ||
fp16 (bool): Whether to enable fp16 validation. Defaults to | ||
False. | ||
""" | ||
|
||
def run(self): | ||
"""Launch validation.""" | ||
self.runner.call_hook('before_val') | ||
self.runner.call_hook('before_val_epoch') | ||
self.runner.model.eval() | ||
for idx, data_batch in enumerate(self.dataloader): | ||
self.run_iter(idx, data_batch) | ||
|
||
# compute metrics | ||
metrics = self.evaluator.evaluate(len(self.dataloader.dataset)) | ||
self._save_fix_subnet() | ||
self.runner.call_hook('after_val_epoch', metrics=metrics) | ||
self.runner.call_hook('after_val') | ||
return metrics | ||
|
||
def _save_fix_subnet(self): | ||
"""Save model subnet config.""" | ||
fix_subnet = export_fix_subnet(self.model) | ||
save_name = 'fix_subnet.yaml' | ||
fileio.dump(fix_subnet, osp.join(self.runner.work_dir, save_name)) | ||
self.runner.logger.info( | ||
'export finished and ' | ||
f'{save_name} saved in {self.runner.work_dir}.') |
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from mmrazor.models.mutables import MutableChannelUnit | ||
from mmrazor.models.mutators import ChannelMutator | ||
from mmrazor.registry import MODELS | ||
from mmrazor.utils import ValidFixMutable | ||
from ..base import BaseAlgorithm | ||
|
||
LossResults = Dict[str, torch.Tensor] | ||
|
@@ -97,6 +98,8 @@ class ItePruneAlgorithm(BaseAlgorithm): | |
mutator_cfg (Union[Dict, ChannelMutator], optional): The config | ||
of a mutator. Defaults to dict( type='ChannelMutator', | ||
channel_unit_cfg=dict( type='SequentialMutableChannelUnit')). | ||
fix_subnet (str | dict | :obj:`FixSubnet`): The path of yaml file or | ||
loaded dict or built :obj:`FixSubnet`. Defaults to None. | ||
data_preprocessor (Optional[Union[Dict, nn.Module]], optional): | ||
Defaults to None. | ||
target_pruning_ratio (dict, optional): The prune-target. The template | ||
|
@@ -110,6 +113,8 @@ class ItePruneAlgorithm(BaseAlgorithm): | |
Defaults to None. | ||
linear_schedule (bool, optional): flag to set linear ratio schedule. | ||
Defaults to True. | ||
is_deployed (bool, optional): flag to set deployed algorithm. | ||
Defaults to False. | ||
""" | ||
|
||
def __init__(self, | ||
|
@@ -118,12 +123,14 @@ def __init__(self, | |
type='ChannelMutator', | ||
channel_unit_cfg=dict( | ||
type='SequentialMutableChannelUnit')), | ||
fix_subnet: Optional[ValidFixMutable] = None, | ||
data_preprocessor: Optional[Union[Dict, nn.Module]] = None, | ||
target_pruning_ratio: Optional[Dict[str, float]] = None, | ||
step_freq=-1, | ||
prune_times=-1, | ||
step_freq=1, | ||
prune_times=1, | ||
init_cfg: Optional[Dict] = None, | ||
linear_schedule=True) -> None: | ||
linear_schedule=True, | ||
is_deployed=False) -> None: | ||
|
||
super().__init__(architecture, data_preprocessor, init_cfg) | ||
|
||
|
@@ -132,10 +139,17 @@ def __init__(self, | |
self.step_freq = step_freq | ||
self.prune_times = prune_times | ||
self.linear_schedule = linear_schedule | ||
self.is_deployed = is_deployed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# mutator | ||
self.mutator: ChannelMutator = MODELS.build(mutator_cfg) | ||
self.mutator.prepare_from_supernet(self.architecture) | ||
if self.is_deployed: | ||
assert fix_subnet is not None | ||
# Avoid circular import | ||
from mmrazor.structures import load_fix_subnet | ||
load_fix_subnet(self.architecture, fix_subnet) | ||
else: | ||
# init mutator | ||
self.mutator: ChannelMutator = MODELS.build(mutator_cfg) | ||
self.mutator.prepare_from_supernet(self.architecture) | ||
|
||
def group_target_pruning_ratio( | ||
self, target: Dict[str, float], | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's weights also should be exported so that we can load it when in finetuning stage.