import numpy as np import torch from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN def load_model(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None): model = LightweightGAN.from_pretrained(model_name, version=model_version) model.eval() return model def generate(model, batch_size=1): with torch.no_grad(): ims = model.G(torch.randn(batch_size, model.latent_dim) ).clamp_(0.0, 1.0) * 255 ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8) return ims