# TopoSinGAN: Learning a Topology-Aware Generative Model from a Single Image
## 2 Materials and Methods  
### Topology Loss Algorithm  
A differentiable topology loss was developed to evaluate the number of terminal nodes present in the generated boundary mask. This loss was integrated into the SinGAN architecture as an auxiliary loss term, \(L_{\text{topo}}\) for the generator. Equation (1) shows all three terms of the loss function. In this equation, \(\lambda_{1}\), \(\lambda_{2}\), and \(\lambda_{3}\) represent the coefficients of the loss terms, which can be adjusted manually:  
\[\text{Loss}=\lambda_{1}L_{\text{adv}}+\lambda_{2}L_{\text{rec}}+\lambda_{3}L_{ \text{topo}} \tag{1}\]  
We propose \(L_{\text{topo}}\) that primarily focuses on minimizing the presence of terminal nodes in the generated mask. The algorithm consists of three components as follows.  
* **Soft Thresholding with Sigmoid Function (\(\sigma(x)\))** To detect terminals, the input mask (\(M\)) undergoes a custom sigmoid function \(\sigma(x)\), resulting in a soft binarization of the tensor. \(\sigma(x)\) acts as a soft thresholding mechanism ensuring continuity across the entire domain. The mathematical representation is as follows: \[\sigma(x)=\frac{1}{1+e^{\kappa(\beta-x)}}\] (2)\[\lim_{x\rightarrow\infty,\alpha>0}\sigma(x)=\begin{cases}1,&x>\beta\\ 0.5,&x=\beta\\ 0,&x<\beta\end{cases} \tag{3}\]  
\[\lim_{x\rightarrow\infty,\alpha<0}\sigma(x)=\begin{cases}0,&x>\beta\\ 0.5,&x=\beta\\ 1,&x<\beta\end{cases} \tag{4}\]  
where \(\beta\) is the threshold point and \(\alpha\) is the thresholding slope. The sigmoid function is smooth and differentiable across its entire domain, ensuring that its gradient can be computed. Specifically, the derivative of \(\sigma(x)\) with respect to \(x\) is given by:  
\[\frac{d\sigma(x)}{dx}=\alpha\cdot\sigma(x)\cdot(1-\sigma(x)) \tag{5}\]  
**Detection of Terminal Nodes via Convolution**  
After applying the sigmoid function, the soft-binarized mask \(M^{\prime}=\sigma(M)\) is convolved with a set of eight fixed kernels, \(K_{1},K_{2},\ldots,K_{8}\), which are depicted in Figure 2. These kernels are specifically designed to target the eight main cardinal and intercardinal directions in a 2D space (north, northeast, east, southeast, south, southwest, west, northwest). Empirically, the use of these kernels has proven effective in identifying discontinuities, thereby enhancing the model's ability to evaluate topological performance. This evaluation is integral to the core of the developed loss function, enabling the application of corrective penalties during the training phase for improved topological accuracy.  
\[C_{i}(M^{\prime})=M^{\prime}\otimes K_{i} \tag{6}\]  
where \(\otimes\) denotes the convolution operator. The convolution operation is differentiable, and thus, gradients can be propagated through this layer.  
**Loss Computation**  
The topology loss is computed as:  
\[L_{\text{topo}}=\frac{1}{8}\sum_{i=1}^{8}\sum_{(x,y)}C_{i}(M^{\prime})_{x,y} \tag{7}\]  
where \(C_{i}(M^{\prime})_{x,y}\) represents the value of the convolution output at position \((x,y)\). Since convolution and summation are linear operations, \(L_{\text{topo}}\) remains  
Including \(L_{\text{topo}}\) in the model's training process is conditional on the scale belonging to the second half of the GAN pyramid, which in our case studies corresponds to level four and beyond, as we had a total of eight levels according to the dimensions of the input image. This implementation strategy is based on empirical results indicating that introducing the topology loss at earlier stages can disrupt the learning of foundational features. By applying \(L_{\text{topo}}\) starting from the second half of the pyramid, the model has already developed basic structures, allowing \(L_{\text{topo}}\) to effectively enhance topological accuracy without impeding initial convergence. The architecture of the developed loss function is depicted in Figure 3.