fovi.arch.modal

Modal pooling: each output takes the most common value among its pooling neighbors.

Used by mode="modal" in KNNPoolingLayer and GridPoolingLayer, for label maps such as semantic or instance segmentation, which arithmetic pooling would mix into labels no object has.

fovi.arch.modal.modal_pool(values: Tensor, neighbors: Tensor, visible: Tensor | None = None) → Tensor[source]

Pool (B, C, N_in) values to (B, C, N_out) by majority vote over a neighbor table.

Each output takes the most common full value (all C channels together) among its neighbors, so neighbors [0, 0, 0], [0, 0, 0] and [0, 0, 1] give [0, 0, 0]. Values are compared bitwise and copied from an input, in its dtype, never converted or mixed: integer IDs of any width and colorized label vectors come back exactly.

Ties go to the value whose first occurrence comes earliest in the table’s neighbor order. Excluded neighbors (the table’s pad token, N_in) and, if given, inputs the image does not see do not vote. Outputs without a voting neighbor are zero. The vote runs on the device without a host synchronization, in chunks that bound its scratch memory.

Parameters:
  • values – (B, C, N_in) values of any dtype.

  • neighbors – (k, N_out) input indices of each output’s neighbors in tie-break order; N_in marks an excluded neighbor.

  • visible – Optional (B, N_in) boolean mask of the inputs each image sees.

Returns:

(B, C, N_out) pooled values with the dtype of values.