Repository navigation
Description
System information (version)
- OpenCV => 3.4
Detailed description
The untrainable mode in ScaleLayer uses the second input as scale factors for the first input.
outputs[0] = inputs[0] * inputs[1] // elementwise multiplication with broadcasting
The code which performs the above operation appears to assume that the batch size is one (but there is no assertion to enforce the requirement).
opencv/modules/dnn/src/layers/scale_layer.cpp
Lines 86 to 94 in 3efa783
| // There is a mode when we multiply a first blob by a second one | |
| // instead of trainable weights. | |
| Mat weights = blobs.empty() ? inputs[1] : (hasWeights ? blobs[0] : Mat()); | |
| Mat bias = hasBias ? blobs.back().reshape(1, 1) : Mat(); | |
| if (!weights.empty()) | |
| weights = weights.reshape(1, 1); | |
| MatShape inpShape = shape(inpBlob); | |
| const int numWeights = !weights.empty() ? weights.total() : bias.total(); | |
| CV_Assert(numWeights != 0); |
weights is reshaped (assuming that batch size is one?).
opencv/modules/dnn/src/layers/scale_layer.cpp
Lines 98 to 104 in 3efa783
| int endAxis; | |
| for (endAxis = axis + 1; endAxis <= inpBlob.dims; ++endAxis) | |
| { | |
| if (total(inpShape, axis, endAxis) == numWeights) | |
| break; | |
| } | |
| CV_Assert(total(inpShape, axis, endAxis) == numWeights); |
This code attempts to look for broadcasting options. It does so by attempting to match numWeights (which is weights.total()) to a subrange in the input tensor starting from axis. This logic will fail to find a match and depending on input shapes will either lead to incorrect outputs or tip-off downstream assertions
Example:
input shape: 2 3 3 3
weights input: 2 3 1 1
The shapes are compatible for an elementwise operation. The current logic will fail to see it.
weights is reshaped to 6 1. The matching code tries to look for a subrange in the input (2 3 3 3) which totals to 6 but there is no such range.
This particular example triggers an assertion.
(-215:Assertion failed) start <= (int)shape.size() && end <= (int)shape.size() && start <= end in function 'total'
Steps to reproduce
A test for PR16436 (not merged yet) can reproduce it:
opencv/modules/dnn/test/test_darknet_importer.cpp
Lines 547 to 548 in a422686
| // TODO: test fails for batches due to a bug/missing feature in ScaleLayer | |
| testDarknetLayer("scale_channels", false, false); |
Change testDarknetLayer("scale_channels", false, false); to testDarknetLayer("scale_channels", false, true);
Activity
YashasSamaga commentedon Feb 21, 2020
Duplicate of #13626
ashishkrshrivastava commentedon Mar 10, 2020
@dkurt lately I have been working on this issue and I think I have solved it locally if my assumptions are correct.
Scale(weights) and shift(bias) can either be input or blobs. in both cases shape should be [ N, C, H, W ].
or it is possible to have [ C, H, W ] ?
All possible shapes that I think are:
[N]
[N, C]
[N, C, H]
[N, C, H, W]
[N, H]
[N, H, W]
[N, W]
blobs shape will always be [1, C, H, W] no matter what batch size of input[0] is. (there wont be any input[1] in this case) ?
if weight is input (input[1]) then its batch size should be equal to batch size of input[0], N for both input[0] and input[1] is same ?
Am I right ?
asmorkalov commentedon May 15, 2023
@dkurt, @YashasSamaga Is the issue still relevant?
opencv-alalek commentedon May 15, 2023
I believe yes. Try
testDarknetLayer(..., /*testBatchProcessing =*/ true)in the mentioned test.Probably it should be targeted to 4.9.0+ milestone.
asmorkalov commentedon Oct 24, 2023
@Abdurrahheem please take a look on it.