Skip to content
Snippets Groups Projects
Commit fa266af4 authored by carlosuc3m's avatar carlosuc3m
Browse files

adapt tensor builder to imglib2

parent 11c81ce3
Branches
No related tags found
No related merge requests found
...@@ -5,7 +5,6 @@ import java.nio.DoubleBuffer; ...@@ -5,7 +5,6 @@ import java.nio.DoubleBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import org.bioimageanalysis.icy.deeplearning.tensor.RaiArrayUtils;
import org.bioimageanalysis.icy.deeplearning.utils.IndexingUtils; import org.bioimageanalysis.icy.deeplearning.utils.IndexingUtils;
import org.tensorflow.Tensor; import org.tensorflow.Tensor;
import org.tensorflow.types.UInt8; import org.tensorflow.types.UInt8;
...@@ -153,11 +152,29 @@ public final class TensorBuilder ...@@ -153,11 +152,29 @@ public final class TensorBuilder
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the ndarray type is not supported. * If the ndarray type is not supported.
*/ */
private static <T extends Type<T>> Tensor<Float> buildFloat(RandomAccessibleInterval<FloatType> ndarray) private static <T extends Type<T>> Tensor<Float> buildFloat(RandomAccessibleInterval<FloatType> imgTensor)
{ {
float[] arr = RaiArrayUtils.floatArray(ndarray); long[] tensorShape = imgTensor.dimensionsAsLongArray();
FloatBuffer buff = FloatBuffer.wrap(arr); Cursor<FloatType> tensorCursor;
Tensor<Float> tensor = Tensor.create(ndarray.dimensionsAsLongArray(), buff); if (imgTensor instanceof IntervalView)
tensorCursor = ((IntervalView<FloatType>) imgTensor).cursor();
else if (imgTensor instanceof Img)
tensorCursor = ((Img<FloatType>) imgTensor).cursor();
else
throw new IllegalArgumentException("The data of the " + Tensor.class + " has "
+ "to be an instance of " + Img.class + " or " + IntervalView.class);
long flatSize = 1;
for (long dd : imgTensor.dimensionsAsLongArray()) { flatSize *= dd;}
float[] flatArr = new float[(int) flatSize];
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
float val = tensorCursor.get().getRealFloat();
flatArr[flatPos] = val;
}
FloatBuffer buff = FloatBuffer.wrap(flatArr);
Tensor<Float> tensor = Tensor.create(imgTensor.dimensionsAsLongArray(), buff);
return tensor; return tensor;
} }
...@@ -170,11 +187,29 @@ public final class TensorBuilder ...@@ -170,11 +187,29 @@ public final class TensorBuilder
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the ndarray type is not supported. * If the ndarray type is not supported.
*/ */
private static <T extends Type<T>> Tensor<Double> buildDouble(RandomAccessibleInterval<DoubleType> ndarray) private static <T extends Type<T>> Tensor<Double> buildDouble(RandomAccessibleInterval<DoubleType> imgTensor)
{ {
double[] arr = RaiArrayUtils.doubleArray(ndarray); long[] tensorShape = imgTensor.dimensionsAsLongArray();
DoubleBuffer buff = DoubleBuffer.wrap(arr); Cursor<DoubleType> tensorCursor;
Tensor<Double> tensor = Tensor.create(ndarray.dimensionsAsLongArray(), buff); if (imgTensor instanceof IntervalView)
tensorCursor = ((IntervalView<DoubleType>) imgTensor).cursor();
else if (imgTensor instanceof Img)
tensorCursor = ((Img<DoubleType>) imgTensor).cursor();
else
throw new IllegalArgumentException("The data of the " + Tensor.class + " has "
+ "to be an instance of " + Img.class + " or " + IntervalView.class);
long flatSize = 1;
for (long dd : imgTensor.dimensionsAsLongArray()) { flatSize *= dd;}
double[] flatArr = new double[(int) flatSize];
while (tensorCursor.hasNext()) {
tensorCursor.fwd();
long[] cursorPos = tensorCursor.positionAsLongArray();
int flatPos = IndexingUtils.multidimensionalIntoFlatIndex(cursorPos, tensorShape);
double val = tensorCursor.get().getRealFloat();
flatArr[flatPos] = val;
}
DoubleBuffer buff = DoubleBuffer.wrap(flatArr);
Tensor<Double> tensor = Tensor.create(imgTensor.dimensionsAsLongArray(), buff);
return tensor; return tensor;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment