|
| 1 | +package org.mastodon.mamut.detection; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.lang.invoke.MethodHandles; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | + |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import io.bioimage.modelrunner.bioimageio.BioimageioRepo; |
| 13 | + |
| 14 | +public class StarDist extends Segmentation3D |
| 15 | +{ |
| 16 | + private static final Logger logger = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() ); |
| 17 | + |
| 18 | + private final ModelType modelType; |
| 19 | + |
| 20 | + private boolean dataIs2D; |
| 21 | + |
| 22 | + private static final String PATH = "stardist"; |
| 23 | + |
| 24 | + public StarDist( final ModelType model ) throws IOException, InterruptedException |
| 25 | + { |
| 26 | + super(); |
| 27 | + this.modelType = model; |
| 28 | + if ( modelType == null ) |
| 29 | + logger.info( "No star dist model path specified. Using pretrained demo model." ); |
| 30 | + else |
| 31 | + { |
| 32 | + Path starDistModelRoot = |
| 33 | + Paths.get( System.getProperty( "user.home" ), ".local", "share", "stardist", "models", modelType.getModelPath() ); |
| 34 | + File directory = starDistModelRoot.toFile(); |
| 35 | + if ( directory.isDirectory() ) |
| 36 | + { |
| 37 | + String[] files = directory.list(); |
| 38 | + if ( files != null && files.length > 0 ) |
| 39 | + logger.debug( "Found {} files in {}", files.length, directory.getAbsolutePath() ); |
| 40 | + else |
| 41 | + BioimageioRepo.connect().downloadByName( "StarDist Plant Nuclei 3D ResNet", directory.getAbsolutePath() ); |
| 42 | + } |
| 43 | + else |
| 44 | + logger.error( "The specified path is not a directory: {}", directory.getAbsolutePath() ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + String generateEnvFileContent() |
| 50 | + { |
| 51 | + return "name: stardist\n" |
| 52 | + + "channels:\n" |
| 53 | + + " - conda-forge\n" |
| 54 | + + "dependencies:\n" |
| 55 | + + " - python=3.10\n" |
| 56 | + + " - cudatoolkit=11.2\n" |
| 57 | + + " - cudnn=8.1.0\n" |
| 58 | + + " - numpy<1.24\n" |
| 59 | + + " - pip\n" |
| 60 | + + " - pip:\n" |
| 61 | + + " - numpy<1.24\n" |
| 62 | + + " - tensorflow==2.10\n" |
| 63 | + + " - stardist==0.8.5\n" |
| 64 | + + " - appose\n"; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + String generateScript() |
| 69 | + { |
| 70 | + return "import numpy as np" + "\n" |
| 71 | + + "import appose" + "\n" |
| 72 | + + "from csbdeep.utils import normalize" + "\n" |
| 73 | + + getImportStarDistCommand() |
| 74 | + + "\n" |
| 75 | + + "task.update(message=\"Imports completed\")" + "\n" |
| 76 | + + "np.random.seed(6)" + "\n" |
| 77 | + + "axes_normalize = (0, 1, 2)" + "\n" |
| 78 | + + "\n" |
| 79 | + + "task.update(message=\"Loading StarDist pretrained 3D model\")" + "\n" |
| 80 | + + getLoadModelCommand() |
| 81 | + + "image_ndarray = image.ndarray()" + "\n" |
| 82 | + + "image_normalized = normalize(image_ndarray, 1, 99.8, axis=axes_normalize)" + "\n" |
| 83 | + + "task.update(message=\"Image shape:\" + str(image_normalized.shape))" + "\n" |
| 84 | + + "\n" |
| 85 | + + "guessed_tiles = model._guess_n_tiles(image_normalized)" + "\n" |
| 86 | + + "task.update(message=\"Guessed tiles: \" + str(guessed_tiles))" + "\n" |
| 87 | + + "\n" |
| 88 | + + "label_image, details = model.predict_instances(image_normalized, axes='ZYX', n_tiles=guessed_tiles)" + "\n" |
| 89 | + + "shared = appose.NDArray(image.dtype, image.shape)" + "\n" |
| 90 | + + "shared.ndarray()[:] = label_image" + "\n" |
| 91 | + + "task.outputs['label_image'] = shared" + "\n"; |
| 92 | + } |
| 93 | + |
| 94 | + public ModelType getModelType() |
| 95 | + { |
| 96 | + return modelType; |
| 97 | + } |
| 98 | + |
| 99 | + private String getImportStarDistCommand() |
| 100 | + { |
| 101 | + if ( modelType == null ) |
| 102 | + { |
| 103 | + if ( dataIs2D ) |
| 104 | + return "from stardist.models import StarDist2D" + "\n "; |
| 105 | + return "from stardist.models import StarDist3D" + "\n "; |
| 106 | + } |
| 107 | + if ( modelType.is2D() ) |
| 108 | + return "from stardist.models import StarDist2D" + "\n "; |
| 109 | + return "from stardist.models import StarDist3D" + "\n "; |
| 110 | + } |
| 111 | + |
| 112 | + private String getLoadModelCommand() |
| 113 | + { |
| 114 | + if ( modelType == null ) |
| 115 | + { |
| 116 | + if ( dataIs2D ) |
| 117 | + return "model = StarDist2D.from_pretrained('2D_demo')" + "\n"; |
| 118 | + else |
| 119 | + return "model = StarDist3D.from_pretrained('3D_demo')" + "\n"; |
| 120 | + } |
| 121 | + String starDistModel = modelType.is2D() ? "StarDist2D" : "StarDist3D"; |
| 122 | + return "model = " + starDistModel + "(None, name='" + modelType.getModelPath() + "', basedir=r\"" + PATH + "\")" + "\n"; |
| 123 | + } |
| 124 | + |
| 125 | + public enum ModelType |
| 126 | + { |
| 127 | + PLANT_NUCLEI_3D( "StarDist Plant Nuclei 3D ResNet", "stardist-plant-nuclei-3d", false ), |
| 128 | + FLUO_2D( "StarDist Fluorescence Nuclei Segmentation", "stardist-fluo-2d", true ), |
| 129 | + H_E( "StarDist H&E Nuclei Segmentation", "stardist-h-e-nuclei", true ), |
| 130 | + DEMO_2D( "StarDist Demo", "stardist-demo-2d", true ), |
| 131 | + DEMO_3D( "StarDist Demo", "stardist-demo-3d", false ); |
| 132 | + |
| 133 | + private final String modelName; |
| 134 | + |
| 135 | + private final String modelPath; |
| 136 | + |
| 137 | + private final boolean is2D; |
| 138 | + |
| 139 | + ModelType( final String modelName, final String modelPath, final boolean is2D ) |
| 140 | + { |
| 141 | + this.modelName = modelName; |
| 142 | + this.modelPath = modelPath; |
| 143 | + this.is2D = is2D; |
| 144 | + } |
| 145 | + |
| 146 | + public String getModelName() |
| 147 | + { |
| 148 | + return modelName; |
| 149 | + } |
| 150 | + |
| 151 | + public String getModelPath() |
| 152 | + { |
| 153 | + return modelPath; |
| 154 | + } |
| 155 | + |
| 156 | + public boolean is2D() |
| 157 | + { |
| 158 | + return is2D; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String toString() |
| 163 | + { |
| 164 | + String dimensionality = is2D ? " (2D)" : " (3D)"; |
| 165 | + return modelName + dimensionality; |
| 166 | + } |
| 167 | + |
| 168 | + public static ModelType fromString( final String modelName ) |
| 169 | + { |
| 170 | + for ( ModelType type : ModelType.values() ) |
| 171 | + { |
| 172 | + if ( type.modelName.equalsIgnoreCase( modelName ) ) |
| 173 | + { |
| 174 | + return type; |
| 175 | + } |
| 176 | + } |
| 177 | + throw new IllegalArgumentException( "No enum constant for model name: " + modelName ); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments