Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor loss plumbing #204

Open
wants to merge 19 commits into
base: test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/pytti/LossAug/DepthLossClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def init_AdaBins(device=None):
class DepthLoss(MSELoss):
@torch.no_grad()
def set_comp(self, pil_image):
# pil_image = pil_image.resize(self.image_shape, Image.LANCZOS)
self.comp.set_(DepthLoss.make_comp(pil_image))
if self.use_mask and self.mask.shape[-2:] != self.comp.shape[-2:]:
self.mask.set_(TF.resize(self.mask, self.comp.shape[-2:]))
Expand Down
26 changes: 1 addition & 25 deletions src/pytti/LossAug/LatentLossClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,13 @@ def __init__(
TF.resize(comp.clone(), (h, w)), weight, stop, name, image_shape
)

# Comp and mask should live on the image representation, not the loss class.
@torch.no_grad()
def set_comp(self, pil_image, device=DEVICE):
self.pil_image = pil_image
self.has_latent = False
self.direct_loss.set_comp(pil_image.resize(self.image_shape, Image.LANCZOS))

@classmethod
@vram_usage_mode("Latent Image Loss")
@torch.no_grad()
def TargetImage(
cls, prompt_string, image_shape, pil_image=None, is_path=False, device=DEVICE
):
text, weight, stop = parse(
prompt_string, r"(?<!^http)(?<!s):|:(?!/)", ["", "1", "-inf"]
)
weight, mask = parse(weight, r"_", ["1", ""])
text = text.strip()
mask = mask.strip()
if pil_image is None and text != "" and is_path:
pil_image = Image.open(fetch(text)).convert("RGB")
comp = (
MSELoss.make_comp(pil_image)
if pil_image is not None
else torch.zeros(1, 1, 1, 1, device=device)
)
out = cls(comp, weight, stop, text + " (latent)", image_shape)
if pil_image is not None:
out.set_comp(pil_image)
out.set_mask(mask)
return out

def set_mask(self, mask, inverted=False):
self.direct_loss.set_mask(mask, inverted)
super().set_mask(mask, inverted)
Expand Down
Loading