You can use the CLI to run inference on an image or folder containing several images.
+To do that, simply use the following command:
+spotiflow-predict PATH
+
where PATH can be either an image or a folder.
+By default, the command will use the general pretrained model. You can specify a different model by using the --pretrained-model flag or the --model-dir flag for a model you trained yourself.
+Moreover, spots are saved to a subfolder spotiflow_results created inside the input folder (this can be changed with the --out-dir flag).
+For more information, please refer to the help message of the CLI:
+spotiflow-predict -h
+
See Data format.
+Finetuning a pre-trained model on a custom dataset is very easy. You can load the model very similarly to what you would normally do to predict on new images (you only need to add one extra parameter!):
+from spotiflow.model import Spotiflow
+from spotiflow.utils import get_data
+
+# Get the data
+train_imgs, train_spots, val_imgs, val_spots = get_data("/path/to/spots_data")
+
+# Initialize the model
+model = Spotiflow.from_pretrained(
+ "general",
+ inference_mode=False,
+)
+
+# Train and save the model
+model.fit(
+ train_imgs,
+ train_spots,
+ val_imgs,
+ val_spots,
+ save_dir="/my/trained/model",
+)
+
Of course, you can also fine-tune from a model you have trained before. In that case, use the from_folder()
method instead of from_pretrained()
(see Predicting spots in an image).
+All the information about training customization from Customizing the training applies here as well. However, note that you cannot change the model architecture when fine-tuning!
Spotiflow: accurate and robust spot detection for fluorescence microscopy
+Spotiflow is a learning-based spot detection method for fluorescence microscopy images. For more information, please refer to our paper.
+First, create and activate a new conda environment.
+(base) $ conda create -n spotiflow python=3.9
+(base) $ conda activate spotiflow
+
Then, install Pytorch using conda
/ mamba
. Please follow the official instructions for your system.
As an example, for MacOS:
+(spotiflow) $ conda install pytorch::pytorch torchvision -c pytorch
+
For a linux system with CUDA (note that you should change the CUDA version to match the one installed on your system):
+(spotiflow) $ conda install pytorch torchvision pytorch-cuda=11.8 -c pytorch -c nvidia
+
Finally, install spotiflow
using pip
:
(spotiflow) $ pip install spotiflow
+
The snippet below shows how to retrieve the spots from an image using one of the pretrained models:
+from skimage.io import imread
+from spotiflow.model import Spotiflow
+from spotiflow.utils import write_coords_csv
+
+
+# Load the desired image
+img = imread("/path/to/your/image")
+
+# Load a pretrained model
+model = Spotiflow.from_pretrained("general")
+
+# Predict spots
+spots, details = model.predict(img) # predict expects a numpy array
+
+# spots is a numpy array with shape (n_spots, 2)
+# details contains additional information about the prediction, like the predicted heatmap, the probability per spot, the flow field, etc.
+
+# Save the results to a CSV file
+write_coords_csv(spots, "/path/to/save/spots.csv")
+
If a custom model is used, simply change the model loadings step to:
+# Load a custom model
+model = Spotiflow.from_folder("/path/to/model")
+
The napari plugin can be used to predict spots in a napari viewer. First, you must install it in the environment containing Spotiflow:
+(spotiflow) $ pip install napari-spotiflow
+
The plugin will then be available in the napari GUI under the name “Spotiflow widget”. This is how the GUI looks like:
+ + +The plugin allows running on two modes: for images (2D
) and volumes (3D
), which can be toggled using the corresponding buttons in the GUI. You can also run on movies by setting the appropriate axis order (should be leading with a T).
Upon pressing the button Run
, The plugin will create a Points
layer containing the predicted spots:
If the option Show CNN output
is checked, the plugin will also create two Image
layers containing the heatmap output of the CNN as well as the stereographic flow.
Finally, the plugin includes two sample 2D images (HybISS and Terra) as well as a synthetic 3D volume. These samples can be loaded from the File
menu (File -> Open sample -> napari-spotiflow
). You can try the plugin with these samples to get a better idea of how it works!
First of all, make sure that the data is organized in the following format:
+spots_data
+├── train
+│ ├── img_001.csv
+│ ├── img_001.tif
+| ...
+│ ├── img_XYZ.csv
+| └── img_XYZ.tif
+└── val
+ ├── val_img_001.csv
+ ├── val_img_001.tif
+ ...
+ ├── val_img_XYZ.csv
+ └── val_img_XYZ.tif
+
The actual naming of the files is not important, but the .csv
and .tif
files corresponding to the same image must have the same name! The .csv
files must contain the spot coordinates in the following format:
y,x
+42.3,24.24
+252.99, 307.97
+...
+
The column names can also be axis-0 (instead of y) and axis-1 instead of x. For the 3D case, the format is similar but with an additional column corresponding to the z coordinate:
+z,y,x
+12.4,42.3,24.24
+61.2,252.99, 307.97
+...
+
In this case, you can also use axis-0, axis-1, and axis-2 instead of z, y, and x, respectively.
+You can easily train a model using the default settings as follows and save it to the directory /my/trained/model:
+from spotiflow.model import Spotiflow
+from spotiflow.utils import get_data
+
+# Get the data
+train_imgs, train_spots, val_imgs, val_spots = get_data("/path/to/spots_data")
+
+# Initialize the model
+model = Spotiflow()
+
+# Train and save the model
+model.fit(
+ train_imgs,
+ train_spots,
+ val_imgs,
+ val_spots,
+ save_dir="/my/trained/model",
+)
+
You can then load it by simply calling:
+model = Spotiflow.from_folder("/my/trained/model")
+
In the 3D case, you should initialize a spotiflow.model.config.SpotiflowModelConfig
object and pass it to the Spotiflow constructor with the appropriate parameter set (see other options for the configuration at the end of the section):
You can also pass other parameters relevant for training to the fit method. For example, you can change the number of epochs, the batch size, the learning rate, etc. You can do that using the train_config parameter. For more information on the arguments allowed, see the documentation of spotiflow.model.spotiflow.Spotiflow.fit()
method as well as spotiflow.model.config.SpotiflowTrainingConfig
. As an example, let’s change the number of epochs and the learning rate:
train_config = {
+ "num_epochs": 100,
+ "learning_rate": 0.001,
+ "smart_crop": True,
+ # other parameters
+}
+
+model.fit(
+ train_imgs,
+ train_spots,
+ val_imgs,
+ val_spots,
+ save_dir="/my/trained/model",
+ train_config=train_config,
+ # other parameters
+)
+
In order to change the model architecture (e.g. number of input/output channels, number of layers, variance for the heatmap generation, etc.), you can create a spotiflow.model.config.SpotiflowModelConfig
object and populate it accordingly. Then you can pass it to the Spotiflow constructor (note that this is necessary for 3D). For example, if our image is RGB and we need the network to use 3 input channels, we can do the following:
from spotiflow.model import SpotiflowModelConfig
+
+# Create the model config
+model_config = SpotiflowModelConfig(
+ in_channels=3,
+ # you can pass other arguments here
+)
+model = Spotiflow(model_config)
+