First time here? Check out the FAQ!

Ask Your Question
1

Problem with concat in cv::dnn? [closed]

asked Nov 16 '17

Maups gravatar image

I have the following code https://pastebin.com/DLfCRjd4 that uses the following model https://drive.google.com/open?id=17Zs... that was created with the following tensorflow code https://pastebin.com/j8q7GFfW

It is supposed to receive an 128x128 grayscale image as input, load the network from the file above, and then get the tensor of the last layer, which is a fully-connected layer. However, I'm getting the following error for both Opencv 3.3 and the master branch:

OpenCV 3.3

OpenCV Error: Assertion failed (srcMat.dims == 2 && srcMat.cols == weights.cols && dstMat.rows == srcMat.rows && dstMat.cols == weights.rows && srcMat.type() == weights.type() && weights.type() == dstMat.type() && srcMat.type() == CV_32F && (biasMat.empty() || (biasMat.type() == srcMat.type() && biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols))) in run, file /home/mauricio/Libraries/OpenCV3.3/opencv-3.3.0/modules/dnn/src/layers/fully_connected_layer.cpp, line 132
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/mauricio/Libraries/OpenCV3.3/opencv-3.3.0/modules/dnn/src/layers/fully_connected_layer.cpp:132: error: (-215) srcMat.dims == 2 && srcMat.cols == weights.cols && dstMat.rows == srcMat.rows && dstMat.cols == weights.rows && srcMat.type() == weights.type() && weights.type() == dstMat.type() && srcMat.type() == CV_32F && (biasMat.empty() || (biasMat.type() == srcMat.type() && biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols)) in function run

master branch

OpenCV Error: Assertion failed (srcMat.dims == 2 && srcMat.cols == weights.cols && dstMat.rows == srcMat.rows && dstMat.cols == weights.rows && srcMat.type() == weights.type() && weights.type() == dstMat.type() && srcMat.type() == 5 && (biasMat.empty() || (biasMat.type() == srcMat.type() && biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols))) in run, file /home/mauricio/Libraries/OpenCVmaster/opencv/modules/dnn/src/layers/fully_connected_layer.cpp, line 152
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/mauricio/Libraries/OpenCVmaster/opencv/modules/dnn/src/layers/fully_connected_layer.cpp:152: error: (-215) srcMat.dims == 2 && srcMat.cols == weights.cols && dstMat.rows == srcMat.rows && dstMat.cols == weights.rows && srcMat.type() == weights.type() && weights.type() == dstMat.type() && srcMat.type() == 5 && (biasMat.empty() || (biasMat.type() == srcMat.type() && biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols)) in function run

I found out that the problem is not in the fully connected layer itself, but actually before it. I have the following layers in my model:

Conv2D 1
add 2
Relu 3
Conv2D_1 4
add_1 5
Relu_1 6
MaxPool 7
Conv2D_2 8
add_2 9
Relu_2 10
MaxPool_1 11
Conv2D_3 12
add_3 13
Relu_3 14
MaxPool_2 15
Conv2D_4 16
add_4 17
Relu_4 18
MaxPool_3 19
Conv2D_5 20
add_5 21
Relu_5 22
Reshape 23
Reshape_1 24
concat 25
MatMul 26

The layers "Reshape" and "Reshape_1" have size 1x16384, so the layer "concat" should have size 1x32768, but it shows the same size of the layers it should be concatenating (1x16384). This is why the assertion for the size in the fully connected layer is ... (more)

Preview: (hide)

Closed for the following reason the question is answered, right answer was accepted by Maups
close date 2017-11-17 09:45:55.718914

1 answer

Sort by » oldest newest most voted
2

answered Nov 17 '17

dkurt gravatar image

@Maups, Thank you for the detailed question! This problem related to the data layout issue. By default, TensorFlow has NHWC (batch, height, width, channels) interpretation but OpenCV works with NCHW. So we need to map layers attributes to appropriate dimensions (i.e. concatenation by channels in TensorFlow defined with axis=3 and we map it to axis=1 internally). The fastest solution for your model is to use axis=-1: flat_concat = tf.concat([flat_h_pool4_drop, flat_h_lconv3], -1) as an alias to the last axis.

We're going to make TensorFlow importer more flexible for this kind of issues soon and your test case will be very helpful, many thanks!

Preview: (hide)

Comments

Thank you so much! It worked for me, but only with the master branch :)

Maups gravatar imageMaups (Nov 17 '17)edit

Question Tools

1 follower

Stats

Asked: Nov 16 '17

Seen: 2,082 times

Last updated: Nov 17 '17