init_instance_by_config
这个接口被高度抽象,dataset也从这里取,model也从这里取,那就下去看看
init_instance_by_config
def init_instance_by_config(
config: InstConf,
default_module=None,
accept_types: Union[type, Tuple[type]] = (),
try_kwargs: Dict = {},
**kwargs,
) -> Any:
从定义中看出config必填,再看一下InstConf
InstConf = Union[InstDictConf, str, object, Path]
class InstDictConf(TypedDict):
# class: str # because class is a keyword of Python. We have to comment it
kwargs: dict # It is optional. {} will be used if not given
module_path: str # It is optional if module is given in the class
接下来看一下
if isinstance(config, (str, Path)):
if isinstance(config, str):
# path like 'file:///<path to pickle file>/obj.pkl'
pr = urlparse(config)
if pr.scheme == "file":
with open(os.path.join(pr.netloc, pr.path), "rb") as f:
return pickle.load(f)
else:
with config.open("rb") as f:
return pickle.load(f)
#如果config是一个str,则用urlparse来解析,
#如果config是一个对象,那么当成pickle文件来读
接下来看关键的,加载class
klass, cls_kwargs = get_callable_kwargs(config, default_module=default_module)
try:
return klass(**cls_kwargs, **try_kwargs, **kwargs)
except (TypeError,):
# TypeError for handling errors like
# 1: `XXX() got multiple values for keyword argument 'YYY'`
# 2: `XXX() got an unexpected keyword argument 'YYY'
return klass(**cls_kwargs, **kwargs)
跳入实现函数get_callable_kwargs
这个函数细节不贴了,大致意思是从路径中找module_path,然后import进来,如果没有,则报错。返回对象名称和kwargs。
那么到这里,相当于class和cls_kwargs进行了查找校验。就是说这个函数仅仅从磁盘上找了配置中的路径是否存在,对参数做了校验,并没有执行。