Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

import specific module from the class by different name as string variable

I wanna get a module from a class with different name (string variable). However, the content has same form of string as the original module name.
For example

import timm
model_configs = timm.models.resnet.default_cfgs['resnet34']  #this one works

target_network_root = 'resnet'
model_configs = timm.models.target_network_root.default_cfgs['resnet34']  #this one doesn't work

Since, the target_network_root can change, I might export another network than resnet, I like to call specific module from timm.models as variable. I really appreciate your support.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use getattr()

For example:

getattr(timm.models, target_network_root)

=> timm.models.resnet
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading