def compute_ignore_margin_objectness_targets(targets: np.ndarray, boxes: np.ndarray, weights: np.ndarray, pos_weights: float, num_outputs: int) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) fm_h, fm_w = targets.shape[:2] targets[:, :, num_outputs] = 1 for y, x, w in zip(y_center, x_center, weights): yi = int(y * fm_h) xi = int(x * fm_w) targets[yi, xi, :num_outputs] = 1 targets[yi, xi, num_outputs] = pos_weights * w for y, x, w in zip(y_center, x_center, weights): yi = int(y * fm_h) xi = int(x * fm_w) for j in [yi - 1, yi, yi + 1]: for i in [xi - 1, xi, xi + 1]: if 0 <= j < fm_h and 0 <= i < fm_w: if targets[j, i, 0] != 1: targets[j, i, num_outputs] = 0 return targets
def compute_multiclass_targets( classes_map: np.ndarray, boxes: np.ndarray, weights: np.ndarray, labels: np.ndarray, add_dustbin: bool, dustbin_index: int, weights_index: int, ) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) fm_height, fm_width = classes_map.shape[:2] classes_map[:, :, weights_index] = 1 if add_dustbin: classes_map[:, :, dustbin_index] = 1 for y, x, w, label_index in zip(y_center, x_center, weights, labels): yi = int(y * fm_height) xi = int(x * fm_width) classes_map[yi, xi, label_index] = 1.0 classes_map[yi, xi, weights_index] = w if add_dustbin: classes_map[yi, xi, dustbin_index] = 0.0 return classes_map
def compute_box_offset_targets(offset_map: np.ndarray, boxes: np.ndarray) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) fm_height, fm_width = offset_map.shape[:2] for k, (y, x) in enumerate(zip(y_center, x_center)): yi = int(y * fm_height) xi = int(x * fm_width) offset_map[yi, xi, 0] = y * fm_height - yi offset_map[yi, xi, 1] = x * fm_width - xi offset_map[yi, xi, 2] = 1 return offset_map
def compute_box_size_targets(hw_map: np.ndarray, boxes: np.ndarray) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) heights, widths = np_frame_ops.boxes_heights_widths(boxes) fm_height, fm_width = hw_map.shape[:2] for k, (y, x) in enumerate(zip(y_center, x_center)): yi = int(y * fm_height) xi = int(x * fm_width) hw_map[yi, xi, 0] = heights[k] * fm_height hw_map[yi, xi, 1] = widths[k] * fm_width hw_map[yi, xi, 2] = 1 return hw_map
def compute_objectness_targets(targets: np.ndarray, boxes: np.ndarray, weights: np.ndarray, pos_weights: float, num_outputs: int) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) fm_height, fm_width = targets.shape[:2] targets[:, :, num_outputs] = 1 for y, x, w in zip(y_center, x_center, weights): yi = int(y * fm_height) xi = int(x * fm_width) targets[yi, xi, :num_outputs] = 1 targets[yi, xi, num_outputs] = pos_weights * w return targets
def compute_mean_box_size_targets(hw_map: np.ndarray, boxes: np.ndarray) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) heights, widths = np_frame_ops.boxes_heights_widths(boxes) fm_height, fm_width = hw_map.shape[:2] for k, (y, x) in enumerate(zip(y_center, x_center)): yi = int(y * fm_height) xi = int(x * fm_width) hw_map[yi, xi, 0] += heights[k] * fm_height hw_map[yi, xi, 1] += widths[k] * fm_width hw_map[yi, xi, 2] += 1 hw_map[:, :, :2] = hw_map[:, :, :2] / (hw_map[:, :, 2:] + 1e-6) # make weights in range (0, 1) hw_map[:, :, 2] = np.minimum(hw_map[:, :, 2], 1) return hw_map
def compute_smooth_objectness_targets(targets: np.ndarray, boxes: np.ndarray, weights: np.ndarray, pos_weights: float, num_outputs: int) -> np.ndarray: y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes) fm_h, fm_w = targets.shape[:2] targets[:, :, num_outputs] = 1 for y, x, w in zip(y_center, x_center, weights): yi = int(y * fm_h) xi = int(x * fm_w) for j in [yi - 1, yi, yi + 1]: for i in [xi - 1, xi, xi + 1]: y_offset = y * fm_h - j x_offset = x * fm_w - i if 0 <= j < fm_h and 0 <= i < fm_w: delta = -0.5 * (y_offset**2 + x_offset**2) targets[j, i, :num_outputs] = np.exp(delta) targets[j, i, num_outputs] = pos_weights * w return targets