minor grammatical fixes to dnn_custom_layers.md

pull/23358/head
Abduragim 2 years ago committed by Alexander Smorkalov
parent 56a4877e30
commit 69fd82fc46
  1. 49
      doc/tutorials/dnn/dnn_custom_layers/dnn_custom_layers.md

@ -3,29 +3,30 @@
@prev_tutorial{tutorial_dnn_javascript} @prev_tutorial{tutorial_dnn_javascript}
## Introduction ## Introduction
Deep learning is a fast growing area. The new approaches to build neural networks Deep learning is a fast-growing area. New approaches to building neural networks
usually introduce new types of layers. They could be modifications of existing usually introduce new types of layers. These could be modifications of existing
ones or implement outstanding researching ideas. ones or implementation of outstanding research ideas.
OpenCV gives an opportunity to import and run networks from different deep learning OpenCV allows importing and running networks from different deep learning frameworks.
frameworks. There are a number of the most popular layers. However you can face There is a number of the most popular layers. However, you can face a problem that
a problem that your network cannot be imported using OpenCV because of unimplemented layers. your network cannot be imported using OpenCV because some layers of your network
can be not implemented in the deep learning engine of OpenCV.
The first solution is to create a feature request at https://github.com/opencv/opencv/issues The first solution is to create a feature request at https://github.com/opencv/opencv/issues
mentioning details such a source of model and type of new layer. A new layer could mentioning details such as a source of a model and a type of new layer.
be implemented if OpenCV community shares this need. The new layer could be implemented if the OpenCV community shares this need.
The second way is to define a **custom layer** so OpenCV's deep learning engine The second way is to define a **custom layer** so that OpenCV's deep learning engine
will know how to use it. This tutorial is dedicated to show you a process of deep will know how to use it. This tutorial is dedicated to show you a process of deep
learning models import customization. learning model's import customization.
## Define a custom layer in C++ ## Define a custom layer in C++
Deep learning layer is a building block of network's pipeline. Deep learning layer is a building block of network's pipeline.
It has connections to **input blobs** and produces results to **output blobs**. It has connections to **input blobs** and produces results to **output blobs**.
There are trained **weights** and **hyper-parameters**. There are trained **weights** and **hyper-parameters**.
Layers' names, types, weights and hyper-parameters are stored in files are generated by Layers' names, types, weights and hyper-parameters are stored in files are
native frameworks during training. If OpenCV mets unknown layer type it throws an generated by native frameworks during training. If OpenCV encounters unknown
exception trying to read a model: layer type it throws an exception while trying to read a model:
``` ```
Unspecified error: Can't create layer "layer_name" of type "MyType" in function getLayerInstance Unspecified error: Can't create layer "layer_name" of type "MyType" in function getLayerInstance
@ -61,7 +62,7 @@ This method should create an instance of you layer and return cv::Ptr with it.
@snippet dnn/custom_layers.hpp MyLayer::getMemoryShapes @snippet dnn/custom_layers.hpp MyLayer::getMemoryShapes
Returns layer's output shapes depends on input shapes. You may request an extra Returns layer's output shapes depending on input shapes. You may request an extra
memory using `internals`. memory using `internals`.
- Run a layer - Run a layer
@ -71,20 +72,20 @@ memory using `internals`.
Implement a layer's logic here. Compute outputs for given inputs. Implement a layer's logic here. Compute outputs for given inputs.
@note OpenCV manages memory allocated for layers. In the most cases the same memory @note OpenCV manages memory allocated for layers. In the most cases the same memory
can be reused between layers. So your `forward` implementation should not rely that can be reused between layers. So your `forward` implementation should not rely on that
the second invocation of `forward` will has the same data at `outputs` and `internals`. the second invocation of `forward` will have the same data at `outputs` and `internals`.
- Optional `finalize` method - Optional `finalize` method
@snippet dnn/custom_layers.hpp MyLayer::finalize @snippet dnn/custom_layers.hpp MyLayer::finalize
The chain of methods are the following: OpenCV deep learning engine calls `create` The chain of methods is the following: OpenCV deep learning engine calls `create`
method once then it calls `getMemoryShapes` for an every created layer then you method once, then it calls `getMemoryShapes` for every created layer, then you
can make some preparations depends on known input dimensions at cv::dnn::Layer::finalize. can make some preparations depend on known input dimensions at cv::dnn::Layer::finalize.
After network was initialized only `forward` method is called for an every network's input. After network was initialized only `forward` method is called for every network's input.
@note Varying input blobs' sizes such height or width or batch size you make OpenCV @note Varying input blobs' sizes such height, width or batch size make OpenCV
reallocate all the internal memory. That leads efficiency gaps. Try to initialize reallocate all the internal memory. That leads to efficiency gaps. Try to initialize
and deploy models using a fixed batch size and image's dimensions. and deploy models using a fixed batch size and image's dimensions.
## Example: custom layer from Caffe ## Example: custom layer from Caffe
@ -201,7 +202,7 @@ deep learning model. That was trained with one and only difference comparing to
a current version of [Caffe framework](http://caffe.berkeleyvision.org/). `Crop` a current version of [Caffe framework](http://caffe.berkeleyvision.org/). `Crop`
layers that receive two input blobs and crop the first one to match spatial dimensions layers that receive two input blobs and crop the first one to match spatial dimensions
of the second one used to crop from the center. Nowadays Caffe's layer does it of the second one used to crop from the center. Nowadays Caffe's layer does it
from the top-left corner. So using the latest version of Caffe or OpenCV you'll from the top-left corner. So using the latest version of Caffe or OpenCV you will
get shifted results with filled borders. get shifted results with filled borders.
Next we're going to replace OpenCV's `Crop` layer that makes top-left cropping by Next we're going to replace OpenCV's `Crop` layer that makes top-left cropping by
@ -217,7 +218,7 @@ a centric one.
@snippet dnn/edge_detection.py Register @snippet dnn/edge_detection.py Register
That's it! We've replaced an implemented OpenCV's layer to a custom one. That's it! We have replaced an implemented OpenCV's layer to a custom one.
You may find a full script in the [source code](https://github.com/opencv/opencv/tree/3.4/samples/dnn/edge_detection.py). You may find a full script in the [source code](https://github.com/opencv/opencv/tree/3.4/samples/dnn/edge_detection.py).
<table border="0"> <table border="0">

Loading…
Cancel
Save