Select module classes and functions using yaml, without any if-statements.
Project description
easy_module_attribute_getter
Installation:
pip install easy_module_attribute_getter
Simple example, using PytorchGetter which extends EasyModuleAttributeGetter:
1. Specify class names and arguments in your yaml config file:
models:
modelA:
densenet121:
pretrained: True
memory_efficient: True
modelB:
resnext50_32x4d:
pretrained: True
losses:
lossA:
CrossEntropyLoss:
lossB:
L1Loss:
2. Read yaml file, and get objects from modules:
from easy_module_attribute_getter import YamlReader, PytorchGetter
yaml_reader = YamlReader()
args, _, _ = yaml_reader.load_yamls(['example.yaml'])
pytorch_getter = PytorchGetter()
models = pytorch_getter.get_multiple("model", args.models)
losses = pytorch_getter.get_multiple("loss", args.losses)
# "models" is a dictionary with keys "modelA" and "modelB" as specified
# in the yaml file. The values are the loaded objects (in this case
# pytorch models).
# The same is true for "losses".
Easily register your own modules into an existing getter.
from pytorch_metric_learning import losses, miners, samplers
pytorch_getter = PytorchGetter()
# The 'loss' key already exists, so the 'losses' module will be appended to the existing module.
pytorch_getter.register('loss', losses)
pytorch_getter.register('miner', miners)
pytorch_getter.register('sampler', samplers)
# Both modules will be searched when get() or get_multiple() is used.
# The first loss comes from the module that was just registered.
# The second loss comes from the Pytorch library that is registered by default.
metric_loss = pytorch_getter.get('loss', class_name='TripletMarginLoss', return_uninitialized=True)
kl_div_loss = pytorch_getter.get('loss', class_name='KLDivLoss', return_uninitialized=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for easy_module_attribute_getter-0.9.12.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b09e45f2de4142b4b0d2efefeac91a04f9e4389ae224d3bdb1789e6ea3e732d5 |
|
MD5 | a7ac60ecf6d1508448b8078fb231e657 |
|
BLAKE2b-256 | 7a48270dea5e7915202d86b399fc2e36dde634bdf73dea348b5a691d4fa441c4 |
Close
Hashes for easy_module_attribute_getter-0.9.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9217d94c481f5c8018e990693f37d3d6e0c84e88ae85b4d1969f59b72748c518 |
|
MD5 | df167747bea2b42bdd0340c1b5591a68 |
|
BLAKE2b-256 | f61f48f6ed0b00a8c724beb17e244d14d8c9c97434bab706df1d99556f6682bc |