Skip to content

Commit fc127ae

Browse files
committed
🎶 Octaves set automatically from target size, or by command.
1 parent ef1159f commit fc127ae

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/texturize/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
--variations=V Number of images to generate at same time. [default: 1]
3131
--seed=SEED Configure the random number generation.
3232
--mode=MODE Either "patch" or "gram" to manually specify critics.
33-
--octaves=O Number of octaves to process. [default: 5]
33+
--octaves=O Number of octaves to process. Defaults to 5 for 512x512, or
34+
4 for 256x256 equivalent pixel count.
3435
--threshold=T Quality for optimization, lower is better. Defaults to 1e-3
3536
for "patch" and 1e-7 for "gram".
3637
--iterations=I Maximum number of iterations each octave. [default: 99]
@@ -51,7 +52,6 @@
5152

5253
import os
5354
import glob
54-
import math
5555
import itertools
5656

5757
import docopt
@@ -83,7 +83,7 @@ def split_string(text: str):
8383
"variations": Use(int),
8484
"seed": Or(None, Use(int)),
8585
"mode": Or(None, "patch", "gram", "hist"),
86-
"octaves": Use(int),
86+
"octaves": Or(None, Use(int)),
8787
"threshold": Or(None, Use(float)),
8888
"iterations": Use(int),
8989
"device": Or(None, "cpu", "cuda"),
@@ -134,7 +134,7 @@ def main():
134134
if command == "enhance":
135135
zoom = config["zoom"]
136136
cmd = commands.Enhance(target_img, source_img, mode=mode, zoom=zoom)
137-
config["octaves"] = int(math.log(zoom, 2) + 1.0)
137+
config["octaves"] = cmd.octaves
138138
config["size"] = (target_img.size[0] * zoom, target_img.size[1] * zoom)
139139
if command == "remake":
140140
cmd = commands.Remake(

src/texturize/api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# neural-texturize — Copyright (c) 2020, Novelty Factory KG. See LICENSE for details.
22

33
import os
4+
import math
45

56
import torch
67
import torch.nn.functional as F
@@ -16,7 +17,7 @@ def process_iterations(
1617
cmd,
1718
log: object = None,
1819
size: tuple = None,
19-
octaves: int = -1,
20+
octaves: int = None,
2021
variations: int = 1,
2122
iterations: int = 200,
2223
threshold: float = 1e-5,
@@ -26,9 +27,13 @@ def process_iterations(
2627
"""Synthesize a new texture and return a PyTorch tensor at each iteration.
2728
"""
2829

30+
# Configure the default options dynamically, unless overriden.
31+
threshold = threshold or {"patch": 1e-3, "gram": 1e-7, "hist": 1e-6}[cmd.mode]
32+
factor = math.sqrt((size[0] * size[1]) / (32 ** 2))
33+
octaves = octaves or getattr(cmd, "octaves", int(math.log(factor, 2) + 1.0))
34+
2935
# Setup the application to use throughout the synthesis.
3036
app = Application(log, device, precision)
31-
threshold = threshold or {"patch": 1e-3, "gram": 1e-7, "hist": 1e-6}[cmd.mode]
3237

3338
# Encoder used by all the critics at every octave.
3439
encoder = models.VGG11(pretrained=True, pool_type=torch.nn.AvgPool2d)

src/texturize/commands.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# neural-texturize — Copyright (c) 2020, Novelty Factory KG. See LICENSE for details.
22

3+
import math
4+
35
import torch
46
import torch.nn.functional as F
57

@@ -89,8 +91,9 @@ def prepare_seed_tensor(self, app, size, previous=None):
8991

9092

9193
class Enhance(Command):
92-
def __init__(self, target, source, mode=None):
94+
def __init__(self, target, source, mode=None, zoom=1):
9395
self.mode = mode or "gram"
96+
self.octaves = int(math.log(zoom, 2) + 1.0)
9497
self.critics = list(create_default_critics(self.mode).values())
9598
self.source = load_tensor_from_image(source, device="cpu")
9699
self.target = load_tensor_from_image(target, device="cpu")

0 commit comments

Comments
 (0)