Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation window for avoiding recomputing embeddings #790

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions micro_sam/sam_annotator/_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def __init__(self, viewer: "napari.viewer.Viewer", ndim: int) -> None:
def _update_image(self, segmentation_result=None):
state = AnnotatorState()

# Whether embeddings already exist and avoid clearing objects in layers.
if state._embeddings_are_same:
return

# Update the image shape if it has changed.
if state.image_shape != self._shape:
if len(state.image_shape) != self._ndim:
Expand Down
3 changes: 1 addition & 2 deletions micro_sam/sam_annotator/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ def initialize_predictor(
# Initialize the model if necessary.
if predictor is None:
self.predictor, state = util.get_sam_model(
device=device, model_type=model_type,
checkpoint_path=checkpoint_path, return_state=True
device=device, model_type=model_type, checkpoint_path=checkpoint_path, return_state=True
)
if prefer_decoder and "decoder_state" in state:
self.decoder = get_decoder(
Expand Down
22 changes: 19 additions & 3 deletions micro_sam/sam_annotator/_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,9 @@ def _create_settings_widget(self):
self.device = "auto"
device_options = ["auto"] + util._available_devices()

self.device_dropdown, layout = self._add_choice_param("device", self.device, device_options,
tooltip=get_tooltip("embedding", "device"))
self.device_dropdown, layout = self._add_choice_param(
"device", self.device, device_options, tooltip=get_tooltip("embedding", "device")
)
setting_values.layout().addLayout(layout)

# Create UI for the save path.
Expand Down Expand Up @@ -1062,6 +1063,16 @@ def _validate_inputs(self):
# Otherwise we either don't have an embedding path or it is empty. We can proceed in both cases.
return False

def _validate_existing_embeddings(self, state):
if state.image_embeddings is None:
return False
else:
val_results = {
"message_type": "info",
"message": "Embeddings have already been precomputed. Press OK to recompute the embeddings."
}
return _generate_message(val_results["message_type"], val_results["message"])

def __call__(self, skip_validate=False):
# Validate user inputs.
if not skip_validate and self._validate_inputs():
Expand All @@ -1071,8 +1082,13 @@ def __call__(self, skip_validate=False):
image = self.image_selection.get_value()

# Update the image embeddings:
# Reset the state.
state = AnnotatorState()
if self._validate_existing_embeddings(state):
state._embeddings_are_same = True # Whether embeddings already exist to control existing objects in layers.
return

state._embeddings_are_same = False
# Reset the state.
state.reset_state()

# Get image dimensions.
Expand Down
Loading