Skip to content

Commit 50f1f54

Browse files
committed
custom model logic
1 parent 77f2c19 commit 50f1f54

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/main/java/fiji/plugin/appose/cellpose/cp3/CellposeAppose.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.Map;
1919

20+
import javax.management.RuntimeErrorException;
2021
import javax.swing.JDialog;
2122
import javax.swing.JProgressBar;
2223
import javax.swing.WindowConstants;
@@ -69,7 +70,7 @@ public class CellposeAppose extends DynamicCommand implements Initializable
6970
"neurips_cellpose_transformer"}, description="Choose CP model to run")
7071
private String cp_model = "cyto3"; // cellpose model
7172

72-
@Parameter(label = "Custom model", description = "Custom model path, overrides the Cellpose model", style="file", required = false)
73+
@Parameter(label = "Custom model", description = "Custom model path, overrides the Cellpose model", style="file", required = false, validater = "validateCustomModel")
7374
private File custom_model = null;
7475

7576
@Parameter( label = "Diameter", min="0", description="Average diameter of a cell/nuclei (in pixels)" )
@@ -324,7 +325,9 @@ public < T extends RealType< T > & NativeType< T > > void process( final ImagePl
324325
final Map< String, Object > inputs = new HashMap<>();
325326
inputs.put( "image", NDArrays.asNDArray( img ) );
326327
inputs.put( "use_3D", use3d );
327-
inputs.put( "model", cp_model );
328+
// return null if custom model
329+
inputs.put( "model", ( custom_model == null ) ? cp_model : null );
330+
inputs.put( "custom_model", ( custom_model == null ) ? null : custom_model.toString() );
328331
inputs.put( "diameter", cell_diameter );
329332
inputs.put( "cell_channel", parseChannelChoice( cyto_channel ) );
330333
inputs.put( "nuclei_channel", parseChannelChoice( nuclei_channel ));
@@ -583,5 +586,14 @@ public static Integer parseChannelChoice(String str) {
583586
return Integer.parseInt(str);
584587
}
585588

589+
public void validateCustomModel() {
590+
if ( custom_model != null) {
591+
if ( ! custom_model.exists() ) {
592+
IJ.error("The path " + custom_model.toString() + " does not exist !");
593+
throw new RuntimeException("The path " + custom_model.toString() + " does not exist !");
594+
}
595+
}
596+
}
597+
586598

587599
}

src/main/resources/cp3.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def run_cellpose_v3(img: np.ndarray, kwargs: dict) -> tuple[np.ndarray, np.ndarr
6363

6464
model = models.CellposeModel(
6565
model_type=kwargs.get('model_name', 'cyto3'),
66+
pretrained_model=kwargs.get('custom_model', None),
6667
gpu=kwargs.get('use_gpu', False),
6768
device=kwargs.get('device', None)
6869
)
@@ -133,6 +134,7 @@ def share_as_ndarray(img):
133134
else:
134135
file = '../../../sample_data/test.tif'
135136
input_image = io.imread(file)
137+
custom_model = None
136138
model = 'cyto3'
137139
diameter = 30
138140
channels = [0, 1]
@@ -157,6 +159,7 @@ def share_as_ndarray(img):
157159
input_image,
158160
kwargs={
159161
"model_name": model,
162+
"custom_model": custom_model,
160163
"channels": channels,
161164
"diameter": diameter,
162165
"use_3D": use_3D,

0 commit comments

Comments
 (0)