autoforge 1.9.2.3__tar.gz → 1.9.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {autoforge-1.9.2.3 → autoforge-1.9.3}/PKG-INFO +3 -2
- {autoforge-1.9.2.3 → autoforge-1.9.3}/README.md +1 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/pyproject.toml +2 -2
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/AutoForge.egg-info/PKG-INFO +3 -2
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/AutoForge.egg-info/SOURCES.txt +2 -1
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/PruningHelper.py +99 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Modules/Optimizer.py +99 -2
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/auto_forge.py +13 -0
- autoforge-1.9.3/tests/test_remove_height_spikes.py +84 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/setup.cfg +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/AutoForge.egg-info/dependency_links.txt +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/AutoForge.egg-info/entry_points.txt +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/AutoForge.egg-info/requires.txt +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/AutoForge.egg-info/top_level.txt +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/AmpUtils.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/CAdamW.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/FilamentHelper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/Heightmaps/ChristofidesHeightMap.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/Heightmaps/DepthEstimateHeightMap.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/Heightmaps/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/ImageHelper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/OptimizerHelper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/OtherHelper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/OutputHelper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/filamentcolors_library.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Init_comparer/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Init_comparer/auto_forge_init_check.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Init_comparer/plot_results.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Loss/LossFunctions.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Loss/PerceptionLoss.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Loss/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Misc/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Misc/color_fit_all.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Modules/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/__init__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/__main__.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/priority_mask_editor.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_amp_autocast_probe.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_cadamw.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_christofides_height_map.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_christofides_heightmap.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_depth_adjusted_height_map.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_depth_estimate_height_map.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_end_to_end_optimizer.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_filament_and_output.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_filament_helper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_flatforge.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_heightmap_init.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_image_helper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_loss_functions.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_optimizer_helper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_optimizer_module.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_optimizer_smoke.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_output_helper.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_output_helper_extended.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_perception_and_pruning.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_property_based.py +0 -0
- {autoforge-1.9.2.3 → autoforge-1.9.3}/tests/test_pruning_helper.py +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: AutoForge
|
|
3
|
-
Version: 1.9.
|
|
3
|
+
Version: 1.9.3
|
|
4
4
|
Summary: AutoForge is a Python tool for generating 3D printed layered models from an input image.
|
|
5
5
|
Author-email: Hendric Voss <hvoss@techfak.uni-bielefeld.de>
|
|
6
6
|
License: CC BY-NC-SA 4.0
|
|
7
|
-
Requires-Python: >=3.
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
8
|
Description-Content-Type: text/markdown
|
|
9
9
|
Requires-Dist: numpy
|
|
10
10
|
Requires-Dist: torch>=2.9.1
|
|
@@ -158,6 +158,7 @@ This will generate separate STL files for each color, allowing you to print face
|
|
|
158
158
|
- `--pruning_max_layer` Max number of layers allowed after pruning (default: 75).
|
|
159
159
|
- `--random_seed` Random seed for reproducibility (default: 0 (disabled)).
|
|
160
160
|
- `--mps` Flag to use the Metal Performance Shaders (MPS) backend if available.
|
|
161
|
+
- `--no-spike-removal` Disable spike removal for the final STL (not recommended).
|
|
161
162
|
|
|
162
163
|
- `--tensorboard` Flag to enable TensorBoard logging.
|
|
163
164
|
- `--run_name` *(Optional)* Name of the run used for TensorBoard logging.
|
|
@@ -133,6 +133,7 @@ This will generate separate STL files for each color, allowing you to print face
|
|
|
133
133
|
- `--pruning_max_layer` Max number of layers allowed after pruning (default: 75).
|
|
134
134
|
- `--random_seed` Random seed for reproducibility (default: 0 (disabled)).
|
|
135
135
|
- `--mps` Flag to use the Metal Performance Shaders (MPS) backend if available.
|
|
136
|
+
- `--no-spike-removal` Disable spike removal for the final STL (not recommended).
|
|
136
137
|
|
|
137
138
|
- `--tensorboard` Flag to enable TensorBoard logging.
|
|
138
139
|
- `--run_name` *(Optional)* Name of the run used for TensorBoard logging.
|
|
@@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "AutoForge"
|
|
7
|
-
version="1.9.
|
|
7
|
+
version="1.9.3"
|
|
8
8
|
description = "AutoForge is a Python tool for generating 3D printed layered models from an input image."
|
|
9
9
|
authors = [{ name = "Hendric Voss", email = "hvoss@techfak.uni-bielefeld.de" }]
|
|
10
10
|
readme = "README.md"
|
|
11
|
-
requires-python = ">=3.
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
12
|
license = { text = "CC BY-NC-SA 4.0" }
|
|
13
13
|
dependencies = [
|
|
14
14
|
"numpy",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: AutoForge
|
|
3
|
-
Version: 1.9.
|
|
3
|
+
Version: 1.9.3
|
|
4
4
|
Summary: AutoForge is a Python tool for generating 3D printed layered models from an input image.
|
|
5
5
|
Author-email: Hendric Voss <hvoss@techfak.uni-bielefeld.de>
|
|
6
6
|
License: CC BY-NC-SA 4.0
|
|
7
|
-
Requires-Python: >=3.
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
8
|
Description-Content-Type: text/markdown
|
|
9
9
|
Requires-Dist: numpy
|
|
10
10
|
Requires-Dist: torch>=2.9.1
|
|
@@ -158,6 +158,7 @@ This will generate separate STL files for each color, allowing you to print face
|
|
|
158
158
|
- `--pruning_max_layer` Max number of layers allowed after pruning (default: 75).
|
|
159
159
|
- `--random_seed` Random seed for reproducibility (default: 0 (disabled)).
|
|
160
160
|
- `--mps` Flag to use the Metal Performance Shaders (MPS) backend if available.
|
|
161
|
+
- `--no-spike-removal` Disable spike removal for the final STL (not recommended).
|
|
161
162
|
|
|
162
163
|
- `--tensorboard` Flag to enable TensorBoard logging.
|
|
163
164
|
- `--run_name` *(Optional)* Name of the run used for TensorBoard logging.
|
|
@@ -5,6 +5,7 @@ import torch
|
|
|
5
5
|
from tqdm import tqdm
|
|
6
6
|
from joblib import Parallel, delayed
|
|
7
7
|
import threading
|
|
8
|
+
import numpy as np
|
|
8
9
|
|
|
9
10
|
from autoforge.Helper.OptimizerHelper import composite_image_disc
|
|
10
11
|
from autoforge.Loss.LossFunctions import compute_loss
|
|
@@ -864,3 +865,101 @@ def optimise_swap_positions(
|
|
|
864
865
|
|
|
865
866
|
outer_tbar.close()
|
|
866
867
|
return best_dg
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
def _compute_loss_for_heightmap(
|
|
871
|
+
optimizer: FilamentOptimizer,
|
|
872
|
+
disc_global: torch.Tensor,
|
|
873
|
+
disc_height: torch.Tensor,
|
|
874
|
+
*,
|
|
875
|
+
custom_height_logits: torch.Tensor | None = None,
|
|
876
|
+
) -> float:
|
|
877
|
+
"""Compute discrete loss for a given discrete height map/global assignment.
|
|
878
|
+
|
|
879
|
+
If ``custom_height_logits`` is provided, they are used directly (bypassing
|
|
880
|
+
discretization) when compositing the image; otherwise the optimizer's current
|
|
881
|
+
best height logits are used.
|
|
882
|
+
"""
|
|
883
|
+
logits_for_disc = disc_to_logits(disc_global, optimizer.material_colors.shape[0], big_pos=1e5)
|
|
884
|
+
with _gpu_lock, torch.no_grad():
|
|
885
|
+
out_im = optimizer.get_best_discretized_image(
|
|
886
|
+
custom_height_logits=custom_height_logits,
|
|
887
|
+
custom_global_logits=logits_for_disc,
|
|
888
|
+
)
|
|
889
|
+
return compute_loss(comp=out_im, target=optimizer.target).item()
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
def _median_without_outliers(window: np.ndarray, threshold: float, max_outliers: int = 2) -> tuple[float, bool, int]:
|
|
893
|
+
"""Return replacement for center pixel if it is a spike; also report spike count.
|
|
894
|
+
|
|
895
|
+
A spike is when the center pixel is >= ``threshold`` above the window median and
|
|
896
|
+
the window has at most ``max_outliers`` such outliers. Returns (replacement_value,
|
|
897
|
+
is_spike, spike_count).
|
|
898
|
+
"""
|
|
899
|
+
med = np.median(window)
|
|
900
|
+
outlier_mask = window - med >= threshold
|
|
901
|
+
spike_count = int(outlier_mask.sum())
|
|
902
|
+
center_is_outlier = bool(outlier_mask[1, 1])
|
|
903
|
+
if not center_is_outlier or spike_count == 0 or spike_count > max_outliers:
|
|
904
|
+
return float(window[1, 1]), False, 0
|
|
905
|
+
non_outliers = window[~outlier_mask]
|
|
906
|
+
if non_outliers.size == 0:
|
|
907
|
+
return float(window[1, 1]), False, 0
|
|
908
|
+
replacement = float(np.median(non_outliers))
|
|
909
|
+
return replacement, True, spike_count
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
def remove_height_spikes(
|
|
913
|
+
disc_height: torch.Tensor, threshold_layers: int = 3, max_outliers: int = 2
|
|
914
|
+
) -> tuple[torch.Tensor, int]:
|
|
915
|
+
"""Remove tall spikes in a discrete height map using a 3x3 neighborhood.
|
|
916
|
+
|
|
917
|
+
A spike occurs only when the *center* pixel of a 3x3 window is at least
|
|
918
|
+
``threshold_layers`` higher than the window median and the window has at most
|
|
919
|
+
two such outliers. Only those center pixels are replaced with the median of
|
|
920
|
+
the non-outlier values. Operates in layer units (0..max_layers).
|
|
921
|
+
|
|
922
|
+
Returns:
|
|
923
|
+
cleaned_height: torch.Tensor matching ``disc_height`` shape/device.
|
|
924
|
+
spike_count: int number of pixels corrected.
|
|
925
|
+
"""
|
|
926
|
+
if disc_height.ndim != 2:
|
|
927
|
+
raise ValueError("disc_height must be 2D [H,W]")
|
|
928
|
+
if threshold_layers <= 0:
|
|
929
|
+
return disc_height, 0
|
|
930
|
+
if max_outliers <= 0:
|
|
931
|
+
return disc_height, 0
|
|
932
|
+
|
|
933
|
+
dh_np = disc_height.detach().cpu().numpy()
|
|
934
|
+
H, W = dh_np.shape
|
|
935
|
+
if H < 3 or W < 3:
|
|
936
|
+
return disc_height, 0
|
|
937
|
+
|
|
938
|
+
out = dh_np.copy()
|
|
939
|
+
spikes = 0
|
|
940
|
+
tbar = tqdm(range(1, H - 1), desc="Removing height spikes", leave=False)
|
|
941
|
+
for y in tbar:
|
|
942
|
+
for x in range(1, W - 1):
|
|
943
|
+
window = dh_np[y - 1 : y + 2, x - 1 : x + 2]
|
|
944
|
+
med_all = np.median(window)
|
|
945
|
+
mask = window - med_all >= threshold_layers
|
|
946
|
+
spike_count = int(mask.sum())
|
|
947
|
+
if not mask[1, 1] or spike_count == 0 or spike_count > max_outliers:
|
|
948
|
+
continue
|
|
949
|
+
|
|
950
|
+
non_outliers = window[~mask]
|
|
951
|
+
if non_outliers.size == 0:
|
|
952
|
+
continue
|
|
953
|
+
med_non_outliers = float(np.median(non_outliers))
|
|
954
|
+
center_val = float(window[1, 1])
|
|
955
|
+
if center_val < med_non_outliers + threshold_layers:
|
|
956
|
+
continue
|
|
957
|
+
|
|
958
|
+
out[y, x] = med_non_outliers # only replace the center spike
|
|
959
|
+
spikes += 1
|
|
960
|
+
|
|
961
|
+
if spikes == 0:
|
|
962
|
+
return disc_height, 0
|
|
963
|
+
|
|
964
|
+
cleaned = torch.tensor(out, dtype=disc_height.dtype, device=disc_height.device)
|
|
965
|
+
return cleaned, spikes
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import argparse
|
|
2
2
|
import random
|
|
3
|
+
import os
|
|
3
4
|
from typing import Optional
|
|
4
5
|
|
|
5
6
|
import matplotlib.pyplot as plt
|
|
@@ -259,6 +260,31 @@ class FilamentOptimizer:
|
|
|
259
260
|
offsets = gathered * mask # zero-out background
|
|
260
261
|
return pixel_logits + offsets
|
|
261
262
|
|
|
263
|
+
def _remove_height_offset(
|
|
264
|
+
self,
|
|
265
|
+
pixel_logits: Optional[torch.Tensor] = None,
|
|
266
|
+
height_offsets: Optional[torch.Tensor] = None,
|
|
267
|
+
):
|
|
268
|
+
if pixel_logits is None:
|
|
269
|
+
pixel_logits = self.pixel_height_logits
|
|
270
|
+
if height_offsets is None:
|
|
271
|
+
height_offsets = self.height_offsets
|
|
272
|
+
|
|
273
|
+
# Differentiable gather of per-cluster offsets with zero for background (label == 0)
|
|
274
|
+
labels = self.pixel_height_labels.to(torch.long) # [H, W]
|
|
275
|
+
offsets_1d = height_offsets.squeeze(-1) # [L]
|
|
276
|
+
|
|
277
|
+
# Same gradient scaling behavior as apply
|
|
278
|
+
s = getattr(self, "height_offsets_grad_scale", 1.0)
|
|
279
|
+
offsets_1d = offsets_1d * s + offsets_1d.detach() * (1.0 - s)
|
|
280
|
+
|
|
281
|
+
# Map each pixel label to its cluster offset
|
|
282
|
+
gathered = offsets_1d[labels] # [H, W]
|
|
283
|
+
mask = (labels != 0).to(gathered.dtype)
|
|
284
|
+
offsets = gathered * mask # zero-out background
|
|
285
|
+
|
|
286
|
+
return pixel_logits - offsets
|
|
287
|
+
|
|
262
288
|
def _get_tau(self):
|
|
263
289
|
"""
|
|
264
290
|
Compute tau for height & global given how many steps we've done.
|
|
@@ -637,10 +663,22 @@ class FilamentOptimizer:
|
|
|
637
663
|
prune_num_swaps,
|
|
638
664
|
prune_redundant_layers,
|
|
639
665
|
optimise_swap_positions,
|
|
666
|
+
remove_height_spikes,
|
|
667
|
+
_compute_loss_for_heightmap,
|
|
640
668
|
)
|
|
641
669
|
|
|
642
670
|
if search_seed:
|
|
643
|
-
self.rng_seed_search(self.best_discrete_loss,
|
|
671
|
+
self.rng_seed_search(self.best_discrete_loss, 200, autoset_seed=True)
|
|
672
|
+
|
|
673
|
+
# Post-pruning spike cleanup
|
|
674
|
+
#if getattr(self.args, "spike_removal", False):
|
|
675
|
+
# self.post_remove_spikes()
|
|
676
|
+
|
|
677
|
+
#Calculate and Print current loss
|
|
678
|
+
dg, dh = self.get_discretized_solution(best=True)
|
|
679
|
+
if dh is not None:
|
|
680
|
+
current_loss = _compute_loss_for_heightmap(self, dg, dh)
|
|
681
|
+
print(f"Pre-prune discrete loss: {current_loss:.4f}")
|
|
644
682
|
|
|
645
683
|
# clear pytorch and system cache to reduce vram usage
|
|
646
684
|
torch.cuda.empty_cache()
|
|
@@ -677,6 +715,61 @@ class FilamentOptimizer:
|
|
|
677
715
|
)
|
|
678
716
|
|
|
679
717
|
optimise_swap_positions(self)
|
|
718
|
+
if getattr(self.args, "spike_removal", False):
|
|
719
|
+
self.post_remove_spikes()
|
|
720
|
+
# Calculate and Print current loss
|
|
721
|
+
dg, dh = self.get_discretized_solution(best=True)
|
|
722
|
+
if dh is not None:
|
|
723
|
+
current_loss = _compute_loss_for_heightmap(self, dg, dh)
|
|
724
|
+
print(f"Post-prune discrete loss: {current_loss:.4f}")
|
|
725
|
+
|
|
726
|
+
def post_remove_spikes(self):
|
|
727
|
+
|
|
728
|
+
from autoforge.Helper.PruningHelper import (
|
|
729
|
+
remove_height_spikes,
|
|
730
|
+
_compute_loss_for_heightmap,
|
|
731
|
+
)
|
|
732
|
+
dg_post, dh_post = self.get_discretized_solution(best=True)
|
|
733
|
+
if dh_post is not None:
|
|
734
|
+
pre_loss = _compute_loss_for_heightmap(self, dg_post, dh_post)
|
|
735
|
+
|
|
736
|
+
# Work on continuous height map to avoid discretization and numpy round-trips.
|
|
737
|
+
eff_logits = self._apply_height_offset(
|
|
738
|
+
self.best_params["pixel_height_logits"],
|
|
739
|
+
self.best_params["height_offsets"],
|
|
740
|
+
)
|
|
741
|
+
height_map = torch.sigmoid(eff_logits) * float(self.max_layers)
|
|
742
|
+
|
|
743
|
+
dh_clean, spikes = remove_height_spikes(
|
|
744
|
+
height_map, threshold_layers=self.args.spike_threshold_layers
|
|
745
|
+
)
|
|
746
|
+
|
|
747
|
+
normalized = dh_clean.clamp(0, self.max_layers) / float(self.max_layers)
|
|
748
|
+
cleaned_logits = self._remove_height_offset(
|
|
749
|
+
pixel_logits=torch.log(normalized) - torch.log1p(-normalized),
|
|
750
|
+
height_offsets=self.best_params["height_offsets"],
|
|
751
|
+
)
|
|
752
|
+
# normalized = normalized.clamp(1e-6, 1 - 1e-6)
|
|
753
|
+
# cleaned_logits = torch.log(normalized) - torch.log1p(-normalized)
|
|
754
|
+
self.best_params["pixel_height_logits"] = cleaned_logits.to(
|
|
755
|
+
self.device
|
|
756
|
+
)
|
|
757
|
+
self.pixel_height_logits = cleaned_logits.to(self.device)
|
|
758
|
+
dg_post, dh_post = self.get_discretized_solution(best=True)
|
|
759
|
+
post_loss = _compute_loss_for_heightmap(self, dg_post, dh_post)
|
|
760
|
+
print(
|
|
761
|
+
f"Spike removal: loss {pre_loss:.4f} -> {post_loss:.4f} | spikes fixed {spikes}"
|
|
762
|
+
)
|
|
763
|
+
try:
|
|
764
|
+
with open(
|
|
765
|
+
os.path.join(self.args.output_folder, "spike_removal_stats.txt"),
|
|
766
|
+
"a",
|
|
767
|
+
) as f:
|
|
768
|
+
f.write(
|
|
769
|
+
f"post_prune,threshold_layers={self.args.spike_threshold_layers},spikes={spikes},loss_before={pre_loss:.6f},loss_after={post_loss:.6f}\n"
|
|
770
|
+
)
|
|
771
|
+
except Exception:
|
|
772
|
+
pass
|
|
680
773
|
|
|
681
774
|
def _maybe_update_best_discrete(self):
|
|
682
775
|
"""
|
|
@@ -743,7 +836,8 @@ class FilamentOptimizer:
|
|
|
743
836
|
"""
|
|
744
837
|
best_seed = None
|
|
745
838
|
best_loss = start_loss
|
|
746
|
-
|
|
839
|
+
tbar = tqdm(range(num_seeds), desc="Searching for new best seed")
|
|
840
|
+
for i in tbar:
|
|
747
841
|
seed = np.random.randint(0, 1000000)
|
|
748
842
|
effective_logits = self._apply_height_offset(
|
|
749
843
|
self.best_params["pixel_height_logits"],
|
|
@@ -769,6 +863,9 @@ class FilamentOptimizer:
|
|
|
769
863
|
if current_disc_loss < best_loss:
|
|
770
864
|
best_loss = current_disc_loss
|
|
771
865
|
best_seed = seed
|
|
866
|
+
tbar.set_postfix(
|
|
867
|
+
best_loss=f"{best_loss:.4f}"
|
|
868
|
+
)
|
|
772
869
|
if autoset_seed and best_loss < start_loss:
|
|
773
870
|
self.best_seed = best_seed
|
|
774
871
|
return best_seed, best_loss
|
|
@@ -209,6 +209,19 @@ def parse_args() -> argparse.Namespace:
|
|
|
209
209
|
help="Percentage of increment search for fast pruning",
|
|
210
210
|
)
|
|
211
211
|
|
|
212
|
+
parser.add_argument(
|
|
213
|
+
"--spike_removal",
|
|
214
|
+
default=True,
|
|
215
|
+
help="Enable spike removal on the discrete height map before/after pruning to smooth isolated tall pixels",
|
|
216
|
+
action=argparse.BooleanOptionalAction,
|
|
217
|
+
)
|
|
218
|
+
parser.add_argument(
|
|
219
|
+
"--spike_threshold_layers",
|
|
220
|
+
type=int,
|
|
221
|
+
default=1,
|
|
222
|
+
help="Minimum layer delta above the local median to treat a pixel as a spike in a 3x3 window",
|
|
223
|
+
)
|
|
224
|
+
|
|
212
225
|
parser.add_argument(
|
|
213
226
|
"--pruning_max_colors",
|
|
214
227
|
type=int,
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
import numpy as np
|
|
3
|
+
import torch
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from autoforge.Helper.PruningHelper import remove_height_spikes
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@pytest.mark.parametrize("threshold_layers", [1, 2, 3])
|
|
10
|
+
@pytest.mark.parametrize("max_outliers", [1, 2])
|
|
11
|
+
def test_no_change_when_no_outliers(threshold_layers, max_outliers):
|
|
12
|
+
base = np.array([
|
|
13
|
+
[5, 5, 5],
|
|
14
|
+
[5, 5, 5],
|
|
15
|
+
[5, 5, 5],
|
|
16
|
+
], dtype=np.float32)
|
|
17
|
+
dh = torch.tensor(base)
|
|
18
|
+
cleaned, spikes = remove_height_spikes(dh, threshold_layers=threshold_layers, max_outliers=max_outliers)
|
|
19
|
+
assert spikes == 0
|
|
20
|
+
assert torch.equal(cleaned, dh)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _all_heightmaps_with_k_outliers(k: int, high: float, low: float = 0.0):
|
|
24
|
+
"""Generate all 3x3 patterns with exactly k highs and the rest lows."""
|
|
25
|
+
highs = [high] * k
|
|
26
|
+
lows = [low] * (9 - k)
|
|
27
|
+
for perm in set(itertools.permutations(highs + lows)):
|
|
28
|
+
grid = np.array(perm, dtype=np.float32).reshape(3, 3)
|
|
29
|
+
yield grid
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@pytest.mark.parametrize("max_outliers", [1, 2])
|
|
33
|
+
@pytest.mark.parametrize("threshold_layers", [1, 2, 3])
|
|
34
|
+
def test_single_center_outlier_is_fixed(max_outliers, threshold_layers):
|
|
35
|
+
high = threshold_layers + 5
|
|
36
|
+
grid = np.zeros((3, 3), dtype=np.float32)
|
|
37
|
+
grid[1, 1] = high
|
|
38
|
+
dh = torch.tensor(grid)
|
|
39
|
+
cleaned, spikes = remove_height_spikes(dh, threshold_layers=threshold_layers, max_outliers=max_outliers)
|
|
40
|
+
assert spikes == 1
|
|
41
|
+
# New center should be median of surrounding lows (which are all zero here)
|
|
42
|
+
assert cleaned[1, 1].item() == pytest.approx(0.0)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@pytest.mark.parametrize("max_outliers", [1, 2])
|
|
46
|
+
@pytest.mark.parametrize("threshold_layers", [1, 2, 3])
|
|
47
|
+
def test_two_centered_outliers_allowed(max_outliers, threshold_layers):
|
|
48
|
+
if max_outliers < 2:
|
|
49
|
+
pytest.skip("max_outliers too small for two-outlier case")
|
|
50
|
+
high = threshold_layers + 5
|
|
51
|
+
# Place two highs: center and one neighbor
|
|
52
|
+
for dy, dx in [(0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)]:
|
|
53
|
+
grid = np.zeros((3, 3), dtype=np.float32)
|
|
54
|
+
grid[1, 1] = high
|
|
55
|
+
grid[1 + dy, 1 + dx] = high
|
|
56
|
+
dh = torch.tensor(grid)
|
|
57
|
+
cleaned, spikes = remove_height_spikes(dh, threshold_layers=threshold_layers, max_outliers=max_outliers)
|
|
58
|
+
# Center should be fixed (one spike counted) because total outliers=2 and center is one of them
|
|
59
|
+
assert spikes == 1
|
|
60
|
+
assert cleaned[1, 1].item() == pytest.approx(0.0)
|
|
61
|
+
# The neighbor high should remain since we only replace center
|
|
62
|
+
assert cleaned[1 + dy, 1 + dx].item() == pytest.approx(high)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@pytest.mark.parametrize("threshold_layers", [1, 2, 3])
|
|
66
|
+
def test_more_than_allowed_outliers_not_changed(threshold_layers):
|
|
67
|
+
high = threshold_layers + 5
|
|
68
|
+
grid = np.full((3, 3), high, dtype=np.float32)
|
|
69
|
+
dh = torch.tensor(grid)
|
|
70
|
+
cleaned, spikes = remove_height_spikes(dh, threshold_layers=threshold_layers, max_outliers=2)
|
|
71
|
+
# Too many outliers (9), so nothing should change
|
|
72
|
+
assert spikes == 0
|
|
73
|
+
assert torch.equal(cleaned, dh)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.parametrize("threshold_layers", [1, 2, 3])
|
|
77
|
+
def test_center_not_outlier_not_changed(threshold_layers):
|
|
78
|
+
grid = np.zeros((3, 3), dtype=np.float32)
|
|
79
|
+
grid[0, 0] = threshold_layers + 5 # corner high, center low
|
|
80
|
+
dh = torch.tensor(grid)
|
|
81
|
+
cleaned, spikes = remove_height_spikes(dh, threshold_layers=threshold_layers, max_outliers=2)
|
|
82
|
+
assert spikes == 0
|
|
83
|
+
assert torch.equal(cleaned, dh)
|
|
84
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/Heightmaps/ChristofidesHeightMap.py
RENAMED
|
File without changes
|
{autoforge-1.9.2.3 → autoforge-1.9.3}/src/autoforge/Helper/Heightmaps/DepthEstimateHeightMap.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|