[英]YOLOv4 detection in OpenCV.js causing Uncaught exception
我正在尝试使用 OpenCV.js 让 YOLOv4 检测工作。
看起来我已经成功地使用
const net = cv.readNetFromDarknet(files.config, files.weights);
我正在使用教程中的getBlobFromImage
来获取网络输入:
getBlobFromImage = function(inputSize, mean, std, swapRB, image) {
let mat;
if (typeof(image) === 'string') {
mat = cv.imread(image);
} else {
mat = image;
}
let matC3 = new cv.Mat(mat.matSize[0], mat.matSize[1], cv.CV_8UC3);
cv.cvtColor(mat, matC3, cv.COLOR_RGBA2BGR);
let input = cv.blobFromImage(matC3, std, new cv.Size(inputSize[0], inputSize[1]),
new cv.Scalar(mean[0], mean[1], mean[2]), swapRB);
matC3.delete();
return input;
}
但是,当我尝试在网络上forward
运行时,我收到一条错误消息,上面写着Uncaught
,后面跟着一个看似随机的数字。
我试过了
net.setInput(blob)
net.forward()
我得到了错误。
我在网上找到了一个来源,说您需要使用层名称和使用forward2
的输出向量,所以我尝试使用硬编码的层名称(因为 OpenCV.js 没有公开获取层名称的方法):
net.setInput(blob)
const outs = new cv.MatVector();
net.forward2(outs, ["yolo_139", "yolo_150", "yolo_161"]);
而且我仍然得到未捕获的错误。 我还调整了图像的大小,使宽度和高度都是 32 的倍数,这是我在某处读到的必要条件。
有任何想法吗? 我一直在寻找答案,但还没有找到任何东西。
编辑:有关错误的更多详细信息
我只是尝试运行它几次,这是输出:
Uncaught 279968808
Uncaught 279899688
[then, after refresh]
Uncaught 279968808
Uncaught 279899688
这是堆栈跟踪:
___cxa_throw opencv.js:30
<anonymous> opencv.js line 30 > WebAssembly.instantiate:899300
<anonymous> opencv.js line 30 > WebAssembly.instantiate:898391
<anonymous> opencv.js line 30 > WebAssembly.instantiate:315351
<anonymous> opencv.js line 30 > WebAssembly.instantiate:315293
<anonymous> opencv.js line 30 > WebAssembly.instantiate:545156
<anonymous> opencv.js line 30 > WebAssembly.instantiate:525900
<anonymous> opencv.js line 30 > WebAssembly.instantiate:525121
<anonymous> opencv.js line 30 > WebAssembly.instantiate:65086
<anonymous> opencv.js line 30 > WebAssembly.instantiate:65049
<anonymous> opencv.js line 30 > WebAssembly.instantiate:5596483
dynCall_iiiii opencv.js:30
dynCall_iiiii_9 opencv.js line 30 > Function:4
constructor_body opencv.js:30
constructor opencv.js:30
Mat opencv.js line 30 > Function:4
matFromImageData opencv.js:30
imread opencv.js:30
getBlobFromImage DetectApp.js:24
run DetectApp.js:63
<anonymous> debugger eval code:1
所以这是它无法运行getBlobFromImage
的一个实例,但它只是偶尔会失败,它通常会成功。 这是更常见的堆栈跟踪,在dnn_Net$forward2
中出现故障:
___cxa_throw opencv.js:30
<anonymous> opencv.js line 30 > WebAssembly.instantiate:899300
<anonymous> opencv.js line 30 > WebAssembly.instantiate:898391
<anonymous> opencv.js line 30 > WebAssembly.instantiate:315351
<anonymous> opencv.js line 30 > WebAssembly.instantiate:315293
<anonymous> opencv.js line 30 > WebAssembly.instantiate:545156
<anonymous> opencv.js line 30 > WebAssembly.instantiate:525900
<anonymous> opencv.js line 30 > WebAssembly.instantiate:526317
<anonymous> opencv.js line 30 > WebAssembly.instantiate:697106
<anonymous> opencv.js line 30 > WebAssembly.instantiate:327453
<anonymous> opencv.js line 30 > WebAssembly.instantiate:529953
<anonymous> opencv.js line 30 > WebAssembly.instantiate:2720179
<anonymous> opencv.js line 30 > WebAssembly.instantiate:2717263
<anonymous> opencv.js line 30 > WebAssembly.instantiate:2717095
<anonymous> opencv.js line 30 > WebAssembly.instantiate:3384719
<anonymous> opencv.js line 30 > WebAssembly.instantiate:3323788
<anonymous> opencv.js line 30 > WebAssembly.instantiate:3318607
<anonymous> opencv.js line 30 > WebAssembly.instantiate:3365552
<anonymous> opencv.js line 30 > WebAssembly.instantiate:2646791
<anonymous> opencv.js line 30 > WebAssembly.instantiate:141167
<anonymous> opencv.js line 30 > WebAssembly.instantiate:204282
<anonymous> opencv.js line 30 > WebAssembly.instantiate:5596551
dynCall_viiii opencv.js:30
dynCall_viiii_1868 opencv.js line 30 > Function:4
dnn_Net$forward2 opencv.js line 30 > Function:10
run DetectApp.js:66
<anonymous> debugger eval code:1
错误消息非常不透明。 如果您在最新版本上运行并且仍然崩溃而没有明确的错误消息,则可能在 OpenCV 的 github 上提交问题,但首先检查是否尚未报告。
我没有在您的推理代码中看到任何大小调整。 如果您拥有的推理代码尚未调整输入大小,您应该尝试添加它。
神经网络通常需要特定大小的输入*。 对于 YOLOv4,可能是416x416 或 224x224 (很小?),这是在设计网络时确定的。
在训练期间,要么所有输入都已经是正确的大小,要么训练脚本调整了输入的大小。
* 全卷积网络采用任意大小
Uncaught
的数字不是随机数,它们是指针。 当分类器没有正确配置时,通常会发生这样的问题。
确保 config 和 weights 参数有效,还尝试使用显式后端选择
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.