RaymundoSGlz commited on
Commit
17fb818
1 Parent(s): 6986302

Update utils.py

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