Python tfliteGpuDelegate调用写入缓冲区失败源数据大于缓冲区

Python tfliteGpuDelegate调用写入缓冲区失败源数据大于缓冲区,python,tensorflow,tensorflow-lite,Python,Tensorflow,Tensorflow Lite,我按照中的步骤将gpu添加到tflite解释器中。android项目tensorflow/lite/jave/demo给出的mobilenet模型可以在Pixel 2上使用该项目。然而,当它被放到另一个项目中时,代码如下。下面是错误: TfLiteGpuDelegate Invoke: Write to buffer failed. Source data is larger than buffer Node number 31 (TfLiteGpuDelegateV2) failed to i

我按照中的步骤将gpu添加到tflite解释器中。android项目tensorflow/lite/jave/demo给出的mobilenet模型可以在Pixel 2上使用该项目。然而,当它被放到另一个项目中时,代码如下。下面是错误:

TfLiteGpuDelegate Invoke: Write to buffer failed. Source data is larger than buffer
Node number 31 (TfLiteGpuDelegateV2) failed to invoke.
imgData,outputClass已经过检查,并且与tensorflow/lite/jave/demo项目中的类大小相同

import  org.tensorflow.lite.TensorFlowLite;
import org.tensorflow.lite.gpu.GpuDelegate;

/**
 * Wrapper for frozen detection models trained using the Tensorflow Object Detection API:
 * github.com/tensorflow/models/tree/master/research/object_detection
 */
public class TFLiteObjectDetectionAPIModel implements Classifier {
  private static final Logger LOGGER = new Logger();

  // Only return this many results.
  private static final int NUM_DETECTIONS = 1001; // hand landmark ,21, mobilenet, 1001
  // Float model
  private static final float IMAGE_MEAN = 128.0f;
  private static final float IMAGE_STD = 128.0f;
  private boolean isModelQuantized;
  private int inputSize;
  private Vector<String> labels = new Vector<String>();
  private int[] intValues;
  private float[][][] outputLocations;
  private float[][] outputClasses;
  private float[][] outputScores;
  private float[] numDetections;

  private GpuDelegate gpuDelegate = new GpuDelegate();
  private final Interpreter.Options options = (new Interpreter.Options()).addDelegate(gpuDelegate);
  private ByteBuffer imgData;

  private Interpreter tfLite;
  private ArrayList<Recognition> recognitions = new ArrayList<Recognition>();

  private TFLiteObjectDetectionAPIModel() {}

  /** Memory-map the model file in Assets. */
  private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
      throws IOException {
    FileInputStream inputStream = new FileInputStream(new File("/sdcard/sunny/data/"+modelFilename+".tflite"));

    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileChannel.position();
    long declaredLength = fileChannel.size();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
  }

  /**
   * Initializes a native TensorFlow session for classifying images.
   *
   * @param assetManager The asset manager to be used to load assets.
   * @param modelFilename The filepath of the model GraphDef protocol buffer.
   * @param labelFilename The filepath of label file for classes.
   * @param inputSize The size of image input
   * @param isQuantized Boolean representing model is quantized or not
   */
  public static Classifier create(
      final AssetManager assetManager,
      final String modelFilename,
      final String labelFilename,
      final int inputSize,
      final boolean isQuantized)
      throws IOException {
    final TFLiteObjectDetectionAPIModel d = new TFLiteObjectDetectionAPIModel();

    InputStream labelsInput = null;
    String actualFilename = labelFilename.split("file:///android_asset/")[1];
    labelsInput = assetManager.open(actualFilename);
    BufferedReader br = null;
    br = new BufferedReader(new InputStreamReader(labelsInput));
    String line;
    while ((line = br.readLine()) != null) {
      LOGGER.w(line);
      d.labels.add(line);
    }
    br.close();

    d.inputSize = inputSize;
    try {
      d.tfLite = new Interpreter(loadModelFile(assetManager, modelFilename),d.options);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    d.isModelQuantized = isQuantized;
    // Pre-allocate buffers.
    int numBytesPerChannel;
    if (isQuantized) {
      numBytesPerChannel = 1; // Quantized
    } else {
      numBytesPerChannel = 4; // Floating point
    }
    d.imgData = ByteBuffer.allocateDirect(1 * d.inputSize * d.inputSize * 3 * 4);
    d.imgData.order(ByteOrder.nativeOrder());
    d.intValues = new int[d.inputSize * d.inputSize];

    d.outputClasses = new float[1][NUM_DETECTIONS];

    return d;
  }

  @Override
  public List<Recognition> processImage(final AssetManager assetManager, Classifier.Recognition.inputFormat imageFormat, int[] intValues){
    Trace.beginSection("preprocessBitmap");

    imgData.rewind();
    for (int i = 0; i < inputSize; ++i) {
      for (int j = 0; j < inputSize; ++j) {
        int pixelValue = intValues[i * inputSize + j];
        if (isModelQuantized) {
          // Quantized model
          imgData.put((byte) ((pixelValue >> 16) & 0xFF));
          imgData.put((byte) ((pixelValue >> 8) & 0xFF));
          imgData.put((byte) (pixelValue & 0xFF));
        } else { // Float model
          imgData.putFloat((((pixelValue >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
          imgData.putFloat((((pixelValue >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
          imgData.putFloat(((pixelValue & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        }
      }
    }
    Trace.endSection(); // preprocessBitmap

    // Copy the input data into TensorFlow.
    Trace.beginSection("feed");
    outputClasses = new float[1][NUM_DETECTIONS];
    Trace.endSection();

    // Run the inference call.
    Trace.beginSection("run");
    tfLite.run(imgData, outputClasses);
    Trace.endSection();

    return recognitions;
  }


  @Override
  public void enableStatLogging(final boolean logStats) {}

  @Override
  public String getStatString() {
    return "";
  }

  @Override
  public void close() {
    tfLite.close();
    tfLite = null;
    recognitions.clear();
    recognitions=null;
    gpuDelegate.close();
    gpuDelegate = null;
}

TfLite和TfLiteGpu的版本也与tensorflow/lite/jave/demo项目中的版本相同

import  org.tensorflow.lite.TensorFlowLite;
import org.tensorflow.lite.gpu.GpuDelegate;

/**
 * Wrapper for frozen detection models trained using the Tensorflow Object Detection API:
 * github.com/tensorflow/models/tree/master/research/object_detection
 */
public class TFLiteObjectDetectionAPIModel implements Classifier {
  private static final Logger LOGGER = new Logger();

  // Only return this many results.
  private static final int NUM_DETECTIONS = 1001; // hand landmark ,21, mobilenet, 1001
  // Float model
  private static final float IMAGE_MEAN = 128.0f;
  private static final float IMAGE_STD = 128.0f;
  private boolean isModelQuantized;
  private int inputSize;
  private Vector<String> labels = new Vector<String>();
  private int[] intValues;
  private float[][][] outputLocations;
  private float[][] outputClasses;
  private float[][] outputScores;
  private float[] numDetections;

  private GpuDelegate gpuDelegate = new GpuDelegate();
  private final Interpreter.Options options = (new Interpreter.Options()).addDelegate(gpuDelegate);
  private ByteBuffer imgData;

  private Interpreter tfLite;
  private ArrayList<Recognition> recognitions = new ArrayList<Recognition>();

  private TFLiteObjectDetectionAPIModel() {}

  /** Memory-map the model file in Assets. */
  private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
      throws IOException {
    FileInputStream inputStream = new FileInputStream(new File("/sdcard/sunny/data/"+modelFilename+".tflite"));

    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileChannel.position();
    long declaredLength = fileChannel.size();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
  }

  /**
   * Initializes a native TensorFlow session for classifying images.
   *
   * @param assetManager The asset manager to be used to load assets.
   * @param modelFilename The filepath of the model GraphDef protocol buffer.
   * @param labelFilename The filepath of label file for classes.
   * @param inputSize The size of image input
   * @param isQuantized Boolean representing model is quantized or not
   */
  public static Classifier create(
      final AssetManager assetManager,
      final String modelFilename,
      final String labelFilename,
      final int inputSize,
      final boolean isQuantized)
      throws IOException {
    final TFLiteObjectDetectionAPIModel d = new TFLiteObjectDetectionAPIModel();

    InputStream labelsInput = null;
    String actualFilename = labelFilename.split("file:///android_asset/")[1];
    labelsInput = assetManager.open(actualFilename);
    BufferedReader br = null;
    br = new BufferedReader(new InputStreamReader(labelsInput));
    String line;
    while ((line = br.readLine()) != null) {
      LOGGER.w(line);
      d.labels.add(line);
    }
    br.close();

    d.inputSize = inputSize;
    try {
      d.tfLite = new Interpreter(loadModelFile(assetManager, modelFilename),d.options);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    d.isModelQuantized = isQuantized;
    // Pre-allocate buffers.
    int numBytesPerChannel;
    if (isQuantized) {
      numBytesPerChannel = 1; // Quantized
    } else {
      numBytesPerChannel = 4; // Floating point
    }
    d.imgData = ByteBuffer.allocateDirect(1 * d.inputSize * d.inputSize * 3 * 4);
    d.imgData.order(ByteOrder.nativeOrder());
    d.intValues = new int[d.inputSize * d.inputSize];

    d.outputClasses = new float[1][NUM_DETECTIONS];

    return d;
  }

  @Override
  public List<Recognition> processImage(final AssetManager assetManager, Classifier.Recognition.inputFormat imageFormat, int[] intValues){
    Trace.beginSection("preprocessBitmap");

    imgData.rewind();
    for (int i = 0; i < inputSize; ++i) {
      for (int j = 0; j < inputSize; ++j) {
        int pixelValue = intValues[i * inputSize + j];
        if (isModelQuantized) {
          // Quantized model
          imgData.put((byte) ((pixelValue >> 16) & 0xFF));
          imgData.put((byte) ((pixelValue >> 8) & 0xFF));
          imgData.put((byte) (pixelValue & 0xFF));
        } else { // Float model
          imgData.putFloat((((pixelValue >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
          imgData.putFloat((((pixelValue >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
          imgData.putFloat(((pixelValue & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        }
      }
    }
    Trace.endSection(); // preprocessBitmap

    // Copy the input data into TensorFlow.
    Trace.beginSection("feed");
    outputClasses = new float[1][NUM_DETECTIONS];
    Trace.endSection();

    // Run the inference call.
    Trace.beginSection("run");
    tfLite.run(imgData, outputClasses);
    Trace.endSection();

    return recognitions;
  }


  @Override
  public void enableStatLogging(final boolean logStats) {}

  @Override
  public String getStatString() {
    return "";
  }

  @Override
  public void close() {
    tfLite.close();
    tfLite = null;
    recognitions.clear();
    recognitions=null;
    gpuDelegate.close();
    gpuDelegate = null;
}

导入org.tensorflow.lite.TensorFlowLite;
导入org.tensorflow.lite.gpu.GpuDelegate;
/**
*使用Tensorflow对象检测API培训的冻结检测模型包装器:
*github.com/tensorflow/models/tree/master/research/object\u detection
*/
公共类TFLiteObjectDetectionAPIModel实现分类器{
专用静态最终记录器=新记录器();
//只返回这么多结果。
私有静态最终int NUM_DETECTIONS=1001;//hand landmark,21,mobilenet,1001
//浮动模型
专用静态最终浮点图像_平均值=128.0f;
专用静态最终浮动图像_标准=128.0f;
私有布尔是量化的;
私有int输入大小;
私有向量标签=新向量();
私有int[]int值;
专用浮点[][]输出位置;
私有浮点[][]输出类;
私人浮动[]输出分数;
私有float[]numDetections;
私有GpuDelegate GpuDelegate=新的GpuDelegate();
私有最终解释器.Options选项=(新解释器.Options()).addDelegate(gpuDelegate);
私人拜特布弗·伊姆格达塔;
专用解释器tfLite;
私有ArrayList识别=新建ArrayList();
私有TFLiteObjectDetectionAPIModel(){}
/**内存映射资源中的模型文件*/
私有静态MappedByteBuffer加载模型文件(AssetManager资产,字符串模型文件名)
抛出IOException{
FileInputStream-inputStream=newfileinputstream(新文件(“/sdcard/sunny/data/”+modelFilename+”.tflite”);
FileChannel FileChannel=inputStream.getChannel();
long startOffset=fileChannel.position();
long declaredLength=fileChannel.size();
返回fileChannel.map(fileChannel.MapMode.READ_ONLY,startOffset,declaredLength);
}
/**
*初始化用于对图像进行分类的本机TensorFlow会话。
*
*@param assetManager用于加载资产的资产管理器。
*@param modelFilename模型GraphDef协议缓冲区的文件路径。
*@param labelFilename类的标签文件的文件路径。
*@param inputSize图像输入的大小
*@param isQuantized布尔表示模型是否量化
*/
公共静态分类器创建(
最终资产管理人资产管理人,
最终字符串模型文件名,
最后一个字符串labelFilename,
最终整数输入大小,
最终布尔值(量化)
抛出IOException{
最终TFLiteObjectDetectionAPIModel d=新的TFLiteObjectDetectionAPIModel();
InputStream LabelInput=null;
字符串actualFilename=labelFilename.split(“file:///android_asset/")[1];
labelsInput=assetManager.open(实际文件名);
BufferedReader br=null;
br=新的BufferedReader(新的InputStreamReader(LabelInput));
弦线;
而((line=br.readLine())!=null){
LOGGER.w(行);
d、 标签。添加(行);
}
br.close();
d、 inputSize=inputSize;
试一试{
d、 tfLite=新解释器(loadModelFile(assetManager,modelFilename),d.options);
}捕获(例外e){
抛出新的运行时异常(e);
}
d、 isModelQuantized=isQuantized;
//预先分配缓冲区。
int numBytesPerChannel;
如果(已量化){
numBytesPerChannel=1;//量化
}否则{
numBytesPerChannel=4;//浮点
}
d、 imgData=ByteBuffer.allocateDirect(1*d.inputSize*d.inputSize*3*4);
d、 imgData.order(ByteOrder.nativeOrder());
d、 intValues=newint[d.inputSize*d.inputSize];
d、 outputClasses=新浮点[1][NUM_检测];
返回d;
}
@凌驾
公共列表processImage(最终AssetManager AssetManager,Classifier.Recognition.inputFormat imageFormat,int[]intValues){
Trace.beginestation(“预处理位图”);
imgData.rewind();
对于(int i=0;i>16)和0xFF));
imgData.put((字节)((像素值>>8)和0xFF));
put((字节)(像素值&0xFF));
}else{//Float模型
imgData.putFloat(((像素值>>16)和0xFF)-图像平均值/图像标准值);
imgData.putFloat(((像素值>>8)和0xFF)-图像平均值/图像标准值);
imgData.putFloat((像素值&0xFF)-图像平均值/图像标准值);
}
}
}
Trace.endSection();//预处理位图
//将输入数据复制到TensorFlow中。
微量元素(“饲料”);
outputClasses=新浮点[1][NUM_检测];
Trace.endSection();
//运行推断调用。
Trace.beginestation(“run”);
运行(imgData,outputClass);
Trace.endSection();
退货确认;
}
@凌驾
public void enableStatLogging(最终布尔logStats){}
@凌驾
公共字符串getStatString(){
返回“”;
}
@凌驾
公众假期结束(){
tfLite.close();
tfLite=null;
识别。清除();
识别=空;
gpuDelegate.close();
gpuDelegate=null;
}

您是否强制isQuantized?据我所知,GpuDelegate需要浮点数据。否,isQuantized=false。tflite模型也是浮点模型。