Spaces:
Sleeping
Sleeping
initial commit
Browse files- .gitignore +10 -0
- app.py +61 -0
- dnnlib/__init__.py +9 -0
- dnnlib/util.py +477 -0
- legacy.py +320 -0
- requirements.txt +7 -0
.gitignore
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
*.npz
|
| 6 |
+
*.pkl
|
| 7 |
+
.env
|
| 8 |
+
.DS_Store
|
| 9 |
+
.vscode/
|
| 10 |
+
projection_results/
|
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
import legacy
|
| 7 |
+
|
| 8 |
+
# Load the pre-trained StyleGAN model
|
| 9 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 10 |
+
model_path = 'dress_model.pkl' # Place your .pkl in the same directory or update path
|
| 11 |
+
|
| 12 |
+
# Load StyleGAN Generator
|
| 13 |
+
with open(model_path, 'rb') as f:
|
| 14 |
+
G = legacy.load_network_pkl(f)['G_ema'].to(device)
|
| 15 |
+
|
| 16 |
+
def mix_styles(image1_path, image2_path, styles_to_mix):
|
| 17 |
+
# Extract image names (without extensions)
|
| 18 |
+
image1_name = os.path.splitext(os.path.basename(image1_path))[0]
|
| 19 |
+
image2_name = os.path.splitext(os.path.basename(image2_path))[0]
|
| 20 |
+
|
| 21 |
+
# Load latent vectors from .npz
|
| 22 |
+
latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
|
| 23 |
+
latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
|
| 24 |
+
|
| 25 |
+
# Convert to torch tensors
|
| 26 |
+
latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
|
| 27 |
+
latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
|
| 28 |
+
|
| 29 |
+
# Mix layers
|
| 30 |
+
mixed_latent = latent_1_tensor.clone()
|
| 31 |
+
mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]
|
| 32 |
+
|
| 33 |
+
# Generate image
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
image = G.synthesis(mixed_latent, noise_mode='const')
|
| 36 |
+
|
| 37 |
+
# Convert to image
|
| 38 |
+
image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
|
| 39 |
+
mixed_image = Image.fromarray(image[0], 'RGB')
|
| 40 |
+
return mixed_image
|
| 41 |
+
|
| 42 |
+
def style_mixing_interface(image1, image2, mix_value):
|
| 43 |
+
if image1 is None or image2 is None:
|
| 44 |
+
return None
|
| 45 |
+
selected_layers = list(range(mix_value + 1))
|
| 46 |
+
return mix_styles(image1, image2, selected_layers)
|
| 47 |
+
|
| 48 |
+
# Gradio UI
|
| 49 |
+
iface = gr.Interface(
|
| 50 |
+
fn=style_mixing_interface,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Image(label="First Clothing Image", type="filepath"),
|
| 53 |
+
gr.Image(label="Second Clothing Image", type="filepath"),
|
| 54 |
+
gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.Image(label="Mixed Clothing Design"),
|
| 57 |
+
title="Style Mixing for Clothing Design",
|
| 58 |
+
description="Upload two projected images and choose how many early layers to mix. Precomputed latent vectors (projected_w.npz) must be in 'projection_results/{image_name}/'."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
iface.launch()
|
dnnlib/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
| 4 |
+
# and proprietary rights in and to this software, related documentation
|
| 5 |
+
# and any modifications thereto. Any use, reproduction, disclosure or
|
| 6 |
+
# distribution of this software and related documentation without an express
|
| 7 |
+
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
| 8 |
+
|
| 9 |
+
from .util import EasyDict, make_cache_dir_path
|
dnnlib/util.py
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
| 4 |
+
# and proprietary rights in and to this software, related documentation
|
| 5 |
+
# and any modifications thereto. Any use, reproduction, disclosure or
|
| 6 |
+
# distribution of this software and related documentation without an express
|
| 7 |
+
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
| 8 |
+
|
| 9 |
+
"""Miscellaneous utility classes and functions."""
|
| 10 |
+
|
| 11 |
+
import ctypes
|
| 12 |
+
import fnmatch
|
| 13 |
+
import importlib
|
| 14 |
+
import inspect
|
| 15 |
+
import numpy as np
|
| 16 |
+
import os
|
| 17 |
+
import shutil
|
| 18 |
+
import sys
|
| 19 |
+
import types
|
| 20 |
+
import io
|
| 21 |
+
import pickle
|
| 22 |
+
import re
|
| 23 |
+
import requests
|
| 24 |
+
import html
|
| 25 |
+
import hashlib
|
| 26 |
+
import glob
|
| 27 |
+
import tempfile
|
| 28 |
+
import urllib
|
| 29 |
+
import urllib.request
|
| 30 |
+
import uuid
|
| 31 |
+
|
| 32 |
+
from distutils.util import strtobool
|
| 33 |
+
from typing import Any, List, Tuple, Union
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Util classes
|
| 37 |
+
# ------------------------------------------------------------------------------------------
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class EasyDict(dict):
|
| 41 |
+
"""Convenience class that behaves like a dict but allows access with the attribute syntax."""
|
| 42 |
+
|
| 43 |
+
def __getattr__(self, name: str) -> Any:
|
| 44 |
+
try:
|
| 45 |
+
return self[name]
|
| 46 |
+
except KeyError:
|
| 47 |
+
raise AttributeError(name)
|
| 48 |
+
|
| 49 |
+
def __setattr__(self, name: str, value: Any) -> None:
|
| 50 |
+
self[name] = value
|
| 51 |
+
|
| 52 |
+
def __delattr__(self, name: str) -> None:
|
| 53 |
+
del self[name]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class Logger(object):
|
| 57 |
+
"""Redirect stderr to stdout, optionally print stdout to a file, and optionally force flushing on both stdout and the file."""
|
| 58 |
+
|
| 59 |
+
def __init__(self, file_name: str = None, file_mode: str = "w", should_flush: bool = True):
|
| 60 |
+
self.file = None
|
| 61 |
+
|
| 62 |
+
if file_name is not None:
|
| 63 |
+
self.file = open(file_name, file_mode)
|
| 64 |
+
|
| 65 |
+
self.should_flush = should_flush
|
| 66 |
+
self.stdout = sys.stdout
|
| 67 |
+
self.stderr = sys.stderr
|
| 68 |
+
|
| 69 |
+
sys.stdout = self
|
| 70 |
+
sys.stderr = self
|
| 71 |
+
|
| 72 |
+
def __enter__(self) -> "Logger":
|
| 73 |
+
return self
|
| 74 |
+
|
| 75 |
+
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
|
| 76 |
+
self.close()
|
| 77 |
+
|
| 78 |
+
def write(self, text: Union[str, bytes]) -> None:
|
| 79 |
+
"""Write text to stdout (and a file) and optionally flush."""
|
| 80 |
+
if isinstance(text, bytes):
|
| 81 |
+
text = text.decode()
|
| 82 |
+
if len(text) == 0: # workaround for a bug in VSCode debugger: sys.stdout.write(''); sys.stdout.flush() => crash
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
+
if self.file is not None:
|
| 86 |
+
self.file.write(text)
|
| 87 |
+
|
| 88 |
+
self.stdout.write(text)
|
| 89 |
+
|
| 90 |
+
if self.should_flush:
|
| 91 |
+
self.flush()
|
| 92 |
+
|
| 93 |
+
def flush(self) -> None:
|
| 94 |
+
"""Flush written text to both stdout and a file, if open."""
|
| 95 |
+
if self.file is not None:
|
| 96 |
+
self.file.flush()
|
| 97 |
+
|
| 98 |
+
self.stdout.flush()
|
| 99 |
+
|
| 100 |
+
def close(self) -> None:
|
| 101 |
+
"""Flush, close possible files, and remove stdout/stderr mirroring."""
|
| 102 |
+
self.flush()
|
| 103 |
+
|
| 104 |
+
# if using multiple loggers, prevent closing in wrong order
|
| 105 |
+
if sys.stdout is self:
|
| 106 |
+
sys.stdout = self.stdout
|
| 107 |
+
if sys.stderr is self:
|
| 108 |
+
sys.stderr = self.stderr
|
| 109 |
+
|
| 110 |
+
if self.file is not None:
|
| 111 |
+
self.file.close()
|
| 112 |
+
self.file = None
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Cache directories
|
| 116 |
+
# ------------------------------------------------------------------------------------------
|
| 117 |
+
|
| 118 |
+
_dnnlib_cache_dir = None
|
| 119 |
+
|
| 120 |
+
def set_cache_dir(path: str) -> None:
|
| 121 |
+
global _dnnlib_cache_dir
|
| 122 |
+
_dnnlib_cache_dir = path
|
| 123 |
+
|
| 124 |
+
def make_cache_dir_path(*paths: str) -> str:
|
| 125 |
+
if _dnnlib_cache_dir is not None:
|
| 126 |
+
return os.path.join(_dnnlib_cache_dir, *paths)
|
| 127 |
+
if 'DNNLIB_CACHE_DIR' in os.environ:
|
| 128 |
+
return os.path.join(os.environ['DNNLIB_CACHE_DIR'], *paths)
|
| 129 |
+
if 'HOME' in os.environ:
|
| 130 |
+
return os.path.join(os.environ['HOME'], '.cache', 'dnnlib', *paths)
|
| 131 |
+
if 'USERPROFILE' in os.environ:
|
| 132 |
+
return os.path.join(os.environ['USERPROFILE'], '.cache', 'dnnlib', *paths)
|
| 133 |
+
return os.path.join(tempfile.gettempdir(), '.cache', 'dnnlib', *paths)
|
| 134 |
+
|
| 135 |
+
# Small util functions
|
| 136 |
+
# ------------------------------------------------------------------------------------------
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def format_time(seconds: Union[int, float]) -> str:
|
| 140 |
+
"""Convert the seconds to human readable string with days, hours, minutes and seconds."""
|
| 141 |
+
s = int(np.rint(seconds))
|
| 142 |
+
|
| 143 |
+
if s < 60:
|
| 144 |
+
return "{0}s".format(s)
|
| 145 |
+
elif s < 60 * 60:
|
| 146 |
+
return "{0}m {1:02}s".format(s // 60, s % 60)
|
| 147 |
+
elif s < 24 * 60 * 60:
|
| 148 |
+
return "{0}h {1:02}m {2:02}s".format(s // (60 * 60), (s // 60) % 60, s % 60)
|
| 149 |
+
else:
|
| 150 |
+
return "{0}d {1:02}h {2:02}m".format(s // (24 * 60 * 60), (s // (60 * 60)) % 24, (s // 60) % 60)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def ask_yes_no(question: str) -> bool:
|
| 154 |
+
"""Ask the user the question until the user inputs a valid answer."""
|
| 155 |
+
while True:
|
| 156 |
+
try:
|
| 157 |
+
print("{0} [y/n]".format(question))
|
| 158 |
+
return strtobool(input().lower())
|
| 159 |
+
except ValueError:
|
| 160 |
+
pass
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def tuple_product(t: Tuple) -> Any:
|
| 164 |
+
"""Calculate the product of the tuple elements."""
|
| 165 |
+
result = 1
|
| 166 |
+
|
| 167 |
+
for v in t:
|
| 168 |
+
result *= v
|
| 169 |
+
|
| 170 |
+
return result
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
_str_to_ctype = {
|
| 174 |
+
"uint8": ctypes.c_ubyte,
|
| 175 |
+
"uint16": ctypes.c_uint16,
|
| 176 |
+
"uint32": ctypes.c_uint32,
|
| 177 |
+
"uint64": ctypes.c_uint64,
|
| 178 |
+
"int8": ctypes.c_byte,
|
| 179 |
+
"int16": ctypes.c_int16,
|
| 180 |
+
"int32": ctypes.c_int32,
|
| 181 |
+
"int64": ctypes.c_int64,
|
| 182 |
+
"float32": ctypes.c_float,
|
| 183 |
+
"float64": ctypes.c_double
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def get_dtype_and_ctype(type_obj: Any) -> Tuple[np.dtype, Any]:
|
| 188 |
+
"""Given a type name string (or an object having a __name__ attribute), return matching Numpy and ctypes types that have the same size in bytes."""
|
| 189 |
+
type_str = None
|
| 190 |
+
|
| 191 |
+
if isinstance(type_obj, str):
|
| 192 |
+
type_str = type_obj
|
| 193 |
+
elif hasattr(type_obj, "__name__"):
|
| 194 |
+
type_str = type_obj.__name__
|
| 195 |
+
elif hasattr(type_obj, "name"):
|
| 196 |
+
type_str = type_obj.name
|
| 197 |
+
else:
|
| 198 |
+
raise RuntimeError("Cannot infer type name from input")
|
| 199 |
+
|
| 200 |
+
assert type_str in _str_to_ctype.keys()
|
| 201 |
+
|
| 202 |
+
my_dtype = np.dtype(type_str)
|
| 203 |
+
my_ctype = _str_to_ctype[type_str]
|
| 204 |
+
|
| 205 |
+
assert my_dtype.itemsize == ctypes.sizeof(my_ctype)
|
| 206 |
+
|
| 207 |
+
return my_dtype, my_ctype
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def is_pickleable(obj: Any) -> bool:
|
| 211 |
+
try:
|
| 212 |
+
with io.BytesIO() as stream:
|
| 213 |
+
pickle.dump(obj, stream)
|
| 214 |
+
return True
|
| 215 |
+
except:
|
| 216 |
+
return False
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
# Functionality to import modules/objects by name, and call functions by name
|
| 220 |
+
# ------------------------------------------------------------------------------------------
|
| 221 |
+
|
| 222 |
+
def get_module_from_obj_name(obj_name: str) -> Tuple[types.ModuleType, str]:
|
| 223 |
+
"""Searches for the underlying module behind the name to some python object.
|
| 224 |
+
Returns the module and the object name (original name with module part removed)."""
|
| 225 |
+
|
| 226 |
+
# allow convenience shorthands, substitute them by full names
|
| 227 |
+
obj_name = re.sub("^np.", "numpy.", obj_name)
|
| 228 |
+
obj_name = re.sub("^tf.", "tensorflow.", obj_name)
|
| 229 |
+
|
| 230 |
+
# list alternatives for (module_name, local_obj_name)
|
| 231 |
+
parts = obj_name.split(".")
|
| 232 |
+
name_pairs = [(".".join(parts[:i]), ".".join(parts[i:])) for i in range(len(parts), 0, -1)]
|
| 233 |
+
|
| 234 |
+
# try each alternative in turn
|
| 235 |
+
for module_name, local_obj_name in name_pairs:
|
| 236 |
+
try:
|
| 237 |
+
module = importlib.import_module(module_name) # may raise ImportError
|
| 238 |
+
get_obj_from_module(module, local_obj_name) # may raise AttributeError
|
| 239 |
+
return module, local_obj_name
|
| 240 |
+
except:
|
| 241 |
+
pass
|
| 242 |
+
|
| 243 |
+
# maybe some of the modules themselves contain errors?
|
| 244 |
+
for module_name, _local_obj_name in name_pairs:
|
| 245 |
+
try:
|
| 246 |
+
importlib.import_module(module_name) # may raise ImportError
|
| 247 |
+
except ImportError:
|
| 248 |
+
if not str(sys.exc_info()[1]).startswith("No module named '" + module_name + "'"):
|
| 249 |
+
raise
|
| 250 |
+
|
| 251 |
+
# maybe the requested attribute is missing?
|
| 252 |
+
for module_name, local_obj_name in name_pairs:
|
| 253 |
+
try:
|
| 254 |
+
module = importlib.import_module(module_name) # may raise ImportError
|
| 255 |
+
get_obj_from_module(module, local_obj_name) # may raise AttributeError
|
| 256 |
+
except ImportError:
|
| 257 |
+
pass
|
| 258 |
+
|
| 259 |
+
# we are out of luck, but we have no idea why
|
| 260 |
+
raise ImportError(obj_name)
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
def get_obj_from_module(module: types.ModuleType, obj_name: str) -> Any:
|
| 264 |
+
"""Traverses the object name and returns the last (rightmost) python object."""
|
| 265 |
+
if obj_name == '':
|
| 266 |
+
return module
|
| 267 |
+
obj = module
|
| 268 |
+
for part in obj_name.split("."):
|
| 269 |
+
obj = getattr(obj, part)
|
| 270 |
+
return obj
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def get_obj_by_name(name: str) -> Any:
|
| 274 |
+
"""Finds the python object with the given name."""
|
| 275 |
+
module, obj_name = get_module_from_obj_name(name)
|
| 276 |
+
return get_obj_from_module(module, obj_name)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def call_func_by_name(*args, func_name: str = None, **kwargs) -> Any:
|
| 280 |
+
"""Finds the python object with the given name and calls it as a function."""
|
| 281 |
+
assert func_name is not None
|
| 282 |
+
func_obj = get_obj_by_name(func_name)
|
| 283 |
+
assert callable(func_obj)
|
| 284 |
+
return func_obj(*args, **kwargs)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def construct_class_by_name(*args, class_name: str = None, **kwargs) -> Any:
|
| 288 |
+
"""Finds the python class with the given name and constructs it with the given arguments."""
|
| 289 |
+
return call_func_by_name(*args, func_name=class_name, **kwargs)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def get_module_dir_by_obj_name(obj_name: str) -> str:
|
| 293 |
+
"""Get the directory path of the module containing the given object name."""
|
| 294 |
+
module, _ = get_module_from_obj_name(obj_name)
|
| 295 |
+
return os.path.dirname(inspect.getfile(module))
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def is_top_level_function(obj: Any) -> bool:
|
| 299 |
+
"""Determine whether the given object is a top-level function, i.e., defined at module scope using 'def'."""
|
| 300 |
+
return callable(obj) and obj.__name__ in sys.modules[obj.__module__].__dict__
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
def get_top_level_function_name(obj: Any) -> str:
|
| 304 |
+
"""Return the fully-qualified name of a top-level function."""
|
| 305 |
+
assert is_top_level_function(obj)
|
| 306 |
+
module = obj.__module__
|
| 307 |
+
if module == '__main__':
|
| 308 |
+
module = os.path.splitext(os.path.basename(sys.modules[module].__file__))[0]
|
| 309 |
+
return module + "." + obj.__name__
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
# File system helpers
|
| 313 |
+
# ------------------------------------------------------------------------------------------
|
| 314 |
+
|
| 315 |
+
def list_dir_recursively_with_ignore(dir_path: str, ignores: List[str] = None, add_base_to_relative: bool = False) -> List[Tuple[str, str]]:
|
| 316 |
+
"""List all files recursively in a given directory while ignoring given file and directory names.
|
| 317 |
+
Returns list of tuples containing both absolute and relative paths."""
|
| 318 |
+
assert os.path.isdir(dir_path)
|
| 319 |
+
base_name = os.path.basename(os.path.normpath(dir_path))
|
| 320 |
+
|
| 321 |
+
if ignores is None:
|
| 322 |
+
ignores = []
|
| 323 |
+
|
| 324 |
+
result = []
|
| 325 |
+
|
| 326 |
+
for root, dirs, files in os.walk(dir_path, topdown=True):
|
| 327 |
+
for ignore_ in ignores:
|
| 328 |
+
dirs_to_remove = [d for d in dirs if fnmatch.fnmatch(d, ignore_)]
|
| 329 |
+
|
| 330 |
+
# dirs need to be edited in-place
|
| 331 |
+
for d in dirs_to_remove:
|
| 332 |
+
dirs.remove(d)
|
| 333 |
+
|
| 334 |
+
files = [f for f in files if not fnmatch.fnmatch(f, ignore_)]
|
| 335 |
+
|
| 336 |
+
absolute_paths = [os.path.join(root, f) for f in files]
|
| 337 |
+
relative_paths = [os.path.relpath(p, dir_path) for p in absolute_paths]
|
| 338 |
+
|
| 339 |
+
if add_base_to_relative:
|
| 340 |
+
relative_paths = [os.path.join(base_name, p) for p in relative_paths]
|
| 341 |
+
|
| 342 |
+
assert len(absolute_paths) == len(relative_paths)
|
| 343 |
+
result += zip(absolute_paths, relative_paths)
|
| 344 |
+
|
| 345 |
+
return result
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
def copy_files_and_create_dirs(files: List[Tuple[str, str]]) -> None:
|
| 349 |
+
"""Takes in a list of tuples of (src, dst) paths and copies files.
|
| 350 |
+
Will create all necessary directories."""
|
| 351 |
+
for file in files:
|
| 352 |
+
target_dir_name = os.path.dirname(file[1])
|
| 353 |
+
|
| 354 |
+
# will create all intermediate-level directories
|
| 355 |
+
if not os.path.exists(target_dir_name):
|
| 356 |
+
os.makedirs(target_dir_name)
|
| 357 |
+
|
| 358 |
+
shutil.copyfile(file[0], file[1])
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
# URL helpers
|
| 362 |
+
# ------------------------------------------------------------------------------------------
|
| 363 |
+
|
| 364 |
+
def is_url(obj: Any, allow_file_urls: bool = False) -> bool:
|
| 365 |
+
"""Determine whether the given object is a valid URL string."""
|
| 366 |
+
if not isinstance(obj, str) or not "://" in obj:
|
| 367 |
+
return False
|
| 368 |
+
if allow_file_urls and obj.startswith('file://'):
|
| 369 |
+
return True
|
| 370 |
+
try:
|
| 371 |
+
res = requests.compat.urlparse(obj)
|
| 372 |
+
if not res.scheme or not res.netloc or not "." in res.netloc:
|
| 373 |
+
return False
|
| 374 |
+
res = requests.compat.urlparse(requests.compat.urljoin(obj, "/"))
|
| 375 |
+
if not res.scheme or not res.netloc or not "." in res.netloc:
|
| 376 |
+
return False
|
| 377 |
+
except:
|
| 378 |
+
return False
|
| 379 |
+
return True
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def open_url(url: str, cache_dir: str = None, num_attempts: int = 10, verbose: bool = True, return_filename: bool = False, cache: bool = True) -> Any:
|
| 383 |
+
"""Download the given URL and return a binary-mode file object to access the data."""
|
| 384 |
+
assert num_attempts >= 1
|
| 385 |
+
assert not (return_filename and (not cache))
|
| 386 |
+
|
| 387 |
+
# Doesn't look like an URL scheme so interpret it as a local filename.
|
| 388 |
+
if not re.match('^[a-z]+://', url):
|
| 389 |
+
return url if return_filename else open(url, "rb")
|
| 390 |
+
|
| 391 |
+
# Handle file URLs. This code handles unusual file:// patterns that
|
| 392 |
+
# arise on Windows:
|
| 393 |
+
#
|
| 394 |
+
# file:///c:/foo.txt
|
| 395 |
+
#
|
| 396 |
+
# which would translate to a local '/c:/foo.txt' filename that's
|
| 397 |
+
# invalid. Drop the forward slash for such pathnames.
|
| 398 |
+
#
|
| 399 |
+
# If you touch this code path, you should test it on both Linux and
|
| 400 |
+
# Windows.
|
| 401 |
+
#
|
| 402 |
+
# Some internet resources suggest using urllib.request.url2pathname() but
|
| 403 |
+
# but that converts forward slashes to backslashes and this causes
|
| 404 |
+
# its own set of problems.
|
| 405 |
+
if url.startswith('file://'):
|
| 406 |
+
filename = urllib.parse.urlparse(url).path
|
| 407 |
+
if re.match(r'^/[a-zA-Z]:', filename):
|
| 408 |
+
filename = filename[1:]
|
| 409 |
+
return filename if return_filename else open(filename, "rb")
|
| 410 |
+
|
| 411 |
+
assert is_url(url)
|
| 412 |
+
|
| 413 |
+
# Lookup from cache.
|
| 414 |
+
if cache_dir is None:
|
| 415 |
+
cache_dir = make_cache_dir_path('downloads')
|
| 416 |
+
|
| 417 |
+
url_md5 = hashlib.md5(url.encode("utf-8")).hexdigest()
|
| 418 |
+
if cache:
|
| 419 |
+
cache_files = glob.glob(os.path.join(cache_dir, url_md5 + "_*"))
|
| 420 |
+
if len(cache_files) == 1:
|
| 421 |
+
filename = cache_files[0]
|
| 422 |
+
return filename if return_filename else open(filename, "rb")
|
| 423 |
+
|
| 424 |
+
# Download.
|
| 425 |
+
url_name = None
|
| 426 |
+
url_data = None
|
| 427 |
+
with requests.Session() as session:
|
| 428 |
+
if verbose:
|
| 429 |
+
print("Downloading %s ..." % url, end="", flush=True)
|
| 430 |
+
for attempts_left in reversed(range(num_attempts)):
|
| 431 |
+
try:
|
| 432 |
+
with session.get(url) as res:
|
| 433 |
+
res.raise_for_status()
|
| 434 |
+
if len(res.content) == 0:
|
| 435 |
+
raise IOError("No data received")
|
| 436 |
+
|
| 437 |
+
if len(res.content) < 8192:
|
| 438 |
+
content_str = res.content.decode("utf-8")
|
| 439 |
+
if "download_warning" in res.headers.get("Set-Cookie", ""):
|
| 440 |
+
links = [html.unescape(link) for link in content_str.split('"') if "export=download" in link]
|
| 441 |
+
if len(links) == 1:
|
| 442 |
+
url = requests.compat.urljoin(url, links[0])
|
| 443 |
+
raise IOError("Google Drive virus checker nag")
|
| 444 |
+
if "Google Drive - Quota exceeded" in content_str:
|
| 445 |
+
raise IOError("Google Drive download quota exceeded -- please try again later")
|
| 446 |
+
|
| 447 |
+
match = re.search(r'filename="([^"]*)"', res.headers.get("Content-Disposition", ""))
|
| 448 |
+
url_name = match[1] if match else url
|
| 449 |
+
url_data = res.content
|
| 450 |
+
if verbose:
|
| 451 |
+
print(" done")
|
| 452 |
+
break
|
| 453 |
+
except KeyboardInterrupt:
|
| 454 |
+
raise
|
| 455 |
+
except:
|
| 456 |
+
if not attempts_left:
|
| 457 |
+
if verbose:
|
| 458 |
+
print(" failed")
|
| 459 |
+
raise
|
| 460 |
+
if verbose:
|
| 461 |
+
print(".", end="", flush=True)
|
| 462 |
+
|
| 463 |
+
# Save to cache.
|
| 464 |
+
if cache:
|
| 465 |
+
safe_name = re.sub(r"[^0-9a-zA-Z-._]", "_", url_name)
|
| 466 |
+
cache_file = os.path.join(cache_dir, url_md5 + "_" + safe_name)
|
| 467 |
+
temp_file = os.path.join(cache_dir, "tmp_" + uuid.uuid4().hex + "_" + url_md5 + "_" + safe_name)
|
| 468 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 469 |
+
with open(temp_file, "wb") as f:
|
| 470 |
+
f.write(url_data)
|
| 471 |
+
os.replace(temp_file, cache_file) # atomic
|
| 472 |
+
if return_filename:
|
| 473 |
+
return cache_file
|
| 474 |
+
|
| 475 |
+
# Return data as file object.
|
| 476 |
+
assert not return_filename
|
| 477 |
+
return io.BytesIO(url_data)
|
legacy.py
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# NVIDIA CORPORATION and its licensors retain all intellectual property
|
| 4 |
+
# and proprietary rights in and to this software, related documentation
|
| 5 |
+
# and any modifications thereto. Any use, reproduction, disclosure or
|
| 6 |
+
# distribution of this software and related documentation without an express
|
| 7 |
+
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
| 8 |
+
|
| 9 |
+
import click
|
| 10 |
+
import pickle
|
| 11 |
+
import re
|
| 12 |
+
import copy
|
| 13 |
+
import numpy as np
|
| 14 |
+
import torch
|
| 15 |
+
import dnnlib
|
| 16 |
+
from torch_utils import misc
|
| 17 |
+
|
| 18 |
+
#----------------------------------------------------------------------------
|
| 19 |
+
|
| 20 |
+
def load_network_pkl(f, force_fp16=False):
|
| 21 |
+
data = _LegacyUnpickler(f).load()
|
| 22 |
+
|
| 23 |
+
# Legacy TensorFlow pickle => convert.
|
| 24 |
+
if isinstance(data, tuple) and len(data) == 3 and all(isinstance(net, _TFNetworkStub) for net in data):
|
| 25 |
+
tf_G, tf_D, tf_Gs = data
|
| 26 |
+
G = convert_tf_generator(tf_G)
|
| 27 |
+
D = convert_tf_discriminator(tf_D)
|
| 28 |
+
G_ema = convert_tf_generator(tf_Gs)
|
| 29 |
+
data = dict(G=G, D=D, G_ema=G_ema)
|
| 30 |
+
|
| 31 |
+
# Add missing fields.
|
| 32 |
+
if 'training_set_kwargs' not in data:
|
| 33 |
+
data['training_set_kwargs'] = None
|
| 34 |
+
if 'augment_pipe' not in data:
|
| 35 |
+
data['augment_pipe'] = None
|
| 36 |
+
|
| 37 |
+
# Validate contents.
|
| 38 |
+
assert isinstance(data['G'], torch.nn.Module)
|
| 39 |
+
assert isinstance(data['D'], torch.nn.Module)
|
| 40 |
+
assert isinstance(data['G_ema'], torch.nn.Module)
|
| 41 |
+
assert isinstance(data['training_set_kwargs'], (dict, type(None)))
|
| 42 |
+
assert isinstance(data['augment_pipe'], (torch.nn.Module, type(None)))
|
| 43 |
+
|
| 44 |
+
# Force FP16.
|
| 45 |
+
if force_fp16:
|
| 46 |
+
for key in ['G', 'D', 'G_ema']:
|
| 47 |
+
old = data[key]
|
| 48 |
+
kwargs = copy.deepcopy(old.init_kwargs)
|
| 49 |
+
if key.startswith('G'):
|
| 50 |
+
kwargs.synthesis_kwargs = dnnlib.EasyDict(kwargs.get('synthesis_kwargs', {}))
|
| 51 |
+
kwargs.synthesis_kwargs.num_fp16_res = 4
|
| 52 |
+
kwargs.synthesis_kwargs.conv_clamp = 256
|
| 53 |
+
if key.startswith('D'):
|
| 54 |
+
kwargs.num_fp16_res = 4
|
| 55 |
+
kwargs.conv_clamp = 256
|
| 56 |
+
if kwargs != old.init_kwargs:
|
| 57 |
+
new = type(old)(**kwargs).eval().requires_grad_(False)
|
| 58 |
+
misc.copy_params_and_buffers(old, new, require_all=True)
|
| 59 |
+
data[key] = new
|
| 60 |
+
return data
|
| 61 |
+
|
| 62 |
+
#----------------------------------------------------------------------------
|
| 63 |
+
|
| 64 |
+
class _TFNetworkStub(dnnlib.EasyDict):
|
| 65 |
+
pass
|
| 66 |
+
|
| 67 |
+
class _LegacyUnpickler(pickle.Unpickler):
|
| 68 |
+
def find_class(self, module, name):
|
| 69 |
+
if module == 'dnnlib.tflib.network' and name == 'Network':
|
| 70 |
+
return _TFNetworkStub
|
| 71 |
+
return super().find_class(module, name)
|
| 72 |
+
|
| 73 |
+
#----------------------------------------------------------------------------
|
| 74 |
+
|
| 75 |
+
def _collect_tf_params(tf_net):
|
| 76 |
+
# pylint: disable=protected-access
|
| 77 |
+
tf_params = dict()
|
| 78 |
+
def recurse(prefix, tf_net):
|
| 79 |
+
for name, value in tf_net.variables:
|
| 80 |
+
tf_params[prefix + name] = value
|
| 81 |
+
for name, comp in tf_net.components.items():
|
| 82 |
+
recurse(prefix + name + '/', comp)
|
| 83 |
+
recurse('', tf_net)
|
| 84 |
+
return tf_params
|
| 85 |
+
|
| 86 |
+
#----------------------------------------------------------------------------
|
| 87 |
+
|
| 88 |
+
def _populate_module_params(module, *patterns):
|
| 89 |
+
for name, tensor in misc.named_params_and_buffers(module):
|
| 90 |
+
found = False
|
| 91 |
+
value = None
|
| 92 |
+
for pattern, value_fn in zip(patterns[0::2], patterns[1::2]):
|
| 93 |
+
match = re.fullmatch(pattern, name)
|
| 94 |
+
if match:
|
| 95 |
+
found = True
|
| 96 |
+
if value_fn is not None:
|
| 97 |
+
value = value_fn(*match.groups())
|
| 98 |
+
break
|
| 99 |
+
try:
|
| 100 |
+
assert found
|
| 101 |
+
if value is not None:
|
| 102 |
+
tensor.copy_(torch.from_numpy(np.array(value)))
|
| 103 |
+
except:
|
| 104 |
+
print(name, list(tensor.shape))
|
| 105 |
+
raise
|
| 106 |
+
|
| 107 |
+
#----------------------------------------------------------------------------
|
| 108 |
+
|
| 109 |
+
def convert_tf_generator(tf_G):
|
| 110 |
+
if tf_G.version < 4:
|
| 111 |
+
raise ValueError('TensorFlow pickle version too low')
|
| 112 |
+
|
| 113 |
+
# Collect kwargs.
|
| 114 |
+
tf_kwargs = tf_G.static_kwargs
|
| 115 |
+
known_kwargs = set()
|
| 116 |
+
def kwarg(tf_name, default=None, none=None):
|
| 117 |
+
known_kwargs.add(tf_name)
|
| 118 |
+
val = tf_kwargs.get(tf_name, default)
|
| 119 |
+
return val if val is not None else none
|
| 120 |
+
|
| 121 |
+
# Convert kwargs.
|
| 122 |
+
kwargs = dnnlib.EasyDict(
|
| 123 |
+
z_dim = kwarg('latent_size', 512),
|
| 124 |
+
c_dim = kwarg('label_size', 0),
|
| 125 |
+
w_dim = kwarg('dlatent_size', 512),
|
| 126 |
+
img_resolution = kwarg('resolution', 1024),
|
| 127 |
+
img_channels = kwarg('num_channels', 3),
|
| 128 |
+
mapping_kwargs = dnnlib.EasyDict(
|
| 129 |
+
num_layers = kwarg('mapping_layers', 8),
|
| 130 |
+
embed_features = kwarg('label_fmaps', None),
|
| 131 |
+
layer_features = kwarg('mapping_fmaps', None),
|
| 132 |
+
activation = kwarg('mapping_nonlinearity', 'lrelu'),
|
| 133 |
+
lr_multiplier = kwarg('mapping_lrmul', 0.01),
|
| 134 |
+
w_avg_beta = kwarg('w_avg_beta', 0.995, none=1),
|
| 135 |
+
),
|
| 136 |
+
synthesis_kwargs = dnnlib.EasyDict(
|
| 137 |
+
channel_base = kwarg('fmap_base', 16384) * 2,
|
| 138 |
+
channel_max = kwarg('fmap_max', 512),
|
| 139 |
+
num_fp16_res = kwarg('num_fp16_res', 0),
|
| 140 |
+
conv_clamp = kwarg('conv_clamp', None),
|
| 141 |
+
architecture = kwarg('architecture', 'skip'),
|
| 142 |
+
resample_filter = kwarg('resample_kernel', [1,3,3,1]),
|
| 143 |
+
use_noise = kwarg('use_noise', True),
|
| 144 |
+
activation = kwarg('nonlinearity', 'lrelu'),
|
| 145 |
+
),
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Check for unknown kwargs.
|
| 149 |
+
kwarg('truncation_psi')
|
| 150 |
+
kwarg('truncation_cutoff')
|
| 151 |
+
kwarg('style_mixing_prob')
|
| 152 |
+
kwarg('structure')
|
| 153 |
+
unknown_kwargs = list(set(tf_kwargs.keys()) - known_kwargs)
|
| 154 |
+
if len(unknown_kwargs) > 0:
|
| 155 |
+
raise ValueError('Unknown TensorFlow kwarg', unknown_kwargs[0])
|
| 156 |
+
|
| 157 |
+
# Collect params.
|
| 158 |
+
tf_params = _collect_tf_params(tf_G)
|
| 159 |
+
for name, value in list(tf_params.items()):
|
| 160 |
+
match = re.fullmatch(r'ToRGB_lod(\d+)/(.*)', name)
|
| 161 |
+
if match:
|
| 162 |
+
r = kwargs.img_resolution // (2 ** int(match.group(1)))
|
| 163 |
+
tf_params[f'{r}x{r}/ToRGB/{match.group(2)}'] = value
|
| 164 |
+
kwargs.synthesis.kwargs.architecture = 'orig'
|
| 165 |
+
#for name, value in tf_params.items(): print(f'{name:<50s}{list(value.shape)}')
|
| 166 |
+
|
| 167 |
+
# Convert params.
|
| 168 |
+
from training import networks
|
| 169 |
+
G = networks.Generator(**kwargs).eval().requires_grad_(False)
|
| 170 |
+
# pylint: disable=unnecessary-lambda
|
| 171 |
+
_populate_module_params(G,
|
| 172 |
+
r'mapping\.w_avg', lambda: tf_params[f'dlatent_avg'],
|
| 173 |
+
r'mapping\.embed\.weight', lambda: tf_params[f'mapping/LabelEmbed/weight'].transpose(),
|
| 174 |
+
r'mapping\.embed\.bias', lambda: tf_params[f'mapping/LabelEmbed/bias'],
|
| 175 |
+
r'mapping\.fc(\d+)\.weight', lambda i: tf_params[f'mapping/Dense{i}/weight'].transpose(),
|
| 176 |
+
r'mapping\.fc(\d+)\.bias', lambda i: tf_params[f'mapping/Dense{i}/bias'],
|
| 177 |
+
r'synthesis\.b4\.const', lambda: tf_params[f'synthesis/4x4/Const/const'][0],
|
| 178 |
+
r'synthesis\.b4\.conv1\.weight', lambda: tf_params[f'synthesis/4x4/Conv/weight'].transpose(3, 2, 0, 1),
|
| 179 |
+
r'synthesis\.b4\.conv1\.bias', lambda: tf_params[f'synthesis/4x4/Conv/bias'],
|
| 180 |
+
r'synthesis\.b4\.conv1\.noise_const', lambda: tf_params[f'synthesis/noise0'][0, 0],
|
| 181 |
+
r'synthesis\.b4\.conv1\.noise_strength', lambda: tf_params[f'synthesis/4x4/Conv/noise_strength'],
|
| 182 |
+
r'synthesis\.b4\.conv1\.affine\.weight', lambda: tf_params[f'synthesis/4x4/Conv/mod_weight'].transpose(),
|
| 183 |
+
r'synthesis\.b4\.conv1\.affine\.bias', lambda: tf_params[f'synthesis/4x4/Conv/mod_bias'] + 1,
|
| 184 |
+
r'synthesis\.b(\d+)\.conv0\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/Conv0_up/weight'][::-1, ::-1].transpose(3, 2, 0, 1),
|
| 185 |
+
r'synthesis\.b(\d+)\.conv0\.bias', lambda r: tf_params[f'synthesis/{r}x{r}/Conv0_up/bias'],
|
| 186 |
+
r'synthesis\.b(\d+)\.conv0\.noise_const', lambda r: tf_params[f'synthesis/noise{int(np.log2(int(r)))*2-5}'][0, 0],
|
| 187 |
+
r'synthesis\.b(\d+)\.conv0\.noise_strength', lambda r: tf_params[f'synthesis/{r}x{r}/Conv0_up/noise_strength'],
|
| 188 |
+
r'synthesis\.b(\d+)\.conv0\.affine\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/Conv0_up/mod_weight'].transpose(),
|
| 189 |
+
r'synthesis\.b(\d+)\.conv0\.affine\.bias', lambda r: tf_params[f'synthesis/{r}x{r}/Conv0_up/mod_bias'] + 1,
|
| 190 |
+
r'synthesis\.b(\d+)\.conv1\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/Conv1/weight'].transpose(3, 2, 0, 1),
|
| 191 |
+
r'synthesis\.b(\d+)\.conv1\.bias', lambda r: tf_params[f'synthesis/{r}x{r}/Conv1/bias'],
|
| 192 |
+
r'synthesis\.b(\d+)\.conv1\.noise_const', lambda r: tf_params[f'synthesis/noise{int(np.log2(int(r)))*2-4}'][0, 0],
|
| 193 |
+
r'synthesis\.b(\d+)\.conv1\.noise_strength', lambda r: tf_params[f'synthesis/{r}x{r}/Conv1/noise_strength'],
|
| 194 |
+
r'synthesis\.b(\d+)\.conv1\.affine\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/Conv1/mod_weight'].transpose(),
|
| 195 |
+
r'synthesis\.b(\d+)\.conv1\.affine\.bias', lambda r: tf_params[f'synthesis/{r}x{r}/Conv1/mod_bias'] + 1,
|
| 196 |
+
r'synthesis\.b(\d+)\.torgb\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/ToRGB/weight'].transpose(3, 2, 0, 1),
|
| 197 |
+
r'synthesis\.b(\d+)\.torgb\.bias', lambda r: tf_params[f'synthesis/{r}x{r}/ToRGB/bias'],
|
| 198 |
+
r'synthesis\.b(\d+)\.torgb\.affine\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/ToRGB/mod_weight'].transpose(),
|
| 199 |
+
r'synthesis\.b(\d+)\.torgb\.affine\.bias', lambda r: tf_params[f'synthesis/{r}x{r}/ToRGB/mod_bias'] + 1,
|
| 200 |
+
r'synthesis\.b(\d+)\.skip\.weight', lambda r: tf_params[f'synthesis/{r}x{r}/Skip/weight'][::-1, ::-1].transpose(3, 2, 0, 1),
|
| 201 |
+
r'.*\.resample_filter', None,
|
| 202 |
+
)
|
| 203 |
+
return G
|
| 204 |
+
|
| 205 |
+
#----------------------------------------------------------------------------
|
| 206 |
+
|
| 207 |
+
def convert_tf_discriminator(tf_D):
|
| 208 |
+
if tf_D.version < 4:
|
| 209 |
+
raise ValueError('TensorFlow pickle version too low')
|
| 210 |
+
|
| 211 |
+
# Collect kwargs.
|
| 212 |
+
tf_kwargs = tf_D.static_kwargs
|
| 213 |
+
known_kwargs = set()
|
| 214 |
+
def kwarg(tf_name, default=None):
|
| 215 |
+
known_kwargs.add(tf_name)
|
| 216 |
+
return tf_kwargs.get(tf_name, default)
|
| 217 |
+
|
| 218 |
+
# Convert kwargs.
|
| 219 |
+
kwargs = dnnlib.EasyDict(
|
| 220 |
+
c_dim = kwarg('label_size', 0),
|
| 221 |
+
img_resolution = kwarg('resolution', 1024),
|
| 222 |
+
img_channels = kwarg('num_channels', 3),
|
| 223 |
+
architecture = kwarg('architecture', 'resnet'),
|
| 224 |
+
channel_base = kwarg('fmap_base', 16384) * 2,
|
| 225 |
+
channel_max = kwarg('fmap_max', 512),
|
| 226 |
+
num_fp16_res = kwarg('num_fp16_res', 0),
|
| 227 |
+
conv_clamp = kwarg('conv_clamp', None),
|
| 228 |
+
cmap_dim = kwarg('mapping_fmaps', None),
|
| 229 |
+
block_kwargs = dnnlib.EasyDict(
|
| 230 |
+
activation = kwarg('nonlinearity', 'lrelu'),
|
| 231 |
+
resample_filter = kwarg('resample_kernel', [1,3,3,1]),
|
| 232 |
+
freeze_layers = kwarg('freeze_layers', 0),
|
| 233 |
+
),
|
| 234 |
+
mapping_kwargs = dnnlib.EasyDict(
|
| 235 |
+
num_layers = kwarg('mapping_layers', 0),
|
| 236 |
+
embed_features = kwarg('mapping_fmaps', None),
|
| 237 |
+
layer_features = kwarg('mapping_fmaps', None),
|
| 238 |
+
activation = kwarg('nonlinearity', 'lrelu'),
|
| 239 |
+
lr_multiplier = kwarg('mapping_lrmul', 0.1),
|
| 240 |
+
),
|
| 241 |
+
epilogue_kwargs = dnnlib.EasyDict(
|
| 242 |
+
mbstd_group_size = kwarg('mbstd_group_size', None),
|
| 243 |
+
mbstd_num_channels = kwarg('mbstd_num_features', 1),
|
| 244 |
+
activation = kwarg('nonlinearity', 'lrelu'),
|
| 245 |
+
),
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Check for unknown kwargs.
|
| 249 |
+
kwarg('structure')
|
| 250 |
+
unknown_kwargs = list(set(tf_kwargs.keys()) - known_kwargs)
|
| 251 |
+
if len(unknown_kwargs) > 0:
|
| 252 |
+
raise ValueError('Unknown TensorFlow kwarg', unknown_kwargs[0])
|
| 253 |
+
|
| 254 |
+
# Collect params.
|
| 255 |
+
tf_params = _collect_tf_params(tf_D)
|
| 256 |
+
for name, value in list(tf_params.items()):
|
| 257 |
+
match = re.fullmatch(r'FromRGB_lod(\d+)/(.*)', name)
|
| 258 |
+
if match:
|
| 259 |
+
r = kwargs.img_resolution // (2 ** int(match.group(1)))
|
| 260 |
+
tf_params[f'{r}x{r}/FromRGB/{match.group(2)}'] = value
|
| 261 |
+
kwargs.architecture = 'orig'
|
| 262 |
+
#for name, value in tf_params.items(): print(f'{name:<50s}{list(value.shape)}')
|
| 263 |
+
|
| 264 |
+
# Convert params.
|
| 265 |
+
from training import networks
|
| 266 |
+
D = networks.Discriminator(**kwargs).eval().requires_grad_(False)
|
| 267 |
+
# pylint: disable=unnecessary-lambda
|
| 268 |
+
_populate_module_params(D,
|
| 269 |
+
r'b(\d+)\.fromrgb\.weight', lambda r: tf_params[f'{r}x{r}/FromRGB/weight'].transpose(3, 2, 0, 1),
|
| 270 |
+
r'b(\d+)\.fromrgb\.bias', lambda r: tf_params[f'{r}x{r}/FromRGB/bias'],
|
| 271 |
+
r'b(\d+)\.conv(\d+)\.weight', lambda r, i: tf_params[f'{r}x{r}/Conv{i}{["","_down"][int(i)]}/weight'].transpose(3, 2, 0, 1),
|
| 272 |
+
r'b(\d+)\.conv(\d+)\.bias', lambda r, i: tf_params[f'{r}x{r}/Conv{i}{["","_down"][int(i)]}/bias'],
|
| 273 |
+
r'b(\d+)\.skip\.weight', lambda r: tf_params[f'{r}x{r}/Skip/weight'].transpose(3, 2, 0, 1),
|
| 274 |
+
r'mapping\.embed\.weight', lambda: tf_params[f'LabelEmbed/weight'].transpose(),
|
| 275 |
+
r'mapping\.embed\.bias', lambda: tf_params[f'LabelEmbed/bias'],
|
| 276 |
+
r'mapping\.fc(\d+)\.weight', lambda i: tf_params[f'Mapping{i}/weight'].transpose(),
|
| 277 |
+
r'mapping\.fc(\d+)\.bias', lambda i: tf_params[f'Mapping{i}/bias'],
|
| 278 |
+
r'b4\.conv\.weight', lambda: tf_params[f'4x4/Conv/weight'].transpose(3, 2, 0, 1),
|
| 279 |
+
r'b4\.conv\.bias', lambda: tf_params[f'4x4/Conv/bias'],
|
| 280 |
+
r'b4\.fc\.weight', lambda: tf_params[f'4x4/Dense0/weight'].transpose(),
|
| 281 |
+
r'b4\.fc\.bias', lambda: tf_params[f'4x4/Dense0/bias'],
|
| 282 |
+
r'b4\.out\.weight', lambda: tf_params[f'Output/weight'].transpose(),
|
| 283 |
+
r'b4\.out\.bias', lambda: tf_params[f'Output/bias'],
|
| 284 |
+
r'.*\.resample_filter', None,
|
| 285 |
+
)
|
| 286 |
+
return D
|
| 287 |
+
|
| 288 |
+
#----------------------------------------------------------------------------
|
| 289 |
+
|
| 290 |
+
@click.command()
|
| 291 |
+
@click.option('--source', help='Input pickle', required=True, metavar='PATH')
|
| 292 |
+
@click.option('--dest', help='Output pickle', required=True, metavar='PATH')
|
| 293 |
+
@click.option('--force-fp16', help='Force the networks to use FP16', type=bool, default=False, metavar='BOOL', show_default=True)
|
| 294 |
+
def convert_network_pickle(source, dest, force_fp16):
|
| 295 |
+
"""Convert legacy network pickle into the native PyTorch format.
|
| 296 |
+
|
| 297 |
+
The tool is able to load the main network configurations exported using the TensorFlow version of StyleGAN2 or StyleGAN2-ADA.
|
| 298 |
+
It does not support e.g. StyleGAN2-ADA comparison methods, StyleGAN2 configs A-D, or StyleGAN1 networks.
|
| 299 |
+
|
| 300 |
+
Example:
|
| 301 |
+
|
| 302 |
+
\b
|
| 303 |
+
python legacy.py \\
|
| 304 |
+
--source=https://nvlabs-fi-cdn.nvidia.com/stylegan2/networks/stylegan2-cat-config-f.pkl \\
|
| 305 |
+
--dest=stylegan2-cat-config-f.pkl
|
| 306 |
+
"""
|
| 307 |
+
print(f'Loading "{source}"...')
|
| 308 |
+
with dnnlib.util.open_url(source) as f:
|
| 309 |
+
data = load_network_pkl(f, force_fp16=force_fp16)
|
| 310 |
+
print(f'Saving "{dest}"...')
|
| 311 |
+
with open(dest, 'wb') as f:
|
| 312 |
+
pickle.dump(data, f)
|
| 313 |
+
print('Done.')
|
| 314 |
+
|
| 315 |
+
#----------------------------------------------------------------------------
|
| 316 |
+
|
| 317 |
+
if __name__ == "__main__":
|
| 318 |
+
convert_network_pickle() # pylint: disable=no-value-for-parameter
|
| 319 |
+
|
| 320 |
+
#----------------------------------------------------------------------------
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=1.12.0
|
| 2 |
+
numpy>=1.21.0
|
| 3 |
+
Pillow>=9.0.0
|
| 4 |
+
gradio>=3.0.0
|
| 5 |
+
imageio>=2.9.0
|
| 6 |
+
opencv-python-headless>=4.5.0
|
| 7 |
+
click>=7.0
|