Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tensorflow 1 Interface
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bioimage Analysis
Tensorflow 1 Interface
Commits
fa266af4
Commit
fa266af4
authored
2 years ago
by
carlosuc3m
Browse files
Options
Downloads
Patches
Plain Diff
adapt tensor builder to imglib2
parent
11c81ce3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/org/bioimageanalysis/icy/tensorflow/v1/tensor/TensorBuilder.java
+46
-11
46 additions, 11 deletions
...imageanalysis/icy/tensorflow/v1/tensor/TensorBuilder.java
with
46 additions
and
11 deletions
src/main/java/org/bioimageanalysis/icy/tensorflow/v1/tensor/TensorBuilder.java
+
46
−
11
View file @
fa266af4
...
@@ -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,12 +152,30 @@ public final class TensorBuilder
...
@@ -153,12 +152,30 @@ 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
)
return
tensor
;
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
;
}
}
/**
/**
...
@@ -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
)
return
tensor
;
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
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment