为什么某些对象检测神经网络在OpenCV 4.1.0中返回全零?

Vla*_*nko 12 java opencv scala caffe tensorflow

我在评估来自Java / Scala的OpenCV 4.1.0中的多个神经网络时遇到问题。网络会返回全零的鱼自行车图像以及其他图像。我在以下方面观察到这一点:

检测可与YOLOv3-spp和YOLOv3-tiny https://pjreddie.com/darknet/yolo/一起使用

这个dnn烹饪有什么问题?

// The reproduce in Scala REPL you need a hack:

def loadLibraryScalaREPL(libraryName: String): Unit = {
  val loadLibrary0 = Runtime.getRuntime.getClass.getDeclaredMethods()(4)
  loadLibrary0.setAccessible(true)
  loadLibrary0.invoke(Runtime.getRuntime, scala.tools.nsc.interpreter.IMain.getClass, libraryName)
}

loadLibraryScalaREPL(org.opencv.core.Core.NATIVE_LIBRARY_NAME)

// To load in Java/Scala application just use System.loadLibrary.

import org.opencv.core.{Scalar, Size}
import org.opencv.dnn.Dnn
import org.opencv.imgcodecs.Imgcodecs

val image = Imgcodecs.imread("/root/fish-bike.jpg")
val net = Dnn.readNetFromCaffe("/tmp/VGG_coco_SSD_512x512_iter_360000.prototxt", "/tmp/VGG_coco_SSD_512x512_iter_360000.caffemodel")
val blob = Dnn.blobFromImage(image, 1/125.0, new Size(512, 512), new Scalar(104,117,123), true)
net.setInput(blob)
val layer = net.forward()
val values = new Array[Float](layer.total().toInt)
layer.get(0,0, values)
values.grouped(7).foreach(x => println(x.toList))
Run Code Online (Sandbox Code Playgroud)

Jul*_*ani 1

一些模型期望通道强度的归一化值。通常,图像以 uint8 像素表示(值范围为 0 ~ 255)。您需要将其转换为 float32(从 -1 ~ 1)。基本上,对于这样的模型,您的图像将被解释为空白图片(大部分都是白色像素)。

这是一个可用于标准化图像的 python 函数:

def processFrame(image):
    img = cv2.resize(image, (input_width, input_height)) # input sizes of detector 
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # Normalize pixel values if using a floating model
    img_rgb = (np.float32(img_rgb) - 127.5) / 127.5
Run Code Online (Sandbox Code Playgroud)