Gustavo Cevallos commited on
Commit ·
bb2f22e
1
Parent(s): 714d9d4
test loading model
Browse files
utils.py
CHANGED
|
@@ -2,13 +2,64 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
|
| 9 |
|
| 10 |
def load_model(model_name="ceyda/butterfly_cropped_uniq1K_512"):
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
gan.eval()
|
| 13 |
return gan
|
| 14 |
|
|
|
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
import json
|
| 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 |
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
|
| 19 |
|
| 20 |
def load_model(model_name="ceyda/butterfly_cropped_uniq1K_512"):
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
Loads a pre-trained LightweightGAN model from Hugging Face Model Hub.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
model_name (str): The name of the pre-trained model to load. Defaults to "ceyda/butterfly_cropped_uniq1K_512".
|
| 27 |
+
model_version (str): The version of the pre-trained model to load. Defaults to None.
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
LightweightGAN: The loaded pre-trained model.
|
| 31 |
+
"""
|
| 32 |
+
# Load the config
|
| 33 |
+
config_file = hf_hub_download(
|
| 34 |
+
repo_id=str(model_name),
|
| 35 |
+
filename=CONFIG_NAME,
|
| 36 |
+
revision=revision,
|
| 37 |
+
cache_dir=cache_dir,
|
| 38 |
+
force_download=force_download,
|
| 39 |
+
proxies=proxies,
|
| 40 |
+
resume_download=resume_download,
|
| 41 |
+
token=token,
|
| 42 |
+
local_files_only=local_files_only,
|
| 43 |
+
)
|
| 44 |
+
with open(config_file, "r", encoding="utf-8") as f:
|
| 45 |
+
config = json.load(f)
|
| 46 |
+
|
| 47 |
+
# Call the _from_pretrained with all the needed arguments
|
| 48 |
+
gan = LightweightGAN(latent_dim=256, image_size=512)
|
| 49 |
+
|
| 50 |
+
gan = gan._from_pretrained(
|
| 51 |
+
model_id=str(model_name),
|
| 52 |
+
revision=revision,
|
| 53 |
+
cache_dir=cache_dir,
|
| 54 |
+
force_download=force_download,
|
| 55 |
+
proxies=proxies,
|
| 56 |
+
resume_download=resume_download,
|
| 57 |
+
local_files_only=local_files_only,
|
| 58 |
+
token=token,
|
| 59 |
+
use_auth_token=False,
|
| 60 |
+
config=config, # usually in **model_kwargs
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
gan.eval()
|
| 64 |
return gan
|
| 65 |
|