Learned edge detector¶
| Fact | Value |
|---|---|
| Also called | HED, Holistically-nested edge detection, PiDiNet, Pixel difference network |
| Status | Available through an optional pretrained model |
| Family | Deep learning and learned processing |
| Engine | Science engine |
| Type | Nonlinear, learned, CNN edge detection |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 128 |
| Code | engine/src/pixelnexus_engine/filters/learned.py, learned_edges |
| Operator code | engine/src/pixelnexus_engine/filters/learned.py, edges_representation |
| Packages | ONNX Runtime (optional) |
Overview¶
A learned edge detector finds the edges that people draw. HED, by Xie and Tu, and PiDiNet, by Su and colleagues, were both trained on the BSDS500 photos, where people traced the outlines of objects. So they mark the borders of objects and regions, and stay quiet on texture, where gradient detectors such as Canny mark every change. The filter runs in the PixelNexus engine.
Each network needs an optional model. It downloads only when you click Download model in the Properties panel, which first shows its size and license, and the engine checks its SHA-256 before it keeps it. HED is a Caffe model, and OpenCV 5 no longer reads Caffe, so tools/export_models.py converts it to ONNX once on your computer. PiDiNet is published as PyTorch weights and is converted the same way. PiDiNet's license allows research and non-commercial use only, and the studio says so before the download. Results carry the label: AI result. Detail may not exist in the original scene.
How it works¶
- Normalize the pixels as the network was trained.
- Run the network, which predicts edges at several scales.
- Fuse the scales into one edge probability, shown as gray.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(s_k\): The side output at one scale
- \(a_k, b\): The learned fusion weights
- \(E\): The edge probability
Both networks make several side outputs s_k, edge maps at different scales, upsampled to the image size. A learned weighting a_k fuses them, and a sigmoid gives the edge probability. HED takes its side outputs from the five stages of VGG16. PiDiNet builds its convolutions from differences between neighbors, so they act like learned gradient operators.
HED trains a side output after each of the five stages of VGG16, so every scale learns to predict edges on its own. Deep supervision makes the early stages useful, and the fused map combines fine and coarse edges. PiDiNet replaces plain convolutions with pixel difference convolutions. They take differences between neighbors before weighting, as Sobel or Laplacian kernels do. After training, they can be rewritten as plain convolutions, which is how PixelNexus runs them.
Kernel or operator¶
A trained network has many learned kernels, not one. The Inspector shows the stages and the model with its license.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Network network |
HED | HED, PiDiNet (research and non-commercial use) | Which network runs. Each downloads on its own. PiDiNet's license is for research and non-commercial use. |
Implementation¶
learned_edges in engine/src/pixelnexus_engine/filters/learned.py runs the ONNX file that tools/export_models.py wrote. For HED, the tool rebuilds the network from its deploy.prototxt in PyTorch and reads the weights from the Caffe file with Caffe's own caffe.proto. Caffe's pooling rounds up, and the side outputs are cropped to the input around their center, as OpenCV's HED sample does. The export is kept only when it matches OpenCV 4's Caffe importer, here to within 3e-6. HED reads BGR from 0 to 255 minus the mean of its training images, from the HED tutorial. PiDiNet runs as the converted plain network of its paper, with the settings of its Table 5 model. Both networks pool four times, so large images run in tiles of 768 pixels that start at multiples of 16, with 128 pixels of margin.
Worked example¶
| Detector | Borders | Inside Stripes | Inside Noise | Inside Blobs | Inside Flat |
|---|---|---|---|---|---|
| Canny | 57.4 | 64.6 | 95.7 | 44.1 | 0.0 |
| HED | 155.7 | 11.4 | 12.3 | 6.4 | 3.7 |
| PiDiNet | 203.1 | 0.1 | 2.7 | 8.2 | 0.3 |
The mean output, from 0 to 255, in a band 6 pixels wide along the borders between the quarters, and inside each quarter 10 pixels away from the borders.
Canny responds more inside the textured quarters than on the borders between them. Both learned detectors do the opposite. HED marks the borders at 155.7 and the insides at 4 to 12. PiDiNet is sharper still, 203.1 on the borders and under 9 inside. The flat quarter stays dark for all three.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. HED has about 15 million weights, PiDiNet about 0.7 million.
Artifacts and limitations¶
Limitations
- Trained on natural photos, so drawings and diagrams may look odd.
- The edge probability is not thinned.
Artifacts
- Thick, blurred edges from HED.
Tests¶
A tiny ONNX model made inside the tests checks that each network gets its own normalization. Where the real models are downloaded and exported, both must mark the outline of a disk far above its inside. HED tiles on the pooling grid must equal a run on the whole image.
References¶
- Xie, S., and Tu, Z. Holistically-nested edge detection. IEEE International Conference on Computer Vision, 1395 to 1403, 2015. DOI 10.1109/ICCV.2015.164.
- Su, Z., Liu, W., Yu, Z., Hu, D., Liao, Q., Tian, Q., Pietikainen, M., and Liu, L. Pixel difference networks for efficient edge detection. IEEE/CVF International Conference on Computer Vision, 5097 to 5107, 2021. DOI 10.1109/ICCV48922.2021.00507.