Skip to content

Commit

Permalink
Merge pull request #516 from computational-cell-analytics/dev
Browse files Browse the repository at this point in the history
Merge dev in preparation for new release
  • Loading branch information
constantinpape authored Apr 21, 2024
2 parents 3c2399d + e7fb2a8 commit c85525a
Show file tree
Hide file tree
Showing 234 changed files with 19,091 additions and 3,513 deletions.
36 changes: 21 additions & 15 deletions .github/workflows/build_installers.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: build_installers

on:
push:
tags:
- '**'
workflow_dispatch:

jobs:
build_intaller:
Expand All @@ -13,15 +11,20 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
os: [windows-latest, ubuntu-latest] # macos-latest

env:
PREPARE_SCRIPT: |
cd deployment
conda install -y -c conda-forge constructor
conda install -y -c conda-forge ruamel.yaml
conda install -y -c conda-forge mamba
mamba env create --file=env.yaml
mamba install -y -c conda-forge constructor
mamba install -y -c conda-forge ruamel.yaml
mamba env create --file=../environment_cpu.yaml -n __MICROSAM_BUILD_ENV__
mamba activate __MICROSAM_BUILD_ENV__
# TODO get the current version here and use it for pinning or enable passing this from dispatch
mamba install -c conda-forge micro_sam
mamba activate base
RUN_SCRIPT: |
python version_getter.py
mkdir ./${{ matrix.os }}_x86_64
Expand All @@ -30,44 +33,47 @@ jobs:
steps:
- name: checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: setup conda
if: matrix.os == 'windows-latest' || matrix.os == 'ubuntu-latest'
if: matrix.os == "windows-latest" || matrix.os == "ubuntu-latest"
uses: conda-incubator/setup-miniconda@v2
with:
miniconda-version: "latest"
auto-activate-base: true
activate-environment: ""
mamba-version: "*"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true

- name: build ${{ matrix.os }}_x86_64
if: matrix.os == 'windows-latest'
if: matrix.os == "windows-latest"
shell: pwsh
run: |
${{ env.PREPARE_SCRIPT }}
conda activate __MICROSAM_BUILD_ENV__
conda install -c anaconda menuinst
mamba activate sam
mamba install -c anaconda menuinst
python windows_menu_setup.py
conda activate base
${{ env.RUN_SCRIPT }}
- name: build ${{ matrix.os }}_x86_64
if: matrix.os == 'ubuntu-latest'
if: matrix.os == "ubuntu-latest"
shell: bash -el {0}
run: |
${{ env.PREPARE_SCRIPT }}
${{ env.RUN_SCRIPT }}
- name: build ${{ matrix.os }}_x86_64_step1
if: matrix.os == 'macos-latest'
if: matrix.os == "macos-latest"
shell: bash -el {0}
run: |
brew install micromamba
/usr/local/opt/micromamba/bin/micromamba shell init -s bash -p ~/micromamba
- name: build ${{ matrix.os }}_x86_64_step2
if: matrix.os == 'macos-latest'
if: matrix.os == "macos-latest"
shell: bash -el {0}
run: |
cd deployment
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Torch-em stuff
checkpoints/
logs/
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
[![codecov](https://codecov.io/gh/computational-cell-analytics/micro-sam/graph/badge.svg?token=7ETPP5CABP)](https://codecov.io/gh/computational-cell-analytics/micro-sam)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.7919746.svg)](https://doi.org/10.5281/zenodo.7919746)

# SegmentAnything for Microscopy
# Segment Anything for Microscopy

Tools for segmentation and tracking in microscopy build on top of [SegmentAnything](https://segment-anything.com/).
**Attention: We are currently updating our software to a new release that will improve it and introduce new features. The documentation is not up-to-date with these changes yet, we will update it as soon as possible!**

Tools for segmentation and tracking in microscopy build on top of [Segment Anything](https://segment-anything.com/).
Segment and track objects in microscopy images interactively with a few clicks!

We implement napari applications for:
Expand All @@ -22,21 +24,16 @@ If you run into any problems or have questions regarding our tool please open an

## Installation and Usage

You can install `micro_sam` via conda:
```
conda install -c conda-forge micro_sam napari pyqt
```
You can then start the `micro_sam` tools by running `$ micro_sam.annotator` in the command line.
Please check [the documentation](https://computational-cell-analytics.github.io/micro-sam/) for details on how to install and use `micro_sam`. You can also find a quickstart guide in [this video](TODO) and find all video tutorials [here](https://www.youtube.com/watch?v=ket7bDUP9tI&list=PLwYZXQJ3f36GQPpKCrSbHjGiH39X4XjSO&pp=gAQBiAQB).

For an introduction in how to use the napari based annotation tools check out [the video tutorials](https://www.youtube.com/watch?v=ket7bDUP9tI&list=PLwYZXQJ3f36GQPpKCrSbHjGiH39X4XjSO&pp=gAQBiAQB).
Please check out [the documentation](https://computational-cell-analytics.github.io/micro-sam/) for more details on the installation and usage of `micro_sam`.

## Contributing

We welcome new contributions!

If you are interested in contributing to micro-sam, please see the [contributing guide](doc/contributing.md) and [developer documentation](doc/development.md). The first step is to [discuss your idea in a new issue](https://github.com/computational-cell-analytics/micro-sam/issues/new) with the current developers.


## Citation

If you are using this repository in your research please cite
Expand Down
26 changes: 13 additions & 13 deletions deployment/construct.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: micro_sam
version: 0.1.0
license_file: ../LICENSE
installer_type: pkg #[osx] # This will trigger pkg build on Mac Os. On windows and linux, native build will be done and this has no effect.
environment: __MICROSAM_BUILD_ENV__
welcome_image: ../doc/images/micro-sam-logo.png
header_image: ../doc/images/micro-sam-logo.png
icon_image: ../doc/images/micro-sam-logo.png
channels:
- conda-forge
welcome_text: Install Segment Anything for Microscopy.
conclusion_text: Segment Anything for Microscopy has been installed.
initialize_by_default: false
name: micro_sam
version: 0.0.1
license_file: ../LICENSE
installer_type: pkg #[osx] # This will trigger pkg build on Mac Os. On windows and linux, native build will be done and this has no effect.
environment: __MICROSAM_BUILD_ENV__
welcome_image: ../doc/images/micro-sam-logo.png
header_image: ../doc/images/micro-sam-logo.png
icon_image: ../doc/images/micro-sam-logo.png
channels:
- conda-forge
welcome_text: Install Segment Anything for Microscopy.
conclusion_text: Segment Anything for Microscopy has been installed.
initialize_by_default: false
4 changes: 2 additions & 2 deletions deployment/version_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
yaml.preserve_quotes = True
ctor_fname = os.path.join("construct.yaml")

with open(ctor_fname, 'r') as stream:
with open(ctor_fname, "r") as stream:
ctor_conf = yaml.load(stream)

ctor_conf["version"] = runpy.run_path(os.path.join("..", "micro_sam", "__version__.py"))["__version__"]

with open(ctor_fname, 'w') as outfile:
with open(ctor_fname, "w") as outfile:
yaml.dump(ctor_conf, outfile)
2 changes: 1 addition & 1 deletion deployment/windows_menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"menu_items":
[
{
"script": "${PREFIX}/Scripts/micro_sam.annotator.exe",
"script": "${PREFIX}/Scripts/napari.exe",
"name": "micro_sam",
"icon": "${PREFIX}/Menu/micro-sam-logo.ico",
"workdir": "${PREFIX}",
Expand Down
103 changes: 103 additions & 0 deletions development/annotators_in_image_series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import os

import h5py
from math import ceil

from micro_sam.sam_annotator import image_series_annotator, annotator_3d


DATA_ROOT = "/home/anwai/data/lucchi/lucchi_test.h5"
EMBEDDING_ROOT = "/home/anwai/embeddiings/test/"
OUTPUT_ROOT = "/home/anwai/data/lucchi/outputs/"


def _get_volume(volume_path):
"""Getting the lucchi test volume"""
with h5py.File(volume_path, "r") as f:
raw = f["raw"][:]
labels = f["labels"][:]

return raw, labels


def segment_volume(input_volume, embedding_path):
"""Load the entire volume in the tool for segmentation.
"""
assert input_volume.ndim == 3
annotator_3d(
image=input_volume,
embedding_path=embedding_path,
model_type="vit_b_em_organelles",
tile_shape=None,
halo=None,
)


def segment_each_slice(input_volume, embedding_dir, output_folder):
"""Load each slice from the volume one-by-one in the tool for segmentation.
"""
assert input_volume.ndim == 3

all_slices = [each_slice for each_slice in input_volume]
image_series_annotator(
images=all_slices,
output_folder=output_folder,
model_type="vit_b_em_organelles",
embedding_path=embedding_dir,
tile_shape=None,
halo=None,
)


def segment_each_n_slices(z_batch, input_volume, embedding_dir, output_folder):
"""Load n slices from the volume one-by-one in the tool for segmentation.
"""
assert input_volume.ndim == 3

n_z_slices = input_volume.shape[0]
all_z_idxx = int(ceil(n_z_slices / z_batch))

all_per_n_slices_volumes = []
for z_id in range(all_z_idxx):
z_start = z_id * z_batch
z_stop = min((z_id + 1) * z_batch, n_z_slices)

batch_volume = input_volume[z_start: z_stop]
all_per_n_slices_volumes.append(batch_volume)

print(f"We split the volume into {len(all_per_n_slices_volumes)} sub-volumes.")
image_series_annotator(
images=all_per_n_slices_volumes,
output_folder=output_folder,
model_type="vit_b_em_organelles",
embedding_path=embedding_dir,
tile_shape=None,
halo=None,
is_volumetric=True,
)


def main():
raw, _ = _get_volume(DATA_ROOT)

# segment_volume(
# input_volume=raw,
# embedding_path=os.path.join(EMBEDDING_ROOT, "lucchi_3d_volume")
# )

# segment_each_slice(
# input_volume=raw,
# embedding_dir=os.path.join(EMBEDDING_ROOT, "lucchi_2d_per_slice"),
# output_folder=os.path.join(OUTPUT_ROOT, "per_slice_segmentation")
# )

segment_each_n_slices(
z_batch=15,
input_volume=raw,
embedding_dir=os.path.join(EMBEDDING_ROOT, "lucchi_3d_per_n_slices"),
output_folder=os.path.join(OUTPUT_ROOT, "per_n_slices")
)


if __name__ == "__main__":
main()
31 changes: 0 additions & 31 deletions development/seg_with_decoder.py

This file was deleted.

17 changes: 17 additions & 0 deletions doc/bioimageio/em_organelles_v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Segment Anything for Electron Microscopy

This is a [Segment Anything](https://segment-anything.com/) model that was specialized for segmenting mitochondria and nuclei in electron microscopy with [micro_sam](https://github.com/computational-cell-analytics/micro-sam).
This model uses a %s vision transformer as image encoder.

Segment Anything is a model for interactive and automatic instance segmentation.
We improve it for light microscopy by finetuning on a large and diverse microscopy dataset.
It should perform well for segmenting mitochondria and nuclei in electron microscopy. It can also work well for other organelles, but was not explicitly trained for this purpose. You may get better results for other organelles (e.g. ER or Golgi) with the default Segment Anything models.

See [the dataset overview](https://github.com/computational-cell-analytics/micro-sam/blob/master/doc/datasets/em_organelles_v%i.md) for further informations on the training data and the [micro_sam documentation](https://computational-cell-analytics.github.io/micro-sam/micro_sam.html) for details on how to use the model for interactive and automatic segmentation.


## Validation

The easiest way to validate the model is to visually check the segmentation quality for your data.
If you have annotations you can use for validation you can also quantitative validation, see [here for details](https://github.com/computational-cell-analytics/micro-sam/blob/master/doc/bioimageio/validation.md).
Please note that the required quality for segmentation always depends on the analysis task you want to solve.
Loading

0 comments on commit c85525a

Please sign in to comment.