# Task-Driven Real-World Super-Resolution of Document Scans
## 3 The Proposed Framework for Task-Driven Training
### Network Architecture and Loss Function
#### 3.1.2 CTPN Architecture for Text Region Detection  
To extract spatial features relevant to textual content, we incorporate the CTPN network [28], which was originally introduced to detect horizontal text lines. CTPN is particularly effective at identifying localized regions likely to contain text, making it a suitable auxiliary supervision module within our framework.  
The CTPN architecture is based on a truncated VGG16 backbone, pretrained on the ImageNet dataset [42]. Given a super-resolved image \(\hat{I}\) or an HR reference \(I_{\text{HR}}\), the convolutional encoder outputs a feature map of size \((C\times H^{\prime}\times W^{\prime})\). A subsequent convolutional layer with 3 \(\times\) 3 kernels reduces the dimensionality to 512, resulting in a tensor \(X\in\mathbb{R}^{512\times H^{\prime}\times W^{\prime}}\). This tensor is reshaped into a sequence of vectors of length 512 and processed row-wise by a bidirectional gated recurrent unit (Bi-GRU), which captures horizontal contextual dependencies across the image width--essential for accurate text region detection. The output sequence, consisting of 256-dimensional features from the Bi-GRU, is reshaped back to a spatial map \(X_{\text{seq}}\in\mathbb{R}^{256\times H^{\prime}\times W^{\prime}}\).  
This intermediate representation is then passed through a \(1\times 1\) convolution to restore the dimensionality to 512 channels. The resulting tensor is processed by two parallel \(1\times 1\) convolutional branches. The first of these is a classification head (\(\psi_{\text{CTPN-class}}\)), which predicts, for each anchor (i.e., a fixed-width vertical slice of the input), the text-presence probability. Training of this branch is carried out using a masked cross-entropy loss, where neutral anchors are excluded from supervision. The second branch is the regression head (\(\psi_{\text{CTPN-reg}}\)), which estimates the vertical coordinate offsets for the bounding box corresponding to each anchor. This output is trained with a smooth L1 loss applied only to positive samples. Overall, CTPN detects the text and estimates its location. When it is used as a loss function, it learns an SR network to produce super-resolved images, from which the text regions could be easily detected.  
Let \(F_{\text{cls}}(\cdot)\) and \(F_{\text{reg}}(\cdot)\) denote the outputs of the classification and regression convolutions. Then:  
\[\mathcal{L}_{\text{CTPN-class}}=\text{CrossEntropy}\big{(}F_{\text{cls}}(\hat{I }),\,F_{\text{cls}}(I_{\text{HR}})\big{)}, \tag{3}\]  
\[\mathcal{L}_{\text{CTPN-reg}}=\text{SmoothL1}\big{(}F_{\text{reg}}(\hat{I}),\, F_{\text{reg}}(I_{\text{HR}})\big{)}. \tag{4}\]  
However, instead of comparing the final predicted boxes directly, we extract deep features from the last pre-activation layers in each head, denoted \(\psi_{\text{CTPN-deep}}(\cdot)\in\mathbb{R}^{D}\), and we measure their L1 distance between \(\hat{I}\) and \(I_{\text{HR}}\):  
\[\mathcal{L}_{\text{CTPN-deep}}=\big{\|}\psi_{\text{CTPN-deep}}(\hat{I})-\psi _{\text{CTPN-deep}}(I_{\text{HR}})\big{\|}_{1}. \tag{5}\]  
These losses computed in the feature space make it possible to train the SR network to reconstruct the details that are helpful for text detection and localization.