# DNN module layer gradients

**URL:** <https://forum.opencv.org/t/dnn-module-layer-gradients/9099>\
**Category:** Uncategorized\
**Tags:** dnn\
**Created:** [June 9, 2022, 9:36am UTC](https://forum.opencv.org/t/dnn-module-layer-gradients/9099 "2022-06-09T09:36:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Micka](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Micka](https://forum.opencv.org/u/Micka)\
**Post date:** [June 9, 2022, 9:36am UTC](https://forum.opencv.org/t/dnn-module-layer-gradients/9099/1 "2022-06-09T09:36:00Z")

</div>

I would like to implement and use things like Grad-CAM in my C++ OpenCV applications.  
Am I right, that there is currently no dnn-layer-gradient functionality (or back-propagation) implemented in OpenCV, because OpenCV isn’t used for training?

Is the inference engine and the model representation suitable for implementing gradient techniques, or are there some limitations that make it harder (e.g. because the inference engine is strongly optimized for forward passes)?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 9, 2022, 2:05pm UTC](https://forum.opencv.org/t/dnn-module-layer-gradients/9099/2 "2022-06-09T14:05:35Z")

</div>

> [@Micka](#):
>
> Am I right, that there is currently no dnn-layer-gradient functionality (or back-propagation) implemented in OpenCV, because OpenCV isn’t used for training?

yes, unfortunately you are right about it. there are no gradients at all.  
(so imho, [the grad-cam idea](https://arxiv.org/pdf/1610.02391.pdf) wont work here)

---

<div class="post-metadata">

**Author:** ![Micka](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Micka](https://forum.opencv.org/u/Micka)\
**Post date:** [June 9, 2022, 2:07pm UTC](https://forum.opencv.org/t/dnn-module-layer-gradients/9099/3 "2022-06-09T14:07:07Z")

</div>

but is it technically possible to access all the intermediate activation maps and weights etc. during runtime, to manually implement the gradients?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 9, 2022, 3:52pm UTC](https://forum.opencv.org/t/dnn-module-layer-gradients/9099/4 "2022-06-09T15:52:30Z")

</div>

well you can iterate the layers, and retrieve weights & biases,  
you can also “tap” the forward() pass by specifying a list of output layers  
(that would be the “activations”, right ?)

but how would you get a proper gradient from that ?  
wouldn’t that need the backwards (derived) activation ?

btw, [GitHub - Pandinosaurus/Cpp-VizDnnBlobsOCV: A tutorial to visualize the deep learning blobs generated by the OpenCV dnn module.](https://github.com/Pandinosaurus/Cpp-VizDnnBlobsOCV)
