MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / sample_point_labels

Function sample_point_labels

semantic_sam/modules/point_features.py:221–261  ·  view source on GitHub ↗

Sample point labels from ground truth mask given point_coords. Args: instances (list[Instances]): A list of N Instances, where N is the number of images in the batch. So, i_th elememt of the list contains R_i objects and R_1 + ... + R_N is equal to R. The gr

(instances, point_coords)

Source from the content-addressed store, hash-verified

219
220
221def sample_point_labels(instances, point_coords):
222 """
223 Sample point labels from ground truth mask given point_coords.
224
225 Args:
226 instances (list[Instances]): A list of N Instances, where N is the number of images
227 in the batch. So, i_th elememt of the list contains R_i objects and R_1 + ... + R_N is
228 equal to R. The ground-truth gt_masks in each instance will be used to compute labels.
229 points_coords (Tensor): A tensor of shape (R, P, 2), where R is the total number of
230 instances and P is the number of points for each instance. The coordinates are in
231 the absolute image pixel coordinate space, i.e. [0, H] x [0, W].
232
233 Returns:
234 Tensor: A tensor of shape (R, P) that contains the labels of P sampled points.
235 """
236 with torch.no_grad():
237 gt_mask_logits = []
238 point_coords_splits = torch.split(
239 point_coords, [len(instances_per_image) for instances_per_image in instances]
240 )
241 for i, instances_per_image in enumerate(instances):
242 if len(instances_per_image) == 0:
243 continue
244 assert isinstance(
245 instances_per_image.gt_masks, BitMasks
246 ), "Point head works with GT in 'bitmask' format. Set INPUT.MASK_FORMAT to 'bitmask'."
247
248 gt_bit_masks = instances_per_image.gt_masks.tensor
249 h, w = instances_per_image.gt_masks.image_size
250 scale = torch.tensor([w, h], dtype=torch.float, device=gt_bit_masks.device)
251 points_coord_grid_sample_format = point_coords_splits[i] / scale
252 gt_mask_logits.append(
253 point_sample(
254 gt_bit_masks.to(torch.float32).unsqueeze(1),
255 points_coord_grid_sample_format,
256 align_corners=False,
257 ).squeeze(1)
258 )
259
260 point_labels = cat(gt_mask_logits)
261 return point_labels

Callers

nothing calls this directly

Calls 2

point_sampleFunction · 0.85
toMethod · 0.80

Tested by

no test coverage detected