本文以花朵识别项目为例
一、训练花朵识别模型
使用Colab训练TensorFlow Lite模型。训练后下载相应的模型文件(model.tflite)、标签文件(labels.txt)。
二、配置项目
implementation('org.tensorflow:tensorflow-lite:0.0.0-nightly') {
changing = true } implementation('org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly') {
changing = true } implementation('org.tensorflow:tensorflow-lite-support:0.0.0-nightly') {
changing = true }
讯享网
为防止编译项目时TensorFlow Lite模型文件被编译,android{}代码块中添加
讯享网aaptOptions {
noCompress "tflite" }
三、编写代码
1、初始化解释器,加载模型、标签文件,指定运行代理、线程数量
/ * 初始化分类器 * @param activity * @param device * @param numThreads * @throws IOException */ protected Classifier(Activity activity, Device device, int numThreads) throws IOException {
tfliteModel = FileUtil.loadMappedFile(activity, getModelPath()); switch (device) {
case NNAPI: nnApiDelegate = new NnApiDelegate(); tfliteOptions.addDelegate(nnApiDelegate); break; case GPU: gpuDelegate = new GpuDelegate(); tfliteOptions.addDelegate(gpuDelegate); break; case CPU: break; } tfliteOptions.setNumThreads(numThreads); tflite = new Interpreter(tfliteModel, tfliteOptions); // Loads labels out from the label file. labels = FileUtil.loadLabels(activity, getLabelPath()); // Reads type and shape of input and output tensors, respectively. int imageTensorIndex = 0; int[] imageShape = tflite.getInputTensor(imageTensorIndex).shape(); // {
1, height, width, 3} imageSizeY = imageShape[1]; imageSizeX = imageShape[2]; DataType imageDataType = tflite.getInputTensor(imageTensorIndex).dataType(); int probabilityTensorIndex = 0; int[] probabilityShape = tflite.getOutputTensor(probabilityTensorIndex).shape(); // {
1, NUM_CLASSES} DataType probabilityDataType = tflite.getOutputTensor(probabilityTensorIndex).dataType(); // Creates the input tensor. inputImageBuffer = new TensorImage(imageDataType); // Creates the output tensor and its processor. outputProbabilityBuffer = TensorBuffer.createFixedSize(probabilityShape, probabilityDataType); // Creates the post processor for the output probability. probabilityProcessor = new TensorProcessor.Builder().add(getPostprocessNormalizeOp()).build(); LOGGER.d("Created a Tensorflow Lite Image Classifier."); }
2、运行解释器,根据图片获取相应的预测结果。
讯享网/ * 运行解释器并返回结果 * @param bitmap * @param sensorOrientation * @return */ public List<Recognition> recognizeImage(final Bitmap bitmap, int sensorOrientation) {
// Logs this method so that it can be analyzed with systrace. Trace.beginSection("recognizeImage"); Trace.beginSection("loadImage"); long startTimeForLoadImage = SystemClock.uptimeMillis(); inputImageBuffer = loadImage(bitmap, sensorOrientation); long endTimeForLoadImage = SystemClock.uptimeMillis(); Trace.endSection(); LOGGER.v("Timecost to load the image: " + (endTimeForLoadImage - startTimeForLoadImage)); // Runs the inference call. Trace.beginSection("runInference"); long startTimeForReference = SystemClock.uptimeMillis(); tflite.run(inputImageBuffer.getBuffer(), outputProbabilityBuffer.getBuffer().rewind()); long endTimeForReference = SystemClock.uptimeMillis(); Trace.endSection(); LOGGER.v("Timecost to run model inference: " + (endTimeForReference - startTimeForReference)); // Gets the map of label and probability. Map<String, Float> labeledProbability = new TensorLabel(labels, probabilityProcessor.process(outputProbabilityBuffer)) .getMapWithFloatValue(); Trace.endSection(); // Gets top-k results. return getTopKProbability(labeledProbability); }
3、关闭解释器、释放资源
public void close() {
if (tflite != null) {
tflite.close(); tflite = null; } if (gpuDelegate != null) {
gpuDelegate.close(); gpuDelegate = null; } if (nnApiDelegate != null) {
nnApiDelegate.close(); nnApiDelegate = null; } tfliteModel = null; }
四、关键类解释
讯享网try (Interpreter interpreter = new Interpreter(tensorflow_lite_model_file)) {
interpreter.run(input, output); }
GpuDelegate delegate = new GpuDelegate(); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate); Interpreter interpreter = new Interpreter(tensorflow_lite_model_file, options); try {
interpreter.run(input, output); }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/73800.html