Back to Openvino

Model Creation Sample

docs/articles_en/get-started/learn-openvino/openvino-samples/model-creation.rst

2026.1.28.8 KB
Original Source

Model Creation Sample

.. meta:: :description: Learn how to create a model on the fly with a provided weights file and infer it later using Synchronous Inference Request API (Python, C++).

This sample demonstrates how to run inference using a :doc:model <../../../openvino-workflow/running-inference/model-representation> built on the fly that uses weights from the LeNet classification model, which is known to work well on digit classification tasks. You do not need an XML file, the model is created from the source code on the fly. Before using the sample, refer to the following requirements:

  • The sample accepts a model weights file (*.bin).
  • The sample has been validated with a LeNet model.
  • To build the sample, use instructions available at :ref:Build the Sample Applications <build-samples> section in "Get Started with Samples" guide.

How It Works ####################

At startup, the sample application reads command-line parameters, :doc:builds a model <../../../openvino-workflow/running-inference/model-representation> and passes the weights file. Then, it loads the model and input data to the OpenVINO™ Runtime plugin. Finally, it performs synchronous inference and processes output data, logging each step in a standard output stream.

.. tab-set::

.. tab-item:: Python :sync: python

  .. scrollbox::

     .. doxygensnippet:: samples/python/model_creation_sample/model_creation_sample.py
        :language: python

.. tab-item:: C++ :sync: cpp

  .. scrollbox::

     .. doxygensnippet:: samples/cpp/model_creation_sample/main.cpp
        :language: cpp

You can see the explicit description of each sample step at :doc:Integration Steps <../../../openvino-workflow/running-inference> section of "Integrate OpenVINO™ Runtime with Your Application" guide.

Running ####################

To run the sample, you need to specify model weights and a device.

.. tab-set::

.. tab-item:: Python :sync: python

  .. code-block:: console

     python model_creation_sample.py <path_to_weights_file> <device_name>

.. tab-item:: C++ :sync: cpp

  .. code-block:: console

     model_creation_sample <path_to_weights_file> <device_name>

.. note::

  • This sample supports models with FP32 weights only.
  • The lenet.bin weights file is generated by :doc:model conversion API <../../../openvino-workflow/model-preparation/convert-model-to-ir> from the public LeNet model, with the input_shape [64,1,28,28] parameter specified.
  • The original model is available in the Caffe repository <https://github.com/BVLC/caffe/tree/master/examples/mnist>__ on GitHub.

Example ++++++++++++++++++++

.. tab-set::

.. tab-item:: Python :sync: python

  .. code-block:: console

     python model_creation_sample.py lenet.bin GPU

.. tab-item:: C++ :sync: cpp

  .. code-block:: console

     model_creation_sample lenet.bin GPU

Sample Output ####################

.. tab-set::

.. tab-item:: Python :sync: python

  The sample application logs each step in a standard output stream and outputs 10 inference results.

  .. code-block:: console

     [ INFO ] Creating OpenVINO Runtime Core
     [ INFO ] Loading the model using openvino with weights from lenet.bin
     [ INFO ] Loading the model to the plugin
     [ INFO ] Starting inference in synchronous mode
     [ INFO ] Top 1 results:
     [ INFO ] Image 0
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 0       1.0000000   0
     [ INFO ]
     [ INFO ] Image 1
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 1       1.0000000   1
     [ INFO ]
     [ INFO ] Image 2
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 2       1.0000000   2
     [ INFO ]
     [ INFO ] Image 3
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 3       1.0000000   3
     [ INFO ]
     [ INFO ] Image 4
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 4       1.0000000   4
     [ INFO ]
     [ INFO ] Image 5
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 5       1.0000000   5
     [ INFO ]
     [ INFO ] Image 6
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 6       1.0000000   6
     [ INFO ]
     [ INFO ] Image 7
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 7       1.0000000   7
     [ INFO ]
     [ INFO ] Image 8
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 8       1.0000000   8
     [ INFO ]
     [ INFO ] Image 9
     [ INFO ]
     [ INFO ] classid probability label
     [ INFO ] -------------------------
     [ INFO ] 9       1.0000000   9
     [ INFO ]
     [ INFO ] This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool

.. tab-item:: C++ :sync: cpp

  The sample application logs each step in a standard output stream and outputs top-10 inference results.

  .. code-block:: console

     [ INFO ] OpenVINO Runtime version ......... <version>
     [ INFO ] Build ........... <build>
     [ INFO ]
     [ INFO ] Device info:
     [ INFO ] GPU
     [ INFO ] Intel GPU plugin version ......... <version>
     [ INFO ] Build ........... <build>
     [ INFO ]
     [ INFO ]
     [ INFO ] Create model from weights: lenet.bin
     [ INFO ] model name: lenet
     [ INFO ]     inputs
     [ INFO ]         input name: NONE
     [ INFO ]         input type: f32
     [ INFO ]         input shape: {64, 1, 28, 28}
     [ INFO ]     outputs
     [ INFO ]         output name: output_tensor
     [ INFO ]         output type: f32
     [ INFO ]         output shape: {64, 10}
     [ INFO ] Batch size is 10
     [ INFO ] model name: lenet
     [ INFO ]     inputs
     [ INFO ]         input name: NONE
     [ INFO ]         input type: u8
     [ INFO ]         input shape: {10, 28, 28, 1}
     [ INFO ]     outputs
     [ INFO ]         output name: output_tensor
     [ INFO ]         output type: f32
     [ INFO ]         output shape: {10, 10}
     [ INFO ] Compiling a model for the GPU device
     [ INFO ] Create infer request
     [ INFO ] Combine images in batch and set to input tensor
     [ INFO ] Start sync inference
     [ INFO ] Processing output tensor

     Top 1 results:

     Image 0

     classid probability label
     ------- ----------- -----
     0       1.0000000   0

     Image 1

     classid probability label
     ------- ----------- -----
     1       1.0000000   1

     Image 2

     classid probability label
     ------- ----------- -----
     2       1.0000000   2

     Image 3

     classid probability label
     ------- ----------- -----
     3       1.0000000   3

     Image 4

     classid probability label
     ------- ----------- -----
     4       1.0000000   4

     Image 5

     classid probability label
     ------- ----------- -----
     5       1.0000000   5

     Image 6

     classid probability label
     ------- ----------- -----
     6       1.0000000   6

     Image 7

     classid probability label
     ------- ----------- -----
     7       1.0000000   7

     Image 8

     classid probability label
     ------- ----------- -----
     8       1.0000000   8

     Image 9

     classid probability label
     ------- ----------- -----
     9       1.0000000   9

Additional Resources ####################

  • :doc:Integrate the OpenVINO™ Runtime with Your Application <../../../openvino-workflow/running-inference>
  • :doc:Get Started with Samples <get-started-demos>
  • :doc:Using OpenVINO Samples <../openvino-samples>
  • :doc:Convert a Model <../../../openvino-workflow/model-preparation/convert-model-to-ir>
  • Model Creation Python Sample on Github <https://github.com/openvinotoolkit/openvino/blob/master/samples/python/model_creation_sample/README.md>__
  • Model Creation C++ Sample on Github <https://github.com/openvinotoolkit/openvino/blob/master/samples/cpp/model_creation_sample/README.md>__