-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add custom build methods example
- Loading branch information
Showing
4 changed files
with
81 additions
and
8 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,69 @@ | ||
import torch | ||
|
||
from torchvision.models import densenet121 | ||
|
||
from argus import Model | ||
from argus.model.build import ( | ||
choose_attribute_from_dict, | ||
cast_optimizer, | ||
cast_nn_module | ||
) | ||
|
||
|
||
class MyModel(Model): | ||
nn_module = densenet121 | ||
optimizer = torch.optim.Adam | ||
loss = torch.nn.CrossEntropyLoss | ||
|
||
def build_nn_module(self, nn_module_meta, nn_module_params): | ||
if nn_module_meta is None: | ||
raise ValueError("nn_module is required attribute for argus.Model") | ||
|
||
nn_module, nn_module_params = choose_attribute_from_dict(nn_module_meta, | ||
nn_module_params) | ||
nn_module = cast_nn_module(nn_module) | ||
nn_module = nn_module(**nn_module_params) | ||
|
||
# Replace last fully connected layer | ||
num_classes = self.params['num_classes'] | ||
in_features = nn_module.classifier.in_features | ||
nn_module.classifier = torch.nn.Linear(in_features=in_features, | ||
out_features=num_classes) | ||
return nn_module | ||
|
||
def build_optimizer(self, optimizer_meta, optim_params): | ||
optimizer, optim_params = choose_attribute_from_dict(optimizer_meta, | ||
optim_params) | ||
optimizer = cast_optimizer(optimizer) | ||
|
||
# Set small LR for pretrained layers | ||
pretrain_modules = [ | ||
self.nn_module.features | ||
] | ||
pretrain_params = [] | ||
for pretrain_module in pretrain_modules: | ||
pretrain_params += pretrain_module.parameters() | ||
pretrain_ids = list(map(id, pretrain_params)) | ||
other_params = filter(lambda p: id(p) not in pretrain_ids, | ||
self.nn_module.parameters()) | ||
grad_params = [ | ||
{"params": pretrain_params, "lr": optim_params['lr'] * 0.01}, | ||
{"params": other_params, "lr": optim_params['lr']} | ||
] | ||
del optim_params['lr'] | ||
|
||
optimizer = optimizer(params=grad_params, **optim_params) | ||
return optimizer | ||
|
||
|
||
if __name__ == "__main__": | ||
params = { | ||
'nn_module': {'pretrained': True, 'progress': False}, | ||
'optimizer': {'lr': 0.001}, | ||
'device': 'cuda', | ||
'num_classes': 10 | ||
} | ||
|
||
model = MyModel(params) | ||
print("Learning rate for each params group:", model.get_lr()) | ||
print("Last FC layer:", model.nn_module.classifier) |