Instructions to use bertaveira/R3D2-big with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use bertaveira/R3D2-big with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("bertaveira/R3D2-big", dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Release R3D2-big model
Browse files- LICENSE.md +414 -0
- NOTICE +16 -0
- README.md +65 -3
- model_index.json +32 -0
- r3d2_pipeline.py +187 -0
- scheduler/scheduler_config.json +26 -0
- text_encoder/config.json +24 -0
- text_encoder/model.safetensors +3 -0
- tokenizer/merges.txt +0 -0
- tokenizer/special_tokens_map.json +30 -0
- tokenizer/tokenizer_config.json +39 -0
- tokenizer/vocab.json +0 -0
- unet/config.json +72 -0
- unet/diffusion_pytorch_model.safetensors +3 -0
- vae/config.json +38 -0
- vae/diffusion_pytorch_model.safetensors +3 -0
- vae/r3d2_vae.py +304 -0
LICENSE.md
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# R3D2 licenses
|
| 2 |
+
|
| 3 |
+
The model and downstream use or modification are subject to the Waymo Dataset
|
| 4 |
+
License Agreement for Non-Commercial Use, including its Section 4 restrictions.
|
| 5 |
+
SD-Turbo's upstream terms also apply. The code licenses below do not override
|
| 6 |
+
these model-use restrictions. See [NOTICE](NOTICE) for attribution.
|
| 7 |
+
|
| 8 |
+
## Waymo Dataset License Agreement
|
| 9 |
+
|
| 10 |
+
```text
|
| 11 |
+
Source: https://waymo.com/open/terms/
|
| 12 |
+
|
| 13 |
+
License Agreement
|
| 14 |
+
|
| 15 |
+
Waymo Dataset License Agreement for Non-Commercial Use (March 2025)
|
| 16 |
+
|
| 17 |
+
To aid the research community in making advancements in machine perception and autonomous driving technology, Waymo has opened up to the public a curated set of autonomous driving data that can be used for research. The dataset, located at waymo.com/open, is made available free of charge, but under the terms and conditions below. The dataset contains lidar and camera data, which were collected in diverse conditions, modified to respect individuals’ privacy, and labeled and formatted to aid in research. We hope you find it useful.
|
| 18 |
+
|
| 19 |
+
By downloading or using the Dataset, You are agreeing to the following terms and conditions (“Terms"):
|
| 20 |
+
|
| 21 |
+
“Dataset" means the Waymo Open Dataset available at waymo.com/open and any data in the Waymo Open Dataset.
|
| 22 |
+
|
| 23 |
+
“Derivative IP" means any derivative work of the Dataset; any other work made or developed using the Dataset, including any machine-learning model, software, or algorithm, including architectures, weights, and parameters, that have been trained, refined, or tuned, in whole or in part, using the Dataset (or new datasets or outputs generated therefrom); and any invention conceived or reduced to practice, directly or indirectly, through the use of the Dataset.
|
| 24 |
+
|
| 25 |
+
“Non-commercial Purposes" means research, teaching, scientific publication and personal experimentation. Non-commercial Purposes include use of the Dataset to perform benchmarking for purposes of academic or applied research publication. Non-commercial Purposes does not include purposes primarily intended for or directed towards commercial advantage or monetary compensation, or purposes intended for or directed towards litigation, licensing, or enforcement, even in part.
|
| 26 |
+
|
| 27 |
+
“Production Systems" means computer, network, and datacenter infrastructure, and other systems primarily intended to provide a product or service to customers or other third parties, even if no monetary compensation is received for such a service.
|
| 28 |
+
|
| 29 |
+
“Waymo" means Waymo LLC.
|
| 30 |
+
|
| 31 |
+
“You" means the individual or entity exercising the rights granted under this License Agreement.
|
| 32 |
+
|
| 33 |
+
Waymo grants You a non-exclusive, royalty-free, personal license to use, reproduce, and modify the Dataset for Non-commercial Purposes only and expressly subject to the following conditions:
|
| 34 |
+
|
| 35 |
+
1.
|
| 36 |
+
|
| 37 |
+
Attribution: You must provide one of the following attributions in any Derivative IP: This [type of work, e.g., publication, software, model] was made using the Waymo Open Dataset, provided by Waymo LLC under the Waymo Dataset License Agreement for Non-Commercial Use, available at waymo.com/open/terms, and your access and use of such work are governed by the terms and conditions therein.
|
| 38 |
+
|
| 39 |
+
@misc{waymo_open_dataset, title = {Waymo Open Dataset: An autonomous driving dataset}, website = {\url{https://www.waymo.com/open}}, year = {2019-2025} }
|
| 40 |
+
|
| 41 |
+
2.
|
| 42 |
+
|
| 43 |
+
Sharing: Subject to these Terms, you may share as follows:
|
| 44 |
+
|
| 45 |
+
1.
|
| 46 |
+
|
| 47 |
+
De Minimis: You may publish small extracts of data taken from the Dataset for purposes of illustration in Your research publications;
|
| 48 |
+
|
| 49 |
+
2.
|
| 50 |
+
|
| 51 |
+
Dataset Community Distribution Only: You may distribute the Dataset and copies and modifications thereof, but only if (i) distribution is limited to those who have registered at waymo.com/open and agreed to these Terms and (ii) You agree not to prepare, initiate, assert, or otherwise support any claim against Waymo or any authorized licensee of the Dataset for violation of any of Your rights protecting the distributed work; and
|
| 52 |
+
|
| 53 |
+
3.
|
| 54 |
+
|
| 55 |
+
Model Architectures Non-commercial Distribution Only: You may publish, in whole or in part, trained model architectures, including weights and biases, developed using the Dataset, and their associated derivatives including new datasets generated therefrom (collectively, “Distributed WOD Models”), provided that You meet the following conditions:
|
| 56 |
+
|
| 57 |
+
1.
|
| 58 |
+
|
| 59 |
+
You must include a prominent notice to Your users or recipients stating that (x) Your Distributed WOD Models is released under this Agreement and (y) any further downstream use or modification of such Distributed WOD Models is subject to the Terms of this Agreement, including the non-commercial restrictions of Section 4.
|
| 60 |
+
|
| 61 |
+
2.
|
| 62 |
+
|
| 63 |
+
This Agreement shall apply to Your users or recipients with respect to the whole of Your Distributed WOD Models, and You may provide Your own copyright statement and add additional terms and conditions for Your Distributed WOD Models, provided that they do not conflict with the non-commercial restrictions of Section 4 and the Terms of this Agreement;
|
| 64 |
+
|
| 65 |
+
3.
|
| 66 |
+
|
| 67 |
+
You must give any third-party recipients of Your Distributed WOD Models a copy of this Agreement; and
|
| 68 |
+
|
| 69 |
+
4.
|
| 70 |
+
|
| 71 |
+
You must provide attribution in accordance with Section 1 (above).
|
| 72 |
+
|
| 73 |
+
3.
|
| 74 |
+
|
| 75 |
+
Privacy: You may not use the Dataset in any manner that violates applicable privacy laws.
|
| 76 |
+
|
| 77 |
+
4.
|
| 78 |
+
|
| 79 |
+
Additional Restrictions: To ensure the Dataset is only used for Non-commercial Purposes, You further agree to the following:
|
| 80 |
+
|
| 81 |
+
1.
|
| 82 |
+
|
| 83 |
+
You must not use or deploy the Dataset, any models trained on or refined using the Dataset, or the weights or biases from such trained models, in whole or in part, (i) in operation of a vehicle or to assist in the operation of a vehicle, (ii) in any Production Systems, or (iii) for any other primarily commercial purposes; and
|
| 84 |
+
|
| 85 |
+
2.
|
| 86 |
+
|
| 87 |
+
Upon use of any Distributed WOD Models for any purpose that is not a Non-commercial Purpose per Section 4(a), or is otherwise in breach of this Agreement, (i) any rights granted to you to create any Distributed WOD Models and any rights in the Distributed WOD Models, granted by You or Your recipients to third parties, shall be null and void ab initio; (ii) any rights granted under this Agreement or the license or legal agreement governing use and/or distribution of the Distributed WOD Models to any third party recipient is terminated automatically; (iii) such third party recipient of Your Distributed WOD Models shall cease use of and delete the Dataset and the Distributed WOD Models, and any copies thereof; and (iv) the license granted in Section 5(b) of this Agreement immediately becomes effective.
|
| 88 |
+
|
| 89 |
+
5.
|
| 90 |
+
|
| 91 |
+
Limited Non-Assert: In consideration for access to this Dataset and/or this royalty-free license from Waymo, You (also on behalf of Your successors and assignees of any rights protecting Your Derivative IP) (a) agree not to prepare, initiate, assert, or otherwise support any claim against Waymo or any of its affiliates, successors, or assignees, for infringement, misappropriation or other violation of any rights protecting Your Derivative IP, and (b) grant Waymo and the other parties in (a) above a license to such rights, with such license becoming effective only if You breach the obligations of Section 5(a) or Section 4(b).
|
| 92 |
+
|
| 93 |
+
6.
|
| 94 |
+
|
| 95 |
+
Website Terms: You agree to the Waymo Terms of Service (https://waymo.com/terms/) (“ToS") in connection with your use of the Dataset and Waymo’s website, including Sections 9 Indemnification, 10 Disclaimers, and 11 Limitation of Liability.
|
| 96 |
+
|
| 97 |
+
7.
|
| 98 |
+
|
| 99 |
+
ADDITIONAL DISCLAIMER AND LIMITATION OF LIABILITY: YOU AGREE THE DATASET AND ANY DERIVATIVE IP IS PROVIDED “AS-IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING WITHOUT LIMITATION WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. TO THE FULLEST EXTENT PERMITTED BY LAW, IN NO EVENT SHALL WAYMO AND THE OTHER WAYMO PARTIES, AS DEFINED IN THE TOS, BE LIABLE UNDER ANY LEGAL THEORY WITH RESPECT THE DATASET AND ANY DERIVATIVE IP, OR YOUR USE OF THE DATASET OR ANY DERIVATIVE IP, FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, EXEMPLARY, INCIDENTAL, PUNITIVE, OR SPECIAL DAMAGES OR LOST PROFITS.
|
| 100 |
+
|
| 101 |
+
8.
|
| 102 |
+
|
| 103 |
+
Waymo reserves all rights not expressly granted. If you would like a license that allows commercial use of the Dataset, or have any other questions, please contact open-dataset@waymo.com.
|
| 104 |
+
|
| 105 |
+
9.
|
| 106 |
+
|
| 107 |
+
If You fail to comply with these terms, then any rights granted to You hereunder terminate automatically and You agree to remove access to and delete the Dataset.
|
| 108 |
+
|
| 109 |
+
If the institution You identify at registration or another entity exercises the rights granted under this License Agreement by virtue of Your download or use of the Dataset, You represent and warrant that: (a) You are authorized to agree to this license agreement on behalf of the entity or (b) You have confirmed that a person with authority to agree to this license agreement on behalf of the entity has already registered on behalf of the entity.
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
## SD-Turbo — Stability AI Community License
|
| 116 |
+
|
| 117 |
+
```text
|
| 118 |
+
STABILITY AI COMMUNITY LICENSE AGREEMENT
|
| 119 |
+
|
| 120 |
+
Last Updated: July 5, 2024
|
| 121 |
+
|
| 122 |
+
1. INTRODUCTION
|
| 123 |
+
|
| 124 |
+
This Agreement applies to any individual person or entity (“You”, “Your” or “Licensee”) that uses or distributes any portion or element of the Stability AI Materials or Derivative Works thereof for any Research & Non-Commercial or Commercial purpose. Capitalized terms not otherwise defined herein are defined in Section V below.
|
| 125 |
+
|
| 126 |
+
This Agreement is intended to allow research, non-commercial, and limited commercial uses of the Models free of charge. In order to ensure that certain limited commercial uses of the Models continue to be allowed, this Agreement preserves free access to the Models for people or organizations generating annual revenue of less than US $1,000,000 (or local currency equivalent).
|
| 127 |
+
|
| 128 |
+
By clicking “I Accept” or by using or distributing or using any portion or element of the Stability Materials or Derivative Works, You agree that You have read, understood and are bound by the terms of this Agreement. If You are acting on behalf of a company, organization or other entity, then “You” includes you and that entity, and You agree that You: (i) are an authorized representative of such entity with the authority to bind such entity to this Agreement, and (ii) You agree to the terms of this Agreement on that entity’s behalf.
|
| 129 |
+
|
| 130 |
+
2. RESEARCH & NON-COMMERCIAL USE LICENSE
|
| 131 |
+
|
| 132 |
+
Subject to the terms of this Agreement, Stability AI grants You a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable and royalty-free limited license under Stability AI’s intellectual property or other rights owned by Stability AI embodied in the Stability AI Materials to use, reproduce, distribute, and create Derivative Works of, and make modifications to, the Stability AI Materials for any Research or Non-Commercial Purpose. “Research Purpose” means academic or scientific advancement, and in each case, is not primarily intended for commercial advantage or monetary compensation to You or others. “Non-Commercial Purpose” means any purpose other than a Research Purpose that is not primarily intended for commercial advantage or monetary compensation to You or others, such as personal use (i.e., hobbyist) or evaluation and testing.
|
| 133 |
+
|
| 134 |
+
3. COMMERCIAL USE LICENSE
|
| 135 |
+
|
| 136 |
+
Subject to the terms of this Agreement (including the remainder of this Section III), Stability AI grants You a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable and royalty-free limited license under Stability AI’s intellectual property or other rights owned by Stability AI embodied in the Stability AI Materials to use, reproduce, distribute, and create Derivative Works of, and make modifications to, the Stability AI Materials for any Commercial Purpose. “Commercial Purpose” means any purpose other than a Research Purpose or Non-Commercial Purpose that is primarily intended for commercial advantage or monetary compensation to You or others, including but not limited to, (i) creating, modifying, or distributing Your product or service, including via a hosted service or application programming interface, and (ii) for Your business’s or organization’s internal operations.
|
| 137 |
+
If You are using or distributing the Stability AI Materials for a Commercial Purpose, You must register with Stability AI at (https://stability.ai/community-license). If at any time You or Your Affiliate(s), either individually or in aggregate, generate more than USD $1,000,000 in annual revenue (or the equivalent thereof in Your local currency), regardless of whether that revenue is generated directly or indirectly from the Stability AI Materials or Derivative Works, any licenses granted to You under this Agreement shall terminate as of such date. You must request a license from Stability AI at (https://stability.ai/enterprise) , which Stability AI may grant to You in its sole discretion. If you receive Stability AI Materials, or any Derivative Works thereof, from a Licensee as part of an integrated end user product, then Section III of this Agreement will not apply to you.
|
| 138 |
+
|
| 139 |
+
4. GENERAL TERMS
|
| 140 |
+
|
| 141 |
+
Your Research, Non-Commercial, and Commercial License(s) under this Agreement are subject to the following terms.
|
| 142 |
+
a. Distribution & Attribution. If You distribute or make available the Stability AI Materials or a Derivative Work to a third party, or a product or service that uses any portion of them, You shall: (i) provide a copy of this Agreement to that third party, (ii) retain the following attribution notice within a "Notice" text file distributed as a part of such copies: "This Stability AI Model is licensed under the Stability AI Community License, Copyright © Stability AI Ltd. All Rights Reserved”, and (iii) prominently display “Powered by Stability AI” on a related website, user interface, blogpost, about page, or product documentation. If You create a Derivative Work, You may add your own attribution notice(s) to the “Notice” text file included with that Derivative Work, provided that You clearly indicate which attributions apply to the Stability AI Materials and state in the “Notice” text file that You changed the Stability AI Materials and how it was modified.
|
| 143 |
+
b. Use Restrictions. Your use of the Stability AI Materials and Derivative Works, including any output or results of the Stability AI Materials or Derivative Works, must comply with applicable laws and regulations (including Trade Control Laws and equivalent regulations) and adhere to the Documentation and Stability AI’s AUP, which is hereby incorporated by reference. Furthermore, You will not use the Stability AI Materials or Derivative Works, or any output or results of the Stability AI Materials or Derivative Works, to create or improve any foundational generative AI model (excluding the Models or Derivative Works).
|
| 144 |
+
c. Intellectual Property.
|
| 145 |
+
(i) Trademark License. No trademark licenses are granted under this Agreement, and in connection with the Stability AI Materials or Derivative Works, You may not use any name or mark owned by or associated with Stability AI or any of its Affiliates, except as required under Section IV(a) herein.
|
| 146 |
+
(ii) Ownership of Derivative Works. As between You and Stability AI, You are the owner of Derivative Works You create, subject to Stability AI’s ownership of the Stability AI Materials and any Derivative Works made by or for Stability AI.
|
| 147 |
+
(iii) Ownership of Outputs. As between You and Stability AI, You own any outputs generated from the Models or Derivative Works to the extent permitted by applicable law.
|
| 148 |
+
(iv) Disputes. If You or Your Affiliate(s) institute litigation or other proceedings against Stability AI (including a cross-claim or counterclaim in a lawsuit) alleging that the Stability AI Materials, Derivative Works or associated outputs or results, or any portion of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable by You, then any licenses granted to You under this Agreement shall terminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold harmless Stability AI from and against any claim by any third party arising out of or related to Your use or distribution of the Stability AI Materials or Derivative Works in violation of this Agreement.
|
| 149 |
+
(v) Feedback. From time to time, You may provide Stability AI with verbal and/or written suggestions, comments or other feedback related to Stability AI’s existing or prospective technology, products or services (collectively, “Feedback”). You are not obligated to provide Stability AI with Feedback, but to the extent that You do, You hereby grant Stability AI a perpetual, irrevocable, royalty-free, fully-paid, sub-licensable, transferable, non-exclusive, worldwide right and license to exploit the Feedback in any manner without restriction. Your Feedback is provided “AS IS” and You make no warranties whatsoever about any Feedback.
|
| 150 |
+
d. Disclaimer Of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE STABILITY AI MATERIALS AND ANY OUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OR LAWFULNESS OF USING OR REDISTRIBUTING THE STABILITY AI MATERIALS, DERIVATIVE WORKS OR ANY OUTPUT OR RESULTS AND ASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE STABILITY AI MATERIALS, DERIVATIVE WORKS AND ANY OUTPUT AND RESULTS.
|
| 151 |
+
e. Limitation Of Liability. IN NO EVENT WILL STABILITY AI OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF STABILITY AI OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING.
|
| 152 |
+
f. Term And Termination. The term of this Agreement will commence upon Your acceptance of this Agreement or access to the Stability AI Materials and will continue in full force and effect until terminated in accordance with the terms and conditions herein. Stability AI may terminate this Agreement if You are in breach of any term or condition of this Agreement. Upon termination of this Agreement, You shall delete and cease use of any Stability AI Materials or Derivative Works. Section IV(d), (e), and (g) shall survive the termination of this Agreement.
|
| 153 |
+
g. Governing Law. This Agreement will be governed by and constructed in accordance with the laws of the United States and the State of California without regard to choice of law principles, and the UN Convention on Contracts for International Sale of Goods does not apply to this Agreement.
|
| 154 |
+
|
| 155 |
+
5. DEFINITIONS
|
| 156 |
+
|
| 157 |
+
“Affiliate(s)” means any entity that directly or indirectly controls, is controlled by, or is under common control with the subject entity; for purposes of this definition, “control” means direct or indirect ownership or control of more than 50% of the voting interests of the subject entity.
|
| 158 |
+
|
| 159 |
+
"Agreement" means this Stability AI Community License Agreement.
|
| 160 |
+
|
| 161 |
+
“AUP” means the Stability AI Acceptable Use Policy available at (https://stability.ai/use-policy), as may be updated from time to time.
|
| 162 |
+
|
| 163 |
+
"Derivative Work(s)” means (a) any derivative work of the Stability AI Materials as recognized by U.S. copyright laws and (b) any modifications to a Model, and any other model created which is based on or derived from the Model or the Model’s output, including “fine tune” and “low-rank adaptation” models derived from a Model or a Model’s output, but do not include the output of any Model.
|
| 164 |
+
|
| 165 |
+
“Documentation” means any specifications, manuals, documentation, and other written information provided by Stability AI related to the Software or Models.
|
| 166 |
+
|
| 167 |
+
“Model(s)" means, collectively, Stability AI’s proprietary models and algorithms, including machine-learning models, trained model weights and other elements of the foregoing listed on Stability’s Core Models Webpage available at (https://stability.ai/core-models), as may be updated from time to time.
|
| 168 |
+
|
| 169 |
+
"Stability AI" or "we" means Stability AI Ltd. and its Affiliates.
|
| 170 |
+
|
| 171 |
+
"Software" means Stability AI’s proprietary software made available under this Agreement now or in the future.
|
| 172 |
+
|
| 173 |
+
“Stability AI Materials” means, collectively, Stability’s proprietary Models, Software and Documentation (and any portion or combination thereof) made available under this Agreement.
|
| 174 |
+
|
| 175 |
+
“Trade Control Laws” means any applicable U.S. and non-U.S. export control and trade sanctions laws and regulations.
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
---
|
| 180 |
+
|
| 181 |
+
## R3D2 code — Apache License 2.0
|
| 182 |
+
|
| 183 |
+
```text
|
| 184 |
+
Apache License
|
| 185 |
+
Version 2.0, January 2004
|
| 186 |
+
http://www.apache.org/licenses/
|
| 187 |
+
|
| 188 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 189 |
+
|
| 190 |
+
1. Definitions.
|
| 191 |
+
|
| 192 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 193 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 194 |
+
|
| 195 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 196 |
+
the copyright owner that is granting the License.
|
| 197 |
+
|
| 198 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 199 |
+
other entities that control, are controlled by, or are under common
|
| 200 |
+
control with that entity. For the purposes of this definition,
|
| 201 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 202 |
+
direction or management of such entity, whether by contract or
|
| 203 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 204 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 205 |
+
|
| 206 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 207 |
+
exercising permissions granted by this License.
|
| 208 |
+
|
| 209 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 210 |
+
including but not limited to software source code, documentation
|
| 211 |
+
source, and configuration files.
|
| 212 |
+
|
| 213 |
+
"Object" form shall mean any form resulting from mechanical
|
| 214 |
+
transformation or translation of a Source form, including but
|
| 215 |
+
not limited to compiled object code, generated documentation,
|
| 216 |
+
and conversions to other media types.
|
| 217 |
+
|
| 218 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 219 |
+
Object form, made available under the License, as indicated by a
|
| 220 |
+
copyright notice that is included in or attached to the work
|
| 221 |
+
(an example is provided in the Appendix below).
|
| 222 |
+
|
| 223 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 224 |
+
form, that is based on (or derived from) the Work and for which the
|
| 225 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 226 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 227 |
+
of this License, Derivative Works shall not include works that remain
|
| 228 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 229 |
+
the Work and Derivative Works thereof.
|
| 230 |
+
|
| 231 |
+
"Contribution" shall mean any work of authorship, including
|
| 232 |
+
the original version of the Work and any modifications or additions
|
| 233 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 234 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 235 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 236 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 237 |
+
means any form of electronic, verbal, or written communication sent
|
| 238 |
+
to the Licensor or its representatives, including but not limited to
|
| 239 |
+
communication on electronic mailing lists, source code control systems,
|
| 240 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 241 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 242 |
+
excluding communication that is conspicuously marked or otherwise
|
| 243 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 244 |
+
|
| 245 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 246 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 247 |
+
subsequently incorporated within the Work.
|
| 248 |
+
|
| 249 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 250 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 251 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 252 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 253 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 254 |
+
Work and such Derivative Works in Source or Object form.
|
| 255 |
+
|
| 256 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 257 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 258 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 259 |
+
(except as stated in this section) patent license to make, have made,
|
| 260 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 261 |
+
where such license applies only to those patent claims licensable
|
| 262 |
+
by such Contributor that are necessarily infringed by their
|
| 263 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 264 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 265 |
+
institute patent litigation against any entity (including a
|
| 266 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 267 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 268 |
+
or contributory patent infringement, then any patent licenses
|
| 269 |
+
granted to You under this License for that Work shall terminate
|
| 270 |
+
as of the date such litigation is filed.
|
| 271 |
+
|
| 272 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 273 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 274 |
+
modifications, and in Source or Object form, provided that You
|
| 275 |
+
meet the following conditions:
|
| 276 |
+
|
| 277 |
+
(a) You must give any other recipients of the Work or
|
| 278 |
+
Derivative Works a copy of this License; and
|
| 279 |
+
|
| 280 |
+
(b) You must cause any modified files to carry prominent notices
|
| 281 |
+
stating that You changed the files; and
|
| 282 |
+
|
| 283 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 284 |
+
that You distribute, all copyright, patent, trademark, and
|
| 285 |
+
attribution notices from the Source form of the Work,
|
| 286 |
+
excluding those notices that do not pertain to any part of
|
| 287 |
+
the Derivative Works; and
|
| 288 |
+
|
| 289 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 290 |
+
distribution, then any Derivative Works that You distribute must
|
| 291 |
+
include a readable copy of the attribution notices contained
|
| 292 |
+
within such NOTICE file, excluding those notices that do not
|
| 293 |
+
pertain to any part of the Derivative Works, in at least one
|
| 294 |
+
of the following places: within a NOTICE text file distributed
|
| 295 |
+
as part of the Derivative Works; within the Source form or
|
| 296 |
+
documentation, if provided along with the Derivative Works; or,
|
| 297 |
+
within a display generated by the Derivative Works, if and
|
| 298 |
+
wherever such third-party notices normally appear. The contents
|
| 299 |
+
of the NOTICE file are for informational purposes only and
|
| 300 |
+
do not modify the License. You may add Your own attribution
|
| 301 |
+
notices within Derivative Works that You distribute, alongside
|
| 302 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 303 |
+
that such additional attribution notices cannot be construed
|
| 304 |
+
as modifying the License.
|
| 305 |
+
|
| 306 |
+
You may add Your own copyright statement to Your modifications and
|
| 307 |
+
may provide additional or different license terms and conditions
|
| 308 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 309 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 310 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 311 |
+
the conditions stated in this License.
|
| 312 |
+
|
| 313 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 314 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 315 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 316 |
+
this License, without any additional terms or conditions.
|
| 317 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 318 |
+
the terms of any separate license agreement you may have executed
|
| 319 |
+
with Licensor regarding such Contributions.
|
| 320 |
+
|
| 321 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 322 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 323 |
+
except as required for reasonable and customary use in describing the
|
| 324 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 325 |
+
|
| 326 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 327 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 328 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 329 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 330 |
+
implied, including, without limitation, any warranties or conditions
|
| 331 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 332 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 333 |
+
appropriateness of using or redistributing the Work and assume any
|
| 334 |
+
risks associated with Your exercise of permissions under this License.
|
| 335 |
+
|
| 336 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 337 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 338 |
+
unless required by applicable law (such as deliberate and grossly
|
| 339 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 340 |
+
liable to You for damages, including any direct, indirect, special,
|
| 341 |
+
incidental, or consequential damages of any character arising as a
|
| 342 |
+
result of this License or out of the use or inability to use the
|
| 343 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 344 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 345 |
+
other commercial damages or losses), even if such Contributor
|
| 346 |
+
has been advised of the possibility of such damages.
|
| 347 |
+
|
| 348 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 349 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 350 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 351 |
+
or other liability obligations and/or rights consistent with this
|
| 352 |
+
License. However, in accepting such obligations, You may act only
|
| 353 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 354 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 355 |
+
defend, and hold each Contributor harmless for any liability
|
| 356 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 357 |
+
of your accepting any such warranty or additional liability.
|
| 358 |
+
|
| 359 |
+
END OF TERMS AND CONDITIONS
|
| 360 |
+
|
| 361 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 362 |
+
|
| 363 |
+
To apply the Apache License to your work, attach the following
|
| 364 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 365 |
+
replaced with your own identifying information. (Don't include
|
| 366 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 367 |
+
comment syntax for the file format. We also recommend that a
|
| 368 |
+
file or class name and description of purpose be included on the
|
| 369 |
+
same "printed page" as the copyright notice for easier
|
| 370 |
+
identification within third-party archives.
|
| 371 |
+
|
| 372 |
+
Copyright [yyyy] [name of copyright owner]
|
| 373 |
+
|
| 374 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 375 |
+
you may not use this file except in compliance with the License.
|
| 376 |
+
You may obtain a copy of the License at
|
| 377 |
+
|
| 378 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 379 |
+
|
| 380 |
+
Unless required by applicable law or agreed to in writing, software
|
| 381 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 382 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 383 |
+
See the License for the specific language governing permissions and
|
| 384 |
+
limitations under the License.
|
| 385 |
+
```
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
---
|
| 389 |
+
|
| 390 |
+
## TAESD — MIT License
|
| 391 |
+
|
| 392 |
+
```text
|
| 393 |
+
MIT License
|
| 394 |
+
|
| 395 |
+
Copyright (c) 2023 Ollin Boer Bohan
|
| 396 |
+
|
| 397 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 398 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 399 |
+
in the Software without restriction, including without limitation the rights
|
| 400 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 401 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 402 |
+
furnished to do so, subject to the following conditions:
|
| 403 |
+
|
| 404 |
+
The above copyright notice and this permission notice shall be included in all
|
| 405 |
+
copies or substantial portions of the Software.
|
| 406 |
+
|
| 407 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 408 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 409 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 410 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 411 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 412 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 413 |
+
SOFTWARE.
|
| 414 |
+
```
|
NOTICE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This model was made using the Waymo Open Dataset, provided by Waymo LLC under the Waymo Dataset License Agreement for Non-Commercial Use, available at https://waymo.com/open/terms/, and your access and use of such work are governed by the terms and conditions therein.
|
| 2 |
+
|
| 3 |
+
This model is released under the Waymo Dataset License Agreement for
|
| 4 |
+
Non-Commercial Use. All downstream use or modification is subject to that
|
| 5 |
+
agreement, including the non-commercial restrictions of Section 4.
|
| 6 |
+
|
| 7 |
+
This Stability AI Model is licensed under the Stability AI Community License, Copyright © Stability AI Ltd. All Rights Reserved.
|
| 8 |
+
|
| 9 |
+
Powered by Stability AI.
|
| 10 |
+
R3D2 fine-tunes SD-Turbo for realistic 3D asset insertion, adds VAE skip connections,
|
| 11 |
+
and uses a single denoising step at timestep 200. The small model uses a modified
|
| 12 |
+
TAESD VAE. The bundled implementation is from https://github.com/zenseact/R3D2
|
| 13 |
+
and is covered by Apache-2.0. TAESD code is included in the custom VAE
|
| 14 |
+
implementation under MIT. All license texts are included in LICENSE.md.
|
| 15 |
+
SD-Turbo license source: https://huggingface.co/stabilityai/sd-turbo/blob/b261bac6fd2cf515557d5d0707481eafa0485ec2/LICENSE.md
|
| 16 |
+
TAESD license source: https://github.com/madebyollin/taesd/blob/main/LICENSE
|
README.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
| 1 |
---
|
|
|
|
|
|
|
| 2 |
license: other
|
| 3 |
-
license_name: waymo-
|
| 4 |
-
license_link:
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
library_name: diffusers
|
| 3 |
+
pipeline_tag: image-to-image
|
| 4 |
license: other
|
| 5 |
+
license_name: waymo-non-commercial-and-stability-ai-community
|
| 6 |
+
license_link: LICENSE.md
|
| 7 |
+
base_model:
|
| 8 |
+
- stabilityai/sd-turbo
|
| 9 |
+
tags:
|
| 10 |
+
- r3d2
|
| 11 |
+
- autonomous-driving
|
| 12 |
+
- image-to-image
|
| 13 |
+
- custom-code
|
| 14 |
+
- arxiv:2506.07826
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# R3D2-BIG
|
| 18 |
+
|
| 19 |
+
One-step diffusion for realistic lighting and shadows around inserted 3D assets.
|
| 20 |
+
This is the **regular VAE** version of R3D2.
|
| 21 |
+
|
| 22 |
+
[Paper](https://arxiv.org/abs/2506.07826) · [Code](https://github.com/zenseact/R3D2) · [Project](https://research.zenseact.com/publications/R3D2/) · [R3D2](https://huggingface.co/bertaveira/R3D2)
|
| 23 |
+
|
| 24 |
+
**Non-commercial use only.** This model is released under the Waymo Dataset
|
| 25 |
+
License Agreement. All downstream use or modification is subject to its terms,
|
| 26 |
+
including Section 4. Full agreements and attribution: [LICENSE.md](LICENSE.md), [NOTICE](NOTICE).
|
| 27 |
+
|
| 28 |
+
## Use with Diffusers
|
| 29 |
+
|
| 30 |
+
Tested with PyTorch 2.6.0, Diffusers 0.40.0 and Transformers 5.17.0. A CUDA GPU is required for the example below:
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
pip install torch==2.6.0 diffusers==0.40.0 transformers==5.17.0 peft
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
```python
|
| 37 |
+
import torch
|
| 38 |
+
from diffusers import DiffusionPipeline
|
| 39 |
+
from PIL import Image
|
| 40 |
+
|
| 41 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 42 |
+
"bertaveira/R3D2-big",
|
| 43 |
+
trust_remote_code=True,
|
| 44 |
+
torch_dtype=torch.float16,
|
| 45 |
+
).to("cuda")
|
| 46 |
+
|
| 47 |
+
pipe(Image.open("input.png").convert("RGB")).images[0].save("output.png")
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## Use with the original repository
|
| 51 |
+
|
| 52 |
+
Inside [zenseact/R3D2](https://github.com/zenseact/R3D2), after `uv sync`:
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
uv run huggingface-cli download bertaveira/R3D2-big --local-dir checkpoints/R3D2-big
|
| 56 |
+
uv run r3d2/scripts/eval.py --model-path checkpoints/R3D2-big \
|
| 57 |
+
--image-path input.png --output-path output.png
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
The repository CLI uses FP32. For lower GPU memory use, use the FP16 example above.
|
| 61 |
+
|
| 62 |
+
## Credits
|
| 63 |
+
|
| 64 |
+
Built on [SD-Turbo](https://huggingface.co/stabilityai/sd-turbo) and
|
| 65 |
+
[TAESD](https://github.com/madebyollin/taesd), using the
|
| 66 |
+
[Waymo Open Dataset](https://waymo.com/open/). Powered by Stability AI.
|
| 67 |
+
For citation, see the [paper's repository](https://github.com/zenseact/R3D2#citation).
|
model_index.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": [
|
| 3 |
+
"r3d2_pipeline",
|
| 4 |
+
"R3D2Pipeline"
|
| 5 |
+
],
|
| 6 |
+
"_diffusers_version": "0.32.2",
|
| 7 |
+
"image_encoder": [
|
| 8 |
+
null,
|
| 9 |
+
null
|
| 10 |
+
],
|
| 11 |
+
"scheduler": [
|
| 12 |
+
"diffusers",
|
| 13 |
+
"DDPMScheduler"
|
| 14 |
+
],
|
| 15 |
+
"text_encoder": [
|
| 16 |
+
"transformers",
|
| 17 |
+
"CLIPTextModel"
|
| 18 |
+
],
|
| 19 |
+
"timestep": 200,
|
| 20 |
+
"tokenizer": [
|
| 21 |
+
"transformers",
|
| 22 |
+
"CLIPTokenizer"
|
| 23 |
+
],
|
| 24 |
+
"unet": [
|
| 25 |
+
"diffusers",
|
| 26 |
+
"UNet2DConditionModel"
|
| 27 |
+
],
|
| 28 |
+
"vae": [
|
| 29 |
+
"r3d2_vae",
|
| 30 |
+
"R3D2VAE"
|
| 31 |
+
]
|
| 32 |
+
}
|
r3d2_pipeline.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This is an exportable r3d2 pipeline"""
|
| 2 |
+
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
from typing import List, Optional, Union
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import PIL
|
| 10 |
+
import torch
|
| 11 |
+
from diffusers import DDPMScheduler, StableDiffusionPipeline
|
| 12 |
+
from diffusers.image_processor import PipelineImageInput, VaeImageProcessor
|
| 13 |
+
from diffusers.models.unets.unet_2d_condition import UNet2DConditionModel
|
| 14 |
+
from diffusers.utils import BaseOutput
|
| 15 |
+
from peft import LoraConfig
|
| 16 |
+
from safetensors.torch import load_file, save_file
|
| 17 |
+
from transformers import CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection
|
| 18 |
+
|
| 19 |
+
from .vae.r3d2_vae import R3D2VAE, TinyR3D2VAE
|
| 20 |
+
|
| 21 |
+
log = logging.getLogger("rich")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class R3D2PipelineOutput(BaseOutput):
|
| 26 |
+
"""
|
| 27 |
+
Output class for R3D2 pipelines.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
images (`List[PIL.Image.Image]`, `np.ndarray` or `torch.Tensor`)
|
| 31 |
+
List of denoised PIL images of length `batch_size` or NumPy array / torch.Tensor of shape
|
| 32 |
+
`(batch_size, height, width, num_channels)`.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
images: Union[List[PIL.Image.Image], np.ndarray, torch.Tensor]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class R3D2Pipeline(StableDiffusionPipeline):
|
| 39 |
+
"""Pipeline for R3D2 image-to-image generation."""
|
| 40 |
+
|
| 41 |
+
_lora_loadable_modules = ["unet", "vae"]
|
| 42 |
+
|
| 43 |
+
def __init__(
|
| 44 |
+
self,
|
| 45 |
+
vae: Union[R3D2VAE, TinyR3D2VAE],
|
| 46 |
+
text_encoder: CLIPTextModel,
|
| 47 |
+
tokenizer: CLIPTokenizer,
|
| 48 |
+
unet: UNet2DConditionModel,
|
| 49 |
+
scheduler: DDPMScheduler,
|
| 50 |
+
# safety_checker: StableDiffusionSafetyChecker,
|
| 51 |
+
# feature_extractor: CLIPImageProcessor,
|
| 52 |
+
image_encoder: CLIPVisionModelWithProjection = None,
|
| 53 |
+
timestep: int = 200,
|
| 54 |
+
):
|
| 55 |
+
# Skip over the immediate superclass init, but call all upstream init methods
|
| 56 |
+
super(StableDiffusionPipeline, self).__init__()
|
| 57 |
+
|
| 58 |
+
if "r3d2" not in vae.__class__.__name__.lower():
|
| 59 |
+
raise ValueError(
|
| 60 |
+
f"VAE must be a R3D2VAE or TinyR3D2VAE, got {vae.__class__.__name__}.\n"
|
| 61 |
+
"When loading from a non-r3d2 checkpoint, you must initialize components separately. E.g.:\n"
|
| 62 |
+
" vae = R3D2VAE.from_pretrained('stabilityai/sd-turbo')\n"
|
| 63 |
+
" scheduler = DDPMScheduler.from_pretrained('stabilityai/sd-turbo')\n"
|
| 64 |
+
" pipeline = R3D2Pipeline.from_pretrained('stabilityai/sd-turbo', \n"
|
| 65 |
+
" vae=vae,\n"
|
| 66 |
+
" scheduler=scheduler,\n"
|
| 67 |
+
" **kwargs)"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
self.register_modules(
|
| 71 |
+
vae=vae,
|
| 72 |
+
text_encoder=text_encoder,
|
| 73 |
+
tokenizer=tokenizer,
|
| 74 |
+
unet=unet,
|
| 75 |
+
scheduler=scheduler,
|
| 76 |
+
# safety_checker=safety_checker,
|
| 77 |
+
# feature_extractor=feature_extractor,
|
| 78 |
+
image_encoder=image_encoder,
|
| 79 |
+
)
|
| 80 |
+
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
| 81 |
+
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
| 82 |
+
self.scheduler.set_timesteps(timesteps=[timestep])
|
| 83 |
+
self.register_to_config(timestep=timestep)
|
| 84 |
+
|
| 85 |
+
@torch.no_grad()
|
| 86 |
+
def __call__(
|
| 87 |
+
self,
|
| 88 |
+
image: PipelineImageInput,
|
| 89 |
+
output_type: str = "pil",
|
| 90 |
+
generator=None,
|
| 91 |
+
return_dict: bool = True,
|
| 92 |
+
prompt: Optional[str | list[str]] = None,
|
| 93 |
+
) -> R3D2PipelineOutput:
|
| 94 |
+
del prompt # r3d2 is unconditional
|
| 95 |
+
|
| 96 |
+
# Encode (distorted/ugly) image
|
| 97 |
+
device = self._execution_device
|
| 98 |
+
image = self.image_processor.preprocess(image)
|
| 99 |
+
image = image.to(device=device, dtype=self.vae.dtype)
|
| 100 |
+
encoded, skip_activations = self.vae.encode(image, return_dict=False)
|
| 101 |
+
encoded = encoded * self.vae.config.scaling_factor
|
| 102 |
+
|
| 103 |
+
# Use null embedding for prompt, compute first time method is called
|
| 104 |
+
if not hasattr(self, "null_embedding"):
|
| 105 |
+
self.null_embedding = self.encode_prompt("", device, 1, False)[0][0]
|
| 106 |
+
encoder_hidden_states = self.null_embedding.to(device).repeat(encoded.shape[0], 1, 1)
|
| 107 |
+
|
| 108 |
+
# Treat the encoded image as the noisy latent at the given timestep
|
| 109 |
+
timestep = self.scheduler.timesteps[0]
|
| 110 |
+
self.scheduler.scale_model_input(encoded, timestep)
|
| 111 |
+
timestep_unet = torch.tensor([timestep], device=device)
|
| 112 |
+
model_pred = self.unet(encoded, timestep=timestep_unet, encoder_hidden_states=encoder_hidden_states).sample
|
| 113 |
+
img_denoised = self.scheduler.step(model_pred, timestep, encoded, return_dict=True).prev_sample
|
| 114 |
+
img_denoised = img_denoised.to(model_pred.dtype)
|
| 115 |
+
|
| 116 |
+
# Standard pipeline decode stuff
|
| 117 |
+
if not output_type == "latent":
|
| 118 |
+
img_pred = self.vae.decode(img_denoised / self.vae.config.scaling_factor, skip_activations).sample.clamp(
|
| 119 |
+
-1, 1
|
| 120 |
+
)
|
| 121 |
+
else:
|
| 122 |
+
img_pred = img_denoised
|
| 123 |
+
img_pred = self.image_processor.postprocess(img_pred, output_type=output_type)
|
| 124 |
+
self.maybe_free_model_hooks()
|
| 125 |
+
if not return_dict:
|
| 126 |
+
return (img_pred,)
|
| 127 |
+
return R3D2PipelineOutput(images=img_pred)
|
| 128 |
+
|
| 129 |
+
def save_lora_weights(self, save_directory, unet_patterns=("lora",), vae_patterns=("lora",), **kwargs):
|
| 130 |
+
"""Save only LoRA weights and skip connection weights."""
|
| 131 |
+
os.makedirs(save_directory, exist_ok=True)
|
| 132 |
+
|
| 133 |
+
# Add PEFT configs if available. throw error if multiple configs are found
|
| 134 |
+
for module_name, patterns in [("unet", unet_patterns), ("vae", vae_patterns)]:
|
| 135 |
+
module = getattr(self, module_name)
|
| 136 |
+
if hasattr(module, "peft_config"):
|
| 137 |
+
if len(module.peft_config) > 1:
|
| 138 |
+
raise ValueError("Multiple PEFT configs found for UNet, behaviour undefined")
|
| 139 |
+
lora_config = next(iter(module.peft_config.values()))
|
| 140 |
+
lora_dir = os.path.join(save_directory, module_name)
|
| 141 |
+
lora_config.save_pretrained(lora_dir)
|
| 142 |
+
module_lora = {k: v for k, v in module.state_dict().items() if any(p in k for p in patterns)}
|
| 143 |
+
save_file(module_lora, os.path.join(lora_dir, "lora.safetensors"))
|
| 144 |
+
|
| 145 |
+
return save_directory
|
| 146 |
+
|
| 147 |
+
def load_lora_weights(self, lora_weights_name_or_path, **kwargs):
|
| 148 |
+
"""Load R3D2 LoRA weights."""
|
| 149 |
+
for module_name in ["unet", "vae"]:
|
| 150 |
+
module = getattr(self, module_name)
|
| 151 |
+
# Load module LoRA if available
|
| 152 |
+
module_dir = os.path.join(lora_weights_name_or_path, module_name)
|
| 153 |
+
if os.path.exists(module_dir):
|
| 154 |
+
# Read config and create lora layers
|
| 155 |
+
lora_config = LoraConfig.from_pretrained(module_dir)
|
| 156 |
+
module.add_adapter(lora_config)
|
| 157 |
+
# Load lora weights
|
| 158 |
+
module_weights = load_file(os.path.join(module_dir, "lora.safetensors"))
|
| 159 |
+
current_module_sd = module.state_dict()
|
| 160 |
+
for k, v in module_weights.items():
|
| 161 |
+
if k in current_module_sd:
|
| 162 |
+
current_module_sd[k] = v
|
| 163 |
+
else:
|
| 164 |
+
log.warning(f"Key {k} not found in {module_name} state dict")
|
| 165 |
+
module.load_state_dict(current_module_sd)
|
| 166 |
+
return self
|
| 167 |
+
|
| 168 |
+
# needed to override the default here to vae instead of text_encoder
|
| 169 |
+
def fuse_lora(self, components: list[str] = ["unet", "vae"], **kwargs):
|
| 170 |
+
super().fuse_lora(components=components, **kwargs)
|
| 171 |
+
|
| 172 |
+
@classmethod
|
| 173 |
+
def from_lora(cls, lora_weights_name_or_path, use_tiny: bool = True, **kwargs):
|
| 174 |
+
if use_tiny:
|
| 175 |
+
vae = TinyR3D2VAE.from_pretrained("madebyollin/taesd")
|
| 176 |
+
else:
|
| 177 |
+
vae = R3D2VAE.from_pretrained("stabilityai/sd-turbo", subfolder="vae")
|
| 178 |
+
scheduler = DDPMScheduler.from_pretrained("stabilityai/sd-turbo", subfolder="scheduler")
|
| 179 |
+
pipeline = cls.from_pretrained("stabilityai/sd-turbo", vae=vae, scheduler=scheduler)
|
| 180 |
+
try:
|
| 181 |
+
pipeline.load_lora_weights(lora_weights_name_or_path)
|
| 182 |
+
except Exception as e:
|
| 183 |
+
log.warning("Failed to load LoRA weights, if you are using a TinyVAE, try use_tiny=True.")
|
| 184 |
+
raise e
|
| 185 |
+
pipeline.fuse_lora()
|
| 186 |
+
pipeline.unload_lora_weights()
|
| 187 |
+
return pipeline
|
scheduler/scheduler_config.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "DDPMScheduler",
|
| 3 |
+
"_diffusers_version": "0.32.2",
|
| 4 |
+
"beta_end": 0.012,
|
| 5 |
+
"beta_schedule": "scaled_linear",
|
| 6 |
+
"beta_start": 0.00085,
|
| 7 |
+
"clip_sample": false,
|
| 8 |
+
"clip_sample_range": 1.0,
|
| 9 |
+
"dynamic_thresholding_ratio": 0.995,
|
| 10 |
+
"interpolation_type": "linear",
|
| 11 |
+
"num_train_timesteps": 1000,
|
| 12 |
+
"prediction_type": "epsilon",
|
| 13 |
+
"rescale_betas_zero_snr": false,
|
| 14 |
+
"sample_max_value": 1.0,
|
| 15 |
+
"set_alpha_to_one": false,
|
| 16 |
+
"sigma_max": null,
|
| 17 |
+
"sigma_min": null,
|
| 18 |
+
"skip_prk_steps": true,
|
| 19 |
+
"steps_offset": 1,
|
| 20 |
+
"thresholding": false,
|
| 21 |
+
"timestep_spacing": "trailing",
|
| 22 |
+
"timestep_type": "discrete",
|
| 23 |
+
"trained_betas": null,
|
| 24 |
+
"use_karras_sigmas": false,
|
| 25 |
+
"variance_type": "fixed_small"
|
| 26 |
+
}
|
text_encoder/config.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"CLIPTextModel"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.0,
|
| 6 |
+
"bos_token_id": 0,
|
| 7 |
+
"dropout": 0.0,
|
| 8 |
+
"eos_token_id": 2,
|
| 9 |
+
"hidden_act": "gelu",
|
| 10 |
+
"hidden_size": 1024,
|
| 11 |
+
"initializer_factor": 1.0,
|
| 12 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 4096,
|
| 14 |
+
"layer_norm_eps": 1e-05,
|
| 15 |
+
"max_position_embeddings": 77,
|
| 16 |
+
"model_type": "clip_text_model",
|
| 17 |
+
"num_attention_heads": 16,
|
| 18 |
+
"num_hidden_layers": 23,
|
| 19 |
+
"pad_token_id": 1,
|
| 20 |
+
"projection_dim": 512,
|
| 21 |
+
"torch_dtype": "float32",
|
| 22 |
+
"transformers_version": "4.49.0",
|
| 23 |
+
"vocab_size": 49408
|
| 24 |
+
}
|
text_encoder/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:67e013543d4fac905c882e2993d86a2d454ee69dc9e8f37c0c23d33a48959d15
|
| 3 |
+
size 1361596304
|
tokenizer/merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer/special_tokens_map.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<|startoftext|>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": true,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "<|endoftext|>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": true,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "!",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"unk_token": {
|
| 24 |
+
"content": "<|endoftext|>",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": true,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
}
|
| 30 |
+
}
|
tokenizer/tokenizer_config.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"0": {
|
| 5 |
+
"content": "!",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": false,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"49406": {
|
| 13 |
+
"content": "<|startoftext|>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": true,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"49407": {
|
| 21 |
+
"content": "<|endoftext|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": true,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
"bos_token": "<|startoftext|>",
|
| 30 |
+
"clean_up_tokenization_spaces": true,
|
| 31 |
+
"do_lower_case": true,
|
| 32 |
+
"eos_token": "<|endoftext|>",
|
| 33 |
+
"errors": "replace",
|
| 34 |
+
"extra_special_tokens": {},
|
| 35 |
+
"model_max_length": 77,
|
| 36 |
+
"pad_token": "!",
|
| 37 |
+
"tokenizer_class": "CLIPTokenizer",
|
| 38 |
+
"unk_token": "<|endoftext|>"
|
| 39 |
+
}
|
tokenizer/vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
unet/config.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "UNet2DConditionModel",
|
| 3 |
+
"_diffusers_version": "0.32.2",
|
| 4 |
+
"act_fn": "silu",
|
| 5 |
+
"addition_embed_type": null,
|
| 6 |
+
"addition_embed_type_num_heads": 64,
|
| 7 |
+
"addition_time_embed_dim": null,
|
| 8 |
+
"attention_head_dim": [
|
| 9 |
+
5,
|
| 10 |
+
10,
|
| 11 |
+
20,
|
| 12 |
+
20
|
| 13 |
+
],
|
| 14 |
+
"attention_type": "default",
|
| 15 |
+
"block_out_channels": [
|
| 16 |
+
320,
|
| 17 |
+
640,
|
| 18 |
+
1280,
|
| 19 |
+
1280
|
| 20 |
+
],
|
| 21 |
+
"center_input_sample": false,
|
| 22 |
+
"class_embed_type": null,
|
| 23 |
+
"class_embeddings_concat": false,
|
| 24 |
+
"conv_in_kernel": 3,
|
| 25 |
+
"conv_out_kernel": 3,
|
| 26 |
+
"cross_attention_dim": 1024,
|
| 27 |
+
"cross_attention_norm": null,
|
| 28 |
+
"down_block_types": [
|
| 29 |
+
"CrossAttnDownBlock2D",
|
| 30 |
+
"CrossAttnDownBlock2D",
|
| 31 |
+
"CrossAttnDownBlock2D",
|
| 32 |
+
"DownBlock2D"
|
| 33 |
+
],
|
| 34 |
+
"downsample_padding": 1,
|
| 35 |
+
"dropout": 0.0,
|
| 36 |
+
"dual_cross_attention": false,
|
| 37 |
+
"encoder_hid_dim": null,
|
| 38 |
+
"encoder_hid_dim_type": null,
|
| 39 |
+
"flip_sin_to_cos": true,
|
| 40 |
+
"freq_shift": 0,
|
| 41 |
+
"in_channels": 4,
|
| 42 |
+
"layers_per_block": 2,
|
| 43 |
+
"mid_block_only_cross_attention": null,
|
| 44 |
+
"mid_block_scale_factor": 1,
|
| 45 |
+
"mid_block_type": "UNetMidBlock2DCrossAttn",
|
| 46 |
+
"norm_eps": 1e-05,
|
| 47 |
+
"norm_num_groups": 32,
|
| 48 |
+
"num_attention_heads": null,
|
| 49 |
+
"num_class_embeds": null,
|
| 50 |
+
"only_cross_attention": false,
|
| 51 |
+
"out_channels": 4,
|
| 52 |
+
"projection_class_embeddings_input_dim": null,
|
| 53 |
+
"resnet_out_scale_factor": 1.0,
|
| 54 |
+
"resnet_skip_time_act": false,
|
| 55 |
+
"resnet_time_scale_shift": "default",
|
| 56 |
+
"reverse_transformer_layers_per_block": null,
|
| 57 |
+
"sample_size": 64,
|
| 58 |
+
"time_cond_proj_dim": null,
|
| 59 |
+
"time_embedding_act_fn": null,
|
| 60 |
+
"time_embedding_dim": null,
|
| 61 |
+
"time_embedding_type": "positional",
|
| 62 |
+
"timestep_post_act": null,
|
| 63 |
+
"transformer_layers_per_block": 1,
|
| 64 |
+
"up_block_types": [
|
| 65 |
+
"UpBlock2D",
|
| 66 |
+
"CrossAttnUpBlock2D",
|
| 67 |
+
"CrossAttnUpBlock2D",
|
| 68 |
+
"CrossAttnUpBlock2D"
|
| 69 |
+
],
|
| 70 |
+
"upcast_attention": null,
|
| 71 |
+
"use_linear_projection": true
|
| 72 |
+
}
|
unet/diffusion_pytorch_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca215babf9eb651a78f414d697c89abd96cb52b330ef9012fc568ddfc3e27abb
|
| 3 |
+
size 3463726504
|
vae/config.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "R3D2VAE",
|
| 3 |
+
"_diffusers_version": "0.32.2",
|
| 4 |
+
"act_fn": "silu",
|
| 5 |
+
"block_out_channels": [
|
| 6 |
+
128,
|
| 7 |
+
256,
|
| 8 |
+
512,
|
| 9 |
+
512
|
| 10 |
+
],
|
| 11 |
+
"down_block_types": [
|
| 12 |
+
"DownEncoderBlock2D",
|
| 13 |
+
"DownEncoderBlock2D",
|
| 14 |
+
"DownEncoderBlock2D",
|
| 15 |
+
"DownEncoderBlock2D"
|
| 16 |
+
],
|
| 17 |
+
"force_upcast": true,
|
| 18 |
+
"in_channels": 3,
|
| 19 |
+
"latent_channels": 4,
|
| 20 |
+
"latents_mean": null,
|
| 21 |
+
"latents_std": null,
|
| 22 |
+
"layers_per_block": 2,
|
| 23 |
+
"mid_block_add_attention": true,
|
| 24 |
+
"norm_num_groups": 32,
|
| 25 |
+
"out_channels": 3,
|
| 26 |
+
"sample_size": 768,
|
| 27 |
+
"scaling_factor": 0.18215,
|
| 28 |
+
"shift_factor": null,
|
| 29 |
+
"up_block_types": [
|
| 30 |
+
"UpDecoderBlock2D",
|
| 31 |
+
"UpDecoderBlock2D",
|
| 32 |
+
"UpDecoderBlock2D",
|
| 33 |
+
"UpDecoderBlock2D"
|
| 34 |
+
],
|
| 35 |
+
"use_post_quant_conv": true,
|
| 36 |
+
"use_quant_conv": true,
|
| 37 |
+
"use_skip": true
|
| 38 |
+
}
|
vae/diffusion_pytorch_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3406c6d830482e029be10a9de02cb2acc1ac4aa29fa4216f798244025fc6638d
|
| 3 |
+
size 336347532
|
vae/r3d2_vae.py
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, Tuple, Union
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import AutoencoderKL, AutoencoderTiny
|
| 5 |
+
from diffusers.configuration_utils import register_to_config
|
| 6 |
+
from diffusers.loaders import PeftAdapterMixin
|
| 7 |
+
from diffusers.models.autoencoders.vae import DecoderOutput, DiagonalGaussianDistribution
|
| 8 |
+
from diffusers.utils import BaseOutput
|
| 9 |
+
from diffusers.utils.accelerate_utils import apply_forward_hook
|
| 10 |
+
|
| 11 |
+
TINY_VAE_SRC = "madebyollin/taesd" # cqyan/hybrid-sd-tinyvae
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class R3D2AutoencoderOutput(BaseOutput):
|
| 15 |
+
latents: torch.Tensor
|
| 16 |
+
skip_activations: list[torch.Tensor]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class R3D2VAE(AutoencoderKL):
|
| 20 |
+
"""VAE with skip connections from encoder to decoder for R3D2 models."""
|
| 21 |
+
|
| 22 |
+
@register_to_config
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
# Parent params
|
| 26 |
+
in_channels: int = 3,
|
| 27 |
+
out_channels: int = 3,
|
| 28 |
+
down_block_types: Tuple[str] = ("DownEncoderBlock2D",),
|
| 29 |
+
up_block_types: Tuple[str] = ("UpDecoderBlock2D",),
|
| 30 |
+
block_out_channels: Tuple[int] = (64,),
|
| 31 |
+
layers_per_block: int = 1,
|
| 32 |
+
act_fn: str = "silu",
|
| 33 |
+
latent_channels: int = 4,
|
| 34 |
+
norm_num_groups: int = 32,
|
| 35 |
+
sample_size: int = 32,
|
| 36 |
+
use_quant_conv: bool = True,
|
| 37 |
+
use_post_quant_conv: bool = True,
|
| 38 |
+
mid_block_add_attention: bool = True,
|
| 39 |
+
# R3D2 params
|
| 40 |
+
use_skip: bool = True,
|
| 41 |
+
):
|
| 42 |
+
super().__init__(
|
| 43 |
+
in_channels=in_channels,
|
| 44 |
+
out_channels=out_channels,
|
| 45 |
+
down_block_types=down_block_types,
|
| 46 |
+
up_block_types=up_block_types,
|
| 47 |
+
block_out_channels=block_out_channels,
|
| 48 |
+
layers_per_block=layers_per_block,
|
| 49 |
+
act_fn=act_fn,
|
| 50 |
+
latent_channels=latent_channels,
|
| 51 |
+
norm_num_groups=norm_num_groups,
|
| 52 |
+
sample_size=sample_size,
|
| 53 |
+
use_quant_conv=use_quant_conv,
|
| 54 |
+
use_post_quant_conv=use_post_quant_conv,
|
| 55 |
+
mid_block_add_attention=mid_block_add_attention,
|
| 56 |
+
)
|
| 57 |
+
self.decoder.skip_convs = torch.nn.ModuleList(
|
| 58 |
+
[
|
| 59 |
+
torch.nn.Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False),
|
| 60 |
+
torch.nn.Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False),
|
| 61 |
+
torch.nn.Conv2d(128, 256, kernel_size=(1, 1), stride=(1, 1), bias=False),
|
| 62 |
+
]
|
| 63 |
+
)
|
| 64 |
+
# Initialize to zero-conv (ish)
|
| 65 |
+
for conv in self.decoder.skip_convs:
|
| 66 |
+
torch.nn.init.constant_(conv.weight, 1e-5)
|
| 67 |
+
|
| 68 |
+
self.use_skip = use_skip
|
| 69 |
+
|
| 70 |
+
def _patched_enc_fwd(self, sample: torch.Tensor) -> tuple[torch.Tensor, list[torch.Tensor]]:
|
| 71 |
+
if self.gradient_checkpointing:
|
| 72 |
+
raise NotImplementedError("Gradient checkpointing not implemented, see super class")
|
| 73 |
+
|
| 74 |
+
sample = self.conv_in(sample)
|
| 75 |
+
skips = []
|
| 76 |
+
for block in self.down_blocks:
|
| 77 |
+
if use_skip and block.downsamplers is not None: # last block doesn't downsample
|
| 78 |
+
for resnet in block.resnets:
|
| 79 |
+
sample = resnet(sample, temb=None)
|
| 80 |
+
skips.append(sample)
|
| 81 |
+
for downsampler in block.downsamplers:
|
| 82 |
+
sample = downsampler(sample)
|
| 83 |
+
else:
|
| 84 |
+
sample = block(sample)
|
| 85 |
+
|
| 86 |
+
# remaining from super
|
| 87 |
+
sample = self.mid_block(sample)
|
| 88 |
+
sample = self.conv_norm_out(sample)
|
| 89 |
+
sample = self.conv_act(sample)
|
| 90 |
+
sample = self.conv_out(sample)
|
| 91 |
+
return sample, skips
|
| 92 |
+
|
| 93 |
+
self.encoder.forward = _patched_enc_fwd.__get__(self.encoder, self.encoder.__class__)
|
| 94 |
+
|
| 95 |
+
# Patch decoder
|
| 96 |
+
def _patched_dec_fwd(
|
| 97 |
+
self,
|
| 98 |
+
sample: torch.Tensor,
|
| 99 |
+
skip_activations: list[torch.Tensor],
|
| 100 |
+
):
|
| 101 |
+
if self.gradient_checkpointing:
|
| 102 |
+
raise NotImplementedError("Gradient checkpointing not implemented, see super class")
|
| 103 |
+
|
| 104 |
+
# init stage (from super)
|
| 105 |
+
sample = self.conv_in(sample)
|
| 106 |
+
upscale_dtype = next(iter(self.up_blocks.parameters())).dtype
|
| 107 |
+
sample = self.mid_block(sample)
|
| 108 |
+
sample = sample.to(upscale_dtype)
|
| 109 |
+
|
| 110 |
+
# reverse order from going down to going up
|
| 111 |
+
if use_skip:
|
| 112 |
+
skip_activations = skip_activations[::-1]
|
| 113 |
+
skip_i = 0
|
| 114 |
+
|
| 115 |
+
# up with skip connections
|
| 116 |
+
for up_block in self.up_blocks:
|
| 117 |
+
sample = up_block(sample)
|
| 118 |
+
if use_skip and up_block.upsamplers is not None:
|
| 119 |
+
sample = sample + self.skip_convs[skip_i](skip_activations[skip_i])
|
| 120 |
+
skip_i += 1
|
| 121 |
+
|
| 122 |
+
# post-process (from super)
|
| 123 |
+
sample = self.conv_norm_out(sample)
|
| 124 |
+
sample = self.conv_act(sample)
|
| 125 |
+
sample = self.conv_out(sample)
|
| 126 |
+
return sample
|
| 127 |
+
|
| 128 |
+
self.decoder.forward = _patched_dec_fwd.__get__(self.decoder, self.decoder.__class__)
|
| 129 |
+
|
| 130 |
+
@classmethod
|
| 131 |
+
def from_pretrained(cls, pretrained_model_name_or_path, subfolder=None, **kwargs):
|
| 132 |
+
"""Load pretrained VAE and convert to R3D2VAE if needed."""
|
| 133 |
+
if pretrained_model_name_or_path == "stabilityai/sd-turbo":
|
| 134 |
+
# load with settings that allow key mismatch
|
| 135 |
+
kwargs["low_cpu_mem_usage"] = False
|
| 136 |
+
kwargs["device_map"] = None
|
| 137 |
+
return super().from_pretrained(pretrained_model_name_or_path, subfolder=subfolder, **kwargs)
|
| 138 |
+
|
| 139 |
+
@apply_forward_hook
|
| 140 |
+
def encode(
|
| 141 |
+
self, x: torch.Tensor, return_dict: bool = True
|
| 142 |
+
) -> Union[R3D2AutoencoderOutput, Tuple[torch.Tensor, list[torch.Tensor]]]:
|
| 143 |
+
if self.use_slicing or self.use_tiling:
|
| 144 |
+
raise NotImplementedError("Slicing and tiling not supported for R3D2VAE")
|
| 145 |
+
|
| 146 |
+
latents, skip_activations = self.encoder(x)
|
| 147 |
+
if self.quant_conv is not None:
|
| 148 |
+
latents = self.quant_conv(latents)
|
| 149 |
+
|
| 150 |
+
# for r3d2 we don't care about the distribution, so just pick the mean
|
| 151 |
+
latents = DiagonalGaussianDistribution(latents).mean
|
| 152 |
+
if not return_dict:
|
| 153 |
+
return (latents, skip_activations)
|
| 154 |
+
return R3D2AutoencoderOutput(latents=latents, skip_activations=skip_activations)
|
| 155 |
+
|
| 156 |
+
@apply_forward_hook
|
| 157 |
+
def decode(
|
| 158 |
+
self,
|
| 159 |
+
z: torch.Tensor,
|
| 160 |
+
skip_activations: Optional[list[torch.Tensor]] = None,
|
| 161 |
+
return_dict: bool = True,
|
| 162 |
+
) -> Union[DecoderOutput, Tuple[torch.Tensor]]:
|
| 163 |
+
if self.use_slicing or self.use_tiling:
|
| 164 |
+
raise NotImplementedError("Slicing and tiling not supported for R3D2VAE")
|
| 165 |
+
|
| 166 |
+
if self.post_quant_conv is not None:
|
| 167 |
+
z = self.post_quant_conv(z)
|
| 168 |
+
dec_out = self.decoder(z, skip_activations)
|
| 169 |
+
|
| 170 |
+
if not return_dict:
|
| 171 |
+
return (dec_out,)
|
| 172 |
+
return DecoderOutput(sample=dec_out)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
class TinyR3D2VAE(AutoencoderTiny, PeftAdapterMixin):
|
| 176 |
+
"""Tiny VAE with skip connections from encoder to decoder for R3D2 models."""
|
| 177 |
+
|
| 178 |
+
@register_to_config
|
| 179 |
+
def __init__(
|
| 180 |
+
self,
|
| 181 |
+
# Parent params
|
| 182 |
+
in_channels: int = 3,
|
| 183 |
+
out_channels: int = 3,
|
| 184 |
+
encoder_block_out_channels: Tuple[int, ...] = (64, 64, 64, 64),
|
| 185 |
+
decoder_block_out_channels: Tuple[int, ...] = (64, 64, 64, 64),
|
| 186 |
+
act_fn: str = "relu",
|
| 187 |
+
upsample_fn: str = "nearest",
|
| 188 |
+
latent_channels: int = 4,
|
| 189 |
+
upsampling_scaling_factor: int = 2,
|
| 190 |
+
num_encoder_blocks: Tuple[int, ...] = (1, 3, 3, 3),
|
| 191 |
+
num_decoder_blocks: Tuple[int, ...] = (3, 3, 3, 1),
|
| 192 |
+
latent_magnitude: int = 3,
|
| 193 |
+
latent_shift: float = 0.5,
|
| 194 |
+
force_upcast: bool = False,
|
| 195 |
+
scaling_factor: float = 1.0,
|
| 196 |
+
shift_factor: float = 0.0,
|
| 197 |
+
# R3D2 params
|
| 198 |
+
use_skip: bool = True,
|
| 199 |
+
):
|
| 200 |
+
super().__init__(
|
| 201 |
+
in_channels=in_channels,
|
| 202 |
+
out_channels=out_channels,
|
| 203 |
+
encoder_block_out_channels=encoder_block_out_channels,
|
| 204 |
+
decoder_block_out_channels=decoder_block_out_channels,
|
| 205 |
+
act_fn=act_fn,
|
| 206 |
+
upsample_fn=upsample_fn,
|
| 207 |
+
latent_channels=latent_channels,
|
| 208 |
+
upsampling_scaling_factor=upsampling_scaling_factor,
|
| 209 |
+
num_encoder_blocks=num_encoder_blocks,
|
| 210 |
+
num_decoder_blocks=num_decoder_blocks,
|
| 211 |
+
latent_magnitude=latent_magnitude,
|
| 212 |
+
latent_shift=latent_shift,
|
| 213 |
+
force_upcast=force_upcast,
|
| 214 |
+
scaling_factor=scaling_factor,
|
| 215 |
+
shift_factor=shift_factor,
|
| 216 |
+
)
|
| 217 |
+
self.decoder.skip_convs = torch.nn.ModuleList(
|
| 218 |
+
[
|
| 219 |
+
torch.nn.Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False),
|
| 220 |
+
torch.nn.Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False),
|
| 221 |
+
torch.nn.Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False),
|
| 222 |
+
]
|
| 223 |
+
)
|
| 224 |
+
# Initialize to zero-conv (ish)
|
| 225 |
+
for conv in self.decoder.skip_convs:
|
| 226 |
+
torch.nn.init.constant_(conv.weight, 1e-5)
|
| 227 |
+
|
| 228 |
+
self.use_skip = use_skip
|
| 229 |
+
|
| 230 |
+
# Patch encoder
|
| 231 |
+
enc_indices = []
|
| 232 |
+
for i, layer in enumerate(self.encoder.layers):
|
| 233 |
+
if getattr(layer, "stride", (1, 1))[0] == 2:
|
| 234 |
+
enc_indices.append(i)
|
| 235 |
+
|
| 236 |
+
def _patched_enc_fwd(self, x):
|
| 237 |
+
x = x.add(1).div(2)
|
| 238 |
+
curr, skips = 0, []
|
| 239 |
+
for skip_layer in enc_indices:
|
| 240 |
+
x = self.layers[curr:skip_layer](x)
|
| 241 |
+
if use_skip:
|
| 242 |
+
skips.append(x)
|
| 243 |
+
curr = skip_layer
|
| 244 |
+
return self.layers[curr:](x), skips
|
| 245 |
+
|
| 246 |
+
self.encoder.forward = _patched_enc_fwd.__get__(self.encoder, self.encoder.__class__)
|
| 247 |
+
|
| 248 |
+
# Patch decoder
|
| 249 |
+
dec_indices = []
|
| 250 |
+
for i, layer in enumerate(self.decoder.layers):
|
| 251 |
+
if getattr(layer, "scale_factor", 1) >= 2:
|
| 252 |
+
dec_indices.append(i + 1)
|
| 253 |
+
|
| 254 |
+
def _patched_dec_fwd(self, x, skip_activations):
|
| 255 |
+
x = torch.tanh(x / 3) * 3
|
| 256 |
+
curr = 0
|
| 257 |
+
for i, skip_layer in enumerate(dec_indices):
|
| 258 |
+
x = self.layers[curr : skip_layer + 1](x)
|
| 259 |
+
if use_skip:
|
| 260 |
+
x = x + self.skip_convs[i](skip_activations[::-1][i])
|
| 261 |
+
curr = skip_layer + 1
|
| 262 |
+
x = self.layers[curr:](x)
|
| 263 |
+
return x.mul(2).sub(1)
|
| 264 |
+
|
| 265 |
+
self.decoder.forward = _patched_dec_fwd.__get__(self.decoder, self.decoder.__class__)
|
| 266 |
+
|
| 267 |
+
@classmethod
|
| 268 |
+
def from_pretrained(cls, pretrained_model_name_or_path, subfolder=None, **kwargs):
|
| 269 |
+
"""Load pretrained VAE and convert to R3D2VAE if needed."""
|
| 270 |
+
if pretrained_model_name_or_path == "stabilityai/sd-turbo":
|
| 271 |
+
pretrained_model_name_or_path = TINY_VAE_SRC # patch so we can load pipeline from sd-turbo
|
| 272 |
+
if pretrained_model_name_or_path == TINY_VAE_SRC:
|
| 273 |
+
subfolder = None # src only contains vae weights
|
| 274 |
+
kwargs.pop("variant", None)
|
| 275 |
+
# load with settings that allow key mismatch, due to no skip conns
|
| 276 |
+
kwargs["low_cpu_mem_usage"] = False
|
| 277 |
+
kwargs["device_map"] = None
|
| 278 |
+
return super().from_pretrained(pretrained_model_name_or_path, subfolder=subfolder, **kwargs)
|
| 279 |
+
|
| 280 |
+
@apply_forward_hook
|
| 281 |
+
def encode(
|
| 282 |
+
self, x: torch.Tensor, return_dict: bool = True
|
| 283 |
+
) -> Union[R3D2AutoencoderOutput, Tuple[torch.Tensor, list[torch.Tensor]]]:
|
| 284 |
+
if self.use_slicing or self.use_tiling:
|
| 285 |
+
raise NotImplementedError("Slicing and tiling not supported for TinyR3D2VAE")
|
| 286 |
+
latents, skip_activations = self.encoder(x)
|
| 287 |
+
if not return_dict:
|
| 288 |
+
return (latents, skip_activations)
|
| 289 |
+
return R3D2AutoencoderOutput(latents=latents, skip_activations=skip_activations)
|
| 290 |
+
|
| 291 |
+
@apply_forward_hook
|
| 292 |
+
def decode(
|
| 293 |
+
self,
|
| 294 |
+
x: torch.Tensor,
|
| 295 |
+
skip_activations: Optional[list[torch.Tensor]] = None,
|
| 296 |
+
generator: Optional[torch.Generator] = None,
|
| 297 |
+
return_dict: bool = True,
|
| 298 |
+
) -> Union[DecoderOutput, Tuple[torch.Tensor]]:
|
| 299 |
+
if self.use_slicing or self.use_tiling:
|
| 300 |
+
raise NotImplementedError("Slicing and tiling not supported for TinyR3D2VAE")
|
| 301 |
+
x = self.decoder(x, skip_activations)
|
| 302 |
+
if not return_dict:
|
| 303 |
+
return (x,)
|
| 304 |
+
return DecoderOutput(sample=x)
|