Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions docs/source/networks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions monai/networks/layers/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down Expand Up @@ -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):
Expand Down