我正在尝试用yolov2
中的TensorFlow Lite
运行Android
。我在Android中集成了Yolo v2
,但未检测到任何图像。为了在Android中使用YoLo v2
模型,我遵循了以下步骤:
使用curl https://pjreddie.com/media/files/yolov2-tiny.weights -o yolov2-tiny.weights
下载的砝码
使用curl https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov2-tiny.cfg -o yolov2-tiny.cfg
下载的配置文件
下载的标签文件curl https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names -o label.txt
使用flow --model yolov2-tiny.cfg --load yolov2-tiny.weights --savepb
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
将张量流缓冲区转换为张量流lite最后,我有两个文件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
。我找不到丢失的元素。
任何人都可以指导为什么未检测到对象吗?
您是否曾尝试将TF模型转换为tflite?您也可以简单地尝试首先使用python运行tflite模型。这些将提示问题的根源。