StyTr2: Image Style Transfer with Transformers

Yingying Deng, Fan Tang, Weiming Dong, Chongyang Ma, Xingjia Pan, Lei Wang, Changsheng Xu

Abstract

Image style transfer has traditionally been dominated by Convolutional Neural Networks (CNNs). However, the inherent locality of convolution operations often leads to "content leak" and a failure to capture global structural dependencies. We present StyTr$^2$, a transformer-based framework designed to address these limitations. By utilizing a pure transformer architecture and a novel Content-Aware Positional Encoding (CAPE), StyTr$^2$ achieves superior stylization quality with enhanced structural preservation.

Introduction

The goal of image style transfer is to render the content of one image in the artistic style of another. While CNN-based methods have achieved significant success, they suffer from a fundamental limitation: a restricted receptive field. Because CNNs process information locally, they struggle to maintain long-range spatial relationships, often resulting in distorted global structures—a phenomenon known as "content leak."

To overcome this, we propose StyTr$^2$, which leverages the global attention mechanism of Transformers. Unlike CNNs, Transformers possess a global receptive field from the very first layer, allowing for a more holistic understanding of image composition and style application.

Methodology

The StyTr$^2$ architecture is composed of several key innovations designed to optimize the transformer for vision-based style transfer.

Dual Transformer Encoders and Decoder

Standard Vision Transformers (ViTs) typically use a single encoder. In contrast, StyTr$^2$ employs two separate encoders to generate domain-specific sequences for the content and style images independently. These sequences are then processed by a multi-layer transformer decoder. Unlike auto-regressive transformers used in natural language processing, this decoder processes all image patches in parallel, attending to the style sequence to stylize the content features.

The StyTr2 architecture featuring dual transformer encoders and a content-aware positional encoding (CAPE) module.

Content-Aware Positional Encoding (CAPE)

Traditional positional encodings (PE), such as fixed sinusoidal functions, can introduce artifacts when images are resized or contain repetitive textures. To mitigate this, we introduce CAPE. CAPE is learned based on the semantic features of the input image and is designed to be scale-invariant. This ensures that spatial relationships remain consistent across different input resolutions, preventing the "vertical track" artifacts common in standard transformer-based vision models.

CNN Refinement Decoder

While the core stylization happens within the transformer, a lightweight three-layer CNN decoder is utilized at the final stage. This component upsamples the stylized patch sequences back into a high-resolution image, refining fine-grained details and ensuring a smooth final output.

Experimental Results

StyTr$^2$ was evaluated against several state-of-the-art (SOTA) methods, including AdaIN, SANet, and IEST, using the MS-COCO and WikiArt datasets.

Qualitative Evaluation

Visual comparisons demonstrate that StyTr$^2$ produces sharper details and more consistent style patterns than its predecessors. By maintaining global dependencies, the model avoids the structural blurring often seen in CNN-based approaches.

Qualitative comparison of StyTr2 against state-of-the-art style transfer methods across various content and style pairs.

Quantitative Performance and User Study

StyTr$^2$ achieved the lowest content loss in perceptual evaluations, indicating superior preservation of the original image structure. Furthermore, a user study involving 100 participants confirmed a strong preference for StyTr$^2$ in terms of overall artistic quality, content integrity, and stylistic consistency.

Conclusion

StyTr$^2$ represents a significant shift in image style transfer by replacing local convolution with global self-attention. Through the introduction of dual encoders and CAPE, the model effectively eliminates content leak and handles varying resolutions with high fidelity. While the computational cost of transformers remains higher than optimized CNNs, the leap in visual quality establishes StyTr$^2$ as a powerful new baseline for high-quality artistic stylization.

Summary updated

BibTeX
@inproceedings{deng2022stytr,
  author = {Yingying Deng and Fan Tang and Weiming Dong and Chongyang Ma and Xingjia Pan and Lei Wang and Changsheng Xu},
  title = {{StyTr2: Image Style Transfer with Transformers}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2022},
  doi = {10.1109/cvpr52688.2022.01104},
}
Figure