Add lg and variant style spaces
Browse files- README.md +8 -0
- sref.py +18 -10
- sref_space_lg.npz +3 -0
- sref_space_variant.npz +3 -0
README.md
CHANGED
|
@@ -71,6 +71,14 @@ pos = torch.cat([pos.new_zeros(B, 108, 3), pos], dim=1) # the block
|
|
| 71 |
|
| 72 |
Run this on every denoising step. With classifier-free guidance, add the block on the conditional pass. No model weights change. It adds 108 tokens to the sequence.
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
## Strength
|
| 75 |
|
| 76 |
- `6` is a full restyle for most codes.
|
|
|
|
| 71 |
|
| 72 |
Run this on every denoising step. With classifier-free guidance, add the block on the conditional pass. No model weights change. It adds 108 tokens to the sequence.
|
| 73 |
|
| 74 |
+
## Variants
|
| 75 |
+
|
| 76 |
+
Three style spaces ship side by side, `sref_space.npz`, `sref_space_lg.npz` and `sref_space_variant.npz`: they're roughly equal, but slightly different, and the same code gives a different style in each.
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
block = style_block(768519172, strength=6.0, space_name="lg") # or "variant"; "default" when omitted
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
## Strength
|
| 83 |
|
| 84 |
- `6` is a full restyle for most codes.
|
sref.py
CHANGED
|
@@ -9,22 +9,30 @@ from pathlib import Path
|
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
SD = _space["sd"].astype(np.float32) # [64]
|
| 15 |
-
TOKENS, WIDTH = BASIS.shape[1], BASIS.shape[2]
|
| 16 |
|
| 17 |
|
| 18 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""The 64 style coordinates for a code."""
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
-
def style_block(code: int, strength: float = 6.0) -> np.ndarray:
|
| 24 |
"""The token block for a code: [108, 6144]."""
|
| 25 |
-
return
|
| 26 |
|
| 27 |
|
| 28 |
-
def block_from_coords(z: np.ndarray, strength: float = 6.0) -> np.ndarray:
|
| 29 |
"""The token block for 64 coordinates you supply, for blending or editing styles."""
|
| 30 |
-
|
|
|
|
|
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
|
| 12 |
+
SPACES = {"default": "sref_space.npz", "lg": "sref_space_lg.npz", "variant": "sref_space_variant.npz"}
|
| 13 |
+
_loaded = {}
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
+
def space(name: str = "default"):
|
| 17 |
+
"""(basis [64, 108, 6144], sd [64]) of a style space. The same code is a different style in each space."""
|
| 18 |
+
if name not in _loaded:
|
| 19 |
+
f = np.load(Path(__file__).parent / SPACES[name], allow_pickle=False)
|
| 20 |
+
_loaded[name] = (f["basis"].astype(np.float32), f["sd"].astype(np.float32))
|
| 21 |
+
return _loaded[name]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def coords(code: int, space_name: str = "default") -> np.ndarray:
|
| 25 |
"""The 64 style coordinates for a code."""
|
| 26 |
+
_, sd = space(space_name)
|
| 27 |
+
return np.random.default_rng(code).standard_normal(len(sd)).astype(np.float32) * sd
|
| 28 |
|
| 29 |
|
| 30 |
+
def style_block(code: int, strength: float = 6.0, space_name: str = "default") -> np.ndarray:
|
| 31 |
"""The token block for a code: [108, 6144]."""
|
| 32 |
+
return block_from_coords(coords(code, space_name), strength, space_name)
|
| 33 |
|
| 34 |
|
| 35 |
+
def block_from_coords(z: np.ndarray, strength: float = 6.0, space_name: str = "default") -> np.ndarray:
|
| 36 |
"""The token block for 64 coordinates you supply, for blending or editing styles."""
|
| 37 |
+
basis, _ = space(space_name)
|
| 38 |
+
return strength * np.tensordot(np.asarray(z, dtype=np.float32), basis, axes=1)
|
sref_space_lg.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5639bf3084e17a5efabab794ed2a9b30aadb41a4cc4412255bda8382abd1aafe
|
| 3 |
+
size 84935412
|
sref_space_variant.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f0a7159fcd0100a36638eb5acdb6ae4829d340580601017096c8b21c1d8d542f
|
| 3 |
+
size 84935412
|