From a8caa23624c49eb6d2e5bf1b309808a43a96313d Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 27 Mar 2020 13:51:25 +0000 Subject: [PATCH] adds factories doc to docs/ --- docs/source/networks.rst | 28 ++++++++-------------------- monai/networks/layers/factories.py | 22 +++++++++++++++------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/docs/source/networks.rst b/docs/source/networks.rst index 5d456c1528d..8b05382ba92 100644 --- a/docs/source/networks.rst +++ b/docs/source/networks.rst @@ -26,26 +26,14 @@ Blocks Layers ------ -`get_conv_type` -~~~~~~~~~~~~~~~ -.. automethod:: monai.networks.layers.factories.get_conv_type - -`get_dropout_type` -~~~~~~~~~~~~~~~~~~ -.. automethod:: monai.networks.layers.factories.get_dropout_type - -`get_normalize_type` -~~~~~~~~~~~~~~~~~~~~ -.. automethod:: monai.networks.layers.factories.get_normalize_type - -`get_maxpooling_type` -~~~~~~~~~~~~~~~~~~~~~ -.. automethod:: monai.networks.layers.factories.get_maxpooling_type - -`get_avgpooling_type` -~~~~~~~~~~~~~~~~~~~~~ -.. automethod:: monai.networks.layers.factories.get_avgpooling_type - +`Factories` +~~~~~~~~~~~ +.. automodule:: monai.networks.layers.factories +.. currentmodule:: monai.networks.layers.factories + +`LayerFactory` +############## +.. autoclass:: LayerFactory .. automodule:: monai.networks.layers.simplelayers .. currentmodule:: monai.networks.layers.simplelayers diff --git a/monai/networks/layers/factories.py b/monai/networks/layers/factories.py index 0f49abc8d63..05213ab68bc 100644 --- a/monai/networks/layers/factories.py +++ b/monai/networks/layers/factories.py @@ -8,45 +8,53 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + """ Defines factories for creating layers in generic, extensible, and dimensionally independent ways. A separate factory object is created for each type of layer, and factory functions keyed to names are added to these objects. Whenever a layer is requested the factory name and any necessary arguments are passed to the factory object. The return value -is typically a type but can be any callable producing a layer object. +is typically a type but can be any callable producing a layer object. The factory objects contain functions keyed to names converted to upper case, these names can be referred to as members of the factory so that they can function as constant identifiers. eg. instance normalisation is named `Norm.INSTANCE`. -For example, to get a transpose convolution layer the name is needed and then a dimension argument is provided which is +For example, to get a transpose convolution layer the name is needed and then a dimension argument is provided which is passed to the factory function: +.. code-block:: python + dimension = 3 name = Conv.CONVTRANS conv = Conv[name, dimension] -This allows the `dimension` value to be set in the constructor, for example so that the dimensionality of a network is +This allows the `dimension` value to be set in the constructor, for example so that the dimensionality of a network is parameterizable. Not all factories require arguments after the name, the caller must be aware which are required. Defining new factories involves creating the object then associating it with factory functions: +.. code-block:: python + fact = LayerFactory() @fact.factory_function('test') def make_something(x, y): # do something with x and y to choose which layer type to return return SomeLayerType - ... + ... - layer = fact[fact.TEST, 1, 2] # request object from factory TEST with 1 and 2 as values for x and y + # request object from factory TEST with 1 and 2 as values for x and y + layer = fact[fact.TEST, 1, 2] Typically the caller of a factory would know what arguments to pass (ie. the dimensionality of the requested type) but can be parameterized with the factory name and the arguments to pass to the created type at instantiation time: +.. code-block:: python + def use_factory(fact_args): fact_name, type_args = split_args layer_type = fact[fact_name, 1, 2] return layer_type(**type_args) - ... + ... kw_args = {'arg0':0, 'arg1':True} layer = use_factory( (fact.TEST, kwargs) ) @@ -135,7 +143,7 @@ def __getattr__(self, key): def split_args(args): """ - Split arguments in a way to be suitable for using with the factory types. If `args` is a name it's interpreted + Split arguments in a way to be suitable for using with the factory types. If `args` is a name it's interpreted """ if isinstance(args, str):