From fc3fc86f5f3ea7497c002ae20abb38c47b045314 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Tue, 1 Oct 2024 19:27:08 +0200 Subject: [PATCH] correct javadoc --- .../tensorflow/v2/api050/JavaWorker.java | 8 +- .../tensorflow/v2/api050/shm/ShmBuilder.java | 67 +++------------- .../v2/api050/shm/TensorBuilder.java | 79 +++---------------- .../v2/api050/tensor/ImgLib2Builder.java | 38 +-------- .../v2/api050/tensor/TensorBuilder.java | 66 +++------------- 5 files changed, 40 insertions(+), 218 deletions(-) diff --git a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/JavaWorker.java b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/JavaWorker.java index 2b81b2f..3b98741 100644 --- a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/JavaWorker.java +++ b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/JavaWorker.java @@ -21,7 +21,13 @@ public class JavaWorker { private final Tensorflow2Interface ti; private boolean cancelRequested = false; - + + /** + * Method in the child process that is in charge of keeping the process open and calling the model load, + * model inference and model closing + * @param args + * args of the parent process + */ public static void main(String[] args) { try(Scanner scanner = new Scanner(System.in)){ diff --git a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/ShmBuilder.java b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/ShmBuilder.java index 72ae3e2..edbb477 100644 --- a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/ShmBuilder.java +++ b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/ShmBuilder.java @@ -43,11 +43,10 @@ import net.imglib2.type.numeric.real.FloatType; /** - * A {@link RandomAccessibleInterval} builder for TensorFlow {@link Tensor} objects. - * Build ImgLib2 objects (backend of {@link io.bioimage.modelrunner.tensor.Tensor}) - * from Tensorflow 2 {@link Tensor} + * A utility class that converts {@link TType} tensors into {@link SharedMemoryArray}s for + * interprocessing communication * - * @author Carlos Garcia Lopez de Haro and Daniel Felipe Gonzalez Obando + * @author Carlos Garcia Lopez de Haro */ public final class ShmBuilder { @@ -58,17 +57,15 @@ private ShmBuilder() { } - /** - * Creates a {@link RandomAccessibleInterval} from a given {@link TType} tensor - * - * @param - * the possible ImgLib2 datatypes of the image - * @param tensor - * The {@link TType} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the {@link TType} tensor. - * @throws IllegalArgumentException If the {@link TType} tensor type is not supported. - * @throws IOException - */ + /** + * Create a {@link SharedMemoryArray} from a {@link TType} tensor + * @param tensor + * the tensor to be passed into the other process through the shared memory + * @param memoryName + * the name of the memory region where the tensor is going to be copied + * @throws IllegalArgumentException if the data type of the tensor is not supported + * @throws IOException if there is any error creating the shared memory array + */ public static void build(TType tensor, String memoryName) throws IllegalArgumentException, IOException { if (tensor instanceof TUint8) @@ -97,14 +94,6 @@ else if (tensor instanceof TInt64) } } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned byte-typed {@link TUint8} tensor. - * - * @param tensor - * The {@link TUint8} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link UnsignedByteType}. - * @throws IOException - */ private static void buildFromTensorUByte(TUint8 tensor, String memoryName) throws IOException { long[] arrayShape = tensor.shape().asArray(); @@ -122,14 +111,6 @@ private static void buildFromTensorUByte(TUint8 tensor, String memoryName) throw if (PlatformDetection.isWindows()) shma.close(); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned int32-typed {@link TInt32} tensor. - * - * @param tensor - * The {@link TInt32} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link IntType}. - * @throws IOException - */ private static void buildFromTensorInt(TInt32 tensor, String memoryName) throws IOException { long[] arrayShape = tensor.shape().asArray(); @@ -148,14 +129,6 @@ private static void buildFromTensorInt(TInt32 tensor, String memoryName) throws if (PlatformDetection.isWindows()) shma.close(); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned float32-typed {@link TFloat32} tensor. - * - * @param tensor - * The {@link TFloat32} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link FloatType}. - * @throws IOException - */ private static void buildFromTensorFloat(TFloat32 tensor, String memoryName) throws IOException { long[] arrayShape = tensor.shape().asArray(); @@ -174,14 +147,6 @@ private static void buildFromTensorFloat(TFloat32 tensor, String memoryName) thr if (PlatformDetection.isWindows()) shma.close(); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned float64-typed {@link TFloat64} tensor. - * - * @param tensor - * The {@link TFloat64} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link DoubleType}. - * @throws IOException - */ private static void buildFromTensorDouble(TFloat64 tensor, String memoryName) throws IOException { long[] arrayShape = tensor.shape().asArray(); @@ -200,14 +165,6 @@ private static void buildFromTensorDouble(TFloat64 tensor, String memoryName) th if (PlatformDetection.isWindows()) shma.close(); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned int64-typed {@link TInt64} tensor. - * - * @param tensor - * The {@link TInt64} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link LongType}. - * @throws IOException - */ private static void buildFromTensorLong(TInt64 tensor, String memoryName) throws IOException { long[] arrayShape = tensor.shape().asArray(); diff --git a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/TensorBuilder.java b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/TensorBuilder.java index 0fa68ad..0257481 100644 --- a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/TensorBuilder.java +++ b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/shm/TensorBuilder.java @@ -23,13 +23,6 @@ import io.bioimage.modelrunner.tensor.shm.SharedMemoryArray; import io.bioimage.modelrunner.utils.CommonUtils; -import net.imglib2.RandomAccessibleInterval; -import net.imglib2.img.Img; -import net.imglib2.type.numeric.integer.IntType; -import net.imglib2.type.numeric.integer.LongType; -import net.imglib2.type.numeric.integer.UnsignedByteType; -import net.imglib2.type.numeric.real.DoubleType; -import net.imglib2.type.numeric.real.FloatType; import net.imglib2.util.Cast; import java.nio.ByteBuffer; @@ -55,10 +48,9 @@ import org.tensorflow.types.family.TType; /** - * A TensorFlow 2 {@link Tensor} builder from {@link Img} and - * {@link io.bioimage.modelrunner.tensor.Tensor} objects. + * Utility class to build Tensorflow tensors from shm segments using {@link SharedMemoryArray} * - * @author Carlos Garcia Lopez de Haro and Daniel Felipe Gonzalez Obando + * @author Carlos Garcia Lopez de Haro */ public final class TensorBuilder { @@ -68,16 +60,13 @@ public final class TensorBuilder { private TensorBuilder() {} /** - * Creates {@link TType} instance with the same size and information as the - * given {@link RandomAccessibleInterval}. + * Creates {@link TType} instance from a {@link SharedMemoryArray} * - * @param - * the ImgLib2 data types the {@link RandomAccessibleInterval} can be * @param array - * the {@link RandomAccessibleInterval} that is going to be converted into + * the {@link SharedMemoryArray} that is going to be converted into * a {@link TType} tensor - * @return a {@link TType} tensor - * @throws IllegalArgumentException if the type of the {@link RandomAccessibleInterval} + * @return the Tensorflow {@link TType} as the one stored in the shared memory segment + * @throws IllegalArgumentException if the type of the {@link SharedMemoryArray} * is not supported */ public static TType build(SharedMemoryArray array) throws IllegalArgumentException @@ -103,17 +92,7 @@ else if (array.getOriginalDataType().equals("int64")) { } } - /** - * Creates a {@link TType} tensor of type {@link TUint8} from an - * {@link RandomAccessibleInterval} of type {@link UnsignedByteType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TType} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ - public static TUint8 buildUByte(SharedMemoryArray tensor) + private static TUint8 buildUByte(SharedMemoryArray tensor) throws IllegalArgumentException { long[] ogShape = tensor.getOriginalShape(); @@ -128,17 +107,7 @@ public static TUint8 buildUByte(SharedMemoryArray tensor) return ndarray; } - /** - * Creates a {@link TInt32} tensor of type {@link TInt32} from an - * {@link RandomAccessibleInterval} of type {@link IntType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TInt32} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ - public static TInt32 buildInt(SharedMemoryArray tensor) + private static TInt32 buildInt(SharedMemoryArray tensor) throws IllegalArgumentException { long[] ogShape = tensor.getOriginalShape(); @@ -157,16 +126,6 @@ public static TInt32 buildInt(SharedMemoryArray tensor) return ndarray; } - /** - * Creates a {@link TInt64} tensor of type {@link TInt64} from an - * {@link RandomAccessibleInterval} of type {@link LongType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TInt64} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ private static TInt64 buildLong(SharedMemoryArray tensor) throws IllegalArgumentException { @@ -186,17 +145,7 @@ private static TInt64 buildLong(SharedMemoryArray tensor) return ndarray; } - /** - * Creates a {@link TFloat32} tensor of type {@link TFloat32} from an - * {@link RandomAccessibleInterval} of type {@link FloatType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TFloat32} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ - public static TFloat32 buildFloat(SharedMemoryArray tensor) + private static TFloat32 buildFloat(SharedMemoryArray tensor) throws IllegalArgumentException { long[] ogShape = tensor.getOriginalShape(); @@ -214,16 +163,6 @@ public static TFloat32 buildFloat(SharedMemoryArray tensor) return ndarray; } - /** - * Creates a {@link TFloat64} tensor of type {@link TFloat64} from an - * {@link RandomAccessibleInterval} of type {@link DoubleType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TFloat64} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ private static TFloat64 buildDouble(SharedMemoryArray tensor) throws IllegalArgumentException { diff --git a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/ImgLib2Builder.java b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/ImgLib2Builder.java index 736065b..d5e86e8 100644 --- a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/ImgLib2Builder.java +++ b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/ImgLib2Builder.java @@ -67,7 +67,8 @@ private ImgLib2Builder() * @return The {@link RandomAccessibleInterval} built from the {@link TType} tensor. * @throws IllegalArgumentException If the {@link TType} tensor type is not supported. */ - public static > RandomAccessibleInterval build(TType tensor) throws IllegalArgumentException + @SuppressWarnings("unchecked") + public static > RandomAccessibleInterval build(TType tensor) throws IllegalArgumentException { if (tensor instanceof TUint8) { @@ -95,13 +96,6 @@ else if (tensor instanceof TInt64) } } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned byte-typed {@link TUint8} tensor. - * - * @param tensor - * The {@link TUint8} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link UnsignedByteType}. - */ private static RandomAccessibleInterval buildFromTensorUByte(TUint8 tensor) { long[] arrayShape = tensor.shape().asArray(); @@ -118,13 +112,6 @@ private static RandomAccessibleInterval buildFromTensorUByte(T return Utils.transpose(rai); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned int32-typed {@link TInt32} tensor. - * - * @param tensor - * The {@link TInt32} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link IntType}. - */ private static RandomAccessibleInterval buildFromTensorInt(TInt32 tensor) { long[] arrayShape = tensor.shape().asArray(); @@ -141,13 +128,6 @@ private static RandomAccessibleInterval buildFromTensorInt(TInt32 tenso return Utils.transpose(rai); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned float32-typed {@link TFloat32} tensor. - * - * @param tensor - * The {@link TFloat32} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link FloatType}. - */ private static RandomAccessibleInterval buildFromTensorFloat(TFloat32 tensor) { long[] arrayShape = tensor.shape().asArray(); @@ -164,13 +144,6 @@ private static RandomAccessibleInterval buildFromTensorFloat(TFloat32 return Utils.transpose(rai); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned float64-typed {@link TFloat64} tensor. - * - * @param tensor - * The {@link TFloat64} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link DoubleType}. - */ private static RandomAccessibleInterval buildFromTensorDouble(TFloat64 tensor) { long[] arrayShape = tensor.shape().asArray(); @@ -187,13 +160,6 @@ private static RandomAccessibleInterval buildFromTensorDouble(TFloat return Utils.transpose(rai); } - /** - * Builds a {@link RandomAccessibleInterval} from a unsigned int64-typed {@link TInt64} tensor. - * - * @param tensor - * The {@link TInt64} tensor data is read from. - * @return The {@link RandomAccessibleInterval} built from the tensor, of type {@link LongType}. - */ private static RandomAccessibleInterval buildFromTensorLong(TInt64 tensor) { long[] arrayShape = tensor.shape().asArray(); diff --git a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/TensorBuilder.java b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/TensorBuilder.java index 83577fe..d5a9379 100644 --- a/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/TensorBuilder.java +++ b/src/main/java/io/bioimage/modelrunner/tensorflow/v2/api050/tensor/TensorBuilder.java @@ -26,7 +26,8 @@ import net.imglib2.Cursor; import net.imglib2.RandomAccessibleInterval; import net.imglib2.img.Img; -import net.imglib2.type.Type; +import net.imglib2.type.NativeType; +import net.imglib2.type.numeric.RealType; import net.imglib2.type.numeric.integer.IntType; import net.imglib2.type.numeric.integer.LongType; import net.imglib2.type.numeric.integer.UnsignedByteType; @@ -69,6 +70,8 @@ private TensorBuilder() {} * Creates {@link TType} instance with the same size and information as the * given {@link io.bioimage.modelrunner.tensor.Tensor}. * + * @param + * the possible ImgLib2 datatypes of the image * @param tensor * The dlmodel-runner {@link io.bioimage.modelrunner.tensor.Tensor} that is * going to be converted into a {@link TType} tensor @@ -76,7 +79,7 @@ private TensorBuilder() {} * @throws IllegalArgumentException If the type of the {@link io.bioimage.modelrunner.tensor.Tensor} * is not supported */ - public static TType build(io.bioimage.modelrunner.tensor.Tensor tensor) + public static < T extends RealType< T > & NativeType< T > > TType build(io.bioimage.modelrunner.tensor.Tensor tensor) throws IllegalArgumentException { return build(tensor.getData()); @@ -95,7 +98,8 @@ public static TType build(io.bioimage.modelrunner.tensor.Tensor tensor) * @throws IllegalArgumentException if the type of the {@link RandomAccessibleInterval} * is not supported */ - public static > TType build( + @SuppressWarnings("unchecked") + public static < T extends RealType< T > & NativeType< T > > TType build( RandomAccessibleInterval array) throws IllegalArgumentException { // Create an Icy sequence of the same type of the tensor @@ -120,17 +124,7 @@ else if (Util.getTypeFromInterval(array) instanceof LongType) { } } - /** - * Creates a {@link TType} tensor of type {@link TUint8} from an - * {@link RandomAccessibleInterval} of type {@link UnsignedByteType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TType} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ - public static TUint8 buildUByte(RandomAccessibleInterval tensor) + private static TUint8 buildUByte(RandomAccessibleInterval tensor) throws IllegalArgumentException { long[] ogShape = tensor.dimensionsAsLongArray(); @@ -157,17 +151,7 @@ public static TUint8 buildUByte(RandomAccessibleInterval tenso return ndarray; } - /** - * Creates a {@link TInt32} tensor of type {@link TInt32} from an - * {@link RandomAccessibleInterval} of type {@link IntType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TInt32} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ - public static TInt32 buildInt(RandomAccessibleInterval tensor) + private static TInt32 buildInt(RandomAccessibleInterval tensor) throws IllegalArgumentException { long[] ogShape = tensor.dimensionsAsLongArray(); @@ -195,16 +179,6 @@ public static TInt32 buildInt(RandomAccessibleInterval tensor) return ndarray; } - /** - * Creates a {@link TInt64} tensor of type {@link TInt64} from an - * {@link RandomAccessibleInterval} of type {@link LongType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TInt64} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ private static TInt64 buildLong(RandomAccessibleInterval tensor) throws IllegalArgumentException { @@ -233,17 +207,7 @@ private static TInt64 buildLong(RandomAccessibleInterval tensor) return ndarray; } - /** - * Creates a {@link TFloat32} tensor of type {@link TFloat32} from an - * {@link RandomAccessibleInterval} of type {@link FloatType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TFloat32} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ - public static TFloat32 buildFloat( + private static TFloat32 buildFloat( RandomAccessibleInterval tensor) throws IllegalArgumentException { @@ -271,16 +235,6 @@ public static TFloat32 buildFloat( return ndarray; } - /** - * Creates a {@link TFloat64} tensor of type {@link TFloat64} from an - * {@link RandomAccessibleInterval} of type {@link DoubleType} - * - * @param tensor - * The {@link RandomAccessibleInterval} to fill the tensor with. - * @return The {@link TFloat64} tensor filled with the {@link RandomAccessibleInterval} data. - * @throws IllegalArgumentException if the input {@link RandomAccessibleInterval} type is - * not compatible - */ private static TFloat64 buildDouble( RandomAccessibleInterval tensor) throws IllegalArgumentException