-
Notifications
You must be signed in to change notification settings - Fork 1
Bugfixes and documentation #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
041ed27
ab33a35
12254ef
90ca94d
3cc171a
a32e079
1ed115e
4b96a10
b982b77
d44c6f2
e712e82
b0716bd
0fcd422
49cc52f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,11 @@ def _get_info(self, layer_key: str, layer_list: list[LayerInfo], full_key, paren | |
| key_elements = layer_key.split(".") | ||
|
|
||
| if len(key_elements) > 1: | ||
| parents = {info.var_name: info for info in layer_list if not info.is_leaf_layer} | ||
| if parent_info.parent_info: | ||
|
Author
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. Here, some models threw an exception. During the traversal of the model layers, when listing the childs of a parent, all childs of childs were not excluded. This led to wrong traversal of the layer tree. I excluded all childs of childs by adding the condition |
||
| parents = {info.var_name: info for info in layer_list if not info.is_leaf_layer and info.parent_info.var_name == parent_info.var_name} | ||
| else: | ||
| parents = {info.var_name: info for info in layer_list if not info.is_leaf_layer} | ||
|
|
||
| if key_elements[0] in parents: | ||
| current_info = parents[key_elements[0]] | ||
| return self._get_info(".".join(key_elements[1:]), current_info.children, full_key, current_info) | ||
|
|
@@ -26,4 +30,4 @@ def _get_info(self, layer_key: str, layer_list: list[LayerInfo], full_key, paren | |
| if key_elements[0] in leafs: | ||
| return leafs[key_elements[0]] | ||
|
|
||
| raise Exception(f"Could not resolve layer info for {full_key} - step failed for part {'.'.join(key_elements)}") | ||
| raise Exception(f"Could not resolve layer info for {full_key} - step failed for part {'.'.join(key_elements)}") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,6 +225,8 @@ def _is_layer_compatible(self, layer_key, model_info) -> bool: | |
| return False | ||
| return True | ||
| if isinstance(layer_info.module, torch.nn.Conv2d): | ||
| if layer_info.output_size == []: | ||
|
Author
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. Unhandled state of |
||
| return False | ||
| min_output_size = min(layer_info.output_size[2], layer_info.output_size[3]) | ||
| if min_output_size < 2: | ||
| return False | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,9 @@ def __init__(self, | |
| self._frozen_layers = frozen_layers | ||
| self._layer_dict = {layer_key: module for layer_key, module in self._reference_model.named_modules() if | ||
| not [*module.children()]} | ||
| last_layer = list(self._layer_dict.keys())[-1] | ||
|
Author
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. I discovered, that galen assumes, that the last layer of the model is named "fc" as stated e.g here. This leads to unexpected and difficult to resolve errors during pruning. I propose to always add the last layer of the network to the list of frozen layers. Alternatively, it should be included in the documentation. |
||
| if "p-lin" in self._frozen_layers: | ||
| self._frozen_layers["p-lin"].append(last_layer) # disable pruning of last layer | ||
|
|
||
| def all_layer_keys(self) -> list[str]: | ||
| return [*self._layer_dict] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| python -m tools.search_policy \ | ||
| --model resnet18_pretrained \ | ||
| --ckpt_load_path ./results/checkpoints/resnet18_pretrained/resnet18_pretrained_pre-train_lr0.05_mom0.9_ep93.pth \ | ||
| --log_dir ./logs/resnet18_pretrained \ | ||
| --agent independent-single-layer-pruning \ | ||
| --episodes 410 \ | ||
| --add_search_identifier resnet18_pretrained \ | ||
| --alg_config num_workers=6 reward=r6 r6_beta=-5 mixed_reference_bits=6 reward_target_cost_ratio=0.25 enable_latency_eval=False reward_episode_cost_key=BOPs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| python -m tools.search_policy \ | ||
| --model ./pretrained_models/densenet121.pth \ | ||
| --log_dir ./logs/densenet121 \ | ||
| --agent independent-single-layer-pruning \ | ||
| --episodes 410 \ | ||
| --add_search_identifier densenet121 \ | ||
| --alg_config num_workers=6 reward=r6 r6_beta=-5 mixed_reference_bits=6 reward_target_cost_ratio=0.25 enable_latency_eval=False reward_episode_cost_key=BOPs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,7 +208,7 @@ def parse_arguments() -> Namespace: | |
| log_file_name=args.log_name | ||
| ) | ||
|
|
||
| wandb.init(project=args.wandb_project, entity=args.wandb_entity, config=vars(args), | ||
| wandb.init(project=args.wandb_project, config=vars(args), | ||
|
Author
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.
|
||
| name=trainer.create_identifier(args.epochs)) | ||
|
|
||
| protocol = None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import torch | ||
| import torch.hub | ||
| from torch import nn | ||
|
|
||
| from pathlib import Path | ||
| import torchvision | ||
|
|
||
| class TestModel(nn.Module): | ||
| def __init__(self): | ||
|
|
@@ -28,18 +29,37 @@ def resnet18_cifar(): | |
| model.maxpool = nn.Identity() | ||
| return model | ||
|
|
||
| def mobile_net_pretrained(): | ||
| model = torchvision.models.mobilenet_v2(pretrained=True) | ||
| num_classes = 10 | ||
| model.classifier[1] = torch.nn.Linear(1280, num_classes) | ||
| return model | ||
|
|
||
| def resnet18_pretrained(): | ||
| model = torchvision.models.resnet18(pretrained=True) | ||
| num_features = model.fc.in_features | ||
| model.fc = nn.Linear(num_features, 10) | ||
| return model | ||
|
|
||
| provider = { | ||
| "test_model": test_model, | ||
| "resnet18_cifar": resnet18_cifar | ||
| "resnet18_cifar": resnet18_cifar, | ||
| "mobile_net_pretrained": mobile_net_pretrained, | ||
| "resnet18_pretrained": resnet18_pretrained | ||
| } | ||
|
|
||
|
|
||
| def load_model(select_str, num_classes, checkpoint_path=None): | ||
| if "@" in select_str: | ||
| # model on torch hub | ||
| name, repo = select_str.split("@") | ||
| model = torch.hub.load(repo, name, pretrained=True, num_classes=num_classes) | ||
| elif "/" in select_str: | ||
|
Author
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. Proposal for an additional method for loading pretrained models which were saved using torch.save(model, PATH). |
||
| # local path to pretrained model containing architecture definition | ||
| model = torch.load(select_str) | ||
| name = Path(select_str).stem | ||
| else: | ||
| # predefined architecture definition | ||
| model = provider[select_str]() | ||
| name = select_str | ||
|
|
||
|
|
@@ -52,4 +72,6 @@ def load_checkpoint(checkpoint_path): | |
| if checkpoint_path.endswith('.lightning.ckpt'): | ||
| state_dict = torch.load(checkpoint_path)['state_dict'] | ||
| return {key[6:]: weight for key, weight in state_dict.items()} | ||
| if checkpoint_path.startswith('pretrained_checkpoints'): | ||
| return torch.load(checkpoint_path)['model_state_dict'] | ||
| return torch.load(checkpoint_path) | ||
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.
The torch-pruning API changed since version 0.2.8 requiring a refactoring of the galen code.