import numpy as np import torch from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN def carga_modelo(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None): """ Loads a pre-trained LightweightGAN model from Hugging Face Model Hub. Args: model_name (str): The name of the pre-trained model to load. Defaults to "ceyda/butterfly_cropped_uniq1K_512". model_version (str): The version of the pre-trained model to load. Defaults to None. Returns: LightweightGAN: The loaded pre-trained model. """ gan = LightweightGAN.from_pretrained(model_name, version=model_version) gan.eval() return gan def genera(gan, batch_size=1): """ Generates images using the given GAN model. Args: gan (nn.Module): The GAN model to use for generating images. batch_size (int, optional): The number of images to generate in each batch. Defaults to 1. Returns: numpy.ndarray: A numpy array of generated images. """ with torch.no_grad(): ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255 ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8) return ims