ml.utils --- ML 工具¶
ml.utils 模組包含用於機器學習的工具類別與函式。
函式¶
- ml.utils.threshold(scores: ndarray, threshold: float, scale: float, find_max: bool = False, find_max_axis: int = 1) ndarray¶
依量化後的
threshold對scores(int8、uint8、int16 或 uint16 的量化ndarray)進行閾值處理,並傳回一個包含所有通過閾值之索引的ndarray。會測試
scale以判斷反量化後的值為正或負。當scale > 0時傳回scores > threshold的索引;否則傳回scores < threshold的索引。find_max若為 True,會在內部將scores替換為沿著find_max_axis取得最大值(或最小值,視scale而定)的ndarray。find_max_axis是當find_max為 True 時,用以計算最大/最小值縮減的軸。
- ml.utils.quantize(model: ml.Model, value: ndarray, index: int = 0) ndarray¶
將所傳入的
ndarray除以 scale 並加上模型的零點以進行轉換。當模型在index處的輸出 dtype 為浮點數時,會原樣傳回value。model是其輸出量化參數將被使用的模型。value是要量化的ndarray。index用以選擇要對model的哪一個張量輸出進行量化。
- ml.utils.dequantize(model: ml.Model, value: ndarray, index: int = 0) ndarray¶
將所傳入的
ndarray減去零點後再乘以模型的 scale 以進行轉換。當模型在index處的輸出 dtype 為浮點數時,會原樣傳回value。model是其輸出量化參數將被使用的模型。value是要反量化的ndarray。index用以選擇要對model的哪一個張量輸出進行反量化。
- ml.utils.draw_predictions(image: image.Image, boxes: list[tuple[float, float, float, float]], labels: list[str], colors: list[tuple[int, int, int]], scores: list[float] | None = None, format: str = 'pascal_voc', font_width: int = 8, font_height: int = 10, text_color: tuple[int, int, int] = (255, 255, 255)) None¶
在
image上繪製附帶文字標籤的邊界框(或中心點標記)。boxes是一個(x, y, w, h)元組的清單。labels是一個標籤字串的清單,每個邊界框對應一個。colors是一個(r, g, b)元組的清單,每個邊界框對應一個。scores若不為None,則為每個邊界框的信心分數清單。提供此參數時,每個繪製的標籤後方會加上以" %.2f"格式化的分數。format控制如何解讀邊界框座標:"pascal_voc"-- 範圍在0.0到1.0之間的正規化(xmin, ymin, xmax, ymax)。"point"-- 絕對像素(x, y, w, h);會在邊界框中心繪製一個填滿的圓形標記,而非矩形(適用於中心點偵測器)。其他任何值 -- 絕對像素
(x, y, w, h);以矩形繪製。
font_width是標籤中每個字元的寬度(以像素為單位)。font_height是標籤背景的高度(以像素為單位)。text_color是用於標籤文字的(r, g, b)色彩。
- 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¶
在
image上繪製一個包含關鍵點(x, y, ...)值的ndarray。radius是關鍵點圓形的半徑。當radius == 0時,關鍵點會以單一像素繪製。color是關鍵點的(r, g, b)色彩。thickness是圓形外框的線寬。fill若為 True 則填滿關鍵點圓形。
- 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¶
在
image上繪製一個包含關鍵點(x, y, ...)值的ndarray,然後以線段將它們連接起來。lines是一個(kp0_idx, kp1_idx)元組的清單,指定要連接哪些關鍵點配對。kp_radius是關鍵點圓形的半徑(會傳遞給draw_keypoints)。kp_color是關鍵點的(r, g, b)色彩。kp_thickness是關鍵點圓形外框的線寬。kp_fill若為 True 則填滿關鍵點圓形。line_color是線條的(r, g, b)色彩。line_thickness是線條的線寬。
class NMS -- 軟性非極大值抑制(Soft-Non-Maximum Suppression)¶
NMS 物件會收集一份附帶分數的邊界框清單,透過 Soft-NMS 過濾掉分數較低的重疊邊界框,並將在子視窗中偵測到的邊界框重新對應回原始影像座標。
- class ml.utils.NMS(window_w: int, window_h: int, roi: tuple[int, int, int, int])¶
建立一個
NMS物件。window_w與window_h是模型輸入張量/視窗的寬度與高度。roi是模型執行所在之原始影像的(x, y, w, h)感興趣區域(通常由Normalization()物件傳回)。用以將偵測到的邊界框重新對應回原始影像的座標空間。roi[2]與roi[3]必須 >= 1。- add_bounding_box(xmin: float, ymin: float, xmax: float, ymax: float, score: float, label_index: int, keypoints: ndarray | None = None) None¶
將一個邊界框加入
NMS物件。score落在[0.0, 1.0]之外,或經裁切後寬度或高度為零/為負的邊界框會被捨棄。xmin、ymin、xmax、ymax是視窗像素空間中的邊界框座標,裁切至[0, window_w]/[0, window_h]。score是邊界框的信心分數(0.0-1.0)。label_index是與該邊界框關聯之類別標籤的索引。keypoints是與此邊界框關聯之關鍵點(x, y, ...)值的選用ndarray。
- get_bounding_boxes(threshold: float = 0.1, sigma: float = 0.1) list[list[tuple]]¶
對所有已加入的邊界框執行 Soft-NMS,並傳回一份依
label_index索引的、每類別一個清單的清單。每個內層清單包含對應回原始影像座標的((x, y, w, h), score)元組。若加入時提供了keypoints,元組會額外附上重新對應後的keypointsndarray。呼叫此方法後,請建立一個新的
NMS物件以處理新的一組邊界框。threshold是邊界框在 Soft-NMS 抑制後必須保留、方能被保留下來的最低分數。sigma控制用於懲罰重疊邊界框分數的高斯函式。較小的sigma會導致更積極的抑制。sigma <= 0.0會停用高斯懲罰(重疊邊界框的分數不會衰減)。