def main(conf): device = "cuda:0" if torch.cuda.is_available() else 'cpu' beta_schedule = "linear" beta_start = 1e-4 beta_end = 2e-2 n_timestep = 1000 conf.distributed = dist.get_world_size() > 1 transform = transforms.Compose( [ transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True), ] ) train_set = MultiResolutionDataset( conf.dataset.path, transform, conf.dataset.resolution ) train_sampler = dist.data_sampler( train_set, shuffle=True, distributed=conf.distributed ) train_loader = conf.training.dataloader.make(train_set, sampler=train_sampler) model = UNet( conf.model.in_channel, conf.model.channel, channel_multiplier=conf.model.channel_multiplier, n_res_blocks=conf.model.n_res_blocks, attn_strides=conf.model.attn_strides, dropout=conf.model.dropout, fold=conf.model.fold, ) model = model.to(device) ema = UNet( conf.model.in_channel, conf.model.channel, channel_multiplier=conf.model.channel_multiplier, n_res_blocks=conf.model.n_res_blocks, attn_strides=conf.model.attn_strides, dropout=conf.model.dropout, fold=conf.model.fold, ) ema = ema.to(device) if conf.distributed: model = nn.parallel.DistributedDataParallel( model, device_ids=[dist.get_local_rank()], output_device=dist.get_local_rank(), ) optimizer = conf.training.optimizer.make(model.parameters()) scheduler = conf.training.scheduler.make(optimizer) betas = make_beta_schedule(beta_schedule, beta_start, beta_end, n_timestep) diffusion = GaussianDiffusion(betas).to(device) train(conf, train_loader, model, ema, diffusion, optimizer, scheduler, device)
def __init__(self, beta_strategy, num_diffusion_timesteps, use_timesteps=None, num_classes=1, model_var_type: ModelVarType = ModelVarType.FIXED_SMALL, randflip=True, attention_num_heads=1, attention_resolutions=(16, 8), base_channels=None, channel_mult=None, num_res_blocks=None, scale_shift_norm=True): betas = get_beta_schedule(beta_strategy, num_timesteps=num_diffusion_timesteps) self.original_timesteps = len(betas) self.timestep_map = None if use_timesteps is not None: betas, timestep_map = respace_betas(betas, use_timesteps) self.timestep_map = const_var(timestep_map) self.model_var_type = model_var_type self.diffusion: GaussianDiffusion = GaussianDiffusion( betas=betas, model_var_type=model_var_type) self.num_classes = num_classes self.randflip = randflip self.attention_num_heads = attention_num_heads self.attention_resolutions = attention_resolutions self.base_channels = base_channels self.channel_mult = channel_mult self.num_res_blocks = num_res_blocks self.scale_shift_norm = scale_shift_norm
def main(conf): wandb = None if dist.is_primary() and conf.evaluate.wandb: wandb = load_wandb() wandb.init(project="denoising diffusion") device = "cuda" beta_schedule = "linear" conf.distributed = dist.get_world_size() > 1 transform = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True), ]) train_set = MultiResolutionDataset(conf.dataset.path, transform, conf.dataset.resolution) train_sampler = dist.data_sampler(train_set, shuffle=True, distributed=conf.distributed) train_loader = conf.training.dataloader.make(train_set, sampler=train_sampler) model = conf.model.make() model = model.to(device) ema = conf.model.make() ema = ema.to(device) if conf.distributed: model = nn.parallel.DistributedDataParallel( model, device_ids=[dist.get_local_rank()], output_device=dist.get_local_rank(), ) optimizer = conf.training.optimizer.make(model.parameters()) scheduler = conf.training.scheduler.make(optimizer) if conf.ckpt is not None: ckpt = torch.load(conf.ckpt, map_location=lambda storage, loc: storage) if conf.distributed: model.module.load_state_dict(ckpt["model"]) else: model.load_state_dict(ckpt["model"]) ema.load_state_dict(ckpt["ema"]) betas = conf.diffusion.beta_schedule.make() diffusion = GaussianDiffusion(betas).to(device) train(conf, train_loader, model, ema, diffusion, optimizer, scheduler, device, wandb)
def generate_sample(model): betas = make_beta_schedule('linear', 1e-4, 2e-2, 1000) diffusion = GaussianDiffusion(betas).to('cuda') imgs = p_sample_loop(diffusion, model, [16, 3, 128, 128], 'cuda', capture_every=10) imgs = imgs[1:] id = 0 grid = make_grid(torch.cat([i[id:id + 1] for i in imgs[:-1:4]], 0), nrow=5, normalize=True, range=(-1, 1)) return grid.detach().mul(255).cpu().type(torch.uint8).permute(1, 2, 0).numpy()
import paddle import paddle.nn as nn import paddle.nn.functional as F from model import UNet from diffusion import make_beta_schedule, GaussianDiffusion from config import config conf = config.diffusion betas = make_beta_schedule(**conf.diffusion.beta_schedule) diffusion = GaussianDiffusion(betas) model = UNet(**conf.model) img = paddle.randn([ conf.training.dataloader.batch_size, conf.model.in_channel, conf.dataset.resolution, conf.dataset.resolution ]) time = paddle.randint( 0, conf.diffusion.beta_schedule.n_timestep, (img.shape[0],) ) loss = diffusion.p_loss(model, img, time) print(loss.numpy()) conf = config.improved betas = make_beta_schedule(**conf.diffusion.beta_schedule) diffusion = GaussianDiffusion(betas) model = UNet(**conf.model) img = paddle.randn([ conf.training.dataloader.batch_size,
for i in tqdm(reversed(range(self.num_timesteps)), total=self.num_timesteps): img = self.p_sample( model, img, torch.full((img.shape[0], ), i, dtype=torch.int64).to(device), noise_fn=noise_fn, ) if i % capture_every == 0: imgs.append(img) imgs.append(img) return imgs if __name__ == "__main__": conf = load_config(DiffusionConfig, "config/diffusion.conf", show=False) ckpt = torch.load("ckpt-2400k.pt") model = conf.model.make() model.load_state_dict(ckpt["ema"]) model = model.to("cuda") betas = conf.diffusion.beta_schedule.make() diffusion = GaussianDiffusion(betas).to("cuda") noise = torch.randn([16, 3, 256, 256], device="cuda") imgs = p_sample_loop(diffusion, model, noise, "cuda", capture_every=10) imgs = imgs[1:] save_image(imgs[-1], "sample.png", normalize=True, range=(-1, 1), nrow=4)
num_workers=conf.training.dataloader.num_workers, drop_last=conf.training.dataloader.drop_last, use_shared_memory=False) model = UNet(**conf.model) ema = UNet(**conf.model) ema.eval() clip = nn.ClipGradByNorm(clip_norm=1.0) optimizer_types = { 'adam': paddle.optimizer.Adam, } optimizer = optimizer_types[conf.training.optimizer.type]( parameters=model.parameters(), learning_rate=conf.training.optimizer.lr, grad_clip=clip) scheduler_conf = copy.deepcopy(conf.training.scheduler) scheduler_types = { 'cycle': lr_scheduler.cycle_scheduler, 'step': lr_scheduler.step_scheduler, 'default': lr_scheduler.ConstantScheduler } scheduler_type = scheduler_conf.pop('type', 'default') scheduler = scheduler_types[scheduler_type](optimizer, **scheduler_conf) betas = make_beta_schedule(**conf.diffusion.beta_schedule) diffusion = GaussianDiffusion(betas) train(conf, train_loader, model, ema, diffusion, optimizer, scheduler)