title: How to parse String output of a tensorflow model
body: <p>Created a model using the code here : <a href="https://gist.github.com/gaganmalhotra/1424bd3d0617e784976b29d5846b16b1" rel="nofollow noreferrer">https://gist.github.com/gaganmalhotra/1424bd3d0617e784976b29d5846b16b1</a></p>
  
  <p>To get the predictions of the probabilites in java it can be done using below code:</p>
  
  <pre><code>  public static void main(String[] args) {
      Session session = SavedModelBundle.load("/Users/gagandeep.malhotra/Documents/SampleTF_projects/tf_iris_model/1510707746/", "serve").session();
  
      Tensor x =
          Tensor.create(
              new long[] {2, 4},
              FloatBuffer.wrap(
                  new float[] {
                    6.4f, 3.2f, 4.5f, 1.5f,
                    5.8f, 3.1f, 5.0f, 1.7f
                  }));
  
      final String xName = "Placeholder:0";
      final String scoresName = "dnn/head/predictions/probabilities:0";
  
      List&lt;Tensor&lt;?&gt;&gt; outputs = session.runner()
          .feed(xName, x)
          .fetch(scoresName)
          .run();
  
      // Outer dimension is batch size; inner dimension is number of classes
      float[][] scores = new float[2][3];
  
      outputs.get(0).copyTo(scores);
      System.out.println(Arrays.deepToString(scores));
    }
  </code></pre>
  
  <p><strong>However if we want to copy over the predicted classes (string type) for the code below:</strong></p>
  
  <pre><code>final String xName = "Placeholder:0";
  final String className = "dnn/head/predictions/str_classes:0";
  
  List&lt;Tensor&lt;?&gt;&gt; outputs = session.runner()
      .feed(xName, x)
      .fetch(className)
      .run();
  
  // Outer dimension is batch size; inner dimension is number of classes
  String[][] classes = new String[2][1];
  
  outputs.get(0).copyTo(classes);
  System.out.println(Arrays.deepToString(classes));
  </code></pre>
  
  <p>I end up with an error like this: </p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: cannot copy Tensor with 2 dimensions into an object with 1
      at org.tensorflow.Tensor.throwExceptionIfTypeIsIncompatible(Tensor.java:739)
      at org.tensorflow.Tensor.copyTo(Tensor.java:450)
      at deeplearning.IrisTFLoad.main(IrisTFLoad.java:71)
  </code></pre>
  
  <p><strong>But the dimension is same as the output tensor: [STRING tensor with shape [2, 1]]</strong></p>
  
  <p>PS: The signature definition can be found as below - </p>
  
  <pre><code>The given SavedModel SignatureDef contains the following input(s):
      inputs['x'] tensor_info:
          dtype: DT_FLOAT
          shape: (-1, 4)
          name: Placeholder:0
      The given SavedModel SignatureDef contains the following output(s):
      outputs['class_ids'] tensor_info:
          dtype: DT_INT64
          shape: (-1, 1)
          name: dnn/head/predictions/ExpandDims:0
      outputs['classes'] tensor_info:
          dtype: DT_STRING
          shape: (-1, 1)
          name: dnn/head/predictions/str_classes:0
      outputs['logits'] tensor_info:
          dtype: DT_FLOAT
          shape: (-1, 3)
          name: dnn/head/logits:0
      outputs['probabilities'] tensor_info:
          dtype: DT_FLOAT
          shape: (-1, 3)
          name: dnn/head/predictions/probabilities:0
      Method name is: tensorflow/serving/predict
  </code></pre>
  
  <p>Things tried :</p>
  
  <p>Tensor tensor = (Tensor) outputs.get(0);
     byte[][][] result = tensor.copyTo(new byte[2][1][]);</p>
  
  <p>but errors out as below:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: invalid DataType(7)
      at org.tensorflow.Tensor.readNDArray(Native Method)
      at org.tensorflow.Tensor.copyTo(Tensor.java:451)
      at deeplearning.IrisTFLoad.main(IrisTFLoad.java:74)
  </code></pre>