Skip to content

Missing Darknet neural network support: [shortcut] layers #15724

Closed

Description

@Arcitec

Update: For anyone who needs this new feature right now on Python and can't wait 3 months for the next OpenCV version, read this post on how to compile a custom Python .whl package:

#15739 (comment)


Ping @dkurt @liuyuns @dmonterom @vpisarev @arthur-williams @alalek @AlexeyAB (everyone who has ever worked on OpenCV's darknet module).

New Neural Network Innovation!

There's an incredible new neural network innovation: YOLOv3-Tiny has been reduced to almost half CPU usage while retaining detection accuracy. They call it YOLOv3-Tiny-PRN. It's the new king of low-CPU usage networks! Here is the research and paper in progress (soon to be published): https://github.com/WongKinYiu/PartialResidualNetworks

Here's the performance difference on Darknet (it would be even faster in OpenCV):

66791677-c76bbb00-ef27-11e9-93fe-842dab485bb1

If you do a diff between yolov3-tiny-prn config and the original yolov3-tiny, you will see that the changes are that many layers have half the filter sizes, and there are many [shortcut] layers added instead.

You can also read their paper at the repo above.

The problem is that OpenCV's darknet loader lacks full support for [shortcut] layers. So nobody can enjoy this big PRN innovation on OpenCV's ultra-fast CPU neural network implementation...

References

Reference: Darknet's implementation of shortcut layers.

Reference: OpenCV's incomplete implementation of shortcut layers.

  • else if (layer_type == "shortcut")
    {
    std::string bottom_layer = getParam<std::string>(layer_params, "from", "");
    CV_Assert(!bottom_layer.empty());
    int from = std::atoi(bottom_layer.c_str());
    from += layers_counter;
    current_channels = net->out_channels_vec[from];
    setParams.setShortcut(from);
    }
  • void setShortcut(int from)
    {
    cv::dnn::LayerParams shortcut_param;
    shortcut_param.name = "Shortcut-name";
    shortcut_param.type = "Eltwise";
    shortcut_param.set<std::string>("op", "sum");
    darknet::LayerParameter lp;
    std::string layer_name = cv::format("shortcut_%d", layer_id);
    lp.layer_name = layer_name;
    lp.layer_type = shortcut_param.type;
    lp.layerParams = shortcut_param;
    lp.bottom_indexes.push_back(fused_layer_names.at(from));
    lp.bottom_indexes.push_back(last_layer);
    last_layer = layer_name;
    net->layers.push_back(lp);
    layer_id++;
    fused_layer_names.push_back(last_layer);
    }
  • We can see one big issue immediately: OpenCV's reading of the shortcut layers only reads the from= property. It totally ignores all other properties (mainly the important activation= property).
  • As a sidenote: The PRN network uses [shortcut] with activation=leaky (leaky RELU).
  • Here's @dkurt 16 months ago, confirming that OpenCV's implementation of [shortcut] is wrong (basically it just implements the bare-minimum "Linear" mode for equally-sized layers which are used by the main Yolov3 config, but more advanced usages do not work): dnn.readNetFromDarknet() dosn't support "avgpool" layer #11568 (comment)

Reference: OpenCV's "Eltwise" layer type, with the "op:sum" parameter, that the YOLO "shortcut" layer is being converted into.

Reference: Official pjreddie configs which use shortcut in various ways.

Reference: Python script which completely crashes python due to OpenCV failing to read the shortcut layers.

import os
import cv2

cfg = "yolov3-tiny-prn.cfg"
weights = "yolov3-tiny-prn.weights"

# True, True
print(os.path.isfile(cfg))
print(os.path.isfile(weights))

# This kills python silently. The second "x" does not execute.
print("x") # prints
foo = cv2.dnn.readNetFromDarknet(cfg, weights) # kills python
print("x")

Summary

  • Innovative new Neural Network massively reduces CPU usage and inference time, while keeping the same detection accuracy.
  • OpenCV's darknet parser does not support options (such as activation=leaky) on the [shortcut] layers. The darknet/parser.c that I linked above shows that the official list of [shortcut] options is activation=, from=, alpha= and beta=.
  • OpenCV does not support "shortcut"-linking between layers of different sizes, apparently, according to @dkurt.
  • OpenCV dies without throwing any descriptive error when trying to load the network's config, as seen in the python file (which shows no error output; it just dies).
  • If this can be fixed, OpenCV will be the world's fastest CPU-based framework for running the world's fastest YOLO object detector for low-power devices. Yay. I hope that day is possible in the future...
  • Sidenote regarding that last point: With the current official YOLOv3-Tiny in OpenCV, my i7-8750H is taking ~30ms per forward pass (33.3 FPS). If the performance ratio of YOLOv3-Tiny-PRN's speed improvement is the same in OpenCV as it was in Darknet, the new PRN-based network would take ~18.5ms per forward pass (54 FPS) if OpenCV could support it. That's indeed amazing for a CPU detector!

Activity

  1. changed the title [-]Missing Darknet neural network support: [shortcut] activation=leaky[/-] [+]Missing Darknet neural network support: [shortcut] layers[/+] on Oct 17, 2019
  2. dkurt commented on Oct 17, 2019

    @dkurt
    Member

    Wow, thank you for such detailed feature request :) You did a lot of work to prepare it which motivates us to help you as fast as possible.

  3. Arcitec commented on Oct 17, 2019

    @Arcitec
    Author

    @dkurt Thank you! Well, I am trying to help as much as possible with the necessary research since I wasn't confident enough to try to write the changes to OpenCV's Eltwise layer myself! :-)

    Very happy to see it targeted for the next release milestone. Thanks @alalek. It would be incredible to see this ultra fast network running on OpenCV's ultra fast CPU engine! Letting people deploy nearly realtime 60FPS high-quality detection on CPUs. Super exciting!

    If there's anything else I can do to help, just let me know.

  4. Arcitec commented on Oct 17, 2019

    @Arcitec
    Author

    Small Correction / Extra Information

    The YOLOv3-Tiny-PRN network was designed on @AlexeyAB 's continuation of darknet (which ceased development at the original repo years ago).

    Therefore, it's best to use their code as reference instead. Here are the interesting, relevant files I found for analyzing how the shortcut layer works:

    And perhaps I should also tag @WongKinYiu in this discussion, since the goal of their YOLOv3-Tiny-PRN network was fast inference on low-power devices (including GPU-less devices), and OpenCV is the best CPU-based engine. :-)

  5. WongKinYiu commented on Oct 17, 2019

    @WongKinYiu

    @VideoPlayerCode Hello,

    Both of official darknet and AlexeyAB's darknet can work with PRN.

    And the anti-aliasing is for the issue Antialiased CNNs (ICML 2019).

    @AlexeyAB gives huge contributions for bringing soooo many new functions into darknet.
    The repo supports many state-of-the-art methods.
    eg. TridentNet (ICCV 2019), EfficientNet (ICML 2019), and GIoU (CVPR 2019)...

    Our goal is design networks for which works on low-power ASIC such as NPU or TPU.
    Of course the design also benefit on CPU.
    For CPU, I suggest using depth-wise-convolution-based architectures with tvm, for example MobileNet-v3 (ICCV 2019), ShuffleNet-v2 (ECCV 2018), and EfficientNet (ICML 2019).
    Also, we will release our developed MobileNet-v2-based model in near future.

  6. Arcitec commented on Oct 18, 2019

    @Arcitec
    Author

    @WongKinYiu Hello again! Thanks for the info! It's really great that you knew what the antialiasing was for!

    And yeah, Alexey is incredible and has done so much for Darknet and the world of neural networks. He's a genius!

    Our goal is design networks for which works on low-power ASIC such as NPU or TPU.
    Of course the design also benefit on CPU.

    Yeah as your chart showed, you managed to reduce CPU inference time of YOLOv3-Tiny to 62.5% in your PRN variant, which is almost twice as fast as standard! That's incredible.

    For CPU, I suggest using depth-wise-convolution-based architectures with tvm, for example MobileNet-v3 (ICCV 2019), ShuffleNet-v2 (ECCV 2018), and EfficientNet (ICML 2019).

    The only one I hadn't tried there is ShuffleNet.

    As for MobileNet, its detector is meant for mobile phones. Its accuracy is aaaaawful, so I've always avoided it. Look at its mAP at COCO 0.5, where it gets mAP scores of 22 and 16:

    mobilenet

    Now compare that to your YOLOv3-Tiny-PRN at COCO 0.5, whose mAP is 33.1:

    yolo3tinyprn

    About EfficientNet, it's indeed a very efficient network and I was super impressed when I read about how they had developed a technique for optimizing and properly scaling the network to halve CPU usage while keeping or even increasing accuracy, thanks to the improved net design. So yeah that's great, but it's slower than YOLOv3-Tiny, and EfficientNet is too new right now so it's lacking good Python libraries and variants (there are some small libraries but nothing official/standard yet). I'm waiting for a Tensorflow2 rewrite of EfficientNet (read that Google is working on rewriting all nets for that, slooowly), and will give it another chance then. It was also designed for Image Classification, not Object Detection, although I am aware that many people have made tweaks for use in object detection, but yet again there's nothing official yet. I saw some Google engineers say that they'll write an object detector version on its official tensorflow pages, but nothing has come from that yet.

    I'll research ShuffleNet which you mentioned. A quick search found https://arxiv.org/abs/1807.11164 and it seems like a very good network, and I'll be reading the whole paper and testing it. (Edit: I read the paper and did a lot of searching but it isn't a popular model so there are no pretrained imagenet weights for transfer learning. I may check it out again later, but for now I'll skip it...)

    However, the thing about YOLOv3-Tiny (and now YOLOv3-Tiny-PRN) is that they offer high mAP while being super fast on CPUs. And your PRN design innovation helped a lot on CPU too (on the Darknet engine). So right now YOLOv3-Tiny-PRN is the # 1 most interesting CPU network for me, since you've managed to keep the accuracy of the already proven YOLOv3-Tiny, but made it almost twice as fast on CPU thanks to your redesign! That is what excites me!

    And in case you weren't aware, OpenCV's neural network engine is, in most cases, the fastest CPU-based engine in the world. Mainly thanks to heavy investment in optimization by Intel! :-) That's why YOLOv3-Tiny-PRN support would be a great addition to OpenCV!

    Also, we will release our developed MobileNet-v2-based model in near future.

    I look forward to exploring that. You're doing great work!

  7. dkurt commented on Oct 18, 2019

    @dkurt
    Member

    @VideoPlayerCode, Hi! Can you please test the changes from #15739 with the model?

  8. Arcitec commented on Oct 22, 2019

    @Arcitec
    Author

    This has now been merged. For anyone who wants to read the discussion, or just jump to some performance results (comparing YOLOv3-Tiny vs YOLOv3-Tiny-PRN), check this post:

    #15739 (comment)

    Awesome neural network, and awesome OpenCV engine! <3


    Update: For anyone who needs this new feature right now on Python and can't wait 3 months for the next OpenCV version, read this post on how to compile a custom Python .whl package:

    #15739 (comment)

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions