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

Ease the visualization of latent directions #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 34 additions & 21 deletions apply_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
default=2,
help='channel multiplier factor. config-f = 2, else = 1',
)
parser.add_argument(
"-d_num",
"--degree_num",
type=int,
default=3,
help="number of scalar factors for moving latent vectors along eigenvector",
)
parser.add_argument("--ckpt", type=str, required=True, help="stylegan2 checkpoints")
parser.add_argument(
"--size", type=int, default=256, help="output image size of the generator"
Expand Down Expand Up @@ -64,31 +71,37 @@
latent = torch.randn(args.n_sample, 512, device=args.device)
latent = g.get_latent(latent)

direction = args.degree * eigvec[:, args.index].unsqueeze(0)
direction = eigvec[:, args.index].unsqueeze(0)

img, _ = g(
[latent],
truncation=args.truncation,
truncation_latent=trunc,
input_is_latent=True,
)
img1, _ = g(
[latent + direction],
truncation=args.truncation,
truncation_latent=trunc,
input_is_latent=True,
)
img2, _ = g(
[latent - direction],
truncation=args.truncation,
truncation_latent=trunc,
input_is_latent=True,
)
img_dict = dict()

for u in torch.linspace(- args.degree, args.degree, args.degree_num):

img_batch, _ = g(
[latent + u * direction],
truncation=args.truncation,
truncation_latent=trunc,
input_is_latent=True,
)

for j in range(img_batch.shape[0]):

img = img_batch[j].unsqueeze(0)

try:
img_dict[j].append(img)
except KeyError:
img_dict[j] = [img]

img_list = [
torch.cat(img_dict[j], 0)
for j in range(args.n_sample)
]

grid = utils.save_image(
torch.cat([img1, img, img2], 0),
torch.cat(img_list, 0),
f"{args.out_prefix}_index-{args.index}_degree-{args.degree}.png",
normalize=True,
range=(-1, 1),
nrow=args.n_sample,
nrow=args.degree_num,
)