autocast
This commit is contained in:
parent
893c351761
commit
62308078cf
1 changed files with 52 additions and 41 deletions
|
@ -9,6 +9,8 @@ from einops import rearrange
|
||||||
from torchvision.utils import make_grid
|
from torchvision.utils import make_grid
|
||||||
import time
|
import time
|
||||||
from pytorch_lightning import seed_everything
|
from pytorch_lightning import seed_everything
|
||||||
|
from torch import autocast
|
||||||
|
from contextlib import contextmanager, nullcontext
|
||||||
|
|
||||||
from ldm.util import instantiate_from_config
|
from ldm.util import instantiate_from_config
|
||||||
from ldm.models.diffusion.ddim import DDIMSampler
|
from ldm.models.diffusion.ddim import DDIMSampler
|
||||||
|
@ -178,6 +180,13 @@ def main():
|
||||||
default=42,
|
default=42,
|
||||||
help="the seed (for reproducible sampling)",
|
help="the seed (for reproducible sampling)",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--precision",
|
||||||
|
type=str,
|
||||||
|
help="evaluate at this precision",
|
||||||
|
choices=["full", "autocast"],
|
||||||
|
default="autocast"
|
||||||
|
)
|
||||||
opt = parser.parse_args()
|
opt = parser.parse_args()
|
||||||
seed_everything(opt.seed)
|
seed_everything(opt.seed)
|
||||||
|
|
||||||
|
@ -217,53 +226,55 @@ def main():
|
||||||
if opt.fixed_code:
|
if opt.fixed_code:
|
||||||
start_code = torch.randn([opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f], device=device)
|
start_code = torch.randn([opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f], device=device)
|
||||||
|
|
||||||
|
precision_scope = autocast if opt.precision=="autocast" else nullcontext
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
with model.ema_scope():
|
with precision_scope("cuda"):
|
||||||
tic = time.time()
|
with model.ema_scope():
|
||||||
all_samples = list()
|
tic = time.time()
|
||||||
for n in trange(opt.n_iter, desc="Sampling"):
|
all_samples = list()
|
||||||
for prompts in tqdm(data, desc="data"):
|
for n in trange(opt.n_iter, desc="Sampling"):
|
||||||
uc = None
|
for prompts in tqdm(data, desc="data"):
|
||||||
if opt.scale != 1.0:
|
uc = None
|
||||||
uc = model.get_learned_conditioning(batch_size * [""])
|
if opt.scale != 1.0:
|
||||||
if isinstance(prompts, tuple):
|
uc = model.get_learned_conditioning(batch_size * [""])
|
||||||
prompts = list(prompts)
|
if isinstance(prompts, tuple):
|
||||||
c = model.get_learned_conditioning(prompts)
|
prompts = list(prompts)
|
||||||
shape = [opt.C, opt.H // opt.f, opt.W // opt.f]
|
c = model.get_learned_conditioning(prompts)
|
||||||
samples_ddim, _ = sampler.sample(S=opt.ddim_steps,
|
shape = [opt.C, opt.H // opt.f, opt.W // opt.f]
|
||||||
conditioning=c,
|
samples_ddim, _ = sampler.sample(S=opt.ddim_steps,
|
||||||
batch_size=opt.n_samples,
|
conditioning=c,
|
||||||
shape=shape,
|
batch_size=opt.n_samples,
|
||||||
verbose=False,
|
shape=shape,
|
||||||
unconditional_guidance_scale=opt.scale,
|
verbose=False,
|
||||||
unconditional_conditioning=uc,
|
unconditional_guidance_scale=opt.scale,
|
||||||
eta=opt.ddim_eta,
|
unconditional_conditioning=uc,
|
||||||
dynamic_threshold=opt.dyn,
|
eta=opt.ddim_eta,
|
||||||
x_T=start_code)
|
dynamic_threshold=opt.dyn,
|
||||||
|
x_T=start_code)
|
||||||
|
|
||||||
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
x_samples_ddim = model.decode_first_stage(samples_ddim)
|
||||||
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
||||||
|
|
||||||
if not opt.skip_save:
|
if not opt.skip_save:
|
||||||
for x_sample in x_samples_ddim:
|
for x_sample in x_samples_ddim:
|
||||||
x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
|
x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
|
||||||
Image.fromarray(x_sample.astype(np.uint8)).save(
|
Image.fromarray(x_sample.astype(np.uint8)).save(
|
||||||
os.path.join(sample_path, f"{base_count:05}.png"))
|
os.path.join(sample_path, f"{base_count:05}.png"))
|
||||||
base_count += 1
|
base_count += 1
|
||||||
all_samples.append(x_samples_ddim)
|
all_samples.append(x_samples_ddim)
|
||||||
|
|
||||||
if not opt.skip_grid:
|
if not opt.skip_grid:
|
||||||
# additionally, save as grid
|
# additionally, save as grid
|
||||||
grid = torch.stack(all_samples, 0)
|
grid = torch.stack(all_samples, 0)
|
||||||
grid = rearrange(grid, 'n b c h w -> (n b) c h w')
|
grid = rearrange(grid, 'n b c h w -> (n b) c h w')
|
||||||
grid = make_grid(grid, nrow=n_rows)
|
grid = make_grid(grid, nrow=n_rows)
|
||||||
|
|
||||||
# to image
|
# to image
|
||||||
grid = 255. * rearrange(grid, 'c h w -> h w c').cpu().numpy()
|
grid = 255. * rearrange(grid, 'c h w -> h w c').cpu().numpy()
|
||||||
Image.fromarray(grid.astype(np.uint8)).save(os.path.join(outpath, f'grid-{grid_count:04}.png'))
|
Image.fromarray(grid.astype(np.uint8)).save(os.path.join(outpath, f'grid-{grid_count:04}.png'))
|
||||||
grid_count += 1
|
grid_count += 1
|
||||||
|
|
||||||
toc = time.time()
|
toc = time.time()
|
||||||
|
|
||||||
print(f"Your samples are ready and waiting for you here: \n{outpath} \n"
|
print(f"Your samples are ready and waiting for you here: \n{outpath} \n"
|
||||||
f"Sampling took {toc - tic}s, i.e. produced {opt.n_iter * opt.n_samples / (toc - tic):.2f} samples/sec."
|
f"Sampling took {toc - tic}s, i.e. produced {opt.n_iter * opt.n_samples / (toc - tic):.2f} samples/sec."
|
||||||
|
|
Loading…
Reference in a new issue