# Normalizing Flows for Out-of-Distribution Detection: Application to Coronary Artery Segmentation
## 2 Methods
### Synthetic Mask Perturbations  
Our application's goal is to detect incorrect pairs of (angiography image, lumen mask), i.e., samples where the segmentation is not in full agreement with the image. To test our models, we devised a method to obtain \"wrong\" datapoints (or samples which are not in the distribution of \"correct\" image-mask pairs) starting from our initial data (considered to be \"correct\"). \begin{table}
\begin{tabular}{c c c c c c} \hline \hline
**Stage** & **No. Blocks** & **Block Description** & **Resolution** & **No. Channels** & \begin{tabular}{c} **Total Number of** \\ **Parameters** \\ \end{tabular}  \\ \hline \multirow{2}{*}{1} & \multirow{2}{*}{4} & Additive coupling layer using checkerboard mask; BatchNorm (if not last block) & \multirow{2}{*}{8 \(\times\) 32 \(\times\) 32} & \multirow{2}{*}{2} & \\ \hline \multirow{4}{*}{2} & \multirow{2}{*}{1} & \multirow{2}{*}{3D Squeeze operation} & Stage 2: 16 & \multirow{4}{*}{\(\sim\)8.7 millions} \\  & & & Stage 3: 2 \(\times\) 8 \(\times\) 8 & & Stage 6: 1024 & \\ \cline{1-1} \cline{3-5}  & & & Stage 5: 1 \(\times\) 2 \(\times\) 2 & &  
We augmented the datasets by applying preset perturbations on the lumen segmentation binary mask, while keeping the angiographic image untouched. Three types of mask perturbations were employed:  
* zooming:we applied zoom in/out operations on the mask image with respect to the mask center, so that the resulting mask is still aligned with the angiography, but larger/smaller than before. Figure 3 displays an example for various levels of zoom. * morphing: we applied dilations or erosions along 4 directions on the height \(*\) width plane: left-right, top-bottom, topLeft-bottomRight and topRight-bottomLeft. This perturbation only affects one part of the mask (the eroded or dilated part), while the other part is left untouched. Figure 4 displays an example for various levels of morphing. By convention, negative and positive levels refer to the two ways in the selected direction, with zero meaning original mask position (levels are expressed as ratios of the original mask size along the chosen direction). At every level, either dilation (resulting in prolonged masks) or erosion (resulting in shortened masks) can be applied. * translations: in the same 4 directions on the height \(*\) width plane, we translated whole mask images. Each level increment signifies a pixel shift. Figure 5 shows an example for various levels of translation. For each network architecture, we performed two training procedures: one employing only original (unperturbed) data and one employing a dataset consisting of the original data and its perturbed version. The perturbations are applied during train time, similar to data augmentation techniques, such that each original data sample gets perturbed on all perturbation types, levels and directions over the training epochs. At each epoch, the ratio between untouched and perturbed data is 1-to-1. When only original data is used, the training loss function consists of maximizing the log-probabilities across the train set. When perturbed data is also employed, we used a train loss function (Equation (3)) similar to the hinge loss introduced in [13, 27], where the model tries to maximize predicted log-probs for original (untouched, _in-distrib_) samples and tries to decrease predicted log-probs under a certain threshold \(T\) for perturbed samples (OoD). The hinge loss allows us to shape the learnt probability density modeled by the NF, by directly offering supervision in regions around the original samples in the input domain. Therefore, the NF can be trained to be sensitive to the used synthetic perturbations and to mark perturbed samples as OoD  
\[ \begin{split} L(\theta,\ x)&=\mathbb{E}_{x\in \textit{inDistrib}}(\ln(p_{\theta}(x))-\ \mathbb{E}_{x\in\text{OoD}}(\max(0,\ \ln(p_{\theta}(x))-T))\\ \theta_{optimal}&=\operatorname{argmax}_{\theta}L( \theta,\ x)\end{split}  \tag{3}\]  
where \(p_{\theta}\) is the probability density modeled by the NF.