【问题标题】:No Detections while running Yolov2 with TensorFlow Lite in Android在 Android 中使用 TensorFlow Lite 运行 Yolov2 时未检测到
【发布时间】:2020-04-21 15:54:57
【问题描述】:

我正在尝试在Android 中运行yolov2 和TensorFlow Lite。我已经在 Android 中集成了Yolo v2,但它没有检测到任何图像。为了在Android中使用YoLo v2模型,我遵循了以下步骤:

  1. 使用curl https://pjreddie.com/media/files/yolov2-tiny.weights -o yolov2-tiny.weights下载的权重

  2. 使用curl https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov2-tiny.cfg -o yolov2-tiny.cfg下载的配置文件

  3. 已下载标签文件curl https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names -o label.txt 使用flow --model yolov2-tiny.cfg --load yolov2-tiny.weights --savepb

  4. 将权重转换为张量流协议缓冲区
  5. 使用tflite_convert --graph_def_file='/home/mustansar/Softwares/darkflow/built_graph/yolov2-tiny.pb' --output_file='/home/mustansar/Softwares/darkflow/built_graph/yolov2-tiny.lite' --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --input_shape=1,416,416,3 --input_array=input --output_array=output 将张量流缓冲区转换为张量流精简版

最后我有两个文件yolov2-tiny.lite 和yolov2-tiny.meta。在 Android 中,我使用依赖项:implementation('org.tensorflow:tensorflow-lite:0.0.0-nightly') { changing = true }

我已加载模型并将图像处理为:

检测器类:

@Override
  public List<Recognition> recognizeImage(final Bitmap bitmap) {
    convertBitmapToByteBuffer(bitmap);

    tfLite.run(imgData, outputLocations);

    return findRecognitionsUsingTensorExample();
  }

findRecognitionUsingTensorExample()

public ArrayList<Recognition> findRecognitionsUsingTensorExample() {
    float[][][] output = outputLocations[0];
//
    // Find the best detections.
    final PriorityQueue<Recognition> pq =
            new PriorityQueue<Recognition>(
                    1,
                    new Comparator<Recognition>() {
                      @Override
                      public int compare(final Recognition lhs, final Recognition rhs) {
                        // Intentionally reversed to put high confidence at the head of the queue.
                        return Float.compare(rhs.getConfidence(), lhs.getConfidence());
                      }
                    });

    for (int y = 0; y < gridHeight; ++y) {
      for (int x = 0; x < gridWidth; ++x) {
        for (int b = 0; b < NUM_BOXES_PER_BLOCK; ++b) {
          final int offset =
                  (gridWidth * (NUM_BOXES_PER_BLOCK * (NUM_CLASSES + 5))) * y
                          + (NUM_BOXES_PER_BLOCK * (NUM_CLASSES + 5)) * x
                          + (NUM_CLASSES + 5) * b;

          if(offset >= 416 || offset + 1 >= 416) continue;

          final float xPos = (x + expit(output[y][x][offset + 0])) * blockSize;
          final float yPos = (y + expit(output[y][x][offset + 1])) * blockSize;

          final float w = (float) (Math.exp(output[y][x][offset + 2]) * ANCHORS[2 * b + 0]) * blockSize;
          final float h = (float) (Math.exp(output[y][x][offset + 3]) * ANCHORS[2 * b + 1]) * blockSize;

          final RectF rect =
                  new RectF(
                          Math.max(0, xPos - w / 2),
                          Math.max(0, yPos - h / 2),
                          Math.min(INP_IMG_WIDTH - 1, xPos + w / 2),
                          Math.min(INP_IMG_HEIGHT - 1, yPos + h / 2));
          final float confidence = expit(output[y][x][offset + 4]);

          int detectedClass = -1;
          float maxClass = 0;

          final float[] classes = new float[NUM_CLASSES];
          for (int c = 0; c < NUM_CLASSES; ++c) {
            classes[c] = output[x][y][offset + 5 + c];
          }
          softmax(classes);

          for (int c = 0; c < NUM_CLASSES; ++c) {
            if (classes[c] > maxClass) {
              detectedClass = c;
              maxClass = classes[c];
            }
          }

          final float confidenceInClass = maxClass * confidence;
          if (confidenceInClass > THRESHOLD) {
            LOGGER.i(
                    "%s (%d) %f %s", LABELS[detectedClass], detectedClass, confidenceInClass, rect);
            pq.add(new Recognition("" + offset, LABELS[detectedClass], confidenceInClass, rect));
          }
        }
      }
    }
    final ArrayList<Recognition> recognitions = new ArrayList<Recognition>();
    for (int i = 0; i < Math.min(pq.size(), MAX_RESULTS); ++i) {
      recognitions.add(pq.poll());
    }

    return recognitions;
  }

来自yolov2-tiny.meta,我使用了配置,即来自文件的classes=80、threshold=0.6、image size = 416x416、labels 和来自元文件的anchors。我找不到丢失的元素。

谁能指导一下为什么没有检测到对象?

【问题讨论】:

    标签: tensorflow object-detection tensorflow-lite object-detection-api yolo


    【解决方案1】:

    您是否尝试过在将 TF 模型转换为 tflite 之前运行它?您也可以先尝试使用 python 运行 tflite 模型。这些将提示问题出在哪里。

    【讨论】:

    • 使用 python 验证 tflite 模型要运行什么脚本?
    猜你喜欢
    • 2021-01-26
    • 2020-08-17
    • 2020-01-11
    • 1970-01-01
    • 2019-11-12
    • 2021-12-18
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多