File size: 10,712 Bytes
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2da345
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
 
 
 
 
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
 
 
 
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
 
 
 
 
 
 
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
 
 
 
 
 
 
 
 
f876753
 
fc44d4b
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44d4b
 
 
 
 
 
 
 
f876753
 
 
 
fc44d4b
f876753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import os
import re
import json
from tqdm import tqdm

import torch
from dataclasses import dataclass, field
from diffusers import StableDiffusionPipeline

from .base import Pipeline
from ..models.geometry import StableDiffusionTriplaneDualAttention
from ..utils.mesh_exporter import isosurface, colorize_mesh, DiffMarchingCubeHelper

from diffusers.loaders import AttnProcsLayers
from ..models.networks import get_activation

@dataclass
class TriplaneTurboTextTo3DPipelineConfig:
    """Configuration for TriplaneTurboTextTo3DPipeline"""
    # Basic pipeline settings
    base_model_name_or_path: str = "stabilityai/stable-diffusion-2-1-base"

    
    # Training/sampling settings
    num_steps_sampling: int = 4
    
    # Geometry settings
    radius: float = 1.0
    normal_type: str = "analytic"
    sdf_bias: str = "sphere"
    sdf_bias_params: float = 0.5
    rotate_planes: str = "v1"
    split_channels: str = "v1"
    geo_interpolate: str = "v1"
    tex_interpolate: str = "v2"
    n_feature_dims: int = 3

    sample_scheduler: str = "ddim" # any of "ddpm", "ddim"
    
    # Network settings
    mlp_network_config: dict = field(
        default_factory=lambda: {
            "otype": "VanillaMLP",
            "activation": "ReLU",
            "output_activation": "none",
            "n_neurons": 64,
            "n_hidden_layers": 2,
        }
    )

    # Adapter settings
    space_generator_config: dict = field(
        default_factory=lambda: {
            "training_type": "self_lora_rank_16-cross_lora_rank_16-locon_rank_16" ,
            "output_dim": 64,  # 32 * 2 for v1
            "self_lora_type": "hexa_v1",
            "cross_lora_type": "vanilla",
            "locon_type": "vanilla_v1",
            "prompt_bias": False,
            "vae_attn_type": "basic",  # "basic", "vanilla"
        }
    )

    isosurface_deformable_grid: bool = True
    isosurface_resolution: int = 160
    color_activation: str = "sigmoid-mipnerf"

    @classmethod
    def from_pretrained(cls, pretrained_path):
        """Load config from pretrained path"""
        config_path = os.path.join(pretrained_path, "config.json")
        if os.path.exists(config_path):
            with open(config_path, "r") as f:
                config_dict = json.load(f)
            return cls(**config_dict)
        else:
            print(f"No config file found at {pretrained_path}, using default config")
            return cls()  # Return default config if no config file found

class TriplaneTurboTextTo3DPipeline(Pipeline):
    """
    A pipeline for converting text to 3D models using triplane representation.
    """
    config_name = "config.json"

    def __init__(
        self,
        geometry,
        material,
        base_pipeline,
        sample_scheduler,
        isosurface_helper,
        **kwargs,
    ):
        super().__init__()
        self.geometry = geometry
        self.material = material

        self.base_pipeline = base_pipeline

        self.sample_scheduler = sample_scheduler
        self.isosurface_helper = isosurface_helper


        self.models = {
            "geometry": geometry,
            "base_pipeline": base_pipeline,
        }
        
    @classmethod
    def from_pretrained(
        cls,
        pretrained_model_name_or_path,
        **kwargs,
    ):
        """
        Load pretrained adapter weights, config and update pipeline components.

        Args:
            pretrained_model_name_or_path: Path to pretrained adapter weights
            base_pipeline: Optional base pipeline instance
            **kwargs: Additional arguments to override config values

        Returns:
            pipeline: Updated pipeline instance
        """
        # Load config from pretrained path
        config = TriplaneTurboTextTo3DPipelineConfig.from_pretrained(
            pretrained_model_name_or_path,
            **kwargs,
        )

        # load base pipeline
        base_pipeline = StableDiffusionPipeline.from_pretrained(
            config.base_model_name_or_path,
            **kwargs,
        )

        # load sample scheduler
        if config.sample_scheduler == "ddim":   
            from diffusers import DDIMScheduler
            sample_scheduler = DDIMScheduler.from_pretrained(
                config.base_model_name_or_path,
                subfolder="scheduler",
            )
        else:
            raise ValueError(f"Unknown sample scheduler: {config.sample_scheduler}")

        # load geometry
        geometry = StableDiffusionTriplaneDualAttention(
                config=config,
                vae=base_pipeline.vae,
                unet=base_pipeline.unet,
            )

        # no gradient for geometry
        for param in geometry.parameters():
            param.requires_grad = False

        # and load adapter weights
        if pretrained_model_name_or_path.endswith(".pth"):
            state_dict = torch.load(pretrained_model_name_or_path)["state_dict"]
            new_state_dict = {}
            for key, value in state_dict.items():
                new_key = key.replace("geometry.", "")
                new_state_dict[new_key] = value
            _, unused = geometry.load_state_dict(new_state_dict, strict=False)
            if len(unused) > 0:
                print(f"Unused keys: {unused}")
        else:
            raise ValueError(f"Unknown pretrained model name or path: {pretrained_model_name_or_path}")


        # load material, convert to int
        # material = lambda x: (256 * get_activation(config.color_activation)(x)).int()
        material = get_activation(config.color_activation)

        # Load geometry model
        pipeline = cls(
            base_pipeline=base_pipeline,
            geometry=geometry,
            sample_scheduler=sample_scheduler,
            material=material,
            isosurface_helper=DiffMarchingCubeHelper(
                resolution=config.isosurface_resolution,
            ),
            **kwargs,
        )
        return pipeline


    def encode_prompt(
        self, 
        prompt,
        device,
        num_results_per_prompt = 1,
    ):
        """
        Encodes the prompt into text encoder hidden states.
        
        Args:
            prompt: The prompt to encode.
            device: The device to use for encoding.
            num_results_per_prompt: Number of results to generate per prompt.
            do_classifier_free_guidance: Whether to use classifier-free guidance.
            negative_prompt: The negative prompt to encode.
            
        Returns:
            text_embeddings: Text embeddings tensor.
        """
        # Use base_pipeline to encode prompt
        text_embeddings = self.base_pipeline.encode_prompt(
            prompt=prompt,
            device=device,
            num_images_per_prompt=num_results_per_prompt,
            do_classifier_free_guidance=False,
            negative_prompt=None
        )
        return text_embeddings
        
    @torch.no_grad()
    def __call__(
        self,
        prompt,
        num_results_per_prompt=1,
        generator=None,
        device=None,
        return_dict=True,
        num_inference_steps=4,
        colorize = True,
    ):
        # Implementation similar to Zero123Pipeline
        # Reference code from: https://github.com/zero123/zero123-diffusers
        
        # Validate inputs
        if isinstance(prompt, str):
            batch_size = 1
            prompt = [prompt]
        elif isinstance(prompt, list):
            batch_size = len(prompt)
        else:
            raise ValueError(f"Prompt must be a string or list of strings, got {type(prompt)}")
        
        # Get the device from the first available module

        # Generate latents if not provided
        if device is None:
            device = self.device
        if generator is None:
            generator = torch.Generator(device=device)
        latents = torch.randn(
            (batch_size * 6, 4, 32, 32), # hard-coded for now
            generator=generator,
            device=device,
        )

        # Process text prompt through geometry module
        text_embed, _ = self.encode_prompt(prompt, device, num_results_per_prompt)
        
        # Run diffusion process
        # Set up timesteps for sampling
        timesteps = self._set_timesteps(
            self.sample_scheduler,
            num_inference_steps
        )


        with torch.no_grad():
            # Run diffusion process
            for i, t in tqdm(enumerate(timesteps)):
                # Scale model input
                noisy_latent_input = self.sample_scheduler.scale_model_input(
                    latents, 
                    t
                )

                # Predict noise/sample
                pred = self.geometry.denoise(
                    noisy_input=noisy_latent_input,
                    text_embed=text_embed,
                    timestep=t.to(device),
                )

                # Update latents
                results = self.sample_scheduler.step(pred, t, latents)
                latents = results.prev_sample
                latents_denoised = results.pred_original_sample

            # Use final denoised latents
            latents = latents_denoised
            
            # Generate final 3D representation
            space_cache = self.geometry.decode(latents)

            # Extract mesh from space cache
            mesh_list = isosurface(
                space_cache,
                self.geometry.forward_field,
                self.isosurface_helper,
            )

            if colorize:
                mesh_list = colorize_mesh(
                    space_cache,
                    self.geometry.export,
                    mesh_list,
                    activation=self.material,
                )

            if return_dict:
                return {
                    "space_cache": space_cache,
                    "latents": latents,
                    "mesh": mesh_list,
                }
            else:
                return mesh_list

    def _set_timesteps(
        self,
        scheduler,
        num_steps,
    ):
        """Set up timesteps for sampling.
        
        Args:
            scheduler: The scheduler to use for timestep generation
            num_steps: Number of diffusion steps
            
        Returns:
            timesteps: Tensor of timesteps to use for sampling
        """
        scheduler.set_timesteps(num_steps)
        timesteps_orig = scheduler.timesteps
        # Shift timesteps to start from T
        timesteps_delta = scheduler.config.num_train_timesteps - 1 - timesteps_orig.max()
        timesteps = timesteps_orig + timesteps_delta
        return timesteps