Skip to content

Commit faf1d4c

Browse files
authored
Merge pull request #8 from imglib/newcpsammodels
Newcpsammodels
2 parents e9ce1dc + c343684 commit faf1d4c

3 files changed

Lines changed: 97 additions & 4 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*-
2+
* #%L
3+
* Running Cellpose 3 and 4 from Java with Appose, using ImgLib2 data structure.
4+
* %%
5+
* Copyright (C) 2026 Appose developpers
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without modification,
8+
* are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice, this
11+
* list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* 3. Neither the name of the ImgLib2 nor the names of its contributors
18+
* may be used to endorse or promote products derived from this software without
19+
* specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24+
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30+
* OF THE POSSIBILITY OF SUCH DAMAGE.
31+
* #L%
32+
*/
33+
package net.imglib2.cellpose;
34+
35+
public enum Cellpose4BuiltinModels
36+
{
37+
// General models
38+
CPSAM( "cpsam", "Cellpose-SAM model. General model, works well on most data." ),
39+
CPSAMV2( "cpsam_v2", "Cellpose-SAM model released in June 2026. It handles better low contrast regions" );
40+
//CPDINO( "cpdino", "CellposeDINO model, based on DINO backbone. For RGB images."),
41+
//CPDINOVITB( "cpdino-vitb", "CellposeDINO smaller model, based on DINO backbone. For RGB images.");
42+
43+
private final String name;
44+
45+
private final String description;
46+
47+
Cellpose4BuiltinModels( final String name, final String description )
48+
{
49+
this.name = name;
50+
this.description = description;
51+
}
52+
53+
public String modelName()
54+
{
55+
return name;
56+
}
57+
58+
public String description()
59+
{
60+
return description;
61+
}
62+
63+
@Override
64+
public String toString()
65+
{
66+
return name;
67+
}
68+
69+
/**
70+
* Gets a tooltip string combining the model name and description
71+
*
72+
* @return Formatted string for tooltip display
73+
*/
74+
public String getTooltip()
75+
{
76+
return String.format( "%s: %s", name, description );
77+
}
78+
}

src/main/java/net/imglib2/cellpose/Cellpose4Parameters.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,24 @@
3535
import java.util.Map;
3636

3737
import net.imglib2.appose.ShmImg;
38+
import net.imglib2.cellpose.Cellpose3Parameters.Builder;
3839
import net.imglib2.type.NativeType;
3940
import net.imglib2.type.numeric.IntegerType;
4041
import net.imglib2.type.numeric.RealType;
4142
import net.imglib2.type.numeric.integer.UnsignedByteType;
4243

4344
public class Cellpose4Parameters extends CellposeParameters
4445
{
45-
46+
public final Cellpose4BuiltinModels buitInModel;
47+
4648
public final Integer chan0; // as Integer so that it can be null
4749

4850
public final Integer chan1;
4951

5052
public final Integer chan2;
5153

5254
private Cellpose4Parameters(
55+
final Cellpose4BuiltinModels buitInModel,
5356
final Integer chan0,
5457
final Integer chan1,
5558
final Integer chan2,
@@ -75,6 +78,7 @@ private Cellpose4Parameters(
7578
cellProbThreshold, useGpu, minSize, anisotropy,
7679
stitchThreshold, resample, tileOverlap, computeFlows,
7780
flow3dSmooth, nIter, torchVersion );
81+
this.buitInModel = buitInModel;
7882
this.chan0 = chan0;
7983
this.chan1 = chan1;
8084
this.chan2 = chan2;
@@ -87,7 +91,10 @@ public < T extends RealType< T > & NativeType< T >, R extends IntegerType< R > &
8791
final ShmImg< R > outputLabels,
8892
final ShmImg< UnsignedByteType > outputFlows )
8993
{
94+
9095
final Map< String, Object > inputs = super.toApposeMap( input, axisInfo, outputLabels, outputFlows );
96+
final boolean isBuiltInModel = customModel == null || customModel.equals( "" );
97+
inputs.put( "model_name", isBuiltInModel ? buitInModel.modelName() : null );
9198

9299
final long nChannels = axisInfo.nChannels( input );
93100
inputs.put( "n_channels", nChannels );
@@ -106,6 +113,13 @@ public static Builder builder()
106113
// Builder class for fluent construction
107114
public static class Builder extends CellposeParameters.Builder< Builder >
108115
{
116+
private Cellpose4BuiltinModels model = Cellpose4BuiltinModels.CPSAMV2;
117+
118+
public Builder model( final Cellpose4BuiltinModels model )
119+
{
120+
this.model = model;
121+
return this;
122+
}
109123

110124
private Integer chan0 = 0; // to have one selected by default
111125

@@ -135,7 +149,7 @@ public Builder chan2( final Integer chan2 )
135149
public Cellpose4Parameters build()
136150
{
137151
return new Cellpose4Parameters(
138-
chan0, chan1, chan2, customModel, diameter, do3D, normalize,
152+
model, chan0, chan1, chan2, customModel, diameter, do3D, normalize,
139153
flowThreshold, cellProbThreshold, useGpu, minSize,
140154
anisotropy, stitchThreshold, resample, tileOverlap,
141155
computeFlows, flow3dSmooth, nIter, torchVersion );

src/main/resources/cp4_init.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,18 @@
5050
# load arguments and input from Appose task
5151
if appose_mode:
5252
use_gpu: bool = globals()['use_gpu']
53-
selected_model = "cpsam" if custom_model is None else custom_model
53+
selected_model = model_name if custom_model is None else custom_model
5454
else:
5555
custom_model = None
56+
model_name = "cpsam_v2"
5657
use_gpu = False
5758

5859
use_gpu, device = get_torch_device(use_gpu)
5960

6061
task.update(
6162
current = 1,
6263
maximum= 2,
63-
message=f"CP4: Start Cellpose (device={device}): deploy model {selected_model if selected_model else custom_model}"
64+
message=f"CP4: Start Cellpose (device={device}): deploy model {selected_model}"
6465
)
6566

6667
model = models.CellposeModel(

0 commit comments

Comments
 (0)