Tensorflowlite学习(二)tflite模型在安卓的部署
步骤:一、在app的build.gradle中加入相关依赖。其内容自行百度。二、加载模型:/*** 此方法实现对 .tflite 文件的序列化* @param model* @return* @throws IOException*/private MappedByteBuffer loadModelFile(S...
·
步骤:
一、在app的build.gradle中加入相关依赖。其内容自行百度。
二、加载模型:
/**
* 此方法实现对 .tflite 文件的序列化
* @param model
* @return
* @throws IOException
*/
private MappedByteBuffer loadModelFile(String model) throws IOException {
AssetFileDescriptor fileDescriptor = getApplicationContext().getAssets().openFd(model + ".tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
/**
* 对序列化后的结果进行加载
* @param model
*/
private void load_model(String model) {
try {
tflite = new Interpreter(loadModelFile(model));
Toast.makeText(MainActivity.this, model + " model load success", Toast.LENGTH_SHORT).show();
Log.d(TAG, model + " model load success");
tflite.setNumThreads(4);
load_result = true;
} catch (IOException e) {
Toast.makeText(MainActivity.this, model + " model load fail", Toast.LENGTH_SHORT).show();
Log.d(TAG, model + " model load fail");
load_result = false;
e.printStackTrace();
}
}
三、运行结果:
Interpreter tflite = null;
...
//调用以上两个函数实现tflite的初始化
...
tflite.run(inputData,labelProbArray);
tflite.close(); //释放资源
更多推荐
已为社区贡献3条内容
所有评论(0)