2022-07-08 13:34:45 +02:00
# Layout API Overview {#openvino_docs_OV_UG_Layout_Overview}
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
@sphinxdirective
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
The concept of layout helps you (and your application) to understand what each particular dimension of input/output tensor means. For example, if your input has the ``{1, 3, 720, 1280}`` shape and the ``NCHW`` layout, it is clear that ``N(batch) = 1``, ``C(channels) = 3``, ``H(height) = 720``, and ``W(width) = 1280``. Without the layout information, the ``{1, 3, 720, 1280}`` tuple does not give any idea to your application on what these numbers mean and how to resize the input image to fit the expectations of the model.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
With the ``NCHW`` layout, it is easier to understand what the ``{8, 3, 224, 224}`` model shape means. Without the layout, it is just a 4-dimensional tensor.
2022-02-21 19:20:23 +03:00
2022-07-08 13:34:45 +02:00
Below is a list of cases where input/output layout is important:
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
* Performing model modification:
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
* Applying the :doc:` preprocessing <openvino_docs_OV_UG_Preprocessing_Overview>` steps, such as subtracting means, dividing by scales, resizing an image, and converting ``RGB`` <-> ``BGR``.
* Setting/getting a batch for a model.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
* Doing the same operations as used during the model conversion phase. For more information, refer to the :doc:` Model Optimizer Embedding Preprocessing Computation <openvino_docs_MO_DG_Additional_Optimization_Use_Cases>` guide.
* Improving the readability of a model input and output.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Syntax of Layout
####################
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Short Syntax
++++++++++++++++++++
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
The easiest way is to fully specify each dimension with one alphabet letter.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:simple
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:simple
2022-02-21 19:20:23 +03:00
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
This assigns ``N`` to the first dimension, ``C`` to the second, ``H`` to the third, and ``W`` to the fourth.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Advanced Syntax
++++++++++++++++++++
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
The advanced syntax allows assigning a word to a dimension. To do this, wrap a layout with square brackets ``[]`` and specify each name separated by a comma ``,``.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:complex
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:complex
2022-02-21 19:20:23 +03:00
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
Partially Defined Layout
++++++++++++++++++++++++
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
If a certain dimension is not important, its name can be set to ``?``.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:partially_defined
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:partially_defined
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Dynamic Layout
++++++++++++++++++++
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
If several dimensions are not important, an ellipsis ``...`` can be used to specify those dimensions.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:dynamic
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:dynamic
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Predefined Names
++++++++++++++++++++
2022-02-21 19:20:23 +03:00
2022-07-08 13:34:45 +02:00
A layout has some pre-defined dimension names, widely used in computer vision:
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
* ``N``/``Batch`` - batch size
* ``C``/``Channels`` - channels
* ``D``/``Depth`` - depth
* ``H``/``Height`` - height
* ``W``/``Width`` - width
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
These names are used in :doc:` PreProcessing API <openvino_docs_OV_UG_Preprocessing_Overview>`. There is a set of helper functions to get appropriate dimension index from a layout.
2022-03-24 22:27:29 +03:00
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:predefined
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:predefined
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Equality
++++++++++++++++++++
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Layout names are case-insensitive, which means that ``Layout("NCHW")`` = ``Layout("nChW")`` = ``Layout("[N,c,H,w]")``.
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Dump Layout
++++++++++++++++++++
2022-02-21 19:20:23 +03:00
2022-07-08 13:34:45 +02:00
A layout can be converted to a string in the advanced syntax format. It can be useful for debugging and serialization purposes.
2022-02-21 19:20:23 +03:00
2022-03-24 22:27:29 +03:00
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:dump
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:dump
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
Get layout from Model Input/Output
++++++++++++++++++++++++++++++++++
2022-11-10 19:10:07 +04:00
OpenVINO provides helpers which provide a simple interface to get layout from Model input or output.
2023-03-13 12:13:49 +01:00
.. tab-set::
2022-11-10 19:10:07 +04:00
2023-03-13 12:13:49 +01:00
.. tab-item:: C++
:sync: cpp
2022-11-10 19:10:07 +04:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.cpp
:language: cpp
:fragment: ov:layout:get_from_model
2022-11-10 19:10:07 +04:00
2023-03-13 12:13:49 +01:00
.. tab-item:: Python
:sync: py
2022-11-10 19:10:07 +04:00
2023-03-13 12:13:49 +01:00
.. doxygensnippet:: docs/snippets/ov_layout.py
:language: python
:fragment: ov:layout:get_from_model
2022-11-10 19:10:07 +04:00
2023-03-13 12:13:49 +01:00
See also
####################
2022-11-10 19:10:07 +04:00
2023-03-13 12:13:49 +01:00
* API Reference: ` ov::Layout <classov_1_1Layout.html#doxid -classov-1-1-layout>`__ C++ class
2022-02-21 19:20:23 +03:00
2023-03-13 12:13:49 +01:00
@endsphinxdirective