Transcendental-Programmer commited on
Commit
e3af1ef
Β·
1 Parent(s): 24f4867

Refactor core logic: move and modularize all latent space, sampling, and utility code into faceforge_core/

Browse files
{latent_space_explorer β†’ faceforge_core}/__init__.py RENAMED
File without changes
faceforge_core/attribute_directions.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from typing import Tuple, List, Optional
3
+ from sklearn.decomposition import PCA
4
+ from sklearn.linear_model import LogisticRegression
5
+
6
+ class LatentDirectionFinder:
7
+ """
8
+ Provides methods to discover semantic directions in latent space using PCA or classifier-based approaches.
9
+ """
10
+ def __init__(self, latent_vectors: np.ndarray):
11
+ """
12
+ :param latent_vectors: Array of shape (N, D) where N is the number of samples and D is the latent dimension.
13
+ """
14
+ self.latent_vectors = latent_vectors
15
+
16
+ def pca_direction(self, n_components: int = 10) -> Tuple[np.ndarray, np.ndarray]:
17
+ """
18
+ Perform PCA on the latent vectors to find principal directions.
19
+ :return: (components, explained_variance)
20
+ """
21
+ pca = PCA(n_components=n_components)
22
+ pca.fit(self.latent_vectors)
23
+ return pca.components_, pca.explained_variance_ratio_
24
+
25
+ def classifier_direction(self, labels: List[int]) -> np.ndarray:
26
+ """
27
+ Fit a linear classifier to find a direction separating two classes in latent space.
28
+ :param labels: List of 0/1 labels for each latent vector.
29
+ :return: Normalized direction vector (D,)
30
+ """
31
+ clf = LogisticRegression()
32
+ clf.fit(self.latent_vectors, labels)
33
+ direction = clf.coef_[0]
34
+ direction = direction / np.linalg.norm(direction)
35
+ return direction
faceforge_core/custom_loss.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from typing import Callable
4
+
5
+ def attribute_preserving_loss(
6
+ generated: torch.Tensor,
7
+ original: torch.Tensor,
8
+ attr_predictor: Callable[[torch.Tensor], torch.Tensor],
9
+ y_target: torch.Tensor,
10
+ lambda_pred: float = 1.0,
11
+ lambda_recon: float = 1.0
12
+ ) -> torch.Tensor:
13
+ """
14
+ Custom loss enforcing attribute fidelity and identity preservation.
15
+ L_attr(G(z + alpha d)) = lambda_pred * ||f_attr(G(.)) - y_target||^2 + lambda_recon * ||G(z + alpha d) - G(z)||^2
16
+ :param generated: Generated image tensor (B, ...)
17
+ :param original: Original image tensor (B, ...)
18
+ :param attr_predictor: Function mapping image tensor to attribute prediction
19
+ :param y_target: Target attribute value tensor (B, ...)
20
+ :param lambda_pred: Weight for attribute prediction loss
21
+ :param lambda_recon: Weight for reconstruction loss
22
+ :return: Scalar loss tensor
23
+ """
24
+ pred_loss = torch.nn.functional.mse_loss(attr_predictor(generated), y_target)
25
+ recon_loss = torch.nn.functional.mse_loss(generated, original)
26
+ return lambda_pred * pred_loss + lambda_recon * recon_loss
{latent_space_explorer β†’ faceforge_core}/fast_sd.py RENAMED
File without changes
{latent_space_explorer β†’ faceforge_core}/game_objects.py RENAMED
File without changes
{latent_space_explorer β†’ faceforge_core}/hacked_sdxl_pipeline.py RENAMED
File without changes
faceforge_core/latent_explorer.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from typing import List, Optional, Tuple
3
+
4
+ class LatentPoint:
5
+ """
6
+ Represents a point in latent space with an associated prompt and encoding.
7
+ """
8
+ def __init__(self, text: str, encoding: Optional[np.ndarray], xy_pos: Optional[Tuple[float, float]] = None):
9
+ self.text = text
10
+ self.encoding = encoding
11
+ self.xy_pos = xy_pos if xy_pos is not None else (0.0, 0.0)
12
+
13
+ def move(self, new_xy_pos: Tuple[float, float]):
14
+ self.xy_pos = new_xy_pos
15
+
16
+ class LatentSpaceExplorer:
17
+ """
18
+ Core logic for managing points in latent space and sampling new points.
19
+ """
20
+ def __init__(self):
21
+ self.points: List[LatentPoint] = []
22
+ self.selected_point_idx: Optional[int] = None
23
+
24
+ def add_point(self, text: str, encoding: Optional[np.ndarray], xy_pos: Optional[Tuple[float, float]] = None):
25
+ self.points.append(LatentPoint(text, encoding, xy_pos))
26
+
27
+ def delete_point(self, idx: int):
28
+ if 0 <= idx < len(self.points):
29
+ del self.points[idx]
30
+
31
+ def modify_point(self, idx: int, new_text: str, new_encoding: Optional[np.ndarray]):
32
+ if 0 <= idx < len(self.points):
33
+ self.points[idx].text = new_text
34
+ self.points[idx].encoding = new_encoding
35
+
36
+ def get_encodings(self) -> List[Optional[np.ndarray]]:
37
+ return [p.encoding for p in self.points]
38
+
39
+ def get_prompts(self) -> List[str]:
40
+ return [p.text for p in self.points]
41
+
42
+ def get_positions(self) -> np.ndarray:
43
+ return np.array([p.xy_pos for p in self.points])
44
+
45
+ def sample_encoding(self, point: Tuple[float, float], mode: str = "distance") -> Optional[np.ndarray]:
46
+ """
47
+ Sample a new encoding based on the given point and mode.
48
+ """
49
+ encodings = self.get_encodings()
50
+ positions = self.get_positions()
51
+ if not encodings or len(encodings) == 0:
52
+ return None
53
+ if mode == "distance":
54
+ dists = np.linalg.norm(positions - np.array(point), axis=1)
55
+ coefs = 1.0 / (1.0 + dists ** 2)
56
+ elif mode == "circle":
57
+ point_vec = np.array(point)
58
+ positions_vec = positions
59
+ coefs = np.dot(positions_vec, point_vec)
60
+ else:
61
+ raise ValueError(f"Unknown sampling mode: {mode}")
62
+ coefs = coefs / np.sum(coefs)
63
+ # Weighted sum of encodings
64
+ result = None
65
+ for coef, enc in zip(coefs, encodings):
66
+ if enc is not None:
67
+ if result is None:
68
+ result = coef * enc
69
+ else:
70
+ result += coef * enc
71
+ return result
{latent_space_explorer β†’ faceforge_core}/sampling.py RENAMED
File without changes
{latent_space_explorer β†’ faceforge_core}/utils.py RENAMED
File without changes