Instructions to use Phani0404/nova with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use Phani0404/nova with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Phani0404/nova", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
clean up
Browse filesFiles changed (4)
- README.md+8-1
- main.py+13-3
- mvdream/models.py+13-25
- mvdream/util.py-196
11 unmodified lines1213141516171811 unmodified linespython convert_mvdream_to_diffusers.py --checkpoint_path ./sd-v2.1-base-4view.pt --dump_path ./weights --original_config_file ./sd-v2-base.yaml --half --to_safetensors --test```### run pipeline```pythonimport torchimport kiui11 unmodified lines121314151617181920212223242511 unmodified linespython convert_mvdream_to_diffusers.py --checkpoint_path ./sd-v2.1-base-4view.pt --dump_path ./weights --original_config_file ./sd-v2-base.yaml --half --to_safetensors --test```### usageexample:```bashpython main.py "a cute owl"```detailed usage:```pythonimport torchimport kiui
1234567891011import torchimport kiuifrom mvdream.pipeline_mvdream import MVDreamStableDiffusionPipelinepipe = MVDreamStableDiffusionPipeline.from_pretrained('./weights', torch_dtype=torch.float16)pipe = pipe.to("cuda")prompt = "a photo of an astronaut riding a horse on mars"image = pipe(prompt)kiui.vis.plot_image(image)123456789101112131415161718192021import torchimport kiuiimport numpy as npimport argparsefrom mvdream.pipeline_mvdream import MVDreamStableDiffusionPipelinepipe = MVDreamStableDiffusionPipeline.from_pretrained('./weights', torch_dtype=torch.float16)pipe = pipe.to("cuda")parser = argparse.ArgumentParser(description='MVDream')parser.add_argument('prompt', type=str, default="a cute owl 3d model")args = parser.parse_args()while True:image = pipe(args.prompt)grid = np.concatenate([np.concatenate([image[0], image[2]], axis=0),np.concatenate([image[1], image[3]], axis=0),], axis=1)kiui.vis.plot_image(grid)
9 unmodified lines1011121314151617181936 unmodified lines56575859606162155 unmodified lines21821922022122222322411 unmodified lines23623723823924024124224324424524624724861 unmodified lines310311312313314315316101 unmodified lines418419420421422423424425426427428429430431432433111 unmodified lines5455465475485495505515525535545555565575585595605615 unmodified lines567568569570571572573574575146 unmodified lines7227237247257267277287297307317327337349 unmodified linesfrom .util import (checkpoint,conv_nd,linear,avg_pool_nd,zero_module,normalization,timestep_embedding,)from .attention import SpatialTransformer, SpatialTransformer3D36 unmodified linesadm_in_channels=None,camera_dim=None,):super().__init__()self.unet: MultiViewUNetModel = MultiViewUNetModel(image_size=image_size,in_channels=in_channels,model_channels=model_channels,155 unmodified linesself.use_scale_shift_norm = use_scale_shift_normself.in_layers = nn.Sequential(normalization(channels),nn.SiLU(),conv_nd(dims, channels, self.out_channels, 3, padding=1),)11 unmodified linesself.emb_layers = nn.Sequential(nn.SiLU(),linear(emb_channels,2 * self.out_channels if use_scale_shift_norm else self.out_channels,),)self.out_layers = nn.Sequential(normalization(self.out_channels),nn.SiLU(),nn.Dropout(p=dropout),zero_module(conv_nd(dims, self.out_channels, self.out_channels, 3, padding=1)),61 unmodified linesassert (channels % num_head_channels == 0), f"q,k,v channels {channels} is not divisible by num_head_channels {num_head_channels}"self.num_heads = channels // num_head_channelsself.use_checkpoint = use_checkpointself.norm = normalization(channels)self.qkv = conv_nd(1, channels, channels * 3, 1)if use_new_attention_order:# split qkv before split heads101 unmodified linesreturn count_flops_attn(model, _x, y)class Timestep(nn.Module):def __init__(self, dim):super().__init__()self.dim = dimdef forward(self, t):return timestep_embedding(t, self.dim)class MultiViewUNetModel(nn.Module):"""The full multi-view UNet model with attention, timestep embedding and camera embedding.111 unmodified linestime_embed_dim = model_channels * 4self.time_embed = nn.Sequential(linear(model_channels, time_embed_dim),nn.SiLU(),linear(time_embed_dim, time_embed_dim),)if camera_dim is not None:time_embed_dim = model_channels * 4self.camera_embed = nn.Sequential(linear(camera_dim, time_embed_dim),nn.SiLU(),linear(time_embed_dim, time_embed_dim),)if self.num_classes is not None:5 unmodified lineselif self.num_classes == "sequential":assert adm_in_channels is not Noneself.label_emb = nn.Sequential(nn.Sequential(linear(adm_in_channels, time_embed_dim),nn.SiLU(),linear(time_embed_dim, time_embed_dim),))else:raise ValueError()146 unmodified linesself._feature_size += chself.out = nn.Sequential(normalization(ch),nn.SiLU(),zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)),)if self.predict_codebook_ids:self.id_predictor = nn.Sequential(normalization(ch),conv_nd(dims, model_channels, n_embed, 1),#nn.LogSoftmax(dim=1) # change to cross_entropy and produce non-normalized logits)9 unmodified lines101112131415161736 unmodified lines54555657585960155 unmodified lines21621721821922022122211 unmodified lines23423523623723823924024124224324424524661 unmodified lines308309310311312313314101 unmodified lines416417418419420421111 unmodified lines5335345355365375385395405415425435445455465475485495 unmodified lines555556557558559560561562563146 unmodified lines7107117127137147157167177187197207217229 unmodified linesfrom .util import (checkpoint,conv_nd,avg_pool_nd,zero_module,timestep_embedding,)from .attention import SpatialTransformer, SpatialTransformer3D36 unmodified linesadm_in_channels=None,camera_dim=None,):super().__init__()self.unet = MultiViewUNetModel(image_size=image_size,in_channels=in_channels,model_channels=model_channels,155 unmodified linesself.use_scale_shift_norm = use_scale_shift_normself.in_layers = nn.Sequential(nn.GroupNorm(32, channels),nn.SiLU(),conv_nd(dims, channels, self.out_channels, 3, padding=1),)11 unmodified linesself.emb_layers = nn.Sequential(nn.SiLU(),nn.Linear(emb_channels,2 * self.out_channels if use_scale_shift_norm else self.out_channels,),)self.out_layers = nn.Sequential(nn.GroupNorm(32, self.out_channels),nn.SiLU(),nn.Dropout(p=dropout),zero_module(conv_nd(dims, self.out_channels, self.out_channels, 3, padding=1)),61 unmodified linesassert (channels % num_head_channels == 0), f"q,k,v channels {channels} is not divisible by num_head_channels {num_head_channels}"self.num_heads = channels // num_head_channelsself.use_checkpoint = use_checkpointself.norm = nn.GroupNorm(32, channels)self.qkv = conv_nd(1, channels, channels * 3, 1)if use_new_attention_order:# split qkv before split heads101 unmodified linesreturn count_flops_attn(model, _x, y)class MultiViewUNetModel(nn.Module):"""The full multi-view UNet model with attention, timestep embedding and camera embedding.111 unmodified linestime_embed_dim = model_channels * 4self.time_embed = nn.Sequential(nn.Linear(model_channels, time_embed_dim),nn.SiLU(),nn.Linear(time_embed_dim, time_embed_dim),)if camera_dim is not None:time_embed_dim = model_channels * 4self.camera_embed = nn.Sequential(nn.Linear(camera_dim, time_embed_dim),nn.SiLU(),nn.Linear(time_embed_dim, time_embed_dim),)if self.num_classes is not None:5 unmodified lineselif self.num_classes == "sequential":assert adm_in_channels is not Noneself.label_emb = nn.Sequential(nn.Sequential(nn.Linear(adm_in_channels, time_embed_dim),nn.SiLU(),nn.Linear(time_embed_dim, time_embed_dim),))else:raise ValueError()146 unmodified linesself._feature_size += chself.out = nn.Sequential(nn.GroupNorm(32, ch),nn.SiLU(),zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)),)if self.predict_codebook_ids:self.id_predictor = nn.Sequential(nn.GroupNorm(32, ch),conv_nd(dims, model_channels, n_embed, 1),#nn.LogSoftmax(dim=1) # change to cross_entropy and produce non-normalized logits)
9 unmodified lines10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414581 unmodified lines2272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702717 unmodified lines2792802812822832842852862872882892902915 unmodified lines2972982993003013023033043053063073083093103113123133143153163173183193209 unmodified linesimport mathimport torchimport torch.nn as nnimport numpy as npimport importlibfrom einops import repeatfrom typing import Anydef instantiate_from_config(config):if not "target" in config:if config == '__is_first_stage__':return Noneelif config == "__is_unconditional__":return Noneraise KeyError("Expected key `target` to instantiate.")return get_obj_from_str(config["target"])(**config.get("params", dict()))def get_obj_from_str(string, reload=False):module, cls = string.rsplit(".", 1)if reload:module_imp = importlib.import_module(module)importlib.reload(module_imp)return getattr(importlib.import_module(module, package=None), cls)def make_beta_schedule(schedule,n_timestep,linear_start=1e-4,linear_end=2e-2,cosine_s=8e-3):if schedule == "linear":betas = (torch.linspace(linear_start**0.5,linear_end**0.5,n_timestep,dtype=torch.float64)**2)elif schedule == "cosine":timesteps = (torch.arange(n_timestep + 1, dtype=torch.float64) / n_timestep +cosine_s)alphas = timesteps / (1 + cosine_s) * np.pi / 2alphas = torch.cos(alphas).pow(2)alphas = alphas / alphas[0]betas = 1 - alphas[1:] / alphas[:-1]betas = np.clip(betas, a_min=0, a_max=0.999)elif schedule == "sqrt_linear":betas = torch.linspace(linear_start,linear_end,n_timestep,dtype=torch.float64)elif schedule == "sqrt":betas = torch.linspace(linear_start,linear_end,n_timestep,dtype=torch.float64)**0.5else:raise ValueError(f"schedule '{schedule}' unknown.")return betas.numpy() # type: ignoredef make_ddim_timesteps(ddim_discr_method,num_ddim_timesteps,num_ddpm_timesteps,verbose=True):if ddim_discr_method == 'uniform':c = num_ddpm_timesteps // num_ddim_timestepsddim_timesteps = np.asarray(list(range(0, num_ddpm_timesteps, c)))elif ddim_discr_method == 'quad':ddim_timesteps = ((np.linspace(0, np.sqrt(num_ddpm_timesteps * .8),num_ddim_timesteps))**2).astype(int)else:raise NotImplementedError(f'There is no ddim discretization method called "{ddim_discr_method}"')# assert ddim_timesteps.shape[0] == num_ddim_timesteps# add one to get the final alpha values right (the ones from first scale to data during sampling)steps_out = ddim_timesteps + 1if verbose:print(f'Selected timesteps for ddim sampler: {steps_out}')return steps_outdef make_ddim_sampling_parameters(alphacums,ddim_timesteps,eta,verbose=True):# select alphas for computing the variance schedulealphas = alphacums[ddim_timesteps]alphas_prev = np.asarray([alphacums[0]] +alphacums[ddim_timesteps[:-1]].tolist())# according the the formula provided in https://arxiv.org/abs/2010.02502sigmas = eta * np.sqrt((1 - alphas_prev) / (1 - alphas) * (1 - alphas / alphas_prev))if verbose:print(f'Selected alphas for ddim sampler: a_t: {alphas}; a_(t-1): {alphas_prev}')print(f'For the chosen value of eta, which is {eta}, 'f'this results in the following sigma_t schedule for ddim sampler {sigmas}')return sigmas, alphas, alphas_prevdef betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999):"""Create a beta schedule that discretizes the given alpha_t_bar function,which defines the cumulative product of (1-beta) over time from t = [0,1].:param num_diffusion_timesteps: the number of betas to produce.:param alpha_bar: a lambda that takes an argument t from 0 to 1 andproduces the cumulative product of (1-beta) up to thatpart of the diffusion process.:param max_beta: the maximum beta to use; use values lower than 1 toprevent singularities."""betas = []for i in range(num_diffusion_timesteps):t1 = i / num_diffusion_timestepst2 = (i + 1) / num_diffusion_timestepsbetas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta))return np.array(betas)def extract_into_tensor(a, t, x_shape):b, *_ = t.shapeout = a.gather(-1, t)return out.reshape(b, *((1, ) * (len(x_shape) - 1)))def checkpoint(func, inputs, params, flag):"""81 unmodified linesp.detach().zero_()return moduledef scale_module(module, scale):"""Scale the parameters of a module and return it."""for p in module.parameters():p.detach().mul_(scale)return moduledef mean_flat(tensor):"""Take the mean over all non-batch dimensions."""return tensor.mean(dim=list(range(1, len(tensor.shape))))def normalization(channels):"""Make a standard normalization layer.:param channels: number of input channels.:return: an nn.Module for normalization."""return GroupNorm32(32, channels)# PyTorch 1.7 has SiLU, but we support PyTorch 1.5.class SiLU(nn.Module):def forward(self, x):return x * torch.sigmoid(x)class GroupNorm32(nn.GroupNorm):def forward(self, x):return super().forward(x)def conv_nd(dims, *args, **kwargs):"""Create a 1D, 2D, or 3D convolution module.7 unmodified linesraise ValueError(f"unsupported dimensions: {dims}")def linear(*args, **kwargs):"""Create a linear module."""return nn.Linear(*args, **kwargs)def avg_pool_nd(dims, *args, **kwargs):"""Create a 1D, 2D, or 3D average pooling module.5 unmodified lineselif dims == 3:return nn.AvgPool3d(*args, **kwargs)raise ValueError(f"unsupported dimensions: {dims}")class HybridConditioner(nn.Module):def __init__(self, c_concat_config, c_crossattn_config):super().__init__()self.concat_conditioner: Any = instantiate_from_config(c_concat_config)self.crossattn_conditioner: Any = instantiate_from_config(c_crossattn_config)def forward(self, c_concat, c_crossattn):c_concat = self.concat_conditioner(c_concat)c_crossattn = self.crossattn_conditioner(c_crossattn)return {'c_concat': [c_concat], 'c_crossattn': [c_crossattn]}def noise_like(shape, device, repeat=False):repeat_noise = lambda: torch.randn((1, *shape[1:]), device=device).repeat(shape[0], *((1, ) * (len(shape) - 1)))noise = lambda: torch.randn(shape, device=device)return repeat_noise() if repeat else noise()9 unmodified lines1011121314151681 unmodified lines98991001011021037 unmodified lines1111121131141151165 unmodified lines1221231249 unmodified linesimport mathimport torchimport torch.nn as nnfrom einops import repeatdef checkpoint(func, inputs, params, flag):"""81 unmodified linesp.detach().zero_()return moduledef conv_nd(dims, *args, **kwargs):"""Create a 1D, 2D, or 3D convolution module.7 unmodified linesraise ValueError(f"unsupported dimensions: {dims}")def avg_pool_nd(dims, *args, **kwargs):"""Create a 1D, 2D, or 3D average pooling module.5 unmodified lineselif dims == 3:return nn.AvgPool3d(*args, **kwargs)raise ValueError(f"unsupported dimensions: {dims}")
README.md+8-1
main.py+13-3
mvdream/models.py+13-25
mvdream/util.py-196