|
--- |
|
license: apache-2.0 |
|
datasets: |
|
- aoxo/photorealism-style-adapter-gta-v |
|
- aoxo/latent_diffusion_super_sampling |
|
language: |
|
- en |
|
metrics: |
|
- accuracy |
|
pipeline_tag: image-to-image |
|
tags: |
|
- art |
|
--- |
|
# RealFormer - Photorealism over Supersampling |
|
|
|
EmelinLabs introduces RealFormer, a novel Image-to-Image Transformer-based architecture designed and trained to enhance photorealism in images, particularly focused on bringing a real lifelike style to synthetic artifacts in media. |
|
|
|
![thumbnail](thumbnail.webp) |
|
|
|
## Model Details |
|
|
|
Detailed description of model, its architecture, training data and procedures. |
|
|
|
### Model Description |
|
|
|
RealFormer is an innovative Vision Transformer (ViT) based architecture that combines elements of Linear Attention (approximation attention) with Swin Transformers and adaptive instance normalization (AdaIN) for style transfer. It's designed to transform images,specifically targeted at the video game and animation industry, potentially enhancing their photorealism or applying style transfer. |
|
|
|
- **Developed by:** Alosh Denny |
|
- **Funded by:** EmelinLabs |
|
- **Shared by:** EmelinLabs |
|
- **Model type:** Image-to-Image Transformer |
|
- **Language(s) (NLP):** None (Pre-trained Generative Image Model) |
|
- **License:** Apache-2.0 |
|
- **Finetuned from model [optional]:** Novel; Pre-trained (not finetuned) |
|
|
|
### Model Sources [optional] |
|
|
|
- **Dataset:** [Pre-Training Dataset](https://huggingface.co./datasets/aoxo/latent_diffusion_super_sampling), [Calibration Dataset for Grand Theft Auto V](https://huggingface.co./datasets/aoxo/photorealism-style-adapter-gta-v) |
|
- **Repository:** [Swin Transformer](https://github.com/microsoft/Swin-Transformer) |
|
- **Paper:** [Ze Liu et al. (2021)](https://arxiv.org/abs/2103.14030) |
|
|
|
## Uses |
|
|
|
### Direct Use |
|
|
|
RealFormer is designed for image-to-image translation tasks. It can be used directly for: |
|
|
|
- Enhancing photorealism in synthetic images (e.g., transforming video game graphics to more realistic images) |
|
- Style transfer between rendered frames and post-processed frames |
|
- To be incorporated in pipeline with [DLSS](https://developer.nvidia.com/rtx/dlss) |
|
|
|
### Downstream Use |
|
|
|
Potential downstream uses could include: |
|
|
|
- Integration into game engines for real-time graphics enhancement - AdaIN layers are finetunable for video-game-specific usecases. In this implementation, the models have been pretrained on a variety of video for **super-sampling**, **photorealistic style transfer** and **reverse photorealism**. |
|
- Pre-processing step in computer vision pipelines to improve input image quality - Decoder layers can be frozen for task-specific usecases. |
|
- Photo editing software for synthesized image enhancement |
|
|
|
### Out-of-Scope Use |
|
|
|
This model is not recommended for: |
|
|
|
- Generating or manipulating images in ways that could be deceptive or harmful |
|
- Tasks requiring perfect preservation of specific image details, as the transformation process may alter some artifacts of the image |
|
- Medical or forensic image analysis where any alteration could lead to misinterpretation. Remember, this is a model, not a classification or detection model. |
|
|
|
## Bias, Risks, and Limitations |
|
|
|
- The model may introduce biases present in the training data, potentially altering images in ways that reflect these biases. |
|
- There's a risk of over-smoothing or losing fine details in the image transformation process. |
|
- The model's performance may vary significantly depending on the input image characteristics and how similar they are to the training data. |
|
- As with any image manipulation tool, there's a potential for misuse in creating deceptive or altered images. |
|
|
|
## How to Get Started with the Model |
|
|
|
Use the code below to get started with the model. |
|
|
|
```python |
|
# Instantiate the model |
|
model = RealFormerv3(img_size=256, patch_size=8, emb_dim=768, num_heads=42, num_layers=16, hidden_dim=3072) |
|
|
|
# Move model to GPU if available |
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
model = model.to(device) |
|
|
|
# Load an image |
|
input_image = load_image('path_to_your_image.png') |
|
input_image = input_image.to(device) |
|
|
|
# Perform inference |
|
with torch.no_grad(): |
|
output = model(input_image, input_image) # Using input as both content and style for this example |
|
|
|
# Visualize or save the output |
|
visualize_tensor(output, "Output Image") |
|
``` |
|
|
|
## Training Details |
|
|
|
### Training Data |
|
|
|
The model was trained on [Pre-Training Dataset](https://huggingface.co./datasets/aoxo/latent_diffusion_super_sampling) and then the decoder layers were frozen to finetune it on the [Calibration Dataset for Grand Theft Auto V](https://huggingface.co./datasets/aoxo/photorealism-style-adapter-gta-v). The former includes over 400,000 frames of footage from video games such as WatchDogs 2, Grand Theft Auto V, CyberPunk, several Hollywood films and high-defintion photos. The latter comprises of ~25,000 high-definition semantic segmentation map - rendered frame pairs captured from Grand Theft Auto V in-game and a UNet based Semantic Segmentation Model. |
|
|
|
### Training Procedure |
|
|
|
- Optimizer: Adam |
|
- Learning rate: 0.001 |
|
- Batch size: 8 |
|
- Steps per epoch: 3,125 |
|
- Number of epochs: 100 |
|
- Total number of steps: 312,500 |
|
- Loss function: Combined L1 loss, Perpetual Loss, Style Transfer Loss, Total Variation loss |
|
|
|
#### Preprocessing |
|
|
|
Here's a more concise version of your original paragraph, maintaining the essential information: |
|
|
|
--- |
|
|
|
**Preprocessing of Large-Scale Image Data for Photorealism Enhancement** |
|
This section details our methodology for preprocessing a large-scale dataset of approximately **117 million game-rendered frames** from **9 AAA video games** and **1.24 billion real-world images** from Mapillary Vistas and Cityscapes, all in 4K resolution. The goal is to pair game frames with real images that exhibit the highest cosine similarity based on structural and visual features, ensuring alignment of fine details like object positions, level of detail and motion blur. |
|
|
|
Images and their corresponding style semantic maps were resized to **512 x 512** pixels and corrected to a **24-bit** depth (3 channels) if they exceeded this depth. We employ a novel **feature-mapped channel-split PSNR matching** approach using **EfficientNet** feature extraction, channel splitting, and dual metric computation of PSNR and cosine similarity. **Locality-Sensitive Hashing** (LSH) aids in efficiently identifying the **top-10 nearest neighbors** for each frame. This resulted in a massive dataset of **1.17** billion frame-image pairs and **12.4 billion** image-frame pairs. The final selection process involves assessing similarity consistency across channels to ensure accurate pairings. This scalable preprocessing pipeline enables efficient pairing while preserving critical visual details, laying the foundation for subsequent **contrastive learning** to enhance **photorealism in game-rendered frames**. |
|
|
|
![preprocessing](preprocessing.png) |
|
|
|
#### Training Hyperparameters |
|
|
|
**v1** |
|
- Precision: fp32 |
|
- Embedded dimensions: 768 |
|
- Hidden dimensions: 3072 |
|
- Attention Type: Linear Attention |
|
- Number of attention heads: 16 |
|
- Number of attention layers: 8 |
|
- Number of transformer encoder layers (feed-forward): 8 |
|
- Number of transformer decoder layers (feed-forward): 8 |
|
- Activation function(s): ReLU, GeLU |
|
- Patch Size: 8 |
|
- Swin Window Size: 7 |
|
- Swin Shift Size: 2 |
|
- Style Transfer Module: AdaIN (Adaptive Instance Normalization) |
|
|
|
**v2** |
|
- Precision: fp32 |
|
- Embedded dimensions: 768 |
|
- Hidden dimensions: 3072 |
|
- Attention Type: Location-Based Multi-Head Attention (Linear Attention) |
|
- Number of attention heads: 16 |
|
- Number of attention layers: 8 |
|
- Number of transformer encoder layers (feed-forward): 8 |
|
- Number of transformer decoder layers (feed-forward): 8 |
|
- Activation function(s): ReLU, GELU |
|
- Patch Size: 16 |
|
- Swin Window Size: 7 |
|
- Swin Shift Size: 2 |
|
- Style Transfer Module: AdaIN |
|
|
|
**v3** |
|
- Precision: FP32, FP16, BF16, INT8 |
|
- Embedding Dimensions: 768 |
|
- Hidden Dimensions: 3072 |
|
- Attention Type: Location-Based Multi-Head Attention (Linear Attention) |
|
- Number of Attention Heads: 42 |
|
- Number of Attention Layers: 16 |
|
- Number of Transformer Encoder Layers (Feed-Forward): 16 |
|
- Number of Transformer Decoder Layers (Feed-Forward): 16 |
|
- Activation Functions: ReLU, GeLU |
|
- Patch Size: 8 |
|
- Swin Window Size: 7 |
|
- Swin Shift Size: 2 |
|
- Style Transfer Module: Style Adaptive Layer Normalization (SALN) |
|
|
|
#### Speeds, Sizes, Times |
|
|
|
**Model size:** There are currently five versions of the model: |
|
- v1_1: 224M params |
|
- v1_2: 200M params |
|
- v1_3: 93M params |
|
- v2_1: 2.9M params |
|
- v3: 252.6M params |
|
|
|
**Training hardware:** Each of the models were trained on 2 x T4 GPUs (multi-GPU training). For this reason, linear attention modules were implemented as ring (distributed) attention during training. |
|
|
|
**Total Training Compute Throughput:** 4.13 TFLOPS |
|
|
|
**Total Logged Training Time:** ~210 hours (total time split across four models including overhead) |
|
|
|
**Start Time:** 09-13-2024 |
|
|
|
**End Time:** 09-21-2024 |
|
|
|
**Checkpoint Size:** |
|
- v1_1: 855 MB |
|
- v1_2: 764 MB |
|
- v1_3: 355 MB |
|
- v2_2: 11 MB |
|
- v3: 1.01 GB |
|
- v3_fp16: 505M |
|
- v3_bf16: 505M |
|
- v3_int8: 344M |
|
|
|
## Evaluation Data, Metrics & Results |
|
|
|
This section covers information on how the model was evaluated at each stage. |
|
|
|
### Evaluation Data |
|
|
|
Evaluation was performed on real-time footage captured from Grand Theft Auto V, Cyberpunk 2077 and WatchDogs 2. |
|
|
|
### Metrics |
|
|
|
- Peak Signal-to-Noise Ratio (PSNR) |
|
- Cosine Similarity Score (CSS) |
|
- L1 Loss |
|
- Contrastive Loss (CL) |
|
- Combined loss (L1 loss + PSNR + CSS + CL) |
|
|
|
### Results |
|
|
|
- In-game ![ingame-car](ingame-car.jpg) |
|
|
|
- Ours ![ours-car](ours-car.jpg) |
|
|
|
- In-game ![ingame-car2](ingame-car2.png) |
|
|
|
- Ours ![ours-car2](ours-car2.png) |
|
|
|
- In-game ![ingame-roads](ingame-roads.png) |
|
|
|
- Ours ![ours-roads](ours-roads.png) |
|
|
|
- In-game ![ingame-roads2](ingame-roads2.png) |
|
|
|
- Ours ![ours-roads2](ours-roads2.png) |
|
|
|
## Environmental Impact |
|
|
|
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly --> |
|
|
|
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700). |
|
|
|
- **Hardware Type:** 2 x Nvidia T4 16GB GPUs |
|
- **Hours used:** 210 (per GPU); 420 (combined) |
|
- **Cloud Provider:** Kaggle |
|
- **Compute Region:** US |
|
- **Carbon Emitted:** 8.82 kg CO2 |
|
|
|
## Technical Specifications |
|
|
|
### Model Architecture and Objective |
|
|
|
RealFormer is a Transformer-based low-latency Style Transfer Generative LM that attempts to reconstruct each frame into a more photorealistic image. |
|
The objective of RealFormer is to attain the maximum level of detail to the real-world, which even current video games with exhaustive graphics are not able to. |
|
|
|
**Flagship Architecture v4:** The v4 model builds upon the previous version by introducing **Attention Guided Attention (AGA)**, which leverages learned attention weights from a motion-guided cross-attention preprocessing stage. These pre-learned weights, conditioned into the untrained attention mechanism, improve the model's ability to focus on dynamic regions within consecutive frames. Additionally, v4 continues to incorporate **Style Adaptive Layer Normalization (SALN)** to enhance feature extraction. This architecture significantly improves temporal coherence and photorealistic enhancement by transferring knowledge from motion vector-based attention, without retraining the learned weights, leading to more efficient training and better performance in capturing real-world dynamics. |
|
|
|
```python |
|
RealFormerAGA( |
|
(patch_embed): DynamicPatchEmbedding( |
|
(proj): Conv2d(2048, 768, kernel_size=(1, 1), stride=(1, 1)) |
|
) |
|
(encoder_layers): ModuleList( |
|
(0-15): 16 x TransformerEncoderBlock( |
|
(attn): CrossAttentionLayer( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
(ff): Sequential( |
|
(0): Linear(in_features=768, out_features=3072, bias=True) |
|
(1): ReLU() |
|
(2): Linear(in_features=3072, out_features=768, bias=True) |
|
) |
|
(norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(norm2): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
) |
|
(decoder_layers): ModuleList( |
|
(0-15): 16 x TransformerDecoderBlock( |
|
(attn1): CrossAttentionLayer( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
(attn2): CrossAttentionLayer( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
(ff): Sequential( |
|
(0): Linear(in_features=768, out_features=3072, bias=True) |
|
(1): ReLU() |
|
(2): Linear(in_features=3072, out_features=768, bias=True) |
|
) |
|
(norm1): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(norm2): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(norm3): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
) |
|
) |
|
(swin_layers): ModuleList( |
|
(0-15): 16 x SwinTransformerBlock( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(mlp): Sequential( |
|
(0): Linear(in_features=768, out_features=3072, bias=True) |
|
(1): GELU(approximate='none') |
|
(2): Linear(in_features=3072, out_features=768, bias=True) |
|
) |
|
(norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(norm2): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
) |
|
) |
|
(refinement): RefinementBlock( |
|
(conv): Conv2d(768, 3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) |
|
(bn): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) |
|
(relu): ReLU(inplace=True) |
|
) |
|
(final_layer): Conv2d(3, 2048, kernel_size=(1, 1), stride=(1, 1)) |
|
(style_encoder): Sequential( |
|
(0): Conv2d(2048, 768, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) |
|
(1): ReLU() |
|
(2): AdaptiveAvgPool2d(output_size=1) |
|
(3): Flatten(start_dim=1, end_dim=-1) |
|
(4): Linear(in_features=768, out_features=768, bias=True) |
|
) |
|
) |
|
``` |
|
|
|
**v3 Architecture:** The v3 model introduces Style Adaptive Layer Normalization (SALN) & Location-based Multi-head Attention (LbMhA) to improve feature extraction at lower parameters. The two other predecessors attained a similar level of accuracy without the LbMhA layers, but with SALN, outperformed by upto ~13%. The general architecture is as follows: |
|
|
|
```python |
|
RealFormerv3( |
|
(patch_embed): DynamicPatchEmbedding( |
|
(proj): Conv2d(2048, 768, kernel_size=(1, 1), stride=(1, 1)) |
|
) |
|
(encoder_layers): ModuleList( |
|
(0-7): 8 x TransformerEncoderBlock( |
|
(attn): CrossAttentionLayer( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
(ff): Sequential( |
|
(0): Linear(in_features=768, out_features=3072, bias=True) |
|
(1): ReLU() |
|
(2): Linear(in_features=3072, out_features=768, bias=True) |
|
) |
|
(norm1): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(norm2): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
) |
|
(decoder_layers): ModuleList( |
|
(0-7): 8 x TransformerDecoderBlock( |
|
(attn1): CrossAttentionLayer( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
(attn2): CrossAttentionLayer( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(dropout): Dropout(p=0.1, inplace=False) |
|
) |
|
(ff): Sequential( |
|
(0): Linear(in_features=768, out_features=3072, bias=True) |
|
(1): ReLU() |
|
(2): Linear(in_features=3072, out_features=768, bias=True) |
|
) |
|
(norm1): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(norm2): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
(norm3): StyleAdaptiveLayerNorm( |
|
(norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(fc): Linear(in_features=768, out_features=1536, bias=True) |
|
) |
|
) |
|
) |
|
(swin_layers): ModuleList( |
|
(0-7): 8 x SwinTransformerBlock( |
|
(attn): MultiheadAttention( |
|
(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) |
|
) |
|
(mlp): Sequential( |
|
(0): Linear(in_features=768, out_features=3072, bias=True) |
|
(1): GELU(approximate='none') |
|
(2): Linear(in_features=3072, out_features=768, bias=True) |
|
) |
|
(norm1): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
(norm2): LayerNorm((768,), eps=1e-05, elementwise_affine=True) |
|
) |
|
) |
|
(refinement): RefinementBlock( |
|
(conv): Conv2d(768, 3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) |
|
(bn): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) |
|
(relu): ReLU(inplace=True) |
|
) |
|
(final_layer): Conv2d(3, 2048, kernel_size=(1, 1), stride=(1, 1)) |
|
(style_encoder): Sequential( |
|
(0): Conv2d(2048, 768, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) |
|
(1): ReLU() |
|
(2): AdaptiveAvgPool2d(output_size=1) |
|
(3): Flatten(start_dim=1, end_dim=-1) |
|
(4): Linear(in_features=768, out_features=768, bias=True) |
|
) |
|
) |
|
``` |
|
|
|
### Compute Infrastructure |
|
|
|
#### Hardware |
|
|
|
2 x Nvidia T4 16GB GPUs |
|
|
|
#### Software |
|
|
|
- PyTorch |
|
- torchvision |
|
- einops |
|
- numpy |
|
- PIL (Python Imaging Library) |
|
- matplotlib (for visualization) |
|
|
|
## Model Card Authors |
|
|
|
Alosh Denny |
|
|
|
## Model Card Contact |
|
|
|
[email protected] |