--- license: bsd-3-clause library_name: safetensors pipeline_tag: image-segmentation tags: - cellpose - cellpose-sam - cell-segmentation - microscopy - bioimage-analysis - safetensors base_model: mouseland/cellpose-sam --- # Cellpose-SAM (cpsam_v2) – safetensors This is the **Cellpose-SAM `cpsam_v2`** model by Pachitariu, Rariden and Stringer (MouseLand), converted from the original PyTorch checkpoint to [safetensors](https://github.com/huggingface/safetensors). The weights are **unchanged**. They are only stored in another file format. I did not retrain or fine-tune anything. The original model is at [mouseland/cellpose-sam](https://huggingface.co/mouseland/cellpose-sam). I made this conversion for [EVAnalyzer](https://github.com/evanalyzer/evanalyzer), which runs Cellpose-SAM through its own Rust implementation and does not need Python or PyTorch. The file is a plain safetensors file, so any other tool that reads safetensors can load it too. ## Files | File | Content | |---|---| | `cpsam_v2.evanet` | the weights as safetensors, f32, about 1.2 GB | `.evanet` is EVAnalyzer's extension for neural network models. The file itself is a standard safetensors file, so you can rename it to `.safetensors` if your tool needs that. ## What changed in the conversion - The tensors keep Cellpose's PyTorch names (`encoder.blocks.0.attn.qkv.weight`, ...) and are stored as f32. The `module.` prefix from DataParallel is removed. - The relative position tables (`rel_pos_h` / `rel_pos_w`) keep SAM's original sizes (27 rows in the windowed blocks, 127 in the global blocks). - `W2` is left out. It is a fixed identity matrix (the pixel shuffle of the output). The script checks that it is the identity before it removes it. - `diam_labels` and `diam_mean` are left out because the network does not use them. - `__metadata__` holds the architecture and the source: - `evanalyzer_arch`: `cellpose_sam` - `arch_version`: `1` - `config`: `{"in_channels":3,"tile_size":256,"patch_size":8,"embed_dim":1024,"depth":24,"num_heads":16,"mlp_dim":4096,"neck_dim":256,"out_channels":3}` - `source`, `license`, `citation` ## Check against the original The conversion script also runs Cellpose's own `CPSAM` module (f32, CPU) on three fixed 256 × 256 test tiles: gray with zero-filled channels, gray copied to all channels, and a single channel. EVAnalyzer's test suite compares its output with these reference outputs. ## Use in Python with Cellpose The file loads into Cellpose's `CPSAM`. You only have to add the identity `W2` again: ```python import torch from safetensors.torch import load_file from cellpose.vit import CPSAM state = load_file("cpsam_v2.evanet") net = CPSAM() w2 = net.state_dict()["W2"] state["W2"] = torch.eye(w2.shape[0]).reshape(w2.shape) net.load_state_dict(state, strict=False) # diam_labels / diam_mean keep their defaults net.eval() ``` ## Input and output - Input: tiles of 256 × 256 pixels with 3 channels. Normalize each image to about 0..1 with Cellpose's `normalize99` (1st and 99th percentile) before you tile it. - Output per tile: 3 channels, `dY`, `dX` (the flow field) and the cell probability as a logit. To get masks, use Cellpose's flow-following post-processing. ## License The original weights are published under **BSD-3-Clause** (Cellpose, © HHMI). The SAM image encoder the model starts from is **Apache-2.0** (© Meta). This conversion keeps these licenses. See the original repositories for the full license texts. **Training data:** the Cellpose README says that the Cellpose models are trained on data that is licensed under **CC-BY-NC**. Check if this matters for your use, especially for commercial use. If you are unsure, ask the Cellpose authors. ## Citation If you use this model, please cite the original work: ```bibtex @article{pachitariu2025cellposesam, title = {Cellpose-SAM: superhuman generalization for cellular segmentation}, author = {Pachitariu, Marius and Rariden, Michael and Stringer, Carsen}, journal = {bioRxiv}, year = {2025}, doi = {10.1101/2025.04.28.651001} } ``` ## Credits All credit for the model goes to the Cellpose authors at [MouseLand](https://github.com/MouseLand/cellpose) and to Meta for [Segment Anything](https://github.com/facebookresearch/segment-anything). This repository is not affiliated with or endorsed by them.