mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
Merge branch 'generative' of https://github.com/plenoptix/nerfactory into generative
This commit is contained in:
commit
7215766c51
3 files changed
+31
-17
No files matched your search
@@ -117,7 +117,7 @@ class StableDiffusion(nn.Module):
|
|||||||
# diffused_img = self.latents_to_img(latents_noisy - noise_pred)
|
# diffused_img = self.latents_to_img(latents_noisy - noise_pred)
|
||||||
# diffused_img = diffused_img.detach().cpu().permute(0, 2, 3, 1).numpy().reshape((512, 512, 3))
|
# diffused_img = diffused_img.detach().cpu().permute(0, 2, 3, 1).numpy().reshape((512, 512, 3))
|
||||||
# diffused_img = (diffused_img * 255).round().astype("uint8")
|
# diffused_img = (diffused_img * 255).round().astype("uint8")
|
||||||
# plt.imsave("sd_img.png", diffused_img)
|
# plt.imsave("sdimg.png", diffused_img)
|
||||||
|
|
||||||
# w(t), sigma_t^2
|
# w(t), sigma_t^2
|
||||||
w = 1 - self.alphas[t]
|
w = 1 - self.alphas[t]
|
||||||
|
|||||||
@@ -66,12 +66,14 @@ class DreamFusionModelConfig(ModelConfig):
|
|||||||
"""Number of samples in field evaluation"""
|
"""Number of samples in field evaluation"""
|
||||||
orientation_loss_mult: float = 0.01
|
orientation_loss_mult: float = 0.01
|
||||||
"""Orientation loss multipier on computed normals."""
|
"""Orientation loss multipier on computed normals."""
|
||||||
pred_normal_loss_mult: float = 0.0003
|
pred_normal_loss_mult: float = 0.001
|
||||||
"""Predicted normal loss multiplier."""
|
"""Predicted normal loss multiplier."""
|
||||||
random_light_source: bool = True
|
random_light_source: bool = True
|
||||||
"""Randomizes light source per output."""
|
"""Randomizes light source per output."""
|
||||||
initialize_density: bool = True
|
initialize_density: bool = True
|
||||||
"""Initialize density in center of scene."""
|
"""Initialize density in center of scene."""
|
||||||
|
init_density_strength: float = 50.0
|
||||||
|
"""Initial strength of center density"""
|
||||||
sphere_collider: bool = True
|
sphere_collider: bool = True
|
||||||
"""Use spherical collider instead of box"""
|
"""Use spherical collider instead of box"""
|
||||||
|
|
||||||
@@ -183,9 +185,13 @@ class DreamFusionModel(Model):
|
|||||||
field_outputs = self.field(ray_samples_uniform, compute_normals=True)
|
field_outputs = self.field(ray_samples_uniform, compute_normals=True)
|
||||||
|
|
||||||
density = field_outputs[FieldHeadNames.DENSITY]
|
density = field_outputs[FieldHeadNames.DENSITY]
|
||||||
if self.initialize_density:
|
if self.initialize_density and self.training:
|
||||||
pos = ray_samples_uniform.frustums.get_positions()
|
pos = ray_samples_uniform.frustums.get_positions()
|
||||||
density_blob = 50 * self.density_strength * torch.exp(-torch.norm(pos, dim=-1) / (2 * 0.04))[..., None]
|
density_blob = (
|
||||||
|
self.config.init_density_strength
|
||||||
|
* self.density_strength
|
||||||
|
* torch.exp(-torch.norm(pos, dim=-1) / (2 * 0.04))[..., None]
|
||||||
|
)
|
||||||
density += density_blob
|
density += density_blob
|
||||||
|
|
||||||
weights = ray_samples_uniform.get_weights(density)
|
weights = ray_samples_uniform.get_weights(density)
|
||||||
@@ -212,7 +218,7 @@ class DreamFusionModel(Model):
|
|||||||
pred_normals = self.renderer_normals(field_outputs[FieldHeadNames.PRED_NORMALS], weights=weights) # .detach()
|
pred_normals = self.renderer_normals(field_outputs[FieldHeadNames.PRED_NORMALS], weights=weights) # .detach()
|
||||||
|
|
||||||
# lambertian shading
|
# lambertian shading
|
||||||
if self.config.random_light_source:
|
if self.config.random_light_source and self.training: # and if training
|
||||||
light_d = ray_bundle.origins[0] + torch.randn(3, dtype=torch.float).to(normals)
|
light_d = ray_bundle.origins[0] + torch.randn(3, dtype=torch.float).to(normals)
|
||||||
else:
|
else:
|
||||||
light_d = ray_bundle.origins[0]
|
light_d = ray_bundle.origins[0]
|
||||||
|
|||||||
@@ -25,12 +25,9 @@ class DreamfusionPipelineConfig(VanillaPipelineConfig):
|
|||||||
"""specifies the datamanager config"""
|
"""specifies the datamanager config"""
|
||||||
prompt: str = "A high quality photo of a pineapple"
|
prompt: str = "A high quality photo of a pineapple"
|
||||||
"""prompt for stable dreamfusion"""
|
"""prompt for stable dreamfusion"""
|
||||||
|
|
||||||
location_based_prompting: bool = True
|
location_based_prompting: bool = True
|
||||||
"""enables location based prompting"""
|
"""enables location based prompting"""
|
||||||
opacity_penalty: bool = True
|
|
||||||
"""enables penalty to encourage transparent scenes"""
|
|
||||||
target_transmittance: float = 0.25
|
|
||||||
"""target transmittance for opacity penalty"""
|
|
||||||
side_prompt: str = ", side view"
|
side_prompt: str = ", side view"
|
||||||
"""appended to prompt for side view"""
|
"""appended to prompt for side view"""
|
||||||
front_prompt: str = ", front view"
|
front_prompt: str = ", front view"
|
||||||
@@ -38,6 +35,14 @@ class DreamfusionPipelineConfig(VanillaPipelineConfig):
|
|||||||
back_prompt: str = ", back view"
|
back_prompt: str = ", back view"
|
||||||
"""appended to prompt for back view"""
|
"""appended to prompt for back view"""
|
||||||
|
|
||||||
|
opacity_penalty: bool = True
|
||||||
|
"""enables penalty to encourage transparent scenes"""
|
||||||
|
target_transmittance: float = 0.5
|
||||||
|
"""target transmittance for opacity penalty"""
|
||||||
|
|
||||||
|
guidance_scale: float = 100
|
||||||
|
"""guidance scale for sds loss"""
|
||||||
|
|
||||||
|
|
||||||
class DreamfusionPipeline(VanillaPipeline):
|
class DreamfusionPipeline(VanillaPipeline):
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -51,11 +56,12 @@ class DreamfusionPipeline(VanillaPipeline):
|
|||||||
super().__init__(config, device, test_mode, world_size, local_rank)
|
super().__init__(config, device, test_mode, world_size, local_rank)
|
||||||
self.generative = True
|
self.generative = True
|
||||||
self.sd = StableDiffusion(device)
|
self.sd = StableDiffusion(device)
|
||||||
self.base_text_embedding = self.sd.get_text_embeds(config.prompt, "")
|
|
||||||
if config.location_based_prompting:
|
if config.location_based_prompting:
|
||||||
self.front_text_embedding = self.sd.get_text_embeds(f"{config.prompt}{config.front_prompt}", "")
|
self.front_text_embedding = self.sd.get_text_embeds(f"{config.prompt}{config.front_prompt}", "")
|
||||||
self.side_text_embedding = self.sd.get_text_embeds(f"{config.prompt}{config.side_prompt}", "")
|
self.side_text_embedding = self.sd.get_text_embeds(f"{config.prompt}{config.side_prompt}", "")
|
||||||
self.back_text_embedding = self.sd.get_text_embeds(f"{config.prompt}{config.back_prompt}", "")
|
self.back_text_embedding = self.sd.get_text_embeds(f"{config.prompt}{config.back_prompt}", "")
|
||||||
|
else:
|
||||||
|
self.base_text_embedding = self.sd.get_text_embeds(config.prompt, "")
|
||||||
|
|
||||||
@profiler.time_function
|
@profiler.time_function
|
||||||
def custom_step(self, step: int, grad_scaler: GradScaler, optimizers: Optimizers):
|
def custom_step(self, step: int, grad_scaler: GradScaler, optimizers: Optimizers):
|
||||||
@@ -78,13 +84,13 @@ class DreamfusionPipeline(VanillaPipeline):
|
|||||||
accumulation = np.clip(accumulation, 0.0, 1.0)
|
accumulation = np.clip(accumulation, 0.0, 1.0)
|
||||||
plt.imsave("nerf_accumulation.jpg", accumulation)
|
plt.imsave("nerf_accumulation.jpg", accumulation)
|
||||||
|
|
||||||
background = model_outputs["background"].view(64, 64, 3).detach().cpu().numpy()
|
# background = model_outputs["background"].view(64, 64, 3).detach().cpu().numpy()
|
||||||
background = np.clip(background, 0.0, 1.0)
|
# background = np.clip(background, 0.0, 1.0)
|
||||||
plt.imsave("nerf_background.jpg", background)
|
# plt.imsave("nerf_background.jpg", background)
|
||||||
|
|
||||||
shaded = model_outputs["shaded"].view(64, 64, 3).detach().cpu().numpy()
|
# shaded = model_outputs["shaded"].view(64, 64, 3).detach().cpu().numpy()
|
||||||
shaded = np.clip(shaded, 0.0, 1.0)
|
# shaded = np.clip(shaded, 0.0, 1.0)
|
||||||
plt.imsave("nerf_textureless.jpg", shaded)
|
# plt.imsave("nerf_textureless.jpg", shaded)
|
||||||
|
|
||||||
if self.config.location_based_prompting:
|
if self.config.location_based_prompting:
|
||||||
if batch["central"] > 315 or batch["central"] <= 45:
|
if batch["central"] > 315 or batch["central"] <= 45:
|
||||||
@@ -98,7 +104,9 @@ class DreamfusionPipeline(VanillaPipeline):
|
|||||||
else:
|
else:
|
||||||
text_embedding = self.base_text_embedding
|
text_embedding = self.base_text_embedding
|
||||||
|
|
||||||
sds_loss, latents, grad = self.sd.sds_loss(text_embedding, train_output)
|
sds_loss, latents, grad = self.sd.sds_loss(
|
||||||
|
text_embedding, train_output, guidance_scale=self.config.guidance_scale
|
||||||
|
)
|
||||||
# TODO: opacity penalty using transmittance, not accumultation
|
# TODO: opacity penalty using transmittance, not accumultation
|
||||||
if self.config.opacity_penalty:
|
if self.config.opacity_penalty:
|
||||||
accum_mean = np.mean(1.0 - accumulation)
|
accum_mean = np.mean(1.0 - accumulation)
|
||||||
|
|||||||
Reference in new issue
Block a user