Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "slice index 0 of dimension 0 out of bounds" error in Tensorflow for Java?

I have a model with the following signature that I'm trying to invoke using tensorflow for Java:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['jpegbase64_bytes'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['predictions'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 256)
        name: model/global_average_pooling2d/Mean:0
  Method name is: tensorflow/serving/predict

My code to invoke the model looks like this:

float[] predict(byte[] imageBytes) {
    try (Tensor result = SavedModelBundle.load("model.pb", "serve").session().runner()
            .feed("myinput", 0, TString.tensorOfBytes(NdArrays.scalarOfObject(imageBytes)))
            .fetch("myoutput")
            .run()
            .get(0)) {
        float[] buffer = new float[256];
        FloatNdArray floatNdArray = FloatDenseNdArray.create(RawDataBufferFactory.create(buffer, false),
                Shape.of(1, description.getNumFeatures()));
        ((TFloat32) result).copyTo(floatNdArray);
        return buffer;
    }
}

However, this throws the following errors:

slice index 0 of dimension 0 out of bounds.
     [[{{node map/TensorArrayUnstack/strided_slice}}]]
org.tensorflow.exceptions.TFInvalidArgumentException: slice index 0 of dimension 0 out of bounds.
     [[{{node map/TensorArrayUnstack/strided_slice}}]]
    at org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:87)
    at org.tensorflow.Session.run(Session.java:691)
    at org.tensorflow.Session.access$100(Session.java:72)
    at org.tensorflow.Session$Runner.runHelper(Session.java:381)
    at org.tensorflow.Session$Runner.run(Session.java:329)
    at com.mridang.myapp.ImageModel.predict(ImageModel.java:69)
    ...
    ...
    ...
    ...

From what I've understood, the model requires a dense-type string tensor while mine isn't. I found this answer on Stackoverflow slice index 0 of dimension 0 out of bounds using Java API but that seems to relate to very old version of tensorflow.

I'm using these dependencies:

layer group: 'org.tensorflow', name: 'tensorflow-core-platform', version: '0.3.1'
layer group: 'org.tensorflow', name: 'tensorflow-framework', version: '0.3.1'
like image 565
Mridang Agarwalla Avatar asked Oct 14 '22 20:10

Mridang Agarwalla


1 Answers

Thanks to @jccampanero answer. A bit of digging and I found a reference in the Zoltar library that showed how to do this.

https://github.com/spotify/zoltar/blob/b2c4c86f06c043aae505c533467e8a42d12da2d8/zoltar-tensorflow/src/main/java/com/spotify/zoltar/tf/TensorFlowPredictFn.java#L69

I need to create a vector of objects apparently and the following snippet did the trick.

float[] predict(byte[] imageBytes) {
    try (Tensor result = SavedModelBundle.load("model.pb", "serve").session().runner()
            .feed("myinput", 0, TString.tensorOfBytes(NdArrays.vectorOfObjects(imageBytes)))
            .fetch("myoutput")
            .run()
            .get(0)) {
        float[] buffer = new float[description.getNumFeatures()];
        FloatNdArray floatNdArray = FloatDenseNdArray.create(RawDataBufferFactory.create(buffer, false),
                Shape.of(1, description.getNumFeatures()));
        ((TFloat32) result).copyTo(floatNdArray);
        return buffer;
    }
}
like image 178
Mridang Agarwalla Avatar answered Oct 18 '22 10:10

Mridang Agarwalla