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

Adding functions to input custom image #142

Open
wants to merge 6 commits into
base: main
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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__pycache__/
*.py[cod]
*$py.class
*.pyc

# C extensions
*.so
Expand Down
53 changes: 53 additions & 0 deletions gan_inv/PTI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import torch
from inversion import inverse_image,get_lr

from tqdm import tqdm
from torch.nn import functional as F
from lpips import util
def toogle_grad(model, flag=True):
for p in model.parameters():
p.requires_grad = flag


class PTI:
def __init__(self,G,l2_lambda = 1,max_pti_step = 400, pti_lr = 3e-4 ):
self.g_ema = G
self.l2_lambda = l2_lambda
self.max_pti_step = max_pti_step
self.pti_lr = pti_lr
def cacl_loss(self,percept, generated_image,real_image):

mse_loss = F.mse_loss(generated_image, real_image)
p_loss = percept(generated_image, real_image).sum()
loss = p_loss +self.l2_lambda * mse_loss
return loss

def train(self,img):
inversed_result = inverse_image(self.g_ema,img,self.g_ema.img_resolution)
w_pivot = inversed_result['latent']
ws = w_pivot.repeat([1, self.g_ema.mapping.num_ws, 1])
toogle_grad(self.g_ema,True)
percept = util.PerceptualLoss(
model="net-lin", net="vgg", use_gpu='cuda:0'
)
optimizer = torch.optim.Adam(self.g_ema.parameters(), lr=self.pti_lr)
print('start PTI')
pbar = tqdm(range(self.max_pti_step))
for i in pbar:
lr = get_lr(i, self.pti_lr)
optimizer.param_groups[0]["lr"] = lr

generated_image,feature = self.g_ema.synthesis(ws,noise_mode='const')
loss = self.cacl_loss(percept,generated_image,inversed_result['real'])
pbar.set_description(
(
f"loss: {loss.item():.4f}"
)
)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
generated_image = self.g_ema.synthesis(ws, noise_mode='const')

return generated_image
9 changes: 9 additions & 0 deletions gan_inv/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

# empty
Binary file added gan_inv/__pycache__/PTI.cpython-39.pyc
Binary file not shown.
Binary file added gan_inv/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added gan_inv/__pycache__/inversion.cpython-39.pyc
Binary file not shown.
Binary file added gan_inv/checkpoints/weights/v0.1/vgg.pth
Binary file not shown.
Loading