博客
关于我
models.__dict__[args.arch] 实例复现
阅读量:799 次
发布时间:2023-02-09

本文共 1207 字,大约阅读时间需要 4 分钟。

在看moco源码时发下有个这样的代码不知道咋用的,复现了一下

model = moco.builder.MoCo(  # 这种初始化模型的方式是因为导包的时候导到了moco这一层        models.__dict__[args.arch],        args.moco_dim, args.moco_k, args.moco_m, args.moco_t, args.mlp)print(model)

具体复现过程如下:

1、文件夹目录如下:只需要红框框住的这三个.py文件就行,test文件夹下有一个__init__.py的启动文件,和一个test2.py文件,test.py文件和test文件夹目录同级
在这里插入图片描述
2、test文件夹下:
2.1 init.py文件夹中的代码如下:

from .test2 import *

2.2 test2.py文件中的代码如下:

__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101']def ResNet():    print('ResNet')def resnet18():    print('resnet18')def resnet34():    print('resnet34')def resnet50():    print('resnet50')def resnet101():    print('resnet101')

3、test.py中的代码如下:

import testclass Test:    def __init__(self, model_name='', age='', sex=''):        self.model_name = model_name        self.age = age        self.sex = sex        print('self.model_name', self.model_name)        print('self.age', self.age)        print('self.sex', self.sex)new_test = Test(test.__dict__['resnet50'], '18', 'boy')print('----------')test.__dict__['resnet50']()  # 调用函数print(new_test.model_name)  # 函数对象new_test.model_name()  # 调用函数

4、执行test.py文件结果如下:

在这里插入图片描述
5、从上边可以看出来,只要是建立了文件夹并其中有__init__.py(包)就可以使用包名.dict[‘包中的内容’]这种方式例如test.dict[‘resnet50’]。

转载地址:http://abffk.baihongyu.com/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>