{'fraction_non_alphanumeric': 0.03078093103310251, 'fraction_numerical': 0.011367730056438378, 'mean_word_length': 4.763321799307959, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 0, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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}}
Perspective Transformation Layer

Incorporating geometric transformations that reflect the relative position changes between an observer and an object into computer vision and deep learning models has attracted much attention in recent years. However, the existing proposals mainly focus on the affine transformation that is insufficient to reflect such geometric position changes. Furthermore, current solutions often apply a neural network module to learn a single transformation matrix, which not only ignores the importance of multi-view analysis but also includes extra training parameters from the module apart from the transformation matrix parameters that increase the model complexity. In this paper, a perspective transformation layer is proposed in the context of deep learning. The proposed layer can learn homography, therefore reflecting the geometric positions between observers and objects. In addition, by directly training its transformation matrices, a single proposed layer can learn an adjustable number of multiple viewpoints without considering module parameters. The experiments and evaluations confirm the superiority of the proposed layer.


I. INTRODUCTION
Human vision is often insusceptible to viewpoint changes; for example, we can recognize an object regardless of the object's presenting angle. In computer vision, realizing such an insusceptible model is of great interest because it can provide spatial robustness [1] to a lot of application scenarios such as the classification and object detection [2], [3]. One efficient approach to simulate the relative position changes between an observer and an object is to perform geometric transforms [4], [5], where the geometry of an image is changed by projecting the coordinates without altering the pixel values. Typical efficient and linear geometric transforms include affine transformation which contains the rotation, scaling, translation, and shearing; and perspective transformation (PT) which indicates how the perception of an object changes when an observer's viewpoint varies.
Although convolutional neural networks (CNNs) have achieved state-of-the-art performance in many computer vision applications [6]- [8], due to the fragility of the neural networks under geometric distortions [9], [10], appropriately responding to the geometric transformations within deep learning frameworks remains a key challenge. Traditional CNNs try to address this geometric distortion issue through data augmentation This  and local pooling layers. Although data augmentation that adds geometric distortions into the training dataset could introduce some geometry information to a deep learning model, there is no performance guarantee for untrained transformations. Consequently, prior knowledge in a specific problem domain is often required to determine the to-be-augmented operations, which is not always feasible. On the other hand, the local pooling layers (e.g. a 2 × 2 or 3 × 3 local max pooling) can only mitigate the impact of translations after many rounds of downsampling, and translation is just one simple instance of geometric transformation. Hence, naive solutions are insufficient to model geometric transformations in deep learning, and researchers have been exploring different solutions.
Incorporating geometric transformations in deep learning has attracted increased attention in recent years [11]- [15]. One most representative idea is to learn a transformation matrix (TM) by a CNN module. So, if the CNN module is inserted into a deep learning model and the learned TM is applied to transform some feature maps, a transform can be simultaneously trained with other layers. However, challenges still exist. First, in most existing work, the learning outcomes are limited to the affine transformations which are insufficient to fully model and reflect the geometric position changes between an observer and an object in a real three-dimensional (3D) world. Furthermore, current modules often output a single TM, which only learns one possible transform. But, it is necessary to analyze multiple viewpoints of objects for a robust computer vision model [16]. Finally, existing modular solutions often create extra to-be-trained module parameters that increase the model complexity. To address the above challenges, in the context of deep neural networks, we propose a PT layer that can learn different viewpoints (see Fig. 1). The advantages of our design are threefold: (i) The PT layer learns homography so that the perception of an object from different viewpoints in a 3D world can be reflected and trained in two-dimensional (2D) images. (ii) One single PT layer can learn multiple TMs to provide multi-viewpoint analysis to the subsequent layers; furthermore, the number of TM for a PT layer is adjustable so that the learning capacity of each PT layer can be tuned. (iii) The proposed PT layer directly trains its TMs with gradient descent, therefore avoiding training any extra module parameters to obtain the TMs.
The remainder of this paper is organized as follows. The related work is discussed in Section II. Details of the proposed PT layer is presented in Section III. Experiments and analysis are presented in Section IV. Finally, conclusions are drawn in Section V.
II. RELATED WORK This section briefly reviews the related work that tries to model geometric transformations in deep learning. Current idea is to develop a neural network module that outputs a single affine TM and to jointly train the module with other layers by transforming some feature maps with this TM.
The spatial transformer network (STN) [11] was the first effort to model the affine transformations. STN proposed a modular CNN block to output an affine TM so that the rotation, scaling, translation, and shearing can be applied to the module inputs. Later, many improvements of STN were proposed. Incorporating a cascade of STNs, Lin et al. [12] proposed an inverse compositional STN that established a connection between the classical Lucas-Kanade algorithm [17] and the STN, hence multiple affine transforms with alignments were applied to the data. Detlefsen et al. [13] proposed a deep diffeomorphic transformer network that developed a diffeomorphic continuous piecewise affine-based transformation and created two modules that learn affine and the continuous affine respectively. Combining the ideas of STN and canonical coordinate representations, Esteves et al. [14] proposed a polar transformer network that achieved the invariance to translations and equivariance to rotations and dilations. One recent successful improvement of STN is the equivariant transformer network (ETN) [15] that proposed to sequentially connect a canonical coordinates transformation and a STN module. Compared to STN, ETN improved the robustness to some continuous transformations.
There were a few proposals involving viewpoints in deep learning. For example, Handa et al. [18] developed a library to project 3D data into a 2D image through a single projection matrix. Garau et al. [19] proposed to apply the capsule network [20] to capture the equivalent viewpoint information for human poses; Yan et al. [21] proposed an encoder-decoder network to learn perspective information for 3D object reconstruction with only 2D supervision; And Yu et al. [22] decomposed the inverse perspective mapping into multiple layers adopting encoder-decoder architecture to provide gradual viewpoint changes for lanes and road marks. Current methods involving the modeling of perspective apply specially designed modules/networks in specific application scenarios, and hence have different aims from the proposed PT layer.
Thus far, most existing methods use a module network to learn one affine transformation or learn viewpoint for a specific use case. To the best of our knowledge, our proposed PT layer is the first method that directly trains its layer TMs to learn multiple PTs for multiple possible viewpoints. Fig. 2 presents the details of the proposed PT layer. In the context of deep learning, the proposed layer input is defined as images or feature maps I ∈ R N ×H×W ×Ch where H denotes the height, W is the width, Ch represents the number of channels, and N is the batch size. A PT layer learns M TMs (M is definable for an adjustable learning capacity). Each TM θ m ∈ R 3×3 , m = 1, 2, ..., M transforms all the input channels. Thus, for each input channel, we can learn M perspectives, and the layer output is defined as O ∈ R N ×H×W ×(Ch×M ) that has Ch × M channels.

A. Homography Transformation Matrix
This section discusses our core learning target: the homography TM. In computer vision, under an assumption of a pinhole camera model [23], any two images of the same planar surface in space are related by a homography. One major purpose in image homography is to compute a homography matrix (which estimates the parameters of the pinhole camera model). And once the homography matrix is obtained, we can apply it to (i) map planar surfaces in the 3D real world to the corresponding 2D images; and (ii) transform between different perspectives or viewpoints of the same image.
The homography matrix can be derived from a pin hole camera matrix [23] C that contains the information of an internal camera matrix IN and an external camera matrix EX: (1) IN includes information about the focal distance f , the scaling factors of both vertical (x-axis direction) and horizontal (yaxis direction) on an image s x and s y , the translation amount tr x and tr y , and a shearing factor sh. Using homogeneous coordinates, IN can be written as: And EX indicates the rotation and translation of the camera frame to the world frame with its rotation parameters r i,j and translation amount tr i : C maps a point (x, y, z) in the 3D world space onto the image plane. And the homography matrix can be derived as a case of projecting a flat plane scene (x, y, 0) onto the image plane. Mathematically, We propose to train the homography matrix which has 8 degrees of freedom (i.e., θ 1,1 to θ 3,2 ) to learn the projection and the perspectives in the 3D world on a 2D image. In contrast, current literature trains the affine transformation where the θ 3,1 and θ 3,2 in Equation 4 (i.e., the projection vector) are always 0. Consequently, the affine transformation can merely contain rotation, scaling, translation, and shearing and has to preserve parallelism while the homography does not preserve parallelism, length, or angle.

B. Transformation and Interpolation
This section describes the forward pass of the proposed PT layer: the transformation and interpolation. Let (x, y) | 0 <= x <= W, 0 <= y <= H denote the location of a point in one input channel, in homogeneous coordinates, the proposed PT layer transforms each point as: where θ m is m-th 3 × 3 to-be-learned TM with 8 degrees of freedom shown as θ 1,1 to θ 3,2 in Equation 4, and (x , y , ω) is the transformed coordinates in which ω represents the distance between a camera and an image plane. We can convert homogeneous coordinates back to Cartesian coordinates by Thus, giving a point (x, y) on an input feature map, the PT layer transforms it to a new location (x , y ) on its corresponding output perspective feature map. The next step is to perform interpolation to determine each new value at each new location. At the ch-th channel, denoting the value of the input at (x, y) as I ch x,y and its corresponding output perspective feature map as O ch , we can compute the interpolation as: where K(·, ·) is a (differentiable) kernel function that can be implemented with different strategies. For example, if we use a bilinear interpolation, the kernel function is: And if we use a bicubic interpolation, K 1 in Equation 8 is: where α is the free parameter in a bicubic interpolation.

C. Back-propagation
Besides learning the homography, another advantage of the proposed PT layer is its ability to directly train its multiple TMs with gradient descent. So, we show that the PT layer is differentiable in this section.
The operations of the PT layer include transformation and interpolation (i.e., Equations 5, 6,and 7). We first show that the interpolation in Equation 7 is differentiable by computing the partial derivatives of the output O ch with respect to I ch x,y , x , and y : And we can obtain ∂O ch /∂y similar to Equation 11. As discussed in Equations 8 and 9, we have implemented the bilinear and bicubic interpolation kernels. For the bilinear kernel, Equations 10 and 11 can be written as: and And for the bicubic kernel, Equation 10 can be written as: where K 1 (u) can be denoted by Equation 9. In addition, the K 1 in Equation 11 is: So far, we have shown that the interpolation in Equation 7 is differentiable.
When performing back-propagation, the gradient flows through the interpolation (Equation 7), the coordinates conversion (Equation 6), and the transformation (Equation 5). For the coordinates conversion, it directly transmits the gradient obtained by the interpolation without altering anything. And finally, the transformation is differentiable and its derivative is (x, y, 1) T .
In sum, the proposed PT layer is differentiable so that it is stackable in deep neural networks, and the TMs can be trained by gradient descent together with other model weights. Notably, because each TM (θ m ) is applied in the PT of all input channels, it affects multiple positions. To update the parameters in a TM, we average all the derivatives obtained from all the affected positions.

IV. EXPERIMENTS AND ANALYSIS
This section experimentally analyzes quantitative and analytical evaluation of the proposed PT layer. Section IV-A introduces our data preparation. Section IV-B validates the functionality of the proposed PT layer by learning known target PTs. Section IV-C discusses the ablation study that compares the performance with and without the PT layer. Section IV-D analyzes the influence of multiple TMs of the PT layer. Finally, Section IV-E compares the performance of the proposed PT layer against other well-known modules. Besides, some additional experiments of image segmentation and potential application discussions and are in the project's Github page 1 .

A. Data Preparation
Distorted MNIST. The modified national institute of standards and technology database (MNIST) [24] is a standard dataset of handwritten digits that consists of 60, 000 training and 10, 000 testing grayscale images of size 28 × 28. For 1 https://github.com/kcnishan/Perspective Transformation Layer.git our training dataset, we apply random PTs on 55, 000 images and leave the remaining 5, 000 unmodified. Some images are left unmodified since the unmodified images can provide the original geometric information to the PT layer. For the testing set, we apply random PTs on all 10, 000 images. Figure 3 shows a few examples of randomly transformed MNIST images.  Figure 4). The SVHN can facilitate our experiments because the street number images are collected by cameras that naturally introduce various viewpoints. In our experiments, we show that the PT layers can learn good viewpoints to help a downstream classifier that recognizes the street numbers. Imagenette. The Imagenette [26] is a collection of 10 representative classes from the huge Imagenet dataset [27]. The Imagenette dataset includes 9, 469 training and 3, 925 testing RGB images of various sizes. Out of these, we use the ones with 320 × 320 size and resized them to 224 × 224. We adopt this representative and small version of the Imagenet for two main reasons: (i) the Imagenette validates the proposed PT layer with larger (224 × 224) images compared to the MNIST and the SVHN; and (ii) rather than applying all the 1, 000 classes with more than millions of images from the Imagenet, the Imagenette provides a fast reproducibility. In our experiments, we test the proposed PT layer on both the original and distorted Imagenette for a comprehensive performance evaluation. In terms of the distorted Imagenette, for the training set of 9, 469 images, we apply random PTs on approximately 8, 285 images (87.5%) and leave the remaining 1, 184 images (12.5%) unmodified. For the 3, 925 testing images we use the same split of transformed and unmodified images. Figure 5 shows a few examples of Imagenette images along with their distortions.

B. Functionality Validation
This section validates the fundamental functionality of the proposed PT layer. As a layer designed to learn different viewpoints, the proposed PT layer should be able to easily rectify the inputs transformed by PTs if the original images are given as the labels. Experiment scheme. As illustrated in Figure 6, we create a simple model with two PT layers (each layer has only 1 TM) where the model inputs are the MNIST images with PTs, and the outputs are the undistorted images. We train this model by minimizing the mean squared error (MSE) between the model outputs and the original images. Results. This simple two-layer model can successfully learn the rectifications within a few epochs. Although we actually find that even a single PT layer can learn this rectification, we present this two-layer model to illustrate a progressive transformation of each PT layer towards the final target (see a few examples in Figure 7). The results of the two-layer model not only demonstrate the functionality of the PT layers but also show that the PT layers are stackable (the gradients flow through them) in a deep learning framework.

C. Ablation Study
This section discusses the advantage of using the proposed PT layer. In addition to the study that compares the performance of a model with and without PT layer(s), we also test the effect of different inserting positions and multiple PT layers. Experiment scheme. As presented in Figure 8, we adopt the CNN architecture used in the evaluation from the G-CNN [28] (all convolutional layers have 32 filters) and select the places after the input, the first, the second, and the third convolutional layers as the inserting positions of a PT layer. To empirically test a good position for the PT layer, we insert a PT layer (with four TMs) at one position at a time. And to test multiple PT layers in a model, we also test the case of placing a PT layer at all the positions mentioned above. The ablation study is performed on the distorted MNIST and the SVHN. We train the models for 30 epochs with the goal of classification using categorical cross-entropy as the loss function, which is minimized by the ADAM optimizer [29] at a starting learning rate of 0.0001. Notably, we add a 1 × 1 convolutional layer after a PT layer. The filter number of the convolutional layer is equal to the channel number of the input to the PT layer. In this way, the channel shapes of the feature maps are the same before and after the PT layer. Results. Table I shows the classification testing accuracy, where one can observe that inserting the PT layer(s) improves the classification performance at each position (the baseline model is the one without any PT layers). In addition, placing a PT layer at all the selected positions achieves the best accuracy, which indicates that increasing the number of PT layers can enhance the performance of a model. Given a deep learning model, the number of PT layers and the inserting positions are tunable hyperparameters.

D. Multiple Transformation Matrices
This section presents the experiments of a PT layer with multiple TMs. The influence of different numbers of TMs is analyzed. Experiment scheme. We use the well-known ResNet-18 [6] and the VGG-16 [30] (pretrained on the Imagenet dataset) as the baseline models. A PT layer with different numbers of TMs is inserted after the input layer in each baseline model. All the models are trained with the goal of classification using categorical cross-entropy as the loss, which is minimized by the ADAM optimizer at a starting learning rate of 0.0001. Thus, in order to analyze the influence of multiple TMs, the number of TMs is the only difference between the models' training configurations. Also, the PT layer in nature may favor the datasets including PT distortions. To exclude this possible bias, we test on both the distorted and original datasets. Results. Tables II and III (PTL-x denotes a PT layer with x TMs, x = 4,8,16,32 here) present the experimental results. In most cases, because increasing the TM number enhances the learning capacity, a larger number of TMs can improve the model performance. When the TM number is already large, keeping increasing the TM number could only bring incremental improvements. This is because it is the baseline model that cap the overall performance. There is one case (i.e., PTL-32 in the ResNet-18 on the original Imagenette) where keeping increasing the TM number decreases the performance. This is because increasing the learning capacity of one of the layers cannot guarantee the improved performance of the whole model [31], [32]. Given a deep learning model, the number of TMs is a tunable hyperparameter for the proposed PT layer.
In addition, we have validated that each trained TM in a PT layer is different so that various possible viewpoint options are learned and offered to the successive layers. Figure 9 shows an example image from the SVHN dataset and a few of its learning outcomes, where we can observe different viewpoints (caused by different learned TMs).

E. Comparison
This section compares the performance of the proposed PT layer against the state-of-the-art methods on our datasets. Specifically, to extract geometric features, the proposed PT layer, the STN module, and the ETN module are respectively inserted after the input of a baseline model, and the classification accuracies are compared.
It is worth noting that constructing an ETN module requires providing information about the order and type of distortion to its transformer layers. In contrast, the proposed PT layer does not need any prior knowledge of the distortions. In this experiment, we provide the following prior knowledge to the ETN: the distortion consists of a translation, a PT on the horizontal axis, a PT on the vertical axis, and a scaling on the horizontal axis.
Another advantage of the proposed PT layer is that it directly applies gradient descent to train its TMs. Thus the PT layer only requires the TM parameters and does not introduce any to-be-trained module network parameters. Each learning homography matrix has 8 degrees of freedom, so a PT layer has TMs × 8 × Ch (where Ch is number of channels) training parameters (e.g., a PT layer will have 16 × 8 × 1 = 128 training parameters when we have 16 TMs and single channel). In contrast, existing modularized solutions create the extra module parameters, for example, to learn one affine matrix, a typical STN module introduces 36, 000 extra training parameters with two convolutional and two fully-connected layers. Experiment scheme. The performance comparison experiment scheme is similar to the one described in Section IV-D. We use the ResNet-18 and the VGG-16 models on different distorted and original datasets and compare the performances of the PT layer, the STN, and the ETN. The number of TMs of the PT layer is 16 or 32 in these experiments.  Results. Tables IV and V illustrate the superiority of the proposed PT layer. With different models on the original and distorted datasets, the proposed PT layer always has better classification accuracies than both the baseline model and the models with ETN and STN modules. The PT layer significantly improves the accuracy, especially when it is difficult for a baseline model to handle a dataset. For example, when we use the VGG-16 for the distorted and original Imagenette, the PT layer can provide approximately 10% more accuracy than the baseline. We also visualize some after-layer and after-module outputs from the PT layer, the STN, and the ETN. Some example input images and their corresponding outputs of the PT layer and the STN and ETN modules are shown in Figure 10. Compared to the STN and the ETN, the proposed PT layer learns good viewpoints (we are just displaying one of them) without cropping out the objects, highlights (centralizes) the target objects, and has smaller changes to the overall pixel intensities.
V. CONCLUSION This paper introduces a PT layer that learns viewpoints on images in deep learning. The proposed PT layer learns homography to reflect the changes in the 3D world to a 2D image, provides the capacity of learning multiple and adjustable viewpoints, and facilitates its training of TMs with gradient descent by developing differentiable operations. Experimentally, we have validated the basic functionality of the proposed PT layer, confirmed the PT layer's promising performance by the ablation study, analyzed the increased learning capacity of multiple TMs, and demonstrated our superiority as compared to other state-of-the-art methods.