|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
Functions for downloading pre-trained Sana models |
|
""" |
|
import argparse |
|
import os |
|
|
|
import torch |
|
from termcolor import colored |
|
from torchvision.datasets.utils import download_url |
|
|
|
from sana.tools import hf_download_or_fpath |
|
|
|
pretrained_models = {} |
|
|
|
|
|
def find_model(model_name): |
|
""" |
|
Finds a pre-trained G.pt model, downloading it if necessary. Alternatively, loads a model from a local path. |
|
""" |
|
if model_name in pretrained_models: |
|
return download_model(model_name) |
|
|
|
|
|
model_name = hf_download_or_fpath(model_name) |
|
assert os.path.isfile(model_name), f"Could not find Sana checkpoint at {model_name}" |
|
print(colored(f"[Sana] Loading model from {model_name}", attrs=["bold"])) |
|
return torch.load(model_name, map_location=lambda storage, loc: storage) |
|
|
|
|
|
def download_model(model_name): |
|
""" |
|
Downloads a pre-trained Sana model from the web. |
|
""" |
|
assert model_name in pretrained_models |
|
local_path = f"output/pretrained_models/{model_name}" |
|
if not os.path.isfile(local_path): |
|
hf_endpoint = os.environ.get("HF_ENDPOINT") |
|
if hf_endpoint is None: |
|
hf_endpoint = "https://huggingface.co" |
|
os.makedirs("output/pretrained_models", exist_ok=True) |
|
web_path = f"" |
|
download_url(web_path, "output/pretrained_models/") |
|
model = torch.load(local_path, map_location=lambda storage, loc: storage) |
|
return model |
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("--model_names", nargs="+", type=str, default=pretrained_models) |
|
args = parser.parse_args() |
|
model_names = args.model_names |
|
model_names = set(model_names) |
|
|
|
|
|
for model in model_names: |
|
download_model(model) |
|
print("Done.") |
|
|