Class: NeuralNet

TM NPU MicroPython

Product
SIMACTIC S7-1500 TM NPU 2.0
Language
en-US
Category
Manual

The NeuralNet class provides an interface to run a neural network with supplied input data.

NeuralNet.run(input_data, results_dict=False)

NeuralNet.run(input_data, results_dict=False) runs the neural network with the given input data, returning the results.

  • input_data can be one of the following:

    • An ExternalBuffer object, where the data is passed in directly to the input tensor without performing a type conversion

    • A dictionary of input tensors

    • A list of values

    • An array of values

    If multiple input data arguments are passed, these will be assigned to the available input tensors in order.

    If a dictionary of input tensors is passed in, it must be in the form:

    • Keys are the names of the input tensors

    • Values as an array, list, or ExternalBuffer as above

    Note:

    input_data must not exceed a frame size defined by the “target_resolution” parameter when initializing the video pipeline (vid_pipeline.init()).

results = net.run({'input': frame.data()}, results_dict=False)

  • results_dict defines whether the inference results are returned as a dictionary or not, and can be either True or False:

    • results_dict = False:

      Results are returned as an array.

      Note:

      Only single output tensors are supported.

    • results_dict = True:

      If results_dict is True, a dictionary is returned instead, with keys being the names of the output tensors, and values being the arrays of the corresponding tensor data.

      Note:

      Be aware that MicroPython evaluates every value to ‘True’, that is not ‘False’, ‘0’ or ‘None’.

NeuralNet.release() frees underlying hardware resources, such as SHAVEs, of a previously initalized NeuralNet object. Only the targeted object is dealt with. The object cannot be used again afterwards. Attempting to use the object after release will result in an “Invalid NN handler” exception being thrown. The object is automatically removed from the memory by the garbage collector. Use this method to release a previously initialized neural network in order to free resources before initializing a new or updated neural network.

Note:

Attempting to release the allocated hardware resources (SHAVEs and hardware accelerators) of an initialized NeuralNet object by using only the standard MicroPython “del” function will not result in the resources being freed. For fully freeing the resources always use “NeuralNet.release()” and “del” in succession (see example below).