|
| 1 | +import copy |
| 2 | + |
| 3 | +import napari |
| 4 | +from napari.utils.notifications import show_info |
| 5 | +from qtpy.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QComboBox |
| 6 | +from skimage.feature import peak_local_max |
| 7 | +from torch_em.util.prediction import predict_with_halo |
| 8 | + |
1 | 9 | from .base_widget import BaseWidget |
| 10 | +from .util import _load_custom_model, _available_devices, _get_current_tiling |
| 11 | +from ..model_utils import get_model, get_model_registry, get_device, get_default_tiling |
| 12 | + |
| 13 | + |
| 14 | +def _run_detection(image, model, model_type, tiling, device): |
| 15 | + block_shape = [tiling["tile"][ax] for ax in "zyx"] |
| 16 | + halo = [tiling["halo"][ax] for ax in "zyx"] |
| 17 | + prediction = predict_with_halo( |
| 18 | + image, model, gpu_ids=[device], block_shape=block_shape, halo=halo, |
| 19 | + tqdm_desc="Run prediction" |
| 20 | + ).squeeze() |
| 21 | + detections = peak_local_max(prediction, min_distance=2, threshold_abs=0.5) |
| 22 | + return detections |
| 23 | + |
| 24 | + |
| 25 | +class DetectionWidget(BaseWidget): |
| 26 | + def __init__(self): |
| 27 | + super().__init__() |
| 28 | + |
| 29 | + self.viewer = napari.current_viewer() |
| 30 | + layout = QVBoxLayout() |
| 31 | + self.tiling = {} |
| 32 | + |
| 33 | + # Create the image selection dropdown. |
| 34 | + self.image_selector_name = "Image data" |
| 35 | + self.image_selector_widget = self._create_layer_selector(self.image_selector_name, layer_type="Image") |
| 36 | + |
| 37 | + # Create buttons and widgets. |
| 38 | + self.predict_button = QPushButton("Run Detection") |
| 39 | + self.predict_button.clicked.connect(self.on_predict) |
| 40 | + self.model_selector_widget = self.load_model_widget() |
| 41 | + self.settings = self._create_settings_widget() |
| 42 | + |
| 43 | + # Add the widgets to the layout. |
| 44 | + layout.addWidget(self.image_selector_widget) |
| 45 | + layout.addWidget(self.model_selector_widget) |
| 46 | + layout.addWidget(self.settings) |
| 47 | + layout.addWidget(self.predict_button) |
| 48 | + |
| 49 | + self.setLayout(layout) |
| 50 | + |
| 51 | + def load_model_widget(self): |
| 52 | + model_widget = QWidget() |
| 53 | + title_label = QLabel("Select Model:") |
| 54 | + |
| 55 | + model_list = list(get_model_registry().urls.keys()) |
| 56 | + |
| 57 | + # Exclude the detection models. |
| 58 | + segmentation_models = ["Synapses"] |
| 59 | + model_list = [name for name in model_list if name in segmentation_models] |
| 60 | + |
| 61 | + models = ["- choose -"] + model_list |
| 62 | + self.model_selector = QComboBox() |
| 63 | + self.model_selector.addItems(models) |
| 64 | + # Create a layout and add the title label and combo box |
| 65 | + layout = QVBoxLayout() |
| 66 | + layout.addWidget(title_label) |
| 67 | + layout.addWidget(self.model_selector) |
| 68 | + |
| 69 | + # Set layout on the model widget |
| 70 | + model_widget.setLayout(layout) |
| 71 | + return model_widget |
| 72 | + |
| 73 | + def on_predict(self): |
| 74 | + # Get the model and postprocessing settings. |
| 75 | + model_type = self.model_selector.currentText() |
| 76 | + custom_model_path = self.checkpoint_param.text() |
| 77 | + if model_type == "- choose -" and custom_model_path is None: |
| 78 | + show_info("INFO: Please choose a model.") |
| 79 | + return |
| 80 | + |
| 81 | + device = get_device(self.device_dropdown.currentText()) |
| 82 | + |
| 83 | + # Load the model. Override if user chose custom model |
| 84 | + if custom_model_path: |
| 85 | + model = _load_custom_model(custom_model_path, device) |
| 86 | + if model: |
| 87 | + show_info(f"INFO: Using custom model from path: {custom_model_path}") |
| 88 | + model_type = "custom" |
| 89 | + else: |
| 90 | + show_info(f"ERROR: Failed to load custom model from path: {custom_model_path}") |
| 91 | + return |
| 92 | + else: |
| 93 | + model = get_model(model_type, device) |
| 94 | + |
| 95 | + # Get the image data. |
| 96 | + image = self._get_layer_selector_data(self.image_selector_name) |
| 97 | + if image is None: |
| 98 | + show_info("INFO: Please choose an image.") |
| 99 | + return |
| 100 | + |
| 101 | + # Get the current tiling. |
| 102 | + self.tiling = _get_current_tiling(self.tiling, self.default_tiling, model_type) |
| 103 | + # TODO extra segmentation for filtering. |
| 104 | + detections = _run_detection(image, model=model, model_type=model_type, tiling=self.tiling, device=device) |
| 105 | + |
| 106 | + self.viewer.add_points(detections, name=model_type) |
| 107 | + show_info(f"INFO: Detection of {model_type} added to layers.") |
| 108 | + |
| 109 | + def _create_settings_widget(self): |
| 110 | + setting_values = QWidget() |
| 111 | + # setting_values.setToolTip(get_tooltip("embedding", "settings")) |
| 112 | + setting_values.setLayout(QVBoxLayout()) |
| 113 | + |
| 114 | + # Create UI for the device. |
| 115 | + device = "auto" |
| 116 | + device_options = ["auto"] + _available_devices() |
| 117 | + |
| 118 | + self.device_dropdown, layout = self._add_choice_param("device", device, device_options) |
| 119 | + setting_values.layout().addLayout(layout) |
| 120 | + |
| 121 | + # Create UI for the tile shape. |
| 122 | + self.default_tiling = get_default_tiling() |
| 123 | + self.tiling = copy.deepcopy(self.default_tiling) |
| 124 | + self.tiling["tile"]["x"], self.tiling["tile"]["y"], self.tiling["tile"]["z"], layout = self._add_shape_param( |
| 125 | + ("tile_x", "tile_y", "tile_z"), |
| 126 | + (self.default_tiling["tile"]["x"], self.default_tiling["tile"]["y"], self.default_tiling["tile"]["z"]), |
| 127 | + min_val=0, max_val=2048, step=16, |
| 128 | + # tooltip=get_tooltip("embedding", "tiling") |
| 129 | + ) |
| 130 | + setting_values.layout().addLayout(layout) |
| 131 | + |
| 132 | + # Create UI for the halo. |
| 133 | + self.tiling["halo"]["x"], self.tiling["halo"]["y"], self.tiling["halo"]["z"], layout = self._add_shape_param( |
| 134 | + ("halo_x", "halo_y", "halo_z"), |
| 135 | + (self.default_tiling["halo"]["x"], self.default_tiling["halo"]["y"], self.default_tiling["halo"]["z"]), |
| 136 | + min_val=0, max_val=512, |
| 137 | + ) |
| 138 | + setting_values.layout().addLayout(layout) |
2 | 139 |
|
| 140 | + self.checkpoint_param, layout = self._add_string_param( |
| 141 | + name="checkpoint", value="", title="Load Custom Model", |
| 142 | + placeholder="path/to/checkpoint.pt", |
| 143 | + ) |
| 144 | + setting_values.layout().addLayout(layout) |
3 | 145 |
|
4 | | -class SegmentationWidget(BaseWidget): |
5 | | - pass |
| 146 | + settings = self._make_collapsible(widget=setting_values, title="Advanced Settings") |
| 147 | + return settings |
0 commit comments