-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
tianhaoxie
wants to merge
6
commits into
XingangPan:main
Choose a base branch
from
tianhaoxie:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd9c8ec
Add files via upload
tianhaoxie 54403de
Add files via upload
tianhaoxie 2c428ff
Add files via upload
tianhaoxie d6aa972
Add files via upload
tianhaoxie 5dc6156
Update .gitignore
tianhaoxie caeb80f
Merge branch 'XingangPan:main' into main
tianhaoxie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and for the model, |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @tianhaoxie , you probably don't want to commit .pyc files here
please add it to
.gitignore
on the main repoThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion!