Skip to content

dnn(Scale): errornous handling of batch inputs in untrainable scale mode #16630

Open

Description

@YashasSamaga
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).

// 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?).

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:

// 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

  1. YashasSamaga commented on Feb 21, 2020

    @YashasSamaga
    ContributorAuthor

    Duplicate of #13626

  2. ashishkrshrivastava commented on Mar 10, 2020

    @ashishkrshrivastava
    Contributor

    @dkurt lately I have been working on this issue and I think I have solved it locally if my assumptions are correct.

    1. 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]

    2. 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) ?

    3. 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 ?

  3. asmorkalov commented on May 15, 2023

    @asmorkalov
    Contributor

    @dkurt, @YashasSamaga Is the issue still relevant?

  4. opencv-alalek commented on May 15, 2023

    @opencv-alalek
    Contributor

    I believe yes. Try testDarknetLayer(..., /*testBatchProcessing =*/ true) in the mentioned test.
    Probably it should be targeted to 4.9.0+ milestone.

  5. modified the milestones: 3.4.20, 4.9.0 on May 16, 2023
  6. asmorkalov commented on Oct 24, 2023

    @asmorkalov
    Contributor

    @Abdurrahheem please take a look on it.

  7. modified the milestones: 4.9.0, 4.10.0 on Dec 1, 2023
  8. modified the milestones: 4.10.0, 4.11.0 on May 3, 2024
  9. removed this from the 4.11.0 milestone on Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions