mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-24 00:26:08 +08:00
merging from main
This commit is contained in:
commit
eee05195c0
24 files changed
+437
-97
No files matched your search
@@ -53,16 +53,19 @@ CLIP_SOURCE = "openai/clip-vit-large-patch14"
|
||||
|
||||
@dataclass
|
||||
class UNet2DConditionOutput:
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
"""Class to hold traced model"""
|
||||
|
||||
>>>>>>> df784e96e7979aaa4320284c087d7036dce67c28
|
||||
sample: torch.FloatTensor
|
||||
|
||||
|
||||
class StableDiffusion(nn.Module):
|
||||
"""Stable Diffusion implementation
|
||||
|
||||
Args:
|
||||
device: device to use
|
||||
num_train_timesteps: number of training timesteps
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, device: Union[torch.device, str], num_train_timesteps: int = 1000) -> None:
|
||||
@@ -89,7 +92,11 @@ class StableDiffusion(nn.Module):
|
||||
pipe.enable_attention_slicing()
|
||||
|
||||
# use jitted unet
|
||||
<<<<<<< HEAD
|
||||
unet_traced_filename = Path(appdirs.user_data_dir("nerfstudio")) / "unet_traced.pt"
|
||||
=======
|
||||
unet_traced_filename = Path(appdirs.user_data_dir("nerfstudio")) / "sd_unet_traced.pt"
|
||||
>>>>>>> df784e96e7979aaa4320284c087d7036dce67c28
|
||||
if unet_traced_filename.exists():
|
||||
CONSOLE.print("Loading traced UNet.")
|
||||
unet_traced = torch.jit.load(unet_traced_filename)
|
||||
@@ -102,16 +109,26 @@ class StableDiffusion(nn.Module):
|
||||
self.in_channels = pipe.unet.in_channels
|
||||
self.device = pipe.unet.device
|
||||
|
||||
<<<<<<< HEAD
|
||||
def forward(self, latent_model_input, t, encoder_hidden_states):
|
||||
=======
|
||||
def forward(self, latent_model_input, t, encoder_hidden_states): # pylint: disable=no-self-use
|
||||
"""Forward pass"""
|
||||
>>>>>>> df784e96e7979aaa4320284c087d7036dce67c28
|
||||
sample = unet_traced(latent_model_input, t, encoder_hidden_states)[0]
|
||||
return UNet2DConditionOutput(sample=sample)
|
||||
|
||||
self.unet = TracedUNet()
|
||||
del pipe.unet
|
||||
else:
|
||||
<<<<<<< HEAD
|
||||
CONSOLE.print(
|
||||
"Warning: Loading UNet without JIT acceleration. Run trace_stablediff.py in the scripts folder for a speedup!"
|
||||
)
|
||||
=======
|
||||
CONSOLE.print("[bold yellow] Warning: Loading UNet without JIT acceleration.")
|
||||
CONSOLE.print(r"Run [yellow]python scripts/generative/trace_stable_diffusion.py[/yellow] for a speedup!")
|
||||
>>>>>>> df784e96e7979aaa4320284c087d7036dce67c28
|
||||
self.unet = pipe.unet
|
||||
self.unet.to(memory_format=torch.channels_last)
|
||||
|
||||
@@ -125,11 +142,9 @@ class StableDiffusion(nn.Module):
|
||||
self, prompt: Union[str, List[str]], negative_prompt: Union[str, List[str]]
|
||||
) -> TensorType[2, "max_length", "embed_dim"]:
|
||||
"""Get text embeddings for prompt and negative prompt
|
||||
|
||||
Args:
|
||||
prompt: Prompt text
|
||||
negative_prompt: Negative prompt text
|
||||
|
||||
Returns:
|
||||
Text embeddings
|
||||
"""
|
||||
@@ -166,12 +181,10 @@ class StableDiffusion(nn.Module):
|
||||
guidance_scale: float = 100.0,
|
||||
) -> Tuple[torch.Tensor, TensorType["BS", 4, "H", "W"], TensorType["BS", 4, "H", "W"]]:
|
||||
"""Score Distilation Sampling loss proposed in DreamFusion paper (https://dreamfusion3d.github.io/)
|
||||
|
||||
Args:
|
||||
text_embeddings: Text embeddings
|
||||
image: Rendered image
|
||||
guidance_scale: How much to weigh the guidance
|
||||
|
||||
Returns:
|
||||
A tuple of Loss, latent, and gradient
|
||||
"""
|
||||
@@ -212,7 +225,6 @@ class StableDiffusion(nn.Module):
|
||||
latents: Optional[TensorType["BS", 4, "H", "W"]] = None,
|
||||
) -> TensorType["BS", 4, "H", "W"]:
|
||||
"""Produce latents for a given text embedding
|
||||
|
||||
Args:
|
||||
text_embeddings: Text embeddings
|
||||
height: Height of the image
|
||||
@@ -220,7 +232,6 @@ class StableDiffusion(nn.Module):
|
||||
num_inference_steps: Number of inference steps
|
||||
guidance_scale: How much to weigh the guidance
|
||||
latents: Latents to start with
|
||||
|
||||
Returns:
|
||||
Latents
|
||||
"""
|
||||
@@ -240,7 +251,9 @@ class StableDiffusion(nn.Module):
|
||||
|
||||
# predict the noise residual
|
||||
with torch.no_grad():
|
||||
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings)["sample"]
|
||||
noise_pred = self.unet(
|
||||
latent_model_input, t.to(self.device), encoder_hidden_states=text_embeddings
|
||||
).sample
|
||||
|
||||
# perform guidance
|
||||
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
||||
@@ -252,10 +265,8 @@ class StableDiffusion(nn.Module):
|
||||
|
||||
def latents_to_img(self, latents: TensorType["BS", 4, "H", "W"]) -> TensorType["BS", 3, "H", "W"]:
|
||||
"""Convert latents to images
|
||||
|
||||
Args:
|
||||
latents: Latents to convert
|
||||
|
||||
Returns:
|
||||
Images
|
||||
"""
|
||||
@@ -271,10 +282,8 @@ class StableDiffusion(nn.Module):
|
||||
|
||||
def imgs_to_latent(self, imgs: TensorType["BS", 3, "H", "W"]) -> TensorType["BS", 4, "H", "W"]:
|
||||
"""Convert images to latents
|
||||
|
||||
Args:
|
||||
imgs: Images to convert
|
||||
|
||||
Returns:
|
||||
Latents
|
||||
"""
|
||||
@@ -294,14 +303,12 @@ class StableDiffusion(nn.Module):
|
||||
latents=None,
|
||||
) -> np.ndarray:
|
||||
"""Generate an images from a prompts.
|
||||
|
||||
Args:
|
||||
prompts: The prompt to generate an image from.
|
||||
negative_prompts: The negative prompt to generate an image from.
|
||||
num_inference_steps: The number of inference steps to perform.
|
||||
guidance_scale: The scale of the guidance.
|
||||
latents: The latents to start from, defaults to random.
|
||||
|
||||
Returns:
|
||||
The generated image.
|
||||
"""
|
||||
@@ -328,14 +335,12 @@ class StableDiffusion(nn.Module):
|
||||
self, prompts, negative_prompts="", num_inference_steps=50, guidance_scale=7.5, latents=None
|
||||
) -> np.ndarray:
|
||||
"""Generate an image from a prompt.
|
||||
|
||||
Args:
|
||||
prompts: The prompt to generate an image from.
|
||||
negative_prompts: The negative prompt to generate an image from.
|
||||
num_inference_steps: The number of inference steps to perform.
|
||||
guidance_scale: The scale of the guidance.
|
||||
latents: The latents to start from, defaults to random.
|
||||
|
||||
Returns:
|
||||
The generated image.
|
||||
"""
|
||||
@@ -346,7 +351,6 @@ def generate_image(
|
||||
prompt: str, negative: str = "", seed: int = 0, steps: int = 50, save_path: Path = Path("test_sd.png")
|
||||
):
|
||||
"""Generate an image from a prompt using Stable Diffusion.
|
||||
|
||||
Args:
|
||||
prompt: The prompt to use.
|
||||
negative: The negative prompt to use.
|
||||
|
||||
Reference in new issue
Block a user