You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document details the mathematical derivation of the gradients (Backward Pass) for the Cross-Correlation (CC) and Zero-Normalized Cross-Correlation (ZNCC) operators implemented in this library.
1. Notations
Let $I$ be the input image of size $H \times W$.
Let $T$ be the template (kernel) of size $h \times w$.
Let $N = h \times w$ be the number of elements in the template.
Let $O$ be the output tensor (result of the correlation).
The correlation operation is defined with "valid" padding (no padding), so the output size is $(H-h+1) \times (W-w+1)$.
We denote a window of the image centered (or anchored) at position $u$ as $I_u$. In the implementation, the anchor is top-left, so $I_u$ refers to the patch of size $h \times w$ starting at $u$.
Index $v$ iterates over the spatial domain of the kernel ($0 \le v_y < h, 0 \le v_x < w$).
2. Cross-Correlation (CC)
Forward Pass
The standard Cross-Correlation is defined as:
$$O(u) = (I \star T)(u) = \sum_{v} I(u+v) \cdot T(v)$$
Backward Pass
Let $L$ be the scalar loss function. We are given the upstream gradient $\frac{\partial L}{\partial O}$.
We need to compute the gradients with respect to the inputs: $\frac{\partial L}{\partial I}$ and $\frac{\partial L}{\partial T}$.
From the definition of $O(u)$, $\frac{\partial O(u)}{\partial I(k)} = T(k-u)$ if $k-u$ is a valid index in $T$, and 0 otherwise.
Let $v = k-u$, so $u = k-v$.
This operation corresponds to the convolution of the upstream gradient $\frac{\partial L}{\partial O}$ with the kernel $T$ (flipped if strictly speaking convolution, but usually implemented as correlation with appropriate indexing).
This operation corresponds to the correlation between the Image $I$ and the upstream gradient $\frac{\partial L}{\partial O}$.
$$\frac{\partial L}{\partial T} = I \star \frac{\partial L}{\partial O}$$
3. Zero-Normalized Cross-Correlation (ZNCC)
This is the more complex operator.
3.1 Definitions & Forward Pass
Step 1: Template Standardization
The template $T$ is globally standardized.
Let $\mu_{T}$ be the mean of $T$ and $\sigma_{T}$ be the standard deviation (uncorrected).
Backpropagate $\frac{\delta(u)}{\sigma_{I_u}}$ through the correlation with $\hat{T}$.
Subtract the backpropagation of $\frac{\delta(u) \cdot Z(u)}{N \sigma_{I_u}}$ through the "local standardization" operation (conceptually similar to instance norm backward).
Gradient w.r.t Template ($T$)
$Z(u)$ depends on $T$ via $\hat{T}(v)$.
$$Z(u) = \sum_v \hat{I}_{u}(v) \hat{T}(v)$$
Let $\nabla_{\hat{T}} L$ be the gradient w.r.t the standardized template.