Skip to main content

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, which in this case are 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()
pytorch_getter.register('loss', losses) 
pytorch_getter.register('miner', miners)
pytorch_getter.register('sampler', samplers)
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)

In the above example, the 'loss' key already exists, so the 'losses' module will be appended to the existing module. Both modules are searched when get() or get_multiple() is used. For example, metric_loss contains an object from the pytorch_metric_learning library, whil kl_div_loss contains an object from the PyTorch library, which was registered by default.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_module_attribute_getter-0.9.13.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file easy_module_attribute_getter-0.9.13.tar.gz.

File metadata

  • Download URL: easy_module_attribute_getter-0.9.13.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for easy_module_attribute_getter-0.9.13.tar.gz
Algorithm Hash digest
SHA256 c61b7055b17b24021c13717b21883217f0c74c60f72bfdc9026f3aa15df4ca43
MD5 b704450890ee44dea71e4dfafee2dba9
BLAKE2b-256 f77fe536e3e4412c3186da07009d8566fb15fd526b8a9c6f1495cc3d6f501b2e

See more details on using hashes here.

File details

Details for the file easy_module_attribute_getter-0.9.13-py3-none-any.whl.

File metadata

  • Download URL: easy_module_attribute_getter-0.9.13-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for easy_module_attribute_getter-0.9.13-py3-none-any.whl
Algorithm Hash digest
SHA256 fdd3e9cf7ec39f19565245c72e364f6afb305deee4ad72eeaeee3c8d7a90b6a8
MD5 268a6977d10ce8eba1d00a30d5bd210b
BLAKE2b-256 059f4000990b5cc62b4a31189b813952bcce770aa5e7f0096358d79cd436ee44

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page