danhergir commited on
Commit
f379395
1 Parent(s): 1c28fa9
Files changed (1) hide show
  1. utils.py +68 -5
utils.py CHANGED
@@ -1,15 +1,78 @@
 
 
1
  import numpy as np
2
  import torch
3
- import os
4
  from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- def load_model(model_name='ceyda/butterfly_cropped_uniq1K_512', model_version=None):
7
- gan = LightweightGAN.from_pretrained(model_name, version=model_version, use_auth_token=os.environ["access_token"])
8
  gan.eval()
9
  return gan
10
 
 
11
  def generate(gan, batch_size=1):
 
 
 
 
 
 
 
 
 
 
12
  with torch.no_grad():
13
  ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
14
- ims = ims.permute(0,2,3,1).detach().cpu().numpy().astype(np.uint8)
15
- 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
+ CONFIG_NAME = "config.json"
9
+ revision = None
10
+ cache_dir = None
11
+ force_download = False
12
+ proxies = None
13
+ resume_download = False
14
+ local_files_only = False
15
+ token = None
16
+
17
+
18
+ def load_model(model_name="ceyda/butterfly_cropped_uniq1K_512"):
19
+ """
20
+ Loads a pre-trained LightweightGAN model from Hugging Face Model Hub.
21
+
22
+ Args:
23
+ model_name (str): The name of the pre-trained model to load. Defaults to "ceyda/butterfly_cropped_uniq1K_512".
24
+ model_version (str): The version of the pre-trained model to load. Defaults to None.
25
+
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
 
63
+
64
  def generate(gan, batch_size=1):
65
+ """
66
+ Generates images using the given GAN model.
67
+
68
+ Args:
69
+ gan (nn.Module): The GAN model to use for generating images.
70
+ batch_size (int, optional): The number of images to generate in each batch. Defaults to 1.
71
+
72
+ Returns:
73
+ numpy.ndarray: A numpy array of generated images.
74
+ """
75
  with torch.no_grad():
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