opencv: Dnn.BlobFromImage returns (-1 * -1) Mat

I’m using OpenCV 3.4.3 in java in order to import and use a convolutional neural network built with Caffe. However the function BlobFromImage is returning an empty Mat, and i’m getting the following error when calling the net.forward():

Code:

 import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;

public class OpenCVTests {

	public static void main(String[] args) {

		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

		System.out.println("Loading caffe model...");
		Net net = Dnn.readNetFromCaffe("C:\\Program Files\\Docker\\volumes\\vol1\\model.prototxt");

		Mat image = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\cat.jpg");
		System.out.println(image);

		Mat blob = Dnn.blobFromImage(image, 1.0f, new Size(224, 224));
		System.out.println(blob);

		net.setInput(blob);
		Mat result = net.forward();
		System.out.println(result);

	}
}

Output:

Loading caffe model...
Mat [ 224*224*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x19659e40, dataAddr=0x19678ac0 ]
Mat [ -1*-1*CV_32FC1, isCont=true, isSubmat=false, nativeObj=0x1965a070, dataAddr=0x197a3100 ]
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.3) Z:\Karbala\build tools\opencv-3.4.3\modules\dnn\src\layers\convolution_layer.cpp:233: error: (-215:Assertion failed) blobs.size() != 0 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'
]
	at org.opencv.dnn.Net.forward_1(Native Method)
	at org.opencv.dnn.Net.forward(Net.java:62)
	at test.OpenCVTests.main(OpenCVTests.java:31)

Prototxt:

model.prototxt

Any help would be highly appreciated, thanks in advance!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17 (5 by maintainers)

Most upvoted comments

@peterbence3 @alalek @dkurt I have the same issue! The problem of method call Dnn.blobFromImage() should not be related with the missing .caffemodel weight file since the weight file is not used here. But the problem that returns empty blob still exists.

Take the following as an example:

import org.opencv.core.*;
import org.opencv.dnn.*;
import org.opencv.imgcodecs.Imgcodecs;

public class OpenCVTests {

	public static void main(String[] args) {

		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

		Mat image = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\cat.jpg");
		System.out.println(image);

		Mat blob = Dnn.blobFromImage(image, 1.0f, new Size(224, 224));
		System.out.println(blob);
	}
}

The printed results:

Mat [ 1080*1616*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0xa8dbe0, dataAddr=0x1748b080 ]
Mat [ -1*-1*CV_32FC1, isCont=true, isSubmat=false, nativeObj=0xaa2fd0, dataAddr=0x18d93080 ]

I don’t know why this problem occurs…

@Cogito2012 would you be able to post a gist for getting the dnn working on android?

@peterbence3, see https://github.com/opencv/opencv/issues/10802 Your problem is missed weights file in readNetFromCaffe.

blobFromImage() returns 4 dimensional Mat. Java dumper supports 2D Mat only.

  1. I see DL “model” parameter, but where are weights (.caffemodel file)?