Upload folder using huggingface_hub
Browse files- PIPELINE.md +3 -3
- README.md +3 -3
- checkpoints/R108_dt_R111_fg.onnx +3 -0
- src/fiberpose/analysis.py +27 -9
- src/fiberpose/models.py +3 -1
- src/fiberpose/pipeline.py +7 -1
- src/fiberpose/resources/config.json +1 -1
- src/fiberpose/resources/fiberpose.png +0 -0
- src/fiberpose/web/demo.py +3 -1
PIPELINE.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
- Covers GUI and CLI startup, segmentation, measurement, and batch execution/export.
|
| 6 |
|
| 7 |
## Runtime Modes and Flags
|
| 8 |
-
- `segmentation_mode=watershed` makes `analysis.segment(...)` run a
|
| 9 |
- `segmentation_mode=radial_expansion` makes `analysis.segment(...)` rasterize the expanded ridge fragments.
|
| 10 |
- `merge_expansion_factor` controls overlap-based ridge-fragment merging (default `0.7`).
|
| 11 |
- `segmentation_expansion_factor` controls radial label expansion (default `1.0`).
|
|
@@ -53,12 +53,12 @@
|
|
| 53 |
- pads to the model stride, runs `OnnxFiberposeModel.predict(...)` -> `(dt, foreground_prob)`; `nout != 2` uses the last channel as DT and has no foreground
|
| 54 |
- smooths DT, computes gradients/laplacian; stored via `Frame.set_prediction_outputs(...)`
|
| 55 |
2. If the frame's segmentation signature (prediction signature + `segmentation_params(config)`) matches, stop.
|
| 56 |
-
3. Otherwise `clear_segmentation()` and run `analysis.segment(dt, dt_u8, gx, gy, foreground_prob, **segmentation_params)`:
|
| 57 |
- `RidgeDetector.detect_ridges(...)` (raw ridge lines)
|
| 58 |
- `_group_detected_polylines_into_segmentation_fragments(...)` merges ridges whose `merge_expansion_factor * DT` disks overlap
|
| 59 |
- `_filter_measurement_centerlines(...)` derives measurement centerlines; the shortest fraction is dropped
|
| 60 |
- `_filter_fragments_by_grad_quality(...)` drops fragments with unreliable DT gradients
|
| 61 |
-
- watershed
|
| 62 |
- foreground, when available, prunes labels; only fragments that keep a label are returned
|
| 63 |
4. Store the labels and fragments via `set_segmentation_outputs(...)`. The pipeline draws no images.
|
| 64 |
|
|
|
|
| 5 |
- Covers GUI and CLI startup, segmentation, measurement, and batch execution/export.
|
| 6 |
|
| 7 |
## Runtime Modes and Flags
|
| 8 |
+
- `segmentation_mode=watershed` makes `analysis.segment(...)` run a watershed inside a fiber mask (`watershed_grad_threshold` sets its gradient part).
|
| 9 |
- `segmentation_mode=radial_expansion` makes `analysis.segment(...)` rasterize the expanded ridge fragments.
|
| 10 |
- `merge_expansion_factor` controls overlap-based ridge-fragment merging (default `0.7`).
|
| 11 |
- `segmentation_expansion_factor` controls radial label expansion (default `1.0`).
|
|
|
|
| 53 |
- pads to the model stride, runs `OnnxFiberposeModel.predict(...)` -> `(dt, foreground_prob)`; `nout != 2` uses the last channel as DT and has no foreground
|
| 54 |
- smooths DT, computes gradients/laplacian; stored via `Frame.set_prediction_outputs(...)`
|
| 55 |
2. If the frame's segmentation signature (prediction signature + `segmentation_params(config)`) matches, stop.
|
| 56 |
+
3. Otherwise `clear_segmentation()` and run `analysis.segment(dt, dt_u8, gx, gy, foreground_prob, laplacian, **segmentation_params)`:
|
| 57 |
- `RidgeDetector.detect_ridges(...)` (raw ridge lines)
|
| 58 |
- `_group_detected_polylines_into_segmentation_fragments(...)` merges ridges whose `merge_expansion_factor * DT` disks overlap
|
| 59 |
- `_filter_measurement_centerlines(...)` derives measurement centerlines; the shortest fraction is dropped
|
| 60 |
- `_filter_fragments_by_grad_quality(...)` drops fragments with unreliable DT gradients
|
| 61 |
+
- watershed builds its mask from the DT gradient magnitude above the threshold or a negative Laplacian (ridge), keeps it only where the smoothed DT is positive, unions two 3x3 dilations of the centerline markers, then applies erode -> dilate; radial_expansion rasterizes the expanded fragments
|
| 62 |
- foreground, when available, prunes labels; only fragments that keep a label are returned
|
| 63 |
4. Store the labels and fragments via `set_segmentation_outputs(...)`. The pipeline draws no images.
|
| 64 |
|
README.md
CHANGED
|
@@ -86,7 +86,7 @@ Running `pip install -e .` again reinstalls the CPU build, so repeat these two c
|
|
| 86 |
|
| 87 |
## Pretrained model
|
| 88 |
|
| 89 |
-
The default model, `
|
| 90 |
|
| 91 |
1. the folder set in the `FIBERPOSE_MODELS_DIR` environment variable;
|
| 92 |
2. `checkpoints/` in the repository, when running from a source checkout;
|
|
@@ -272,7 +272,7 @@ Fiberpose/
|
|
| 272 |
├── app.py # Entry point of the Hugging Face Space
|
| 273 |
├── requirements.txt # Dependencies of the Hugging Face Space
|
| 274 |
├── Dockerfile # Web interface as a container
|
| 275 |
-
├── checkpoints/ # Pretrained
|
| 276 |
├── docs/ # README images
|
| 277 |
└── src/fiberpose/
|
| 278 |
├── cli.py # GUI/CLI/web launcher (`fiberpose` command)
|
|
@@ -304,7 +304,7 @@ Fiberpose/
|
|
| 304 |
|
| 305 |
### Model file not found
|
| 306 |
|
| 307 |
-
The default model is expected in `checkpoints/` of the source checkout. If Fiberpose runs from elsewhere, copy `
|
| 308 |
|
| 309 |
### GPU is not used
|
| 310 |
|
|
|
|
| 86 |
|
| 87 |
## Pretrained model
|
| 88 |
|
| 89 |
+
The default model, `R108_dt_R111_fg.onnx`, ships with the repository in `checkpoints/`, so a source checkout works offline. Fiberpose looks for models, by file name, in this order:
|
| 90 |
|
| 91 |
1. the folder set in the `FIBERPOSE_MODELS_DIR` environment variable;
|
| 92 |
2. `checkpoints/` in the repository, when running from a source checkout;
|
|
|
|
| 272 |
├── app.py # Entry point of the Hugging Face Space
|
| 273 |
├── requirements.txt # Dependencies of the Hugging Face Space
|
| 274 |
├── Dockerfile # Web interface as a container
|
| 275 |
+
├── checkpoints/ # Pretrained models (default: R108_dt_R111_fg.onnx)
|
| 276 |
├── docs/ # README images
|
| 277 |
└── src/fiberpose/
|
| 278 |
├── cli.py # GUI/CLI/web launcher (`fiberpose` command)
|
|
|
|
| 304 |
|
| 305 |
### Model file not found
|
| 306 |
|
| 307 |
+
The default model is expected in `checkpoints/` of the source checkout. If Fiberpose runs from elsewhere, copy `R108_dt_R111_fg.onnx` into a folder and point `FIBERPOSE_MODELS_DIR` at it. For a custom model, check that `model_path` in your configuration is a file name in the models folder or an absolute path.
|
| 308 |
|
| 309 |
### GPU is not used
|
| 310 |
|
checkpoints/R108_dt_R111_fg.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8f89b9161cb454968d36fc7e6ea9c6d770a6dd45a891f0aa308d86e99778c6c0
|
| 3 |
+
size 5610733
|
src/fiberpose/analysis.py
CHANGED
|
@@ -32,6 +32,12 @@ logger = logging.getLogger(__name__)
|
|
| 32 |
|
| 33 |
Sample = tuple[int, int, float, float, float, float]
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# ------------------------------------------------------------------ #
|
| 37 |
# Public pipeline stages
|
|
@@ -45,7 +51,7 @@ def predict(model: OnnxFiberposeModel, image: np.ndarray) -> dict[str, np.ndarra
|
|
| 45 |
dt_pad, fg_pad = model.predict(np.pad(image, pad_width, mode="reflect"))
|
| 46 |
|
| 47 |
dt = np.asarray(dt_pad[:h, :w], dtype=np.float32)
|
| 48 |
-
dt_smooth = gaussian_filter(dt, sigma=
|
| 49 |
gy, gx = np.gradient(dt_smooth)
|
| 50 |
return {
|
| 51 |
"dt": dt,
|
|
@@ -64,6 +70,7 @@ def segment(
|
|
| 64 |
gx: np.ndarray,
|
| 65 |
gy: np.ndarray,
|
| 66 |
foreground_prob: np.ndarray | None,
|
|
|
|
| 67 |
*,
|
| 68 |
segmentation_mode: str,
|
| 69 |
watershed_grad_threshold: float,
|
|
@@ -138,7 +145,7 @@ def segment(
|
|
| 138 |
labels = np.clip(labels_i32, 0, np.iinfo(np.uint16).max).astype(np.uint16, copy=False)
|
| 139 |
else:
|
| 140 |
markers = _render_markers(shape, fragments)
|
| 141 |
-
labels = _watershed(dt_u8, gx, gy, markers, gradient_threshold=watershed_grad_threshold)
|
| 142 |
labels = _prune_labels_by_foreground(
|
| 143 |
labels,
|
| 144 |
foreground_prob=foreground_prob,
|
|
@@ -513,26 +520,35 @@ def _clamp_trim_fraction(value: float) -> float:
|
|
| 513 |
|
| 514 |
|
| 515 |
def _watershed(
|
|
|
|
| 516 |
dt_u8: np.ndarray,
|
| 517 |
gx: np.ndarray,
|
| 518 |
gy: np.ndarray,
|
|
|
|
| 519 |
markers: np.ndarray,
|
| 520 |
gradient_threshold: float,
|
| 521 |
) -> np.ndarray:
|
| 522 |
"""
|
| 523 |
-
Watershed on inverted DT, constrained
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
|
| 525 |
-
Two dilations of the detected centerline markers are added to the
|
| 526 |
-
|
| 527 |
-
watershed.
|
| 528 |
"""
|
| 529 |
|
|
|
|
| 530 |
dt_u8 = np.asarray(dt_u8)
|
| 531 |
gx = np.asarray(gx, dtype=np.float32)
|
| 532 |
gy = np.asarray(gy, dtype=np.float32)
|
|
|
|
| 533 |
markers = np.asarray(markers)
|
| 534 |
-
if
|
| 535 |
-
raise ValueError("DT, gradients, and markers must have matching shapes.")
|
| 536 |
if not markers.any():
|
| 537 |
return np.zeros_like(dt_u8, dtype=np.uint16)
|
| 538 |
|
|
@@ -543,6 +559,8 @@ def _watershed(
|
|
| 543 |
dt_norm = dt_u8.astype(np.float32) / 255.0
|
| 544 |
grad_magnitude = np.hypot(gx, gy)
|
| 545 |
fiber_region_mask = np.isfinite(grad_magnitude) & (grad_magnitude >= threshold)
|
|
|
|
|
|
|
| 546 |
|
| 547 |
morphology_kernel = np.ones((3, 3), dtype=np.uint8)
|
| 548 |
centerline_mask = (markers > 0).astype(np.uint8)
|
|
@@ -569,7 +587,7 @@ def _watershed(
|
|
| 569 |
return np.zeros_like(dt_u8, dtype=np.uint16)
|
| 570 |
marker_pixels = markers > 0
|
| 571 |
logger.info(
|
| 572 |
-
"Watershed
|
| 573 |
threshold,
|
| 574 |
int(np.count_nonzero(fiber_region_mask)),
|
| 575 |
int(fiber_region_mask.size),
|
|
|
|
| 32 |
|
| 33 |
Sample = tuple[int, int, float, float, float, float]
|
| 34 |
|
| 35 |
+
# Smoothing applied to the predicted DT before differentiating it; the watershed
|
| 36 |
+
# mask gates on the same smoothed DT, so both must use this value.
|
| 37 |
+
_DT_SMOOTH_SIGMA = 1.5
|
| 38 |
+
# A fiber centerline is a ridge of the DT: its Laplacian is clearly negative.
|
| 39 |
+
_RIDGE_LAPLACIAN_THRESHOLD = 0.03
|
| 40 |
+
|
| 41 |
|
| 42 |
# ------------------------------------------------------------------ #
|
| 43 |
# Public pipeline stages
|
|
|
|
| 51 |
dt_pad, fg_pad = model.predict(np.pad(image, pad_width, mode="reflect"))
|
| 52 |
|
| 53 |
dt = np.asarray(dt_pad[:h, :w], dtype=np.float32)
|
| 54 |
+
dt_smooth = gaussian_filter(dt, sigma=_DT_SMOOTH_SIGMA)
|
| 55 |
gy, gx = np.gradient(dt_smooth)
|
| 56 |
return {
|
| 57 |
"dt": dt,
|
|
|
|
| 70 |
gx: np.ndarray,
|
| 71 |
gy: np.ndarray,
|
| 72 |
foreground_prob: np.ndarray | None,
|
| 73 |
+
laplacian: np.ndarray,
|
| 74 |
*,
|
| 75 |
segmentation_mode: str,
|
| 76 |
watershed_grad_threshold: float,
|
|
|
|
| 145 |
labels = np.clip(labels_i32, 0, np.iinfo(np.uint16).max).astype(np.uint16, copy=False)
|
| 146 |
else:
|
| 147 |
markers = _render_markers(shape, fragments)
|
| 148 |
+
labels = _watershed(dt, dt_u8, gx, gy, laplacian, markers, gradient_threshold=watershed_grad_threshold)
|
| 149 |
labels = _prune_labels_by_foreground(
|
| 150 |
labels,
|
| 151 |
foreground_prob=foreground_prob,
|
|
|
|
| 520 |
|
| 521 |
|
| 522 |
def _watershed(
|
| 523 |
+
dt: np.ndarray,
|
| 524 |
dt_u8: np.ndarray,
|
| 525 |
gx: np.ndarray,
|
| 526 |
gy: np.ndarray,
|
| 527 |
+
laplacian: np.ndarray,
|
| 528 |
markers: np.ndarray,
|
| 529 |
gradient_threshold: float,
|
| 530 |
) -> np.ndarray:
|
| 531 |
"""
|
| 532 |
+
Watershed on inverted DT, constrained to a fiber mask.
|
| 533 |
+
|
| 534 |
+
The mask is the fiber flanks (gradient magnitude above the threshold) plus
|
| 535 |
+
the ridges between them (negative Laplacian, where the gradient vanishes),
|
| 536 |
+
both limited to where the smoothed DT is positive: the predicted background
|
| 537 |
+
is negative and sloped, so neither test alone tells it from a fiber.
|
| 538 |
|
| 539 |
+
Two dilations of the detected centerline markers are added to the mask,
|
| 540 |
+
which keeps fibers too thin for the smoothed DT to stay positive. The
|
| 541 |
+
combined mask is then eroded and dilated before watershed.
|
| 542 |
"""
|
| 543 |
|
| 544 |
+
dt = np.asarray(dt, dtype=np.float32)
|
| 545 |
dt_u8 = np.asarray(dt_u8)
|
| 546 |
gx = np.asarray(gx, dtype=np.float32)
|
| 547 |
gy = np.asarray(gy, dtype=np.float32)
|
| 548 |
+
laplacian = np.asarray(laplacian, dtype=np.float32)
|
| 549 |
markers = np.asarray(markers)
|
| 550 |
+
if any(arr.shape != dt_u8.shape for arr in (dt, gx, gy, laplacian, markers)):
|
| 551 |
+
raise ValueError("DT, gradients, Laplacian, and markers must have matching shapes.")
|
| 552 |
if not markers.any():
|
| 553 |
return np.zeros_like(dt_u8, dtype=np.uint16)
|
| 554 |
|
|
|
|
| 559 |
dt_norm = dt_u8.astype(np.float32) / 255.0
|
| 560 |
grad_magnitude = np.hypot(gx, gy)
|
| 561 |
fiber_region_mask = np.isfinite(grad_magnitude) & (grad_magnitude >= threshold)
|
| 562 |
+
fiber_region_mask |= laplacian < -_RIDGE_LAPLACIAN_THRESHOLD
|
| 563 |
+
fiber_region_mask &= gaussian_filter(dt, sigma=_DT_SMOOTH_SIGMA) > 0.0
|
| 564 |
|
| 565 |
morphology_kernel = np.ones((3, 3), dtype=np.uint8)
|
| 566 |
centerline_mask = (markers > 0).astype(np.uint8)
|
|
|
|
| 587 |
return np.zeros_like(dt_u8, dtype=np.uint16)
|
| 588 |
marker_pixels = markers > 0
|
| 589 |
logger.info(
|
| 590 |
+
"Watershed fiber mask: threshold=%.4f mask=%d/%d marker_pixels=%d/%d",
|
| 591 |
threshold,
|
| 592 |
int(np.count_nonzero(fiber_region_mask)),
|
| 593 |
int(fiber_region_mask.size),
|
src/fiberpose/models.py
CHANGED
|
@@ -16,7 +16,7 @@ from pathlib import Path
|
|
| 16 |
|
| 17 |
import pooch
|
| 18 |
|
| 19 |
-
DEFAULT_MODEL = "
|
| 20 |
|
| 21 |
# TODO: upload the registered files as assets of this release before publishing.
|
| 22 |
_BASE_URL = "https://github.com/vikba/Fiberpose/releases/download/models-v1/"
|
|
@@ -24,10 +24,12 @@ _BASE_URL = "https://github.com/vikba/Fiberpose/releases/download/models-v1/"
|
|
| 24 |
# Model file -> files to download; external-data files (.onnx.data) must sit
|
| 25 |
# next to their .onnx graph.
|
| 26 |
_MODELS: dict[str, tuple[str, ...]] = {
|
|
|
|
| 27 |
"R108_dt_fg.onnx": ("R108_dt_fg.onnx",),
|
| 28 |
}
|
| 29 |
|
| 30 |
_REGISTRY = {
|
|
|
|
| 31 |
"R108_dt_fg.onnx": "sha256:bb515be44e1daa220f8ba457fb1f9d856a2c3865149f4b6b97e12aefdea37536",
|
| 32 |
}
|
| 33 |
|
|
|
|
| 16 |
|
| 17 |
import pooch
|
| 18 |
|
| 19 |
+
DEFAULT_MODEL = "R108_dt_R111_fg.onnx"
|
| 20 |
|
| 21 |
# TODO: upload the registered files as assets of this release before publishing.
|
| 22 |
_BASE_URL = "https://github.com/vikba/Fiberpose/releases/download/models-v1/"
|
|
|
|
| 24 |
# Model file -> files to download; external-data files (.onnx.data) must sit
|
| 25 |
# next to their .onnx graph.
|
| 26 |
_MODELS: dict[str, tuple[str, ...]] = {
|
| 27 |
+
"R108_dt_R111_fg.onnx": ("R108_dt_R111_fg.onnx",),
|
| 28 |
"R108_dt_fg.onnx": ("R108_dt_fg.onnx",),
|
| 29 |
}
|
| 30 |
|
| 31 |
_REGISTRY = {
|
| 32 |
+
"R108_dt_R111_fg.onnx": "sha256:8f89b9161cb454968d36fc7e6ea9c6d770a6dd45a891f0aa308d86e99778c6c0",
|
| 33 |
"R108_dt_fg.onnx": "sha256:bb515be44e1daa220f8ba457fb1f9d856a2c3865149f4b6b97e12aefdea37536",
|
| 34 |
}
|
| 35 |
|
src/fiberpose/pipeline.py
CHANGED
|
@@ -335,7 +335,13 @@ class PipelineController:
|
|
| 335 |
frame.clear_segmentation()
|
| 336 |
try:
|
| 337 |
labels, fragments = analysis.segment(
|
| 338 |
-
frame.dt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
)
|
| 340 |
except Exception as exc:
|
| 341 |
frame.fail(str(exc))
|
|
|
|
| 335 |
frame.clear_segmentation()
|
| 336 |
try:
|
| 337 |
labels, fragments = analysis.segment(
|
| 338 |
+
frame.dt,
|
| 339 |
+
frame.dt_u8,
|
| 340 |
+
frame.gx,
|
| 341 |
+
frame.gy,
|
| 342 |
+
frame.foreground_prob,
|
| 343 |
+
frame.lapl_img,
|
| 344 |
+
**self.segmentation_params(config),
|
| 345 |
)
|
| 346 |
except Exception as exc:
|
| 347 |
frame.fail(str(exc))
|
src/fiberpose/resources/config.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
{
|
| 2 |
-
"model_path": "
|
| 3 |
"use_gpu": true,
|
| 4 |
"scale_label": "1x",
|
| 5 |
"max_angle_deg": 30.0,
|
|
|
|
| 1 |
{
|
| 2 |
+
"model_path": "R108_dt_R111_fg.onnx",
|
| 3 |
"use_gpu": true,
|
| 4 |
"scale_label": "1x",
|
| 5 |
"max_angle_deg": 30.0,
|
src/fiberpose/resources/fiberpose.png
CHANGED
|
|
src/fiberpose/web/demo.py
CHANGED
|
@@ -59,7 +59,9 @@ def build_demo(config: AppConfig | None = None) -> gr.Blocks:
|
|
| 59 |
"# Fiberpose\nSegmentation and morphometry of fibrous microstructures. Upload a micrograph "
|
| 60 |
f"up to {cb.MAX_SIDE} x {cb.MAX_SIDE} px (or click an example) and press **Analyze**. [Source code](https://github.com/vikba/Fiberpose)"
|
| 61 |
"\n\nIf you use Fiberpose in scientific work, please cite the preprint: "
|
| 62 |
-
"
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
session = gr.State() # this browser tab's analysed image (callbacks.Session)
|
| 65 |
with gr.Row(equal_height=True): # settings, each above the image it applies to
|
|
|
|
| 59 |
"# Fiberpose\nSegmentation and morphometry of fibrous microstructures. Upload a micrograph "
|
| 60 |
f"up to {cb.MAX_SIDE} x {cb.MAX_SIDE} px (or click an example) and press **Analyze**. [Source code](https://github.com/vikba/Fiberpose)"
|
| 61 |
"\n\nIf you use Fiberpose in scientific work, please cite the preprint: "
|
| 62 |
+
"V. A. Balashov, D. S. Antonenko. *Fiberpose: state-of-the-art fiber morphometry and instance "
|
| 63 |
+
"segmentation with a distance-transform neural network.* "
|
| 64 |
+
"[arXiv:2611.22953](https://doi.org/10.48550/arXiv.2611.22953) (2026)."
|
| 65 |
)
|
| 66 |
session = gr.State() # this browser tab's analysed image (callbacks.Session)
|
| 67 |
with gr.Row(equal_height=True): # settings, each above the image it applies to
|