Skip to main content

Blog

Learn About Our Meetup

5000+ Members

MEETUPS

LEARN, CONNECT, SHARE

Join our meetup, learn, connect, share, and get to know your Toronto AI community. 

JOB POSTINGS

INDEED POSTINGS

Browse through the latest deep learning, ai, machine learning postings from Indeed for the GTA.

CONTACT

CONNECT WITH US

Are you looking to sponsor space, be a speaker, or volunteer, feel free to give us a shout.

[P] How to get rid of boilerplate if-statements and switches

TL;DR I wrote this package and I find it useful in my projects

The problem

It’s common to specify script parameters in yaml config files. For example:

models: modelA: densenet121: pretrained: True memory_efficient: True modelB: resnext50_32x4d: pretrained: True 

Usually, the config file is loaded and then various if-statements or switches are used to instantiate objects etc:

if args.models["modelA"] == "densenet121": modelA = torchvision.models.densenet121(pretrained = args.pretrained) elif args.models["modelA"] == "googlenet": modelA = torchvision.models.googlenet(pretrained = args.pretrained) elif args.models["modelA"] == "resnet50": modelA = torchvision.models.resnet50(pretrained = args.pretrained) elif args.models["modelA"] == "inception_v3": modelA = torchvision.models.inception_v3(pretrained = args.pretrained) ... 

This is kind of annoying to do, and every time PyTorch adds new classes or functions that you want access to, you need to add new cases to your giant if-statement.

Use this package and get rid of those if-statements

So I wrote easy_module_attribute_getter.

With this package, the above if-statements get reduced to this:

from easy_module_attribute_getter import PytorchGetter pytorch_getter = PytorchGetter() models = pytorch_getter.get_multiple("model", args.models) 

“models” is a dictionary that maps from strings to the desired objects.

Automatically have yaml access to new classes when you upgrade PyTorch

The nice thing about this package is that if you upgrade to a new version of PyTorch which has 20 new classes, you don’t have to change anything. You automatically have access to all the new classes, and you can specify them in your yaml file.

Merge or override options at the command line

You can also merge or override complex config options (i.e. nested dictionaries) at the command line.

For example, if we want a modelC in addition to the 2 models specified in the yaml file:

python example.py --models {modelC: {googlenet: {pretrained: True, aux_logits: True}}} 

Now the models dictionary will contain modelA, modelB, and modelC. Or if we want to override the models option in the config file:

python example.py --models~OVERRIDE~ {modelC: {googlenet: {pretrained: True, aux_logits: True}}} 

Now the models dictionary will contain only modelC.

Transforms and optimizers

There are also some nice features specifically for transforms and optimizers which you can read about here.

Register your own modules

You can register your own modules so that you have direct yaml access to them as well.

Also note that the base class EasyModuleAttributeGetter, isn’t specific to PyTorch, but I wrote the child class PyTorchGetter since that’s what I’m using this package for.

I hope someone finds this useful!

submitted by /u/VanillaCashew
[link] [comments]