Question: Generate image using SDS
(Awesome work here -- I saw there is already a paper out based on your work: latent-nerf)
As a sanity check I'm trying generate an image with "differentiable image parameterization" (DIP) and the SDS algorithm here's the MWE:
import math
from tqdm import tqdm
import torch
import torch.nn as nn
from nerf.sd import StableDiffusion, seed_everything
from torch.optim.lr_scheduler import LambdaLR
import matplotlib.pyplot as plt
def get_cosine_schedule_with_warmup(optimizer, num_warmup_steps, num_training_steps, num_cycles: float = 0.5):
def lr_lambda(current_step):
if current_step < num_warmup_steps:
return float(current_step) / float(max(1, num_warmup_steps))
progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps))
return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress)))
return LambdaLR(optimizer, lr_lambda, -1)
device = 'cuda:0'
guidance = StableDiffusion(device)
# limited memory here, don't need the decoder
guidance.vae.decoder = None
prompt = '3D texture of pebbles'
text_embeddings = guidance.get_text_embeds(prompt, '')
guidance.text_encoder.to('cpu')
torch.cuda.empty_cache()
seed_everything(42)
# put parameters approximately in range(0, 1) since this is what `encode_imgs` expects
rgb = nn.Parameter(torch.randn(1, 3, 512, 512, device=device) / 2 + .5)
optimizer = torch.optim.AdamW([rgb], lr=1e-1, weight_decay=0)
num_steps = 5000
scheduler = get_cosine_schedule_with_warmup(optimizer, 100, int(num_steps*1.5))
for step in tqdm(range(num_steps)):
optimizer.zero_grad()
guidance.train_step(text_embeddings, rgb, guidance_scale=100)
optimizer.step()
scheduler.step()
plt.imshow(rgb.detach().clamp(0, 1).squeeze(0).permute(1,2,0).cpu())
plt.axis('off')
plt.show()I ended up with this:
"3D texture of pebbles"

I've tried using sigmoid activation for the image and various learning rates, but the images still come out super saturated.
The DreamFusion authors claim they were able to get similar DIP results as DDPM
SDS produces detail comparable to ancestral sampling, but enables new transfer learning applications because it operates in parameter space.
My question is: were you able to get this working? Or do you have any suggestions/ideas to get quality similar to DDPM?
Source: ashawkey/stable-dreamfusion