Home | History | Annotate | Download | only in lite
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 package org.tensorflow.lite;
     17 
     18 import java.util.Arrays;
     19 
     20 /**
     21  * A typed multi-dimensional array used in Tensorflow Lite.
     22  *
     23  * <p>The native handle of a {@code Tensor} belongs to {@code NativeInterpreterWrapper}, thus not
     24  * needed to be closed here.
     25  */
     26 final class Tensor {
     27 
     28   static Tensor fromHandle(long nativeHandle) {
     29     return new Tensor(nativeHandle);
     30   }
     31 
     32   /** Reads Tensor content into an array. */
     33   <T> T copyTo(T dst) {
     34     if (NativeInterpreterWrapper.dataTypeOf(dst) != dtype) {
     35       throw new IllegalArgumentException(
     36           String.format(
     37               "Cannot convert an TensorFlowLite tensor with type %s to a Java object of "
     38                   + "type %s (which is compatible with the TensorFlowLite type %s)",
     39               dtype, dst.getClass().getName(), NativeInterpreterWrapper.dataTypeOf(dst)));
     40     }
     41     int[] dstShape = NativeInterpreterWrapper.shapeOf(dst);
     42     if (!Arrays.equals(dstShape, shapeCopy)) {
     43       throw new IllegalArgumentException(
     44           String.format(
     45               "Shape of output target %s does not match with the shape of the Tensor %s.",
     46               Arrays.toString(dstShape), Arrays.toString(shapeCopy)));
     47     }
     48     readMultiDimensionalArray(nativeHandle, dst);
     49     return dst;
     50   }
     51 
     52   final long nativeHandle;
     53   final DataType dtype;
     54   final int[] shapeCopy;
     55 
     56   private Tensor(long nativeHandle) {
     57     this.nativeHandle = nativeHandle;
     58     this.dtype = DataType.fromNumber(dtype(nativeHandle));
     59     this.shapeCopy = shape(nativeHandle);
     60   }
     61 
     62   private static native int dtype(long handle);
     63 
     64   private static native int[] shape(long handle);
     65 
     66   private static native void readMultiDimensionalArray(long handle, Object value);
     67 
     68   static {
     69     TensorFlowLite.init();
     70   }
     71 }
     72