/
/
创建输入数据
size_t inputCount
=
0
;
std::vector<NN_Tensor
*
> inputTensors;
OH_NN_ReturnCode ret
=
OH_NNExecutor_GetInputCount(executor, &inputCount);
if
(ret !
=
OH_NN_SUCCESS || inputCount !
=
inputData.size()) {
/
/
inputData为开发者构造的输入数据
OH_LOG_ERROR(LOG_APP,
"OH_NNExecutor_GetInputCount failed, size mismatch"
);
return
OH_NN_FAILED;
}
for
(size_t i
=
0
; i < inputCount;
+
+
i) {
NN_TensorDesc
*
tensorDesc
=
OH_NNExecutor_CreateInputTensorDesc(executor, i);
NN_Tensor
*
tensor
=
OH_NNTensor_Create(deviceID, tensorDesc);
/
/
deviceID的获取方式可参考
"加载离线模型"
的步骤
3
if
(tensor !
=
nullptr) {
inputTensors.push_back(tensor);
}
OH_NNTensorDesc_Destroy(&tensorDesc);
}
if
(inputTensors.size() !
=
inputCount) {
OH_LOG_ERROR(LOG_APP,
"input size mismatch"
);
DestroyTensors(inputTensors);
/
/
DestroyTensors为释放tensor内存操作函数
return
OH_NN_FAILED;
}
/
/
初始化输入数据
for
(size_t i
=
0
; i < inputTensors.size();
+
+
i) {
void
*
data
=
OH_NNTensor_GetDataBuffer(inputTensors[i]);
size_t dataSize
=
0
;
OH_NNTensor_GetSize(inputTensors[i], &dataSize);
if
(data
=
=
nullptr || dataSize !
=
inputData[i].size()) {
/
/
inputData为模型的输入数据
OH_LOG_ERROR(LOG_APP,
"invalid data or dataSize"
);
return
OH_NN_FAILED;
}
memcpy(data, inputData[i].data(), inputData[i].size());
/
/
inputData为模型的输入数据
}
/
/
创建输出数据,与输入数据的创建方式类似
size_t outputCount
=
0
;
std::vector<NN_Tensor
*
> outputTensors;
ret
=
OH_NNExecutor_GetOutputCount(executor, &outputCount);
if
(ret !
=
OH_NN_SUCCESS) {
OH_LOG_ERROR(LOG_APP,
"OH_NNExecutor_GetOutputCount failed"
);
DestroyTensors(inputTensors);
/
/
DestroyTensors为释放tensor内存操作函数
return
OH_NN_FAILED;
}
for
(size_t i
=
0
; i < outputCount; i
+
+
) {
NN_TensorDesc
*
tensorDesc
=
OH_NNExecutor_CreateOutputTensorDesc(executor, i);
NN_Tensor
*
tensor
=
OH_NNTensor_Create(deviceID, tensorDesc);
/
/
deviceID的获取方式可参考
"加载离线模型"
的步骤
3
if
(tensor !
=
nullptr) {
outputTensors.push_back(tensor);
}
OH_NNTensorDesc_Destroy(&tensorDesc);
}
if
(outputTensors.size() !
=
outputCount) {
DestroyTensors(inputTensors);
/
/
DestroyTensors为释放tensor内存操作函数
DestroyTensors(outputTensors);
/
/
DestroyTensors为释放tensor内存操作函数
OH_LOG_ERROR(LOG_APP,
"output size mismatch"
);
return
OH_NN_FAILED;
}