一、输入数据

1、获取输入tensor

  1. /**
  2. * @brief get input tensor for given name.
  3. * @param session given session.
  4. * @param name given name. if NULL, return first input.
  5. * @return tensor if found, NULL otherwise.
  6. */
  7. Tensor* getSessionInput(const Session* session, const char* name);
  8. /**
  9. * @brief get all input tensors.
  10. * @param session given session.
  11. * @return all output tensors mapped with name.
  12. */
  13. const std::map<std::string, Tensor*>& getSessionInputAll(const Session* session) const;

Interpreter上提供了两个用于获取输入Tensor的方法:getSessionInput用于获取单个输入tensor,
getSessionInputAll用于获取输入tensor映射。

在只有一个输入tensor时,可以在调用getSessionInput时传入NULL/0以获取tensor。


2、直接填充数据

  1. auto inputTensor = interpreter->getSessionInput(session, NULL);
  2. inputTensor->host<float>()[0] = 1.f;

Tensor上最简洁的输入方式是直接利用host填充数据,但这种使用方式仅限于CPU后端,其他后端需要通过deviceid来输入。另一方面,用户需要自行处理NC4HW4NHWC数据格式上的差异。

对于非CPU后端,或不熟悉数据布局的用户,宜使用拷贝数据接口。

3、拷贝数据

3.1、NHWC: TensorFlow、0

  1. auto inputTensor = interpreter->getSessionInput(session, NULL);
  2. auto nhwcTensor = new Tensor(inputTensor, Tensor::TENSORFLOW);
  3. // nhwcTensor-host<float>()[x] = ...
  4. // host到device的输入数据拷贝
  5. inputTensor->copyFromHostTensor(nhwcTensor);
  6. delete nhwcTensor;

3.2、NCHW: Caffe、1

  1. auto inputTensor = interpreter->getSessionInput(session, NULL);
  2. auto nchwTensor = new Tensor(inputTensor, Tensor::CAFFE);
  3. // nchwTensor-host<float>()[x] = ...
  4. inputTensor->copyFromHostTensor(nchwTensor);
  5. delete nchwTensor;

3.3、NC4HW4: Caffe_C4、2

  1. auto inputTensor = interpreter->getSessionInput(session, NULL);
  2. auto nc4hw4Tensor = new Tensor(inputTensor, Tensor::CAFFE_C4);
  3. // nc4hw4Tensor-host<float>()[x] = ...
  4. inputTensor->copyFromHostTensor(nc4hw4Tensor);
  5. delete nc4hw4Tensor;

二、输出数据

1、获取输出tensor

  1. /**
  2. * @brief get output tensor for given name.
  3. * @param session given session.
  4. * @param name given name. if NULL, return first output.
  5. * @return tensor if found, NULL otherwise.
  6. */
  7. Tensor* getSessionOutput(const Session* session, const char* name);
  8. /**
  9. * @brief get all output tensors.
  10. * @param session given session.
  11. * @return all output tensors mapped with name.
  12. */
  13. const std::map<std::string, Tensor*>& getSessionOutputAll(const Session* session) const;

Interpreter上提供了两个用于获取输入Tensor的方法:getSessionInput用于获取单个输入tensor,
getSessionInputAll用于获取输入tensor映射。

在只有一个输入tensor时,可以在调用getSessionInput时传入NULL/0以获取tensor。

2、直接读取数据

由于绝大多数用户都不熟悉MNN底层数据布局,所以**不要使用这种方式!!!**

  1. auto outputTensor = interpreter->getSessionOutput(session, NULL);
  2. auto score = outputTensor->host<float>()[0];
  3. auto index = outputTensor->host<float>()[1];

3、拷贝数据

不熟悉MNN源码的用户,**必须使用这种方式获取输出!!!**

3.1、NHWC: TensorFlow、0

  1. auto outputTensor = interpreter->getSessionOutput(session, NULL);
  2. auto nhwcTensor = new Tensor(outputTensor, Tensor::TENSORFLOW);
  3. // device到host的数据拷贝
  4. outputTensor->copyToHostTensor(nhwcTensor);
  5. // memcpy((void *)data, (void *)nhwcTensor->host<float>(), tensor->size);
  6. auto score = nhwcTensor->host<float>()[0];
  7. auto index = nhwcTensor->host<float>()[1];
  8. // ...
  9. delete nhwcTensor;

3.2、NCHW: Caffe、1

  1. auto outputTensor = interpreter->getSessionOutput(session, NULL);
  2. auto nchwTensor = new Tensor(outputTensor, Tensor::CAFFE);
  3. outputTensor->copyToHostTensor(nchwTensor);
  4. auto score = nchwTensor->host<float>()[0];
  5. auto index = nchwTensor->host<float>()[1];
  6. // ...
  7. delete nchwTensor;

3.3、NC4HW4: Caffe_C4、2

  1. auto outputTensor = interpreter->getSessionOutput(session, NULL);
  2. auto nc4hw4Tensor = new Tensor(outputTensor, Tensor::CAFFE_C4);
  3. outputTensor->copyToHostTensor(nc4hw4Tensor);
  4. auto score = nc4hw4Tensor->host<float>()[0];
  5. auto index = nc4hw4Tensor->host<float>()[1];
  6. // ...
  7. delete nc4hw4Tensor;