ISAT.segment_any package

Submodules

ISAT.segment_any.gpu_resource module

class ISAT.segment_any.gpu_resource.GPUResource_Thread(gpu_id=0)

Bases: QThread

The thread for monitoring GPU0 resources.

Parameters:

gpu_id (int) – The id of the GPU device to monitor. default 0.

message

int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

Type:

str = …, revision

run(self)

ISAT.segment_any.model_zoo module

ISAT.segment_any.segment_any module

class ISAT.segment_any.segment_any.SegAny(checkpoint, use_bfloat16=True)

Bases: object

Segment Anything model class.

Parameters:
  • checkpoint (str) – checkpoint path.

  • use_bfloat16 (bool) – model dtype is bfloat16. default is True.

checkpoint

checkpoint path.

Type:

str

model_dtype

model dtype. eg: torch.bfloat16, torch.float32

Type:

torch.dtype

model_source

model source. eg: mobile_sam, sam_hq, sam2, sam2.1, …

Type:

str

model_type

model type. eg: vit_b, vit_t, hiera_small, …

Type:

str

predictor

Sam Predictor.

Type:

SamPredictor

release()

Release image data. The predictor is NOT deleted here for the same reason as SegAnyVideo.release() — see its docstring.

set_image(image)

Set image to segment.

Parameters:

image (np.ndarray) – The image to segment.

Return type:

None

reset_image()

Reset image to segment.

Return type:

None

predict_with_point_prompt(input_point, input_label)

Segment with point prompt.

Parameters:
  • input_point (list | np.ndarray) – The input points. [(x1, y1), (x2, y2), …]

  • input_label (list | np.ndarray) – The label of points, support value 0 or 1. [0, 0, …]

Return type:

Tensor

predict_with_box_prompt(box)

Segment with box prompt.

Parameters:

box (list | np.ndarray) – [xmin, ymin, xmax, ymax]

Return type:

Tensor

class ISAT.segment_any.segment_any.SegAnyVideo(checkpoint, use_bfloat16=True)

Bases: object

Segment Anything model class for video segmentation. Only SAM2 or SAM2.1 model.

Parameters:
  • checkpoint (str) – checkpoint path.

  • use_bfloat16 (bool) – model dtype is bfloat16. default is True.

checkpoint

checkpoint path.

Type:

str

model_dtype

model dtype. eg: torch.bfloat16, torch.float32

Type:

torch.dtype

model_source

model source. sam2 or sam2.1 .

Type:

str

model_type

model type. eg: hiera_tiny, hiera_small, hiera_base_plus or hiera_large.

Type:

str

predictor

Sam Predictor.

Type:

SamPredictor

release()

Release inference state to free GPU memory used by video frames and tracking outputs.

IMPORTANT: This method intentionally does NOT delete self.predictor. Deleting a SAM2VideoPredictor (which inherits from nn.Module) triggers a recursive C-level destructor that frees thousands of sub-module parameter tensors while holding the Python GIL. When called from a background thread this starves the main thread’s Qt event loop, making the UI unresponsive (~”freeze”).

Instead, the predictor is left alive and will be freed by Python GC when the SegAnyVideo object itself goes out of scope at the end of InitSegAnyThread.run(). At that point init_sam_finish has already re-enabled the UI, so any brief GIL contention during scope-exit cleanup does not cause a visible freeze.

init_state(image_root, image_name_list, offload_video_to_cpu=True, offload_state_to_cpu=True, async_loading_frames=False)

Init state for video segmentation. Need read all images.

Parameters:
  • image_root (str) – The directory of images.

  • image_name_list (list) – list of image file names to segment.

  • offload_video_to_cpu (bool) – offload frames to CPU.

  • offload_state_to_cpu (bool) – offload state to CPU.

  • async_loading_frames (bool) – asynchronize loading frames.

Return type:

None

reset_state()

Remove all input points or mask in all frames throughout the video.

Return type:

None

add_new_mask(frame_idx, ann_obj_id, mask)

Add new mask prompt for video segmentation.

Parameters:
  • frame_idx (int) – Frame index.

  • ann_obj_id (int) – Object ID.

  • mask (torch.Tensor | np.ndarray) – New mask prompt.

Return type:

None

Module contents