You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The method create(float[][][][]) is undefined for the type Tensor (tensorflow-core-api version 0.3) #258
I am trying to convert an image to Tensor. For that first I convert the buffered image to float[][][][] array as below. File inputImage = new File(inputName); BufferedImage bufferedImage = ImageIO.read(inputImage); int imgHeight = bufferedImage.getHeight(); int imgWidth = bufferedImage.getWidth(); int numberOfChannels = bufferedImage.getTransparency(); float[][][][] floatImage = new float[1][imgHeight][imgWidth][numberOfChannels]; for(int i = 0; i < imgHeight; i++) { for(int j = 0; j < imgWidth; j++) { for(int k = 0; k < numberOfChannels; k++) { floatImage[0][i][j][k] = (float) (bufferedImage.getData().getSample(i, j, k)/255.0); } } }
Then, I try to convert the float array to Tensor using Tensor inputTensor = Tensor.create(floatImage).
Is there a way to convert an buffered image to Tensor? I am using ´tensorflow-core-api´ version ´0.3´.
Note: Converting the buffered as above takes longer, maybe there is a faster way to do it in Java.
Sorry, clicked "enter" too fast. So that being said, there are more efficient ways to do this. For example, you could allocate your float tensors right away and copy the buffered image into it, without passing by a standard array, since you already know the shape of it:
TFloat32 t = TFloat32.tensorOf(Shape.of(1, imgWidth, imgHeight, numberOfChannels), data -> {
// loop in your BufferedImage and initialize the tensor calling data.setFloat(pixelColor, idx...)
});
There should be also a way to convert directly your BufferedImage to a FloatDataBuffer that can directly be passed to initialize the tensor using this factory but pixel colors must be in the right order and normalized accordingly.
Let's try to fix it quickly before reopening an issue.
Please make sure to follow the instructions for adding TF to your dependencies. Especially make sure that you don't only depend on tensorflow-core-api but also one of its native archive.
A good trick is to simply depend on a platform that will import everything for you, e.g.
I had the problem that a network trained in TF 2.5.0 and exported as a protobuf .pb for using in TF1.5.0 in java would not allow dropout layers. But in the end it turned out only that the output of a layer would throw IllegalArgumentException when calling toString() on the layer output. The MLP still runs fine in java with the dropout layers.
What follows is the method that loads the MLP following by logging output. Note the try/catch around the toString on layer output.
I cannot tell if the network is actually utilizing the dropout or not, but the accuracy seems OK.
/**
* Loads network, return String message
*
* @param f the File pointing to .pb file
* @return String message
* @throws IOException if error opening file
*/
synchronized public String loadNetwork(File f) throws IOException {
if (f == null) {
throw new IOException("null file");
}
ArrayList<String> ioLayers = new ArrayList();
String sizeMsg = "";
StringBuilder b = new StringBuilder("TensorFlow Graph: \n");
try {
if (f.isDirectory()) {
log.info("loading \"serve\" graph from tensorflow SavedModelBundle folder " + f);
this.tfSavedModelBundle = SavedModelBundle.load(f.getCanonicalPath(), "serve");
this.tfExecutionGraph = tfSavedModelBundle.graph();
} else {
log.info("loading network from file " + f);
byte[] graphDef = Files.readAllBytes(Paths.get(f.getAbsolutePath())); // "tensorflow_inception_graph.pb"
this.tfExecutionGraph = new Graph();
this.tfExecutionGraph.importGraphDef(graphDef);
}
Iterator<Operation> itr = this.tfExecutionGraph.operations();
int opnum = 0;
ioLayers.clear();
while (itr.hasNext()) {
Operation o = itr.next();
// b.append(o.toString().toLowerCase());
// if(s.contains("input") || s.contains("output") || s.contains("placeholder")){
// if (s.contains("input")
// || s.contains("placeholder")
// || s.contains("output")
// || s.contains("prediction")) { // find input placeholder & output
int numOutputs = o.numOutputs();
// if(! s.contains("output_shape") && !s.contains("conv2d_transpos")){
// b.append("********** ");
// ioLayers.add(s);
for (int onum = 0; onum < numOutputs; onum++) {
Output output = o.output(onum);
Shape shape = output.shape();
if (opnum == 0) { // assume input layer
long nin = shape.size(1);
double sqrt = (Math.sqrt(nin));
boolean usesPolarity = sqrt % 1 != 0;
if (usesPolarity) {
sqrt = Math.sqrt(nin / 2);
}
int tiInputDim = (int) Math.round(sqrt);
sizeMsg = String.format("<html>Loaded MLP named \"%s\". <p>Set patchWidthAndHeightPixels=%d and useTIandPol=%s from input # pixels=%d", f.toString(), tiInputDim, usesPolarity, nin);
log.info(sizeMsg);
setPatchWidthAndHeightPixels(tiInputDim);
setUseTIandPol(usesPolarity);
}
String outputString = "(output string)";
try {
b.append(opnum++ + ": " + o.toString() + "\t" + output.toString() + "\n");
} catch (IllegalArgumentException iae) {
b.append(opnum++ + ": " + o.toString() + "\t" + iae.toString() + "\n");
}
// int numDimensions = shape.numDimensions();
// for (int dimidx = 0; dimidx < numDimensions; dimidx++) {
// long dim = shape.size(dimidx);
// }
}
// int inputLength=o.inputListLength("");
}
log.info(b.toString());
} catch (Exception e) {
log.warning(e.toString());
e.printStackTrace();
return e.toString();
}
return sizeMsg;
}
Activity
karllessard commentedon Mar 29, 2021
Yes, you need to convert your float array to a
NdArrayusing theStdArraysutilities, before initializing a tensor with it, i.e.micosacak commentedon Mar 29, 2021
Thank you very much solved the problem!
karllessard commentedon Mar 29, 2021
Sorry, clicked "enter" too fast. So that being said, there are more efficient ways to do this. For example, you could allocate your float tensors right away and copy the buffered image into it, without passing by a standard array, since you already know the shape of it:
There should be also a way to convert directly your
BufferedImageto aFloatDataBufferthat can directly be passed to initialize the tensor using this factory but pixel colors must be in the right order and normalized accordingly.karllessard commentedon Mar 29, 2021
No problem! BTW, 0.3.1 has been released, you should probably use this version now.
karllessard commentedon Mar 30, 2021
Let's try to fix it quickly before reopening an issue.
Please make sure to follow the instructions for adding TF to your dependencies. Especially make sure that you don't only depend on
tensorflow-core-apibut also one of its native archive.A good trick is to simply depend on a platform that will import everything for you, e.g.
micosacak commentedon Mar 30, 2021
Finally, I could manage the first step in loading the model and predicting the image in Java! Thank you very much!
I reported the problem here (#259) as well, now I will close it.
tobidelbruck commentedon Mar 15, 2025
I had the problem that a network trained in TF 2.5.0 and exported as a protobuf .pb for using in TF1.5.0 in java would not allow dropout layers. But in the end it turned out only that the output of a layer would throw IllegalArgumentException when calling toString() on the layer output. The MLP still runs fine in java with the dropout layers.
What follows is the method that loads the MLP following by logging output. Note the try/catch around the toString on layer output.
I cannot tell if the network is actually utilizing the dropout or not, but the accuracy seems OK.
Logging output