{'fraction_non_alphanumeric': 0.02629046669874218, 'fraction_numerical': 0.009657021101106606, 'mean_word_length': 5.038389707408176, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Adversarial Image Composition with Auxiliary Illumination

Dealing with the inconsistency between a foreground object and a background image is a challenging task in high-fidelity image composition. State-of-the-art methods strive to harmonize the composed image by adapting the style of foreground objects to be compatible with the background image, whereas the potential shadow of foreground objects within the composed image which is critical to the composition realism is largely neglected. In this paper, we propose an Adversarial Image Composition Net (AIC-Net) that achieves realistic image composition by considering potential shadows that the foreground object projects in the composed image. A novel branched generation mechanism is proposed, which disentangles the generation of shadows and the transfer of foreground styles for optimal accomplishment of the two tasks simultaneously. A differentiable spatial transformation module is designed which bridges the local harmonization and the global harmonization to achieve their joint optimization effectively. Extensive experiments on pedestrian and car composition tasks show that the proposed AIC-Net achieves superior composition performance qualitatively and quantitatively.


Introduction
With the advances of deep neural networks (DNNs), image composition has been attracting increasing attention as one typical approach for image synthesis, augmented reality, etc. With a foreground object and a background image, a direct combination tends to introduce unrealistic artifacts in the composed image with various inconsistency in colors, illuminations, texture, etc. Several methods have been proposed for harmonizing the foreground object and the background image, including DNN-based image translation [1], local statistic transfer [2], etc. On the other hand, existing methods typically simplify the harmonization by solving the image consistency within the foreground object region only but neglecting that realistic image composition goes far beyond that.
Specifically, a typical visual effect that is often associated with objects in scenes is their projected shadows due to the 3D nature of the world. While embedding a foreground object into a background image for image composition, arXiv:2009.08255v1 [cs.CV] 17 Sep 2020 Fig. 1. Given foreground objects and directly composed local images as shows in columns 1 and 2, the proposed AIC-Net disentangles the shadow generation and style transfer to produce realistic local harmonization as shown in column 3. With our spatial transformation module, the locally harmonized images in column 3 can be adapted and composed into the globally harmonized images as shown in column 4. the shadow effects of the foreground object are critical which can significantly improve the realism of the composed image if handled properly. Due to various constraints in 2D images such as complicated illuminations, losing of 3D information, etc., traditional computer vision methods are highly susceptible to failure for the task of high-fidelity shadow generation.
In this work, we design a novel Adversarial Image Composition Net (AIC-Net) that aims to achieve realistic image composition with adaptive shadow effects as illustrated in Fig. 1. AIC-Net adopts generative adversarial ideas to harmonize the foreground object within the background image and generates realistic shadow effects by learning from real shadows in natural images. It consists of local harmonization and global harmonization which are inter-connected and can be optimized jointly. We design a novel branched generation mechanism for high-fidelity local harmonization which disentangles the shadow generation and the style transfer of the foreground object for optimal accomplishment of two tasks simultaneously. An auxiliary illumination model is introduced to infer the illumination conditions of the background image such as lighting directions which help generate realistic shadows of the foreground object greatly. In addition, a differentiable spatial transformer is introduced to bridge the local region and the global background for joint optimization of the local and global harmonization. Two discriminators are employed where a local discriminator guides to learn style transfer and shadow generation of the foreground object and a global discriminator guide to learn global image harmonization.
The contributions of this work are threefold. First, we design an innovative AIC-Net that is capable of generating high-fidelity shadow of the foreground object adaptively. Second, we design a novel branched generation mechanism that disentangles the style transfer and shadow generation of the foreground object and accomplishes the two tasks optimally and simultaneously. Third, we design spatial transformer module (STM) that achieves joint optimization of local harmonization and global harmonization effectively.
Image harmonization deals with the inconsistency between a foreground region and a background image for composition. Several methods have been proposed to adapt the appearance of the foreground object for image harmonization. For example, [18] matches contrast, texture, and blur by manipulating the scales of a pyramid decomposition of images. [19] studies image statistics that determine the realism of the composite images. [20] proposes a gradient domain to achieve composition which can prevent color bleeding without changing the boundary location. A number of deep-learning based image harmonization methods have also proposed in recent years. For example, [21] utilizes visual semantics to guide the process of replacement to generate realistic, artifact-free images with diverse styles. [2] achieves both spatial and inter-scale statistical consistency by carefully determining the local statistics to be transferred. [1] proposes an end-toend deep convolutional neural network for image harmonization, which can capture both the context and semantic information of the composite images during harmonization. [17] combines a transformation network, a refinement network, a segmentation network and a pair of discriminator networks to achieve image composition via adversarial learning. Besides image harmonization, some work [22,16] focuses on the realism assessment of the composed images. [23] handles image harmonization through domain verification discriminator with the insight that the foreground needs to be translated to the same domain as background.
Illumination estimation is a classic computer vision problem and it is critical for achieving realistic lighting and shadows in image harmonization. Several deep learning based methods have been proposed to recover illumination from 2-dimensional RGB images [24,25,26,27,28].

Generative Adversarial Networks
GANs [29] have achieved great success in image generation from either existing images or random noises. Instead of manually selecting features and parameters, GAN generators learn an optimal mapping from random noise or existing images to synthesized images, while GAN discriminators differentiate the synthesized images from real ones via adversarial learning. Several GAN-based image synthesis methods have been reported in recent years. [30] introduces Laplacian pyramids that improve the quality of GAN-synthesized images greatly. [31] proposes an end-to-end trainable network for inserting an object instance mask into the semantic label map of an image. Other systems attempt to learning more reasonable potential features [32], exploring new training approach [33], visualizing GANs at the unit, object and scene level [34], etc. To expand the applicability, [13] generate shadow for augmented reality by modeling the mapping relation between the virtual object shadow and the real-world.
GAN-based image-to-image translation has been widely explored due to its wide applicability. CycleGAN [35] proposes a cycle-consistent adversarial network for realistic image-to-image translation. [36] investigates conditional adversarial networks for image-to-image translation. [37] adopts unsupervised learning to improve the realism of synthetic images using unlabelled real data. Other GANs [38,39,40,41] also achieve great performance in image-to-image translation.

Proposed Method
The proposed AIC-Net consists of local harmonization and global harmonization which are inter-connected and can be optimized jointly as illustrated in Fig.  2. Detailed network structures and training strategies will be presented in the following subsections.

Local Harmonization
The local harmonization is achieved by a local discriminator and a branched generation network which consists of a shadow branch and a texture branch as illustrated in Fig. 2. Given a background image (BG), a meaningful region within it is first selected for composition with a foreground object (FG). The selection of the meaningful region will be described in more details in Experiment Setting. We thus obtain a directly composed image (CI) x by embedding the foreground object into the selected region in the background image, as well as a foreground mask m f by setting the foreground object pixels to 1 and the rest pixels in x to 0 as illustrated in Fig. 2 In local harmonization, the generator aims to transfer the style of the composed image and generate realistic object shadows concurrently. Although the two processes can be jointly handled through image-to-image translation, they have very different translation objectives and learn with distinct image-to-image mapping mechanisms. The concurrent learning of both degrades the translation severely as illustrated in 'W/o Branched Generation' in Fig. 3. We design a novel branched generation strategy to disentangle style transfer and shadow generation for optimal image harmonization. As shown in Fig. 2, the generator The structure of the proposed AIC-Net: With a background image BG (with specified composition regions) and foreground object FG to be composed, a directly composed images CI can be derived directly. A shadow branch and a texture branch will learn to generate shadow and transfer the style of CI which produces a harmonized Local Image. A spatial transformer module (STM) then learns to compose the Local Image with the background image to produce a Global Image. An Illumination Model will predict SH coefficients which can be used to reconstruct an Illumination Map ('FC' denote fully connected layer). The Local Image will be concatenated with the Illumination Map to be distinguished by the Local Discriminator. The Global Image will be concatenated with an Embedding Mask to be distinguished by the Global Discriminator.
has two branches that share the same Encoder. With a background image and a composed image as the input, the decoder in shadow branch strives to generate shadows in the composed image while the decoder in the texture branch strives to transfer the style of the composed image to be compatible with the background image.
Shadow Branch To avoid synthesizing shadows with false directions as shown in 'W/o Illumination Model' in Fig. 3, we need to infer the global illumination information from the background images. In this work, we adopt a deep learning based method for illumination estimation. Specially, the ground truth of illumination map which is a panorama is represented by spherical harmonics (SH) coefficients [42], then a model with ResNet [43] as the backbone is adopt to regress the SH coefficients from the background images with limited field of view. With the predicted SH coefficients, an illumination map M s can reconstructed as follows: where s is the spatial direction, M is the number of SH coefficients, y i and SH i are the i-th SH basis function and SH coefficients, respectively. We pre-train the illumination model on the Laval Indoor dataset [25] and [44] and fix the model weights while training AIC-Net. The illumination model is able to infer the illumination from cues such as shadow existed in the background. Thus, the feature extracted by the illumination model contains rich information of shadow direction, size and intensity. In the shadow branch as shown in Fig. 2, we concatenate the feature extracted by the illumination model and the feature extracted by Encoder to integrate the shadow and illumination information into the shadow generation. The shadow decoder then strives to generate shadows with realistic shadow directions and styles in the local images. A binary mask (1-m f ) is applied to the output for integration with the texture branch. As the shadow branch may translate other regions beyond the shadow areas undesirably, we feed real images into the shadow branch to keep the non-shadow areas be the same with the input. The shadow branch will thus concentrate on the translation of potential shadow areas for realistic shadow generation. To drive the effective learning of shadow branch, the reconstructed illumination map will be combined with the locally harmonized image to be distinguished by the local discriminator.
Texture Branch The texture branch aims to translate the texture of the foreground region to be harmonious with the background image. As a normal GAN generator tends to over-translate the foreground region which often degrades the foreground features as illustrated in 'W/o Feature Filter' in Fig. 3, we introduce a guided feature filter to preserve the 'content features while performing style transfer. Different from the guided image filter [45] which is conducted in image level, guided feature filter is conducted in the feature space to achieve flexible style transfer. With the foreground object as the content guidance (C) and the feature decoded by the Texture Decoder as the style guidance (S), an output feature (T) with transferred style but preserved contents can be obtained through a linear model as follows: where i is a pixel index and ω k is a local square window centered at pixel k, a k and b k are linear coefficients to be optimized. The minimization of the reconstruction error between C and T can be achieved by the following cost function: where is a regularization parameter that prevents a k from being too large. It can be solved via linear regression: where µ k and σ 2 k are the mean and variance of S in ω k , |ω| is the number of pixels in ω k , and C k = 1 |ω| i∈ω k C i is the mean of C in ω k . Then we can obtain the transferred feature T . A following convolution layer will finally generate images with transferred style. A binary mask m f is applied to the output for further integration with the shadow branch. By denoting the outputs of the shadow branch and the texture branch by x s and x t , the locally harmonized image x h can be obtained as follows:

Global Harmonization
The harmonized local region should be consistent with global image regarding the image style especially the boundary of the local region in the global image, otherwise the global image will tend to present clearly artifacts as shown in 'W/o Global Harmonization' of Fig. 3 which should be addressed as well for high-fidelity global harmonization. We adopt a global discriminator to ensure the consistency in global harmonization. With real background image as the training reference, the global discriminator learns to distinguish the harmonized global image (Fake Global in Fig. 2) and real global images (Real Global in Fig.  2), driving the generator for generating harmonious shadows and an attention network for eliminating the style discrepancy and boundary artifacts. A spatial transformer module (STM) is designed to bridge the local image and global image to achieve joint optimization. Spatial Transformer Module: The spatial transformer is a differentiable module that applies a homography to the local image as illustrated in Fig. 4. We denote the coordinates of the four vertices of the background image by [(-1, 1), (1, 1), (1, -1), (-1, -1)], and the coordinate of the four vertices of the selected region by [(x1, y1), (x2, y2), (x3, y3), (x4, y4)] (−1 < x i < 1, −1 < y i < 1, i = 1, 2, 3, 4). With four paired vertices, the homograhy H can be computed which applied STM to warp the selected region to the local image. Once the local image is harmonized, it can be warped back to the local region of the background image by using the inverse homography H −1 as illustrated in Fig.  4. A warping mask with the selected region as 1 and other regions as 0 can be obtained simultaneously. A globally harmonized image can thus be obtained by masking the local patch into the background image. Since the STM and the masking operation are both differentiable, the local harmonization and the global harmonization can be bridged for end-to-end joint optimization.

Adversarial Training
With a GAN-based structure, the network is trained through adversarial learning between a generator and two discriminators. For clarity purpose, we denote the BG, CI in Fig. 2 as X and x, the generator and the two branches Shadow Branch and Texture Branch as G, G s and G t , Global Discriminator and Local Discriminator as D G and D L . The foreground mask is denoted as m f , the real global and local images as Y and y. The Embedding Mask in real global images and harmonized global images as m Y and m X .
For the local harmonization, the shadow and texture branches strive to generate shadow and translate foreground that are denoted by G s (X, x) and G t (X, x), respectively. The local image x h can thus be denoted by: The local discriminator tries to distinguish the local image while the generator tries to mislead the local discriminator. As we adopt Wasserstein GAN [33] objective for training, the local adversarial loss of the generator and local discriminator can be defined by: To ensure the irrelevant region keep unchanged and preserve more details of the original image, an identity loss is defined as follows: For the global harmonization, the global discriminator will distinguish the pair of global image X h and embedding mask m X which drives the generator to generate consistent texture and suppress artifacts around the local image boundary. The global adversarial loss of the generator and the global discriminator can thus be defined by: The overall objective function for the generator is: where λ G , λ G idt denote weights of global adversarial loss and generator identity loss respectively. The overall objective function for the discriminators is: where λ D G denotes the weight of global adversarial loss for the discriminator. L D and L G drive the learning of the whole model alternately.

Dataset
PRID 2011 [46] was created to evaluate person re-identification methods. The dataset consists of images extracted from multiple person trajectories recorded from two surveillance cameras. WILDTRACK [47] is multi-camera detection and tracking dataset of pedestrians in the wild, where the cameras fields of view has large part overlap.
Penn-Fudan [48] is an image database that was used for pedestrian detection evaluation as reported in [1]. The images are taken from scenes around campus and urban streets. The objects we are interested in these images are pedestrians. Each image will have at least one pedestrian.
UA-DETRAC [49] dataset consists of 100 challenging video sequences captured from over 140 thousand frames of real-world traffic scenes. More than 1.2 million labeled bounding boxes of vehicles with information of occlusion, weather, vehicle category and truncation are annotated.
Cityscapes [50] dataset focuses on semantic understanding of urban street scenes. It has street scenes from 50 cities and provides semantic segmentation of 30 classes. 3475 images are annotated with bounding boxes of persons.
ShapeNet [51] is a large repository of 3D object models which index more than 3 million models of which 220 thousand are classified into 3,135 categories.

Experiment Setting
In the pedestrian harmonization experiment, 2000 background images are collected from [46] and [47] and 500 pedestrians are cropped from [48] as the foreground objects. In the car harmonization experiment, we collect the background images from [49] and [50] which contain images captured under different illumination conditions and viewpoints. Random horizontal flipping is applied to the background images to augment the training set. For the foreground images, 1000 3D car models from [51] are utilized to render 3000 car images in different views.
The local region to compose with the foreground object is derived by a simple mechanism. As the bounding boxes of the foreground objects are know, we first select a box region around an existed bounding box and the one with no overlap with other bounding boxes is selected as the local region.
The proposed method is implemented using the Tensorflow framework. The optimizer is Adadelta which employs adaptive learning rate. The model is trained in 100 thousand iterations with a batch size of 4. In addition, the network is trained on a workstation with an Intel Core i7-7700K CPU and a NVIDIA GeForce GTX 1080 Ti graphics card with 12GB memory. All input images are resized to 256 × 256 in training. Table 1. Comparison of harmonization methods with different metrics: 'Car' and 'Ped' denote the results of car and pedestrian synthesis respectively. 'Local' and 'Global' denote harmonized local and global images. 'MS' metric denotes the manipulation scores. For AMT user study, the number in each cell is the percentage of the harmonized images that are deemed as the most realistic by Turkers.

Experiment Analysis
Quantitative Analysis: The evaluation is performed through Frechet Inception Distance (FID) [52], Manipulation Score (MS) [17] and Amazon Mechanical Turk (AMT) study. For the evaluation with FID, the harmonized images are fed to the Inception network [53] trained on ImageNet [54], and the features from the layer before the last fully-connected layer are used to calculate the Frchet Inception Distance. The Manipulation Score [17] is generated by a state-of-the-art manipulation detection algorithm [55] where a higher score indicates a higher possibility that the image is manipulated. For the AMT study which involves 10 users, we present a set of images (5 images by 5 methods) each time to the user to select the most realistic one. With 20 sets of presented images, an AMT score is derived by averaging the percentage of most realistic images as judged by the 10 users. 'Local' and 'Global' denote the results of locally and globally harmonized images respectively. Table 1 compares the quality and realism of images processed by different harmonization methods. For the local harmonization, AIC-Net outperforms all compared methods with all metrics clearly as it harmonizes the foreground region and generates relevant shadow which improves the image realism significantly. The using of guided filter helps to preserve texture of high quality, thus obtaining the best FID scores. The spatial transformer module achieves the joint optimization of local and global harmonization, leading to best Manipulation score. CycleGAN can also synthesize shadows but the foreground region tends to be translated undesirably as it does not disentangle the shadow generation and style transfer processed. As DeepIH requires paired training images with semantic annotations, it performs terribly bad when paired training images are not available. As style transfer methods, DeepPH and AdaIN harmonize the foreground region successfully but all of them ignore the shadow generation completely. For the global harmonization, AIC-Net outperforms other methods by large margins as its joint optimization of local and global harmonization ensures consistent shadow and smooth local boundary. The lack of global harmonization leads to clear artifacts in the global image harmonized by CycleGAN. For DeepIH, DeepPH and AdaIN, there are not many artifacts but the lack of shadow and guided filter degrades the image realism and quality greatly. Normally, the global harmonized images present better score than local harmonized images as only small part of global image needs to be harmonized.
Qualitative Analysis: We compare the images harmonized by different methods in Fig. 5, where AIC-Net (L) and AIC-Net (G) denote harmonization of local images and global images by our proposed AIC-Net. As Fig. 5 shows, DeepIH [1] can only achieve a slight translation and cannot generate any potential shadow, thus resulting in unrealistic harmonized images. The branched generation in AIC-Net disentangles the style transfer and shadow generation, leading to much more realistic image harmonization. The global discriminator in AIC-Net works better by driving the generator to remove the boundary artifacts. AdaIN [56] transfer the style of the foreground region to be harmonious with the background, but they do not handle shadow generation which leads to unrealistic composition. Besides, AdaIN is unable to preserve the texture quality of harmonized image due to the missing of guided filter.
To validate that the proposed model is able to generate various shadow adaptively, we tested the trained model in different environments as shown in Fig.  6. The first two rows show the backgrounds and local images respectively, the last row shows locally harmonized images and globally harmonized images produced by our model. As shown in the first three columns of Fig. 6, the proposed model generates shadows conditioned on the existed shadow cases in the backgrounds. For background images without clearly shadow case as shown in the last two columns of Fig. 6, the proposed model is still able to synthesize realistic shadows conditioned on the feature extracted by the illumination model.

Ablation Study:
We conducted an ablation study to evaluate the effectiveness of different technical designs in AIC-Net. As shown in Table 2, four variants of AIC-Net are designed including: 1) AIC-Net (WF) denotes AIC-Net without the guided feature filter; 2) AIC-Net (WB) denotes AIC-Net without using the branched generation mechanism; 3) AIC-Net (WS) denotes AIC-Net without incorporating the spatial transformer module; 4) AIC-Net(WI) denotes AIC-Net without illumination model. The evaluations are performed with three metrics including FID, MS and AMT study that involves 10 users. For AMT user study, a set of images (5 generated by AIC-Net and its four variants) are presented to each of 10 users who will choose the most realistic image. We presented 25 sets of harmonized images and the AMT score is derived by averaging the percentages of the most realistic images as judged by the 10 users. The scores of car and pedestrian experiments are averaged for each metric to obtain the final scores.
As Table 2 shows, AIC-Net (WF) obtains clearly lower scores with all metrics as compared with the standard AIC-Net which shows the importance of guided feature filter in generating realistic image harmonization. AIC-Net (WB) Table 2. We conduct ablation study with three metrics (FID, AMT, MS) to evaluate the quality of harmonized local and global images. AIC-Net (WF), AIC-Net (WB), AIC-Net (WS) and AIC-Net(WI) denote AIC-Net without the guided feature filter, without branched generation mechanism, without spatial transformer module and without illumination model respectively. For AMT user study, the percentage numbers represent how often the harmonized images in each category are classified as most realistic by Turkers. We average the FID, AMT and MS scores of car and pedestrian images as the final scores. also obtains lower scores than AIC-Net as the branched generation in AIC-Net disentangles the shadow generation and style transfer to produce more realistic local harmonization as shown in Fig. 3. For AIC-Net (WS), the scores for the global images are much lower than the other models, demonstrating the importance of the spatial transformer module which bridges the local and global harmonization to produce globally realistic harmonization. On the other hand, AIC-Net (WS) achieves similar scores which can be expected as both models are almost the same in local harmonization. AIC-Net (WI) present clearly drop of AMT score compared with standard AIC-Net, indicating the significance of illumination model for the generation of realistic shadow.

Conclusion
This paper presents AIC-Net, an end-to-end trainable network that harmonizes foreground objects and background images with adaptive and realistic shadow effects. A branched generation mechanism is designed to disentangle style transfer and shadow generation to produce high-fidelity local harmonization. An auxiliary illumination model is collaborated in shadow branch to achieve adaptive shadow generation. A spatial transformer module is introduced to bridge the local and global harmonization for joint optimization. The quantitative and qualitative experiments demonstrate the superior harmonization performance and wide adaptability of the proposed method. We will continue to study AIC-Net for image harmonization with shadow generation within more complicated illumination environments.