{'fraction_non_alphanumeric': 0.036882161077193276, 'fraction_numerical': 0.01777201639207159, 'mean_word_length': 4.900567480878362, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Light-weight Document Image Cleanup using Perceptual Loss

Smartphones have enabled effortless capturing and sharing of documents in digital form. The documents, however, often undergo various types of degradation due to aging, stains, or shortcoming of capturing environment such as shadow, non-uniform lighting, etc., which reduces the comprehensibility of the document images. In this work, we consider the problem of document image cleanup on embedded applications such as smartphone apps, which usually have memory, energy, and latency limitations due to the device and/or for best human user experience. We propose a light-weight encoder decoder based convolutional neural network architecture for removing the noisy elements from document images. To compensate for generalization performance with a low network capacity, we incorporate the perceptual loss for knowledge transfer from pre-trained deep CNN network in our loss function. In terms of the number of parameters and product-sum operations, our models are 65-1030 and 3-27 times, respectively, smaller than existing state-of-the-art document enhancement models. Overall, the proposed models offer a favorable resource versus accuracy trade-off and we empirically illustrate the efficacy of our approach on several real-world benchmark datasets.


Introduction
The smartphone camera have simplified the capture of various physical documents in digital form. The ease of share of digital documents (e.g., via messaging/networking apps) have made them a popular source of information dissemination. However, readability of such digitized documents is hampered when the (original) physical document is degraded. For instance, the physical document may contain extraneous elements like stains, wrinkles, ink spills, or can undergo degradation over time. As a result, while scanning such documents (e.g., via a flat-bed scanner), these elements also get incorporated into the document image. In case of capturing document images via mobile cameras, the images are prone to being impacted by shadow, non-uniform lighting, light from multiple sources, light source occlusion, etc. Such noisy elements not only effects the comprehensibility of the corresponding digitized document to the human readers, it may also break down the automatic (document-image) processing/understanding pipeline in various applications (e.g., OCR, bar code reading, form detection, table detection, etc). Few instances of noisy document images are shown in Fig. 1.
Given an input noisy document image, the aim of document image cleanup is to improve its readability and visibility by removing the noisy elements. While general (natural scene) image restoration has been traditionally explored by the computer vision community, recent works have also focused on developing cleanup techniques for document images depending on the type of noise and document-class. These include foreground background separation [23,24,40], differential fading problem [22,44], removal of shadow/smear/strain [21,22,39,46,47,49,50], and handling ink bleed [41,46], etc.
Recent works [6,20,52] view document cleanup as an image to image translation problem, modeled using deep networks. A general direction of research has been to explore deeper and more complicated networks in order to achieve better accuracy [43,52]. However, such deep networks often require high computational resources, which is beyond many mobile and embedded applications on a computationally limited platform. Deeper networks also usually entail a higher inference time (latency), which document image processing mobile apps such as Adobe Lens, CamScanner, Microsoft Office Lens, etc., aim to minimize for best human user experience.
In this work, we propose a light-weight encoder-decoder based convolutional neural network (CNN) with skip-connections for cleaning up document images. Focusing on memory constrained mobile and embedded devices, we design a light-weight deep network architecture. It should be noted that light-weight deep network architecture usually costs generalization performance when compared with deeper networks. Hence, in order to obtain a healthy interplay between resource/latency and accuracy, we propose to employ perceptual loss function (instead of the more popular per-pixel loss function) for document image cleanup. The perceptual loss function [11] enables transfer learning by comparing high-level representation of images, obtained from a pre-trained CNNs (e.g., trained (a) (b) Figure 1: Typical examples of noisy document images; (a) real world degraded images from various datasets [5,35,51], (b) noisy real world images captured via mobile devices. on image classification tasks). We empirically show the effectiveness of the proposed network on several real-world benchmark datasets.
The outline of the paper is as follows. We discuss the existing literature in Section 2. In Section 3, we detail our methodology. The empirical results are presented in Section 4, while Section 5 concludes the paper.

Related work
In this section, we briefly discuss existing approaches that aim to recover/enhance images of degraded documents via techniques involving binarization, and illumination/shadow correction, and deblurring, among others. Document image binarization: A popular framework for document image cleanup is background foreground separation [14], where the foreground pixels are preserved and enhanced and the background is made uniform. Binarization is a technique to segment foreground from the background pixels. Analytical techniques for document image binarization involve segmenting the foreground pixels and background pixels based on some thresholding. Traditional image binarization technique such as [26] compute a global threshold assuming that the pixel intensity distribution follows a bi-modal histogram. As estimating such thresholds may difficult for degraded document images, Moghaddam and Cheriet, in [23], proposed an adaptive generalization of the Otsu's method [26] for document image binarization. In [40], Sauvola and Pietikäinen proposed a local adaptive thresholding method for the image binarization task. To improve Sauvola's algorithm's performance in low contrast setting, Lazzara and Geraud developed its multi-scale generalization in [19]. Recent works on document image binarization have also explored techniques based on conditional random fields [27], fuzzy C-means clustering [24], robust regression [48], and maximum entropy classification [22].
Deep convolutional neural networks (CNNs) have become all-pervasive in computer vision ever since AlexNet [18] won the ILSVRC 2012 ImageNet Challenge [38]. Tensmeyer and Martinez [46] posed document image binarization as a pixel classification problem and developed a fully connected convolution network for it. An encoder-decoder network was proposed in [9] to estimate the background of a document image. Then, Otsu's global thresholding technique [26] is used to obtain a binarized image with uniform background. Afzal et al. [1] employed a long short-term memory (LSTM) network to classify each pixel as background and foreground by considering images to be a two-dimensional sequenec of pixels. In [28], Peng et al. proposed a multi-resolutional attention model to learn the relationship between the text regions and background through convolutional conditional random field [16,45]. To bypass the need of large training datasets with ground truths, Kang et al. [12] employed modular U-Nets [37] pre-trained for specific tasks such as dilation, erosion, histogram equalization, etc. These U-Nets are cascaded using inter-module skip connections and the final network is fine-tuned for the document image binarization task. Document image enhancement: In addition to working within the background foreground separation framework, existing works have developed noise-specific document image cleanup methods such as shadow removal. Bako et al. [2] assumes a constant background color generates a shadow map that matches local background colors to a global reference. Similar to Bako's method, local and background colors are estimated to remove shadow from document images in [49,50]. Inspired by the topological surface filled by water, Jung et al. proposed an illumination correction algorithm for document images in [39]. A document image enhancement approach have been proposed by Krigler et al. by representing the input image as 3D point cloud and adopting the visibility detection technique to detect the pixels to enhance [15]. Recently, Lin et al. [21] proposed a deep architecture to estimate (i) the global background color of the document, and (ii) an attention map which computes the probability of a pixel belonging to the shadow-free background. An illumination correction and document rectification technique using patch based encoder-decoder network is proposed in [20].
Existing works have also explored deep networks for overall document enhancement rather than focusing on correcting specific document degradations. A skip-connected based deep convolutional auto-encoder is proposed in [52]. Instead of learning the transformation function from input to output, this network learns the residual between input and output. This residual when subtracted from the input image results in a noise free enhanced image. An end to end document enhancement framework using conditional Generative Adversarial Networks (cGAN) is proposed in [43], where an U-Net based encoder-decoder architecture is used for the generator network. Document image cleanup for mobile and embedded applications: Low-resource consuming models are desirable for mobile document image processing, e.g., in apps like Adobe Lens, CamScanner, Microsoft Office Lens, etc. However, existing CNN based methods [43,52], discussed above, propose deep architectures with huge number of parameters, making them unsuitable for memory and energy constrained devices. In this work, we propose a comparatively light-weight deep encoder-decoder based network for document image enhancement task. We employ the perceptual loss based transfer learning technique to compensate for generalization performance with a low network capacity.

Proposed approach
As discussed, we propose a light-weight deep network, suitable for mobile document image cleanup applications.

Network architecture
We design an encoder decoder based image to image translation network. The encoder part of the model consists of three convolution layers followed by five residual blocks. The residual blocks were first introduced in [8] for generic image processing tasks. We modify the residual blocks from the original design [8] to suite our network design. The decoder part of the model consists of five convolution layer along with skip connections from the encoder layers. This type of skip connection helps mitigate the vanishing gradient and the exploding gradient issues [8,52,43]. Hence, the skip connections help to simplify the overall learning of the network. Each convolution layer is followed by batch normalization layer and ReLU6 [17] activation layer.
The kernel size of the convolution layer is 3 × 3 and strides for all the layers is set to 1. The padding at each layer is set as "same", which helps to pad the input such that it is fully covered by the filter. Padding "same" with stride 1 helps to keep the spatial dimension of the convolution layer output same as its input. At the end of the network a sigmoid activation function is used to obtain a normalized output between 0 and 1. The output dimension of the last layer of the decoder is either one or three depending on the end task of the network. If the network is trained for the task of binarization or gray scale cleanup then the output dimension of the last layer is set to one. For color cleanup task the output dimension of the last layer is set to three.
We term our models as M-x, where x represents the value of the maximum width of the network. In our experiments, we have considered x to be 16, 32 and 64. The M-64 model is shown in Fig. 2

Loss function
The network is optimized by minimizing the loss function L computed using Eq. 1: Here, 1(It, Ig) = It − Ig 1 is the 1-norm loss between the translated image It and the ground truth image Ig in Y C b Cr color space for color image cleanup. For gray scale cleanup, 1(It, Ig) = It − Ig 1, refer to the 1-norm loss between the two images in gray scale. In addition to pixel-level loss function 1(It, Ig), we also employ the perceptual loss functions 2(It, Ig) and 3(It, Ig) in Eq. 1. Perceptual loss functions [11,36,53] compute the difference between images It and Ig at high level feature representations extracted from a pre-trained CNNs such as those trained on ImageNet image classification task. They are more robust in computing distance between images than pixel-level loss functions. In the context of developing light-weight document image cleanup models, perceptual loss functions serve an additional role of enabling transfer learning. The perceptual loss functions in Eq. 1 helps to transfer the semantic knowledge already learned by the pre-trained CNN network to our smaller network.
The perceptual loss has two components [11]: feature reconstruction loss 2(It, Ig) and style loss 3(It, Ig). Feature reconstruction loss encourages the transformed image to be similar to ground truth image at high level feature representation as computed by a pre-trained network ℘. Let ℘j(I) be the activations of the j th layer of the pre-trained network ℘. Then, Eq 2 represents the feature reconstruction loss: where the shape of ℘j(I) is Hj × Wj × Cj. The feature reconstruction loss penalizes the transformed image when it deviate from the content of the ground truth image. Additionally, we should also penalize the transformed   image if it deviate from the ground truth image in terms of common feature, texture, etc. To achieve this style loss is incorporated as proposed in [11]. The style loss is represented in Eq. 3 as follows: where ℘ represent pre-trained CNN network, J represent set of layers of ℘ used to compute style loss, and G ℘ j (I) represent a Gram matrix containing second-order feature covariances. Let ℘j(I) be the activation of the j th layer of the pre-trained network ℘, where the shape of ℘j(I) is Hj × Wj × Cj. Then, the shape of the Gram matrix G ℘ j (I) is Cj × Cj and each element of G ℘ j (I) is computed according to Eq 4 as follows: In our work, we use the V GG19 network [42] trained on the ImageNet classification task [4] as our pre-trained network ℘ for the perceptual loss. Here, feature reconstruction loss is computed at layer conv1-2 and style reconstruction loss is computed at layers conv1-1, conv2-1, conv3-1, conv4-1, and conv5-1.

Experimental results and discussion
We evaluate the generalization performance of the proposed models on binarization, gray scale, and color cleanup tasks. Experimental setup: In our experiment, the input of the network is set as 256 × 256. The input to the network is a 3 channel RGB image, whereas the output dimension of the network is set as 1 or 3 depending on the downstream task. If the downstream task is to obtain an image in gray scale or a binary image, then the output dimension is set as 1. The output dimension is set as 3 for color cleanup task.
To handle different type of noise at various resolution, the training images are scaled at scale 0.7, 1.0, and 1.4. Further at each scale, the training images are divided into overlapping blocks of 256 × 256. During training, a few random patches from the training images are also used for data augmentation using random brightness-contrast, jpeg noise, ISO noise, and various types of blur [3]. Randomly selected 80% of the training patches is used to train the model while the remaining 20% is kept for validation. The model with best validation performance is saved as the final model. The network is optimized using Adam algorithm [13] with default parameter settings. The parameters λ1, λ2, and λ3 of Eq 1 is set to 1e1, 1e − 1, and 1e1 respectively. During inference, an input image is divided into overlapping blocks of 256 × 256. Each patch is inferred using the trained model. Finally, all the patches are merged to obtain the final result. We use simple averaging for the overlapping pixels of the patches.
Compared algorithms: We compare the proposed models with recently proposed deep CNN based document image cleanup models: SkipNetModel [52] and DE-GAN [43]. Table 1 presents a comparative analysis of our proposed models with SkipNetModel and DE-GAN in terms of: (i) number of multiplication and addition operations (Mult-Adds) associated with the model [10], (ii) number of parameters, (iii) actual size on device, (iv) model load time, and (v) model inference time. A comparison with respect to these parameters is essential if the applicability of any model for memory and energy constrained devices is to determined. We implemented the models using TensorFlow Lite (https://www.tensorflow.org/lite) on an Android device with Qualcomm SM8150 Snapdragon 855 chipset and 6GB RAM size. On the device, we observe that our models are 65-1090 and 3-55 times lighter in size than DE-GAN and SkipNetModel, respectively. Similarly, our models has lesser product-sum operations and prediction time during the inference stage, making them suitable to mobile and embedded applications.

Binarization
We begin by discussing our results on document image binarization task. For our experiment, we have considered the publicly available binarization dataset DIBCO13 [32] and DIBCO17 [35] as test sets. The proposed models and SkipNetModel are trained on the datasets [7], [29], [30], [31], [25], [34] and [33]. While training the models for the test set DIBCO13 [32], we also include the dataset DIBCO17 [35] into our training data. The models for this task are trained using the augmentation strategy described in Sec. 4. The same training strategy is also followed while training the models for the task DIBCO17 [35]. The models are compared using the DIBCO13 [32] evaluation criteria: F-measure, pseudo F-measure (Fps), peak signal to noise ratio (PSNR), and distance reciprocal distortion (DRD). For the metrics F-measue, Fps, and PSNR, higher values correspond to better performance whereas, in input ground truth SkipNetModel [52] M-32  case of the metric DRD lower is better. While evaluating our methods on DIBCO13 dataset, we have compared our methods with traditional binarization algorithms [26,40], state of the art binarization techniques [46,48], DE-GAN [43] and SkipNetModel [52]. Overall performance of these methods are reported in Table 2. In this table, performance of the methods [26,40,43,46,48] are reported as they are reported in [43]. From this table, it is evident that DE-GAN outperforms all other methods in terms of all metrics. However, the proposed method performs better than the traditional binarization algorithms [26,40] and they perform more or less similar to other state of the art techniques. Moreover, from Tables 1 and 2, we can observe that though the proposed method can not outperform the state of the art techniques but they perform similar to most of the state of the art techniques with much lesser computational and memory cost. We have also reported the performance of the proposed methods with the top 5 methods of DIBCO17 competition [35], SkipNetModel and DE-GAN in Table 3. A similar performance of the proposed methods is also observed from this

Gray scale and color cleanup
For the purpose of document cleanup, we first show the effectiveness of the proposed methods in gray scale. The gray scale cleanup part of our experiment is conducted on the publicly available dataset N oisyOffice [5,51] . This dataset consists of two parts, first one is real noisy images consisting of 72 files, and a synthetic dataset consisting of 216 files. There were no groundtruth images available for the real data, therefore, we are not able to include the real dataset for quantitative analysis of our experiment. The model is trained and evaluated on the synthetic data. We divide the synthetic data into two parts -172 images for training and 44 images for testing. The authors of [43] did not share the saved model for gray scale cleanup. Therefore, for this experiment, only the method proposed in [52] is used for comparison. To measure the capability of the proposed models with respect input ground truth SkipNetModel [52] M-32 Figure 5: Typical examples of noisy images from our test set of synthetic data from [51].
to removing noise, we adopt peak signal to noise ratio (PSNR) as the quality metric. In order to determine the dependence of the model performance on the amount of available training data, we have trained the SkipNetModel and the proposed models M-64, M-32, and M-16 by varying the amount of training data from 25% to 100%. The performance of the models with respect to PSNR score is shown in Fig. 7. It can be observed from this figure that in presence of 100% training data, SkipNetModel [52] performs better than the proposed models. However, it can also be observed from this figure that the performance of the proposed models is more or less remains the same. It can also be observed from this figure that the performance of SkipNetModel varies a lot with the variation in the amount of training data. Typical examples of inputs, groundtruths from the test set along with the outputs of the models trained on 100% training data are shown in Fig. 5. We have also shown a few examples of inputs and outputs of the trained models on real data in Fig. 6. It can be seen from this figure that the model M-32 performs better than SkipNetModel in few of the examples. From Figs. 6 and 7, we can conclude that the proposed model is more generalized and performs more robustly with respect to SkipNetModel. Finally, we present our experimental results with respect to document color cleanup task. One challenging aspect of color cleanup is the preservation of color of the foreground pixels. For this experiment, we used a color dataset consisting of 250 mobile captured images. Each of the images are manually cleaned. We followed the same training strategy for training our model as described in Sec. 4. Random real life images (not belonging to the train/test set) and their corresponding outputs are shown in Fig. 8. From this figure, we can observe a decent performance of the proposed model in performing color cleanup of document images. However, to provide a quantitative measure of our method, we compute PSNR, and structural similarity index (SSIM) score on the test : PSNR scores of the models on NoisyOffice dataset [51] with varying the training data from 25% to 100% set of the data in Table 4.

Conclusion
We have proposed an encoder-decoder based document cleanup model for resource constrained environments. To this end, we design a light-weight deep network with only a few residual blocks and skip connections. Our loss function incorporates the perceptual loss, which enables transfer learning from pre-trained deep CNN networks. We develop three models based on our network design, with varying network width. In terms of the number of parameters and product-sum operations, our models are 65-1030 and 3-27 times, respectively, smaller than a recently proposed GAN based document enhancement model [43]. In spite of our relatively low network capacity, the generalization performance of our models on various benchmarks are encouraging and comparable with several document image cleanup techniques with deep architectures such as [46,52]. In addition, our models are more robust to low training data regime than [52]. Hence, the proposed models offer a favorable trade-off between memory/latency and accuracy, making them suitable for mobile document image cleanup applications.