泛微 企业网站建设计划,怎样弄网站的导航栏,wordpress 怎么汉化主题,做分析图网站LayerFactory
用于创建图层的工厂对象#xff0c;这使用给定的工厂函数来实际产生类型或构建可调用程序。这些函数是通过名称来参考的#xff0c;可以在任何时候添加。
用到的关键技术点#xff1a;
装饰器(Decorators), 例如#xff1a;property装饰器#xff0c;创建…LayerFactory
用于创建图层的工厂对象这使用给定的工厂函数来实际产生类型或构建可调用程序。这些函数是通过名称来参考的可以在任何时候添加。
用到的关键技术点
装饰器(Decorators), 例如property装饰器创建只读属性property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用这样可以防止属性被修改几个特殊的函数:__getitem__,__getattr__
LayerFactory 类中的方法 def add_factory_callable(self, name: str, func: Callable) - None: 在给定的名称下将工厂函数添加到此对象中。 def factory_function(self, name: str) - Callable: 装饰器用于添加一个具有给定名称的工厂函数。 def get_constructor(self, factory_name: str, *args) - Any: 获取给定工厂名称和参数的构造函数。 TypeError: When factory_name is not a str. def __getitem__(self, args) - Any: 获取给定的名称或名称/参数对。如果args是一个可调用的它被认为是构造函数本身并被返回。 本身并被返回否则它应该是工厂名称或包含名称和参数的一对。 def __getattr__(self, key): 如果key是一个工厂名称则返回它否则表现为继承。这允许将工厂名称作为常量来引用 例如Fact.FOO表示一个带有工厂函数foo的工厂因子。
为这些层类型定义工厂
Dropout LayerFactory()
Norm LayerFactory()
Act LayerFactory()
Conv LayerFactory()
Pool LayerFactory()
Pad LayerFactory()
利用装饰器函数注册函数实例
Droupt 工厂注册相关的工厂方法其中参考代码如下
Dropout.factory_function(dropout)
def dropout_factory(dim: int) - Type[Union[nn.Dropout, nn.Dropout2d, nn.Dropout3d]]:types (nn.Dropout, nn.Dropout2d, nn.Dropout3d)return types[dim - 1]Act 工厂注册方法
Act.add_factory_callable(elu, lambda: nn.modules.ELU)
Act.add_factory_callable(relu, lambda: nn.modules.ReLU)
Act.add_factory_callable(leakyrelu, lambda: nn.modules.LeakyReLU)调用流程分析
调用卷积工厂如下
from monai.networks.layers.factories import Conv
def test_factories():dimension 3#当我们访问一个不存在的属性的时候就会进入__getattr__#Conv.CONVTRANS 这个属性是不存在的所以作者重写了__getattr__方法# 会从self.factories查找注册方法的keyname Conv.CONVTRANS#[] 会调用__getitem__,作者重写了__gettitem__# __gettitem__ 会判断是否是一个可调用的对象如果不是可调用的对象则调用其构造函数conv Conv[name, dimension]
if __name__ __main__:test_factories()CONVTRANS的构造函数如下
Conv.factory_function(convtrans)
def convtrans_factory(dim: int) - Type[Union[nn.ConvTranspose1d, nn.ConvTranspose2d, nn.ConvTranspose3d]]:types (nn.ConvTranspose1d, nn.ConvTranspose2d, nn.ConvTranspose3d)在调用convtrans_factory方法之前通过装饰器已经把{“CONVTRANS”,convtrans_factory} 注册到 self.factories: Dict[str, Callable] 中。
源码
import warnings
from typing import Any, Callable, Dict, Tuple, Type, Unionimport torch
import torch.nn as nnfrom monai.utils import look_up_option, optional_importInstanceNorm3dNVFuser, has_nvfuser optional_import(apex.normalization, nameInstanceNorm3dNVFuser)__all__ [LayerFactory, Dropout, Norm, Act, Conv, Pool, Pad, split_args]class LayerFactory:Factory object for creating layers, this uses given factory functions to actually produce the types or constructingcallables. These functions are referred to by name and can be added at any time.def __init__(self) - None:self.factories: Dict[str, Callable] {}propertydef names(self) - Tuple[str, ...]:Produces all factory names.return tuple(self.factories)def add_factory_callable(self, name: str, func: Callable) - None:Add the factory function to this object under the given name.self.factories[name.upper()] funcself.__doc__ (The supported member (s are: if len(self.names) 1 else is: ) , .join(f{name} for name in self.names) .\nPlease see :py:class:monai.networks.layers.split_args for additional args parsing.)def factory_function(self, name: str) - Callable:Decorator for adding a factory function with the given name.def _add(func: Callable) - Callable:self.add_factory_callable(name, func)return funcreturn _adddef get_constructor(self, factory_name: str, *args) - Any:Get the constructor for the given factory name and arguments.Raises:TypeError: When factory_name is not a str.if not isinstance(factory_name, str):raise TypeError(ffactory_name must a str but is {type(factory_name).__name__}.)func look_up_option(factory_name.upper(), self.factories)return func(*args)def __getitem__(self, args) - Any:Get the given name or name/arguments pair. If args is a callable it is assumed to be the constructoritself and is returned, otherwise it should be the factory name or a pair containing the name and arguments.# args[0] is actually a type or constructorif callable(args):return args# args is a factory name or a name with argumentsif isinstance(args, str):name_obj, args args, ()else:name_obj, *args argsreturn self.get_constructor(name_obj, *args)def __getattr__(self, key):If key is a factory name, return it, otherwise behave as inherited. This allows referring to factory namesas if they were constants, eg. Fact.FOO for a factory Fact with factory function foo.if key in self.factories:return keyreturn super().__getattribute__(key)
参考链接
LayerFactory 源码文件 Python 魔法方法三 getattrsetattr delattr 装饰器博客