diff --git a/pom.xml b/pom.xml index 00b9b22..792f7d5 100644 --- a/pom.xml +++ b/pom.xml @@ -145,7 +145,7 @@ sign,deploy-to-scijava - 0.5.5 + 0.5.8-SNAPSHOT diff --git a/src/main/java/ai/nets/samj/models/AbstractSamJ.java b/src/main/java/ai/nets/samj/models/AbstractSamJ.java index 67b9f0c..585da43 100644 --- a/src/main/java/ai/nets/samj/models/AbstractSamJ.java +++ b/src/main/java/ai/nets/samj/models/AbstractSamJ.java @@ -268,31 +268,6 @@ else if (task.status == TaskStatus.CRASHED) protected & NativeType> void sendImgLib2AsNp() { createSHMArray((RandomAccessibleInterval) this.img); - String code = ""; - // This line wants to recreate the original numpy array. Should look like: - // input0_appose_shm = shared_memory.SharedMemory(name=input0) - // input0 = np.ndarray(size, dtype="float64", buffer=input0_appose_shm.buf).reshape([64, 64]) - code += "im_shm = shared_memory.SharedMemory(name='" - + shma.getNameForPython() + "', size=" + shma.getSize() - + ")" + System.lineSeparator(); - int size = 1; - for (long l : targetDims) {size *= l;} - code += "im = np.ndarray(" + size + ", dtype='" + CommonUtils.getDataType(Util.getTypeFromInterval(shma.getSharedRAI())) - + "', buffer=im_shm.buf).reshape(["; - for (long ll : targetDims) - code += ll + ", "; - code = code.substring(0, code.length() - 2); - code += "])" + System.lineSeparator(); - code += "input_h = im.shape[1]" + System.lineSeparator(); - code += "input_w = im.shape[0]" + System.lineSeparator(); - code += "globals()['input_h'] = input_h" + System.lineSeparator(); - code += "globals()['input_w'] = input_w" + System.lineSeparator(); - code += "task.update(str(im.shape))" + System.lineSeparator(); - code += "im = torch.from_numpy(np.transpose(im))" + System.lineSeparator(); - code += "task.update('after ' + str(im.shape))" + System.lineSeparator(); - code += "im_shm.unlink()" + System.lineSeparator(); - //code += "box_shm.close()" + System.lineSeparator(); - this.script += code; } private & NativeType> void sendCropAsNp(long[] cropSize) { @@ -318,7 +293,7 @@ else if (cropSize.length == 2) + ")" + System.lineSeparator(); int size = 1; for (long l : targetDims) {size *= l;} - code += "im = np.ndarray(" + size + ", dtype='" + CommonUtils.getDataType(Util.getTypeFromInterval(crop)) + "', buffer=im_shm.buf).reshape(["; + code += "im = np.ndarray(" + size + ", dtype='" + CommonUtils.getDataTypeFromRAI(crop) + "', buffer=im_shm.buf).reshape(["; for (long ll : targetDims) code += ll + ", "; code = code.substring(0, code.length() - 2); @@ -637,7 +612,7 @@ List processMask(RandomAccessibleInterval img, boolean returnAll) throw new IllegalArgumentException("The provided mask should be a 2d image with just one channel of width " + this.shma.getOriginalShape()[1] + " and height " + this.shma.getOriginalShape()[0]); } - SharedMemoryArray maskShma = SharedMemoryArray.buildSHMA(img); + SharedMemoryArray maskShma = SharedMemoryArray.createSHMAFromRAI(img, false, false); try { return processMask(maskShma, returnAll); } catch (IOException | RuntimeException | InterruptedException ex) { diff --git a/src/main/java/ai/nets/samj/models/EfficientSamJ.java b/src/main/java/ai/nets/samj/models/EfficientSamJ.java index e0913e1..cff6b80 100644 --- a/src/main/java/ai/nets/samj/models/EfficientSamJ.java +++ b/src/main/java/ai/nets/samj/models/EfficientSamJ.java @@ -28,6 +28,7 @@ import io.bioimage.modelrunner.apposed.appose.Service.Task; import io.bioimage.modelrunner.apposed.appose.Service.TaskStatus; import io.bioimage.modelrunner.tensor.shm.SharedMemoryArray; +import io.bioimage.modelrunner.utils.CommonUtils; import net.imglib2.RandomAccessibleInterval; import net.imglib2.converter.RealTypeConverters; import net.imglib2.img.array.ArrayImgs; @@ -35,6 +36,7 @@ import net.imglib2.type.numeric.RealType; import net.imglib2.type.numeric.integer.UnsignedByteType; import net.imglib2.type.numeric.real.FloatType; +import net.imglib2.util.Cast; import net.imglib2.util.Intervals; import net.imglib2.view.IntervalView; import net.imglib2.view.Views; @@ -196,7 +198,32 @@ else if (task.status == TaskStatus.CRASHED) @Override protected void createEncodeImageScript() { - this.script = "" + String code = ""; + // This line wants to recreate the original numpy array. Should look like: + // input0_appose_shm = shared_memory.SharedMemory(name=input0) + // input0 = np.ndarray(size, dtype="float64", buffer=input0_appose_shm.buf).reshape([64, 64]) + code += "im_shm = shared_memory.SharedMemory(name='" + + shma.getNameForPython() + "', size=" + shma.getSize() + + ")" + System.lineSeparator(); + int size = 1; + for (long l : targetDims) {size *= l;} + code += "im = np.ndarray(" + size + ", dtype='" + CommonUtils.getDataTypeFromRAI(Cast.unchecked(shma.getSharedRAI())) + + "', buffer=im_shm.buf).reshape(["; + for (long ll : targetDims) + code += ll + ", "; + code = code.substring(0, code.length() - 2); + code += "])" + System.lineSeparator(); + code += "input_h = im.shape[1]" + System.lineSeparator(); + code += "input_w = im.shape[0]" + System.lineSeparator(); + code += "globals()['input_h'] = input_h" + System.lineSeparator(); + code += "globals()['input_w'] = input_w" + System.lineSeparator(); + code += "task.update(str(im.shape))" + System.lineSeparator(); + code += "im = torch.from_numpy(np.transpose(im))" + System.lineSeparator(); + code += "task.update('after ' + str(im.shape))" + System.lineSeparator(); + code += "im_shm.unlink()" + System.lineSeparator(); + //code += "box_shm.close()" + System.lineSeparator(); + this.script += code; + this.script += "" + "task.update(str(im.shape))" + System.lineSeparator() + "aa = predictor.get_image_embeddings(im[None, ...])"; } @@ -379,7 +406,7 @@ private & NativeType> void checkImageIsFine(RandomAcce protected & NativeType> void createSHMArray(RandomAccessibleInterval imShared) { RandomAccessibleInterval imageToBeSent = ImgLib2Utils.reescaleIfNeeded(imShared); long[] dims = imageToBeSent.dimensionsAsLongArray(); - shma = SharedMemoryArray.buildMemorySegmentForImage(new long[] {dims[0], dims[1], dims[2]}, new FloatType()); + shma = SharedMemoryArray.create(new long[] {dims[0], dims[1], dims[2]}, new FloatType(), false, false); adaptImageToModel(imageToBeSent, shma.getSharedRAI()); } } diff --git a/src/main/java/ai/nets/samj/models/EfficientViTSamJ.java b/src/main/java/ai/nets/samj/models/EfficientViTSamJ.java index 6bf90eb..4bf8783 100644 --- a/src/main/java/ai/nets/samj/models/EfficientViTSamJ.java +++ b/src/main/java/ai/nets/samj/models/EfficientViTSamJ.java @@ -33,12 +33,15 @@ import io.bioimage.modelrunner.apposed.appose.Service.TaskStatus; import io.bioimage.modelrunner.tensor.shm.SharedMemoryArray; +import io.bioimage.modelrunner.utils.CommonUtils; import net.imglib2.RandomAccessibleInterval; import net.imglib2.converter.RealTypeConverters; import net.imglib2.img.array.ArrayImgs; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.type.numeric.integer.UnsignedByteType; +import net.imglib2.type.numeric.real.FloatType; +import net.imglib2.util.Cast; import net.imglib2.util.Intervals; import net.imglib2.view.IntervalView; import net.imglib2.view.Views; @@ -304,7 +307,22 @@ protected & NativeType> void setImageOfInterest(Random @Override protected void createEncodeImageScript() { - this.script += "" + script = ""; + script += "im_shm = shared_memory.SharedMemory(name='" + + shma.getNameForPython() + "', size=" + shma.getSize() + + ")" + System.lineSeparator(); + int size = 1; + for (long l : targetDims) {size *= l;} + script += "im = np.ndarray(" + size + ", dtype='" + "', buffer=im_shm.buf).reshape(["; + for (long ll : targetDims) + script += ll + ", "; + script = script.substring(0, script.length() - 2); + script += "])" + System.lineSeparator(); + script += "im = np.transpose(im, (1, 0, 2))" + System.lineSeparator(); + //code += "np.save('/home/carlos/git/aa.npy', im)" + System.lineSeparator(); + script += "im_shm.unlink()" + System.lineSeparator(); + //code += "box_shm.close()" + System.lineSeparator(); + script += "" + "task.update(str(im.shape))" + System.lineSeparator() + "predictor.set_image(im)"; } @@ -313,7 +331,7 @@ protected void createEncodeImageScript() { protected & NativeType> void createSHMArray(RandomAccessibleInterval imShared) { RandomAccessibleInterval imageToBeSent = ImgLib2Utils.reescaleIfNeeded(imShared); long[] dims = imageToBeSent.dimensionsAsLongArray(); - shma = SharedMemoryArray.buildMemorySegmentForImage(new long[] {dims[0], dims[1], dims[2]}, new UnsignedByteType()); + shma = SharedMemoryArray.create(new long[] {dims[0], dims[1], dims[2]}, new UnsignedByteType(), false, false); adaptImageToModel(imageToBeSent, shma.getSharedRAI()); }