The following sections provide guidance on how to use inputs and outputs as intended in selected typical ML use cases.
Processing images
The preferred solution to receive images in your Inference Pipeline is to have an installed Vision Connector and its connected to cameras along with AI Inference Server. Vision Connector is designed to receive images from the cameras and provides those images in 'ImageSet' format for AI Inference Server. This way the images can be received together with its metadata and in a standardized format.
Using Vision Connector with type ImageSet
This format of payload is designed to send the image in any format provided, extended with important metadata.
{
"version": "1", # version of the Metadata format
"count": 1, # Number of images on message
"timestamp": "2024-06-18T13:00:37.189219", # Camera acquisition time
"detail": [ # list of images with detailed information
{
"id": "camera-024581", # unique image identifier.
"timestamp": "1718708437.189219", # Timestamp provided by the camera
"width": 600, # image width
"height": 400, # image height
"format": "BayerRG8", # image format
"metadata": "", # optional extra information on image
"image": image_bytes # image binary with the given 'format'
}
]
}
As observed, the JSON format is capable of encapsulating a collection of images along with related metadata for both the payload and individual images.In this case, the detail[0].image field will be a one-dimensional byte array, regardless of whether it is in a monochrome or RGB format, which is defined in the detail[0].format field of the payload. When working with this payload in your pipeline, the first step is to convert the byte array to the format required by the model or the next step in the pipeline.
Vision use cases often involve GPU Runtime in the AI Inference Server. Therefore, our task is to produce a one-dimensional float array and pass it to the GPURuntime step.
The following example demonstrates a solution using the OpenCV and Numpy libraries.
# build_package.py
# assuming the first step in our Pipeline is 'preprocessing'
# and we define the ImageSet input with the name 'vision_payload'
preprocessing.add_input('vision_payload', 'ImageSet')
[..]# entrypoint.py
# imports ..
WIDTH = HEIGHT = 224 # expected image size
# assuming the image arrives in the payload above with pixel format BayerRG8
def process_input(payload: dict):
# extracting image details from the payload
extracted = payload['vision_payload']
image_detail = extracted["detail"][0]
# extracting image metadata
iuid = image_detail['id']
width = image_detail.get("width", None)
height = image_detail.get("height", None)
if (width, height) != (WIDTH, HEIGHT):
logger.warn("Image is dropped because of unexpected image size")
return None
# reading the byte-array into numpy array with one dimension,
image_data = numpy.frombuffer(image_detail['image'], dtype=numpy.uint8) # BayerRG8, (width x height, )
# forming two-dimensional numpy array with dimensions width and height
image_data = image_data.reshape(width, height) # BayerRG8, (width, height)
# converting from BayerRG8 format to 3-dimensional RGB image
image_data = cv2.cvtColor(image_data, cv2.COLOR_BayerRG2RGB) # RGB, (width, height, 3)
# normalizing into [0,1) range and converting to float32
image_array = image_data.astype(numpy.float32) / 255. # numpy.float32, (width, height, 3)
# flattening the 3 dimensional array and adding to an empty batch
inputs = numpy.array(image_array.ravel()) # numpy.float32, (width x height x 3, )
return {
'input': inputs # assuming that our ML Model has an input with name 'input'
}
Vision Connector supports mainly the standardized GenICam pixel formats, as the most common Mono8, RGB8 formats or BayerXX8 formats to reduce network traffic while the color information is still recorded.
For different pixel format it is also recommended to use the GenICam naming convention as described in section 4.35 of the GenICam_PFNC_2_4.pdf document.
ImageSet as Output
As ImageSet is designed to be received from Vision Connector, this variable type is only allowed as input or between Pipeline steps, but not as output.In this case you need to follow and provide the payload format as described above.For more details, refer to the Vision Connector User Guide.
Other formats
There are other options to use different payload formats to receive images from sources that do not support ImageSet. We recommend a ZMQ connection to receive images, over which you can receive image information in Binary or Object` format.
Processing time series of signals
For time series use cases, the situation is typically as follows. You have several signals, that you want to sample at a regular rate and you want to feed your ML model with a time window of multiple samples at once. For example, your model might be designed to expect a window of 5 samples for 3 variables.
| Time stamp | var1 | var2 | var3 |
|---|---|---|---|
| 9:50:23 | 1.2 | 202 | 25 |
| 9:50:28 | 1.3 | 230 | 5 |
| 9:50:33 | 1.2 | 244 | 18 |
| 9:50:38 | 1.2 | 244 | 18 |
| 9:50:43 | 1.2 | 244 | 18 |
Ideally, each signal is a variable that is read by the Industrial Edge Databus, as configured in the AI Inference Server. Often, the signals from PLCs are captured using the Industrial Edge S7 Connector, which samples these signals from given PLC tags and makes them available on the Databus.
Unfortunately, data points for the signals do not necessarily arrive at a regular rate or synchronously. By default, your Python script is usually called with a single variable, and the others are None. However, the ML model expects an entire matrix of multiple variables in an entire time window.
| Time stamp | var1 | var2 | var3 |
|---|---|---|---|
| 9:50:28 | None | 230 | None |
To ensure the regular rate and the synchronicity of inputs, the AI Inference Server supports inter-signal alignment. You can specify a time interval and receive the inputs for all variables stimulated in that interval. In our example, you specify 5 seconds as the time interval and receive inputs such as:
| Time stamp | var1 | var2 | var3 |
|---|---|---|---|
| 9:50:28 | 1.3 | 230 | 5 |
As there is no guarantee that the data source will deliver a data point in each interval, there is still a possibility that some values are missing and set to None in an input row. However, if the sample rate of inter-signal alignment does not exceed the data rate of the sources, your Python script will mostly provide complete rows of data.
For details of inter-signal alignment, refer to the AI Inference Server user's manual.
Refer to the packaging notebook in the State Identifier end-to-end tutorial and the AI SDK API reference to specify inter-signal alignment to be applied to the input of an ML pipeline when it is packaged for deployment to the AI Inference Server.
However, rows are not yet enough to feed time series ML models when they need data windows consisting of multiple rows. Since the AI Inference Server does not support accumulating windows, the Python script must take over this task. The key point of the server's script interface here is that not all inputs in your Python script result in an output because the ML model can only produce output if the received input has just completed a data window that can be passed to the model to calculate an output.
As described above in Returning the result (Page 35), the script can return None while it accumulates input and the model cannot calculate a value for the output. For reasons of compactness, the following diagram shows this for a window size of two.
This can be even more complex in real life depending on whether the windows are overlapping or not. For a concrete implementation of such accumulation logic, refer to the Python script provided in the State Identifier end-to-end tutorial.
Note that you cannot use parallel component execution if your component relies on building up windows from subsequent data points, as the data points would be distributed to different instances of the component. You can, however, separate the aggregation of data windows and CPU-intensive processing of the windows into a component each and enable parallel execution for the latter only.
Even in this case, there is no guarantee that the processing of the windows finishes in the original sequence of the data. If that is essential, you should supply the windows with a sequence ID in the aggregating component that you pass on to the output of the processing component. That way the consumer of the pipeline can recreate the original sequence.
Processing batch data
In batch use cases, such as the classification of discretely manufactured items, the input data is often available in a single packet on the Databus in a text representation such as a JSON structure or CSV table. For example, data is provided through External Databus as a textual payload. The Industrial Edge Vision Connector also delivers images embedded in a JSON through Databus.
Such data is treated as a single variable of type String, which is passed then to process_input() as a dictionary with a single element. You need to process this input string according to which representation it uses, for example, json.loads() for JSON or a
combination of splitlines() and csv.reader() for CSV.
For a concrete code example that shows how to process a single input variable with a JSON structure, refer to the Image Classification end-to-end tutorial.
One another example is when the data is transferred via PROFINET IO Connector in SIMATIC v1 payload format. In this case you can configure your Pipeline to receive batch input, and process in the entrypoint of the input Pipeline Step as list of payloads. On this purpose, you need to configure the variables of the Step individually, and set inputBatch property of the Step to True:
pipeline_step.batch.inputBatch = TrueIn this example, AI Inference Server is instructed to receive batch of data, package the received data into list of dictionaries, and invoke the process_data(..) method of the entrypoint.