Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
feat: implement rae autoencoder.#13046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
382aad0f82cecca3926d73ecf89d0850c8c24acab025bc9e3f06ea7ad7cb1240d59b22202b14f7cbbf27e6d44996a9bde69522e68906d79ad3cbd5a96520c4fc52959a4fc9f6d06b501d8b2983c68b81261885f328a02ebb2978687debd07b3ffd63dca5923c71cb445c85781d965cab663b5806a787671b4a43fc71749803e757c8861a809ada57634787e5cd86873bff48d3f15873ae834e4958174163a66895bc718899c0f96b196f8a33958fdae805be9af75d8bdf1af7d876e930b9a5266bc5932414d918e33d98a8fa016b1f4ec0f105d3edced9bcfdaf0bed0441224ac0138b3c640d0161e9220decac2c87ee7fc54cd394e2fcc40File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <!-- Copyright 2026 The NYU Vision-X and HuggingFace Teams. All rights reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| --> | ||
| # AutoencoderRAE | ||
| The Representation Autoencoder (RAE) model introduced in [Diffusion Transformers with Representation Autoencoders](https://huggingface.co/papers/2510.11690) by Boyang Zheng, Nanye Ma, Shengbang Tong, Saining Xie from NYU VISIONx. | ||
| RAE combines a frozen pretrained vision encoder (DINOv2, SigLIP2, or MAE) with a trainable ViT-MAE-style decoder. In the two-stage RAE training recipe, the autoencoder is trained in stage 1 (reconstruction), and then a diffusion model is trained on the resulting latent space in stage 2 (generation). | ||
| The following RAE models are released and supported in Diffusers: | ||
| | Model | Encoder | Latent shape (224px input) | | ||
| |:------|:--------|:---------------------------| | ||
| | [`nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08`](https://huggingface.co/nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08) | DINOv2-base | 768 x 16 x 16 | | ||
| | [`nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08-i512`](https://huggingface.co/nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08-i512) | DINOv2-base (512px) | 768 x 32 x 32 | | ||
| | [`nyu-visionx/RAE-dinov2-wReg-small-ViTXL-n08`](https://huggingface.co/nyu-visionx/RAE-dinov2-wReg-small-ViTXL-n08) | DINOv2-small | 384 x 16 x 16 | | ||
| | [`nyu-visionx/RAE-dinov2-wReg-large-ViTXL-n08`](https://huggingface.co/nyu-visionx/RAE-dinov2-wReg-large-ViTXL-n08) | DINOv2-large | 1024 x 16 x 16 | | ||
| | [`nyu-visionx/RAE-siglip2-base-p16-i256-ViTXL-n08`](https://huggingface.co/nyu-visionx/RAE-siglip2-base-p16-i256-ViTXL-n08) | SigLIP2-base | 768 x 16 x 16 | | ||
| | [`nyu-visionx/RAE-mae-base-p16-ViTXL-n08`](https://huggingface.co/nyu-visionx/RAE-mae-base-p16-ViTXL-n08) | MAE-base | 768 x 16 x 16 | | ||
| ## Loading a pretrained model | ||
| ```python | ||
| from diffusers import AutoencoderRAE | ||
| model = AutoencoderRAE.from_pretrained( | ||
| "nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08" | ||
| ).to("cuda").eval() | ||
| ``` | ||
| ## Encoding and decoding a real image | ||
| ```python | ||
| import torch | ||
| from diffusers import AutoencoderRAE | ||
| from diffusers.utils import load_image | ||
| from torchvision.transforms.functional import to_tensor, to_pil_image | ||
| model = AutoencoderRAE.from_pretrained( | ||
| "nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08" | ||
| ).to("cuda").eval() | ||
| image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") | ||
| image = image.convert("RGB").resize((224, 224)) | ||
| x = to_tensor(image).unsqueeze(0).to("cuda") # (1, 3, 224, 224), values in [0, 1] | ||
| with torch.no_grad(): | ||
| latents = model.encode(x).latent # (1, 768, 16, 16) | ||
| recon = model.decode(latents).sample # (1, 3, 256, 256) | ||
| recon_image = to_pil_image(recon[0].clamp(0, 1).cpu()) | ||
| recon_image.save("recon.png") | ||
| ``` | ||
| ## Latent normalization | ||
| Some pretrained checkpoints include per-channel `latents_mean` and `latents_std` statistics for normalizing the latent space. When present, `encode` and `decode` automatically apply the normalization and denormalization, respectively. | ||
| ```python | ||
| model = AutoencoderRAE.from_pretrained( | ||
| "nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08" | ||
| ).to("cuda").eval() | ||
| # Latent normalization is handled automatically inside encode/decode | ||
| # when the checkpoint config includes latents_mean/latents_std. | ||
kashif marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| with torch.no_grad(): | ||
| latents = model.encode(x).latent # normalized latents | ||
| recon = model.decode(latents).sample | ||
| ``` | ||
| ## AutoencoderRAE | ||
| [[autodoc]] AutoencoderRAE | ||
| - encode | ||
| - decode | ||
| - all | ||
| ## DecoderOutput | ||
| [[autodoc]] models.autoencoders.vae.DecoderOutput | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Training AutoencoderRAE | ||
| This example trains the decoder of `AutoencoderRAE` (stage-1 style), while keeping the representation encoder frozen. | ||
| It follows the same high-level training recipe as the official RAE stage-1 setup: | ||
| - frozen encoder | ||
| - train decoder | ||
| - pixel reconstruction loss | ||
| - optional encoder feature consistency loss | ||
| ## Quickstart | ||
| ### Resume or finetune from pretrained weights | ||
| ```bash | ||
| accelerate launch examples/research_projects/autoencoder_rae/train_autoencoder_rae.py \ | ||
| --pretrained_model_name_or_path nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08 \ | ||
| --train_data_dir /path/to/imagenet_like_folder \ | ||
| --output_dir /tmp/autoencoder-rae \ | ||
| --resolution 256 \ | ||
| --train_batch_size 8 \ | ||
| --learning_rate 1e-4 \ | ||
| --num_train_epochs 10 \ | ||
| --report_to wandb \ | ||
| --reconstruction_loss_type l1 \ | ||
| --use_encoder_loss \ | ||
| --encoder_loss_weight 0.1 | ||
| ``` | ||
| ### Train from scratch with a pretrained encoder | ||
| The following command launches RAE training with "facebook/dinov2-with-registers-base" as the base. | ||
kashif marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ```bash | ||
| accelerate launch examples/research_projects/autoencoder_rae/train_autoencoder_rae.py \ | ||
sayakpaul marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| --train_data_dir /path/to/imagenet_like_folder \ | ||
| --output_dir /tmp/autoencoder-rae \ | ||
| --resolution 256 \ | ||
| --encoder_type dinov2 \ | ||
| --encoder_name_or_path facebook/dinov2-with-registers-base \ | ||
| --encoder_input_size 224 \ | ||
| --patch_size 16 \ | ||
| --image_size 256 \ | ||
| --decoder_hidden_size 1152 \ | ||
| --decoder_num_hidden_layers 28 \ | ||
| --decoder_num_attention_heads 16 \ | ||
| --decoder_intermediate_size 4096 \ | ||
| --train_batch_size 8 \ | ||
| --learning_rate 1e-4 \ | ||
| --num_train_epochs 10 \ | ||
| --report_to wandb \ | ||
| --reconstruction_loss_type l1 \ | ||
| --use_encoder_loss \ | ||
| --encoder_loss_weight 0.1 | ||
| ``` | ||
| Note: stage-1 reconstruction loss assumes matching target/output spatial size, so `--resolution` must equal `--image_size`. | ||
| Dataset format is expected to be `ImageFolder`-compatible: | ||
| ```text | ||
| train_data_dir/ | ||
| class_a/ | ||
| img_0001.jpg | ||
| class_b/ | ||
| img_0002.jpg | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.