RaymundoSGlz commited on
Commit
32b48df
·
1 Parent(s): 7bfe2c1

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +47 -2
utils.py CHANGED
@@ -1,9 +1,54 @@
 
 
1
  import numpy as np
2
  import torch
3
  from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
 
 
5
 
6
- def carga_modelo(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  """
8
  Loads a pre-trained LightweightGAN model from Hugging Face Model Hub.
9
 
@@ -14,7 +59,6 @@ def carga_modelo(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=
14
  Returns:
15
  LightweightGAN: The loaded pre-trained model.
16
  """
17
- gan = LightweightGAN.from_pretrained(model_name, version=model_version)
18
  gan.eval()
19
  return gan
20
 
@@ -34,3 +78,4 @@ def genera(gan, batch_size=1):
34
  ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
35
  ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8)
36
  return ims
 
 
1
+ import json
2
+
3
  import numpy as np
4
  import torch
5
  from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ model_id = "ceyda/butterfly_cropped_uniq1K_512"
9
+ CONFIG_NAME = "config.json"
10
+ revision = None
11
+ cache_dir = None
12
+ force_download = False
13
+ proxies = None
14
+ resume_download = False
15
+ local_files_only = False
16
+ token = None
17
+
18
+
19
+ # Load the config
20
+ config_file = hf_hub_download(
21
+ repo_id=str(model_id),
22
+ filename=CONFIG_NAME,
23
+ revision=revision,
24
+ cache_dir=cache_dir,
25
+ force_download=force_download,
26
+ proxies=proxies,
27
+ resume_download=resume_download,
28
+ token=token,
29
+ local_files_only=local_files_only,
30
+ )
31
+ with open(config_file, "r", encoding="utf-8") as f:
32
+ config = json.load(f)
33
 
34
+ # Call the _from_pretrained with all the needed arguments
35
+ gan = LightweightGAN(latent_dim=256, image_size=512)
36
 
37
+ gan = gan._from_pretrained(
38
+ model_id=str(model_id),
39
+ revision=revision,
40
+ cache_dir=cache_dir,
41
+ force_download=force_download,
42
+ proxies=proxies,
43
+ resume_download=resume_download,
44
+ local_files_only=local_files_only,
45
+ token=token,
46
+ use_auth_token=False,
47
+ config=config, # usually in **model_kwargs
48
+ )
49
+
50
+
51
+ def carga_modelo(model_name=model_id, model_version=None):
52
  """
53
  Loads a pre-trained LightweightGAN model from Hugging Face Model Hub.
54
 
 
59
  Returns:
60
  LightweightGAN: The loaded pre-trained model.
61
  """
 
62
  gan.eval()
63
  return gan
64
 
 
78
  ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
79
  ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8)
80
  return ims
81
+