MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / transform_coords

Method transform_coords

sam2_train/utils/transforms.py:44–62  ·  view source on GitHub ↗

Expects a torch tensor with length 2 in the last dimension. The coordinates can be in absolute image or normalized coordinates, If the coords are in absolute image coordinates, normalize should be set to True and original image size is required. Returns Un-norma

(
        self, coords: torch.Tensor, normalize=False, orig_hw=None
    )

Source from the content-addressed store, hash-verified

42 return img_batch
43
44 def transform_coords(
45 self, coords: torch.Tensor, normalize=False, orig_hw=None
46 ) -> torch.Tensor:
47 """
48 Expects a torch tensor with length 2 in the last dimension. The coordinates can be in absolute image or normalized coordinates,
49 If the coords are in absolute image coordinates, normalize should be set to True and original image size is required.
50
51 Returns
52 Un-normalized coordinates in the range of [0, 1] which is expected by the SAM2 model.
53 """
54 if normalize:
55 assert orig_hw is not None
56 h, w = orig_hw
57 coords = coords.clone()
58 coords[..., 0] = coords[..., 0] / w
59 coords[..., 1] = coords[..., 1] / h
60
61 coords = coords * self.resolution # unnormalize coords
62 return coords
63
64 def transform_boxes(
65 self, boxes: torch.Tensor, normalize=False, orig_hw=None

Callers 3

transform_boxesMethod · 0.95
_process_batchMethod · 0.80
_prep_promptsMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected