Skip to content

darknet importer: padding is parsed incorrectly for convolutional layers #16259

Closed

Description

@YashasSamaga

System information (version)

  • OpenCV => 4.2.0
  • Operating System / Platform => Ubuntu 18.04
  • Compiler => g++ 7.4
Detailed description

It appears that the pad value in darknet configuration files indicates the padding mode and not the padding value itself. I think pad being one implies same padding and pad being zero implies manual padding (which is specified in the padding parameter).

https://github.com/AlexeyAB/darknet/blob/dcfeea30f195e0ca1210d580cac8b91b6beaf3f7/src/parser.c#L180-L182

The OpenCV darknet parser treats the pad value as the actual padding value. There is also a comment which shows that the original author of the parser was stunned by the weird pad=1 and sets it to zero. It's actually zero because pad=1 would imply a padding of kernel_size//2 which would anyway give zero.

// correct the strange value of pad=1 for kernel_size=1 in the Darknet cfg-file
if (kernel_size < 3) pad = 0;

Activity

  1. anshu0612 commented on Jan 2, 2020

    @anshu0612

    I would be like to take up the issue @dkurt

  2. dkurt commented on Jan 2, 2020

    @dkurt
    Member

    @anshu0612, feel free to open a PR

  3. ashishkrshrivastava commented on Jan 9, 2020

    @ashishkrshrivastava
    Contributor

    @anshu0612 , have you started working on this issue ? I would like to take this issue if you haven't because I think I already got the solution.

  4. anshu0612 commented on Jan 9, 2020

    @anshu0612

    hi! @ashishkrshrivastava I am already working on it after going the code base and i’m near the solution.
    In case of need I’ll inform you. Thanks

  5. YashasSamaga commented on Jan 13, 2020

    @YashasSamaga
    ContributorAuthor

    @anshu0612 any news?

    All the changes that need to be made are here I think:

    if (layer_type == "convolutional")
    {
    int kernel_size = getParam<int>(layer_params, "size", -1);
    int pad = getParam<int>(layer_params, "pad", 0);
    int stride = getParam<int>(layer_params, "stride", 1);
    int filters = getParam<int>(layer_params, "filters", -1);
    bool batch_normalize = getParam<int>(layer_params, "batch_normalize", 0) == 1;
    int flipped = getParam<int>(layer_params, "flipped", 0);
    if (flipped == 1)
    CV_Error(cv::Error::StsNotImplemented, "Transpose the convolutional weights is not implemented");
    // correct the strange value of pad=1 for kernel_size=1 in the Darknet cfg-file
    if (kernel_size < 3) pad = 0;
    CV_Assert(kernel_size > 0 && filters > 0);
    CV_Assert(current_channels > 0);
    setParams.setConvolution(kernel_size, pad, stride, filters, current_channels,
    batch_normalize);
    current_channels = filters;
    }

    The fix is to replicate these three lines from darknet:

    int pad = option_find_int_quiet(options, "pad",0);
    int padding = option_find_int_quiet(options, "padding",0);
    if(pad) padding = size/2;
    

    You have to read the padding value from the configuration file using int padding = getParam<int>(layer_params, "padding", 0). If pad is one, you have to set padding to kernel_size / 2. You have to pass padding instead of pad as the argument to setConvolution.

  6. anshu0612 commented on Jan 13, 2020

    @anshu0612

    I apologize for the delay.
    @ashishkrshrivastava Please go ahead and open a PR since I am stuck with other work.

  7. ashishkrshrivastava commented on Jan 13, 2020

    @ashishkrshrivastava
    Contributor

    Thankyou @anshu0612, @YashasSamaga I have already made your suggested changes. I will push PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions