File size: 1,559 Bytes
2114012
576457a
 
 
2114012
 
 
 
 
 
 
 
 
 
576457a
 
2114012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576457a
 
 
 
 
 
ab5c1d1
576457a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import numpy as np
import torch
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
from huggingface_hub import hf_hub_download

CONFIG_NAME = "config.json"
revision = None
cache_dir = None
force_download = False
proxies = None
resume_download = False
local_files_only = False
token = None

def carga_modelo(nombre_modelo="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
    # Load the config
    config_file = hf_hub_download(
        repo_id=str(nombre_modelo),
        filename=CONFIG_NAME,
        revision=revision,
        cache_dir=cache_dir,
        force_download=force_download,
        proxies=proxies,
        resume_download=resume_download,
        token=token,
        local_files_only=local_files_only,
    )
    with open(config_file, "r", encoding="utf-8") as f:
        config = json.load(f)
    
    gan = LightweightGAN(latent_dim=256, image_size=512)
    gan = gan._from_pretrained(
        model_id=str(nombre_modelo),
        revision=revision,
        cache_dir=cache_dir,
        force_download=force_download,
        proxies=proxies,
        resume_download=resume_download,
        local_files_only=local_files_only,
        token=token,
        use_auth_token=False,
        config=config,  # usually in **model_kwargs
    )
    gan.eval()
    return gan

def genera(gan, batch_size=1):
    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