ml.utils — ML Utils
The ml.utils module contains utility classes and functions for machine learning.
Functions
- ml.utils.threshold(scores: ndarray, threshold: float, scale: float, find_max: bool = False, find_max_axis: int = 1) ndarray
Thresholds
scores(a quantizedndarrayof int8, uint8, int16, or uint16) by a quantizedthresholdand returns anndarrayof all indices passing the threshold.scaleis tested to determine if the dequantized values are positive or negative. Whenscale > 0indices wherescores > thresholdare returned; otherwise indices wherescores < thresholdare returned.find_maxif True, replacesscoresinternally with anndarrayof the max (or min, depending onscale) alongfind_max_axis.find_max_axisis the axis along which the max/min reduction is computed whenfind_maxis True.
- ml.utils.quantize(model: ml.Model, value: ndarray, index: int = 0) ndarray
Converts the passed
ndarrayby dividing by the scale and adding the zero point of the model. Returnsvalueunchanged when the model output dtype atindexis float.modelis the model whose output quantization parameters are used.valueis thendarrayto quantize.indexselects which tensor output of themodelto quantize against.
- ml.utils.dequantize(model: ml.Model, value: ndarray, index: int = 0) ndarray
Converts the passed
ndarrayby subtracting the zero point and then multiplying by the scale of the model. Returnsvalueunchanged when the model output dtype atindexis float.modelis the model whose output quantization parameters are used.valueis thendarrayto dequantize.indexselects which tensor output of themodelto dequantize against.
- ml.utils.draw_predictions(image: image.Image, boxes: list[tuple[float, float, float, float]], labels: list[str], colors: list[tuple[int, int, int]], format: str = 'pascal_voc', font_width: int = 8, font_height: int = 10, text_color: tuple[int, int, int] = (255, 255, 255)) None
Draws bounding boxes with text labels onto
image.boxesis a list of(x, y, w, h)tuples.labelsis a list of label strings, one per box.colorsis a list of(r, g, b)tuples, one per box.formatis"pascal_voc"to interpret box values as normalized(xmin, ymin, xmax, ymax)in the range 0.0 to 1.0; any other value treats the box values as absolute pixel(x, y, w, h).font_widthis the width in pixels of each character in the label.font_heightis the height in pixels of the label background.text_coloris the(r, g, b)color used for the label text.
- ml.utils.draw_keypoints(image: image.Image, keypoints: ndarray, radius: int = 4, color: tuple[int, int, int] = (255, 0, 0), thickness: int = 1, fill: bool = False) None
Draws an
ndarrayof keypoint(x, y, ...)values onimage.radiusis the keypoint circle radius. Whenradius == 0the keypoints are drawn as single pixels.coloris the(r, g, b)keypoint color.thicknessis the circle outline thickness.fillif True fills the keypoint circles.
- ml.utils.draw_skeleton(image: image.Image, keypoints: ndarray, lines: list[tuple[int, int]], kp_radius: int = 4, kp_color: tuple[int, int, int] = (255, 0, 0), kp_thickness: int = 1, kp_fill: bool = False, line_color: tuple[int, int, int] = (0, 255, 0), line_thickness: int = 1) None
Draws an
ndarrayof keypoint(x, y, ...)values onimageand then connects them with line segments.linesis a list of(kp0_idx, kp1_idx)tuples specifying which keypoint pairs to connect.kp_radiusis the keypoint circle radius (passed through todraw_keypoints).kp_coloris the(r, g, b)keypoint color.kp_thicknessis the keypoint circle outline thickness.kp_fillif True fills the keypoint circles.line_coloris the(r, g, b)line color.line_thicknessis the line thickness.
class NMS – Soft-Non-Maximum Suppression
The NMS object collects a list of bounding boxes with associated scores, filters
overlapping boxes with lower scores via Soft-NMS, and remaps boxes detected in a
sub-window back to the original image coordinates.
- class ml.utils.NMS(window_w: int, window_h: int, roi: tuple[int, int, int, int])
Creates a
NMSobject.window_wandwindow_hare the width and height of the model input tensor / window.roiis the(x, y, w, h)region of interest of the original image that the model was run on (typically returned by theNormalization()object). Used to remap detected boxes back into the original image coordinate space.roi[2]androi[3]must be >= 1.- add_bounding_box(xmin: float, ymin: float, xmax: float, ymax: float, score: float, label_index: int, keypoints: ndarray | None = None) None
Adds a bounding box to the
NMSobject. Boxes withscoreoutside[0.0, 1.0]or with zero/negative width or height after clipping are discarded.xmin,ymin,xmax,ymaxare the bounding box coordinates in window pixel space, clipped to[0, window_w]/[0, window_h].scoreis the confidence score of the bounding box (0.0-1.0).label_indexis the index of the class label associated with the bounding box.keypointsis an optionalndarrayof keypoint(x, y, ...)values associated with this bounding box.
- get_bounding_boxes(threshold: float = 0.1, sigma: float = 0.1) list[list[tuple]]
Performs Soft-NMS over all added boxes and returns a list of per-class lists, indexed by
label_index. Each inner list contains tuples of((x, y, w, h), score)mapped back into the original image coordinates. Ifkeypointswas provided when adding, the tuple is extended with the remappedkeypointsndarray.After calling this method create a new
NMSobject to process a new set of bounding boxes.thresholdis the minimum score a box must retain after Soft-NMS suppression to be kept.sigmacontrols the Gaussian used to penalize the scores of overlapping bounding boxes. A smallersigmaresults in more aggressive suppression.sigma <= 0.0disables the Gaussian penalty (scores of overlapping boxes are not decayed).