Skip to main content

TensorToMask2D

Overview

TensorToMask2D decodes a single spatial channel from a model output tensor into a Mask2D by collecting all pixels whose activation is strictly greater than MaskDecoderParams::threshold.

Header: src/DeepLearning/channel_decoding/TensorToMask2D.hpp
Registry: DecoderFactory::create("TensorToMask2D") — outputTypeName() is "Mask2D"

Dispatched via DecoderDispatch when OutputBindingData::decoder holds MaskDecoderParams.

Parameters

struct MaskDecoderParams {
    float threshold = 0.5f;
};

ParameterUIHints in DecoderParamSchemas.hpp constrain the threshold to (0.01, 1.0].

Tensor layout

The decoder expects a 4D tensor in NCHW order:

Dim Name Indexed by
0 Batch DecoderContext::batch_index
1 Channel DecoderContext::source_channel
2 Height DecoderContext::height
3 Width DecoderContext::width

DecoderContext::height and DecoderContext::width must match tensor.size(2) and tensor.size(3) respectively. SlotAssembler sets these from the output slot shape (last two dimensions).

The selected channel is converted to CPU float32 before thresholding.

Coordinate scaling

When DecoderContext::target_image_size is non-zero, mask coordinates are upsampled from tensor space to the original image size by iterating destination pixels and sampling the nearest source pixel via dl::spatial::discrete_dest_to_source() (SpatialResize), which matches map_dest_to_source() in mask_utils and MaskInspector scaling.

When target_image_size is {0, 0}, mask pixels are returned in tensor pixel coordinates (integer x, y in [0, width) × [0, height)).

Validation

decode() validates at runtime:

  • Tensor is defined and 4-dimensional
  • batch_index and source_channel are in range
  • height and width are positive and match the tensor spatial dimensions

Invalid inputs throw std::invalid_argument or std::out_of_range after logging at spdlog::debug level.

Tests

tests/DeepLearning/channel_decoding/TensorToMask2D.test.cpp — thresholding, empty masks, batch/channel selection, target scaling, validation errors.

Filter with ctest -R "TensorToMask2D".