forked from rosinality/stylegan2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style_transfer_demo.py
executable file
·277 lines (237 loc) · 9.96 KB
/
style_transfer_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import argparse
import math
import random
import os
import numpy as np
import torch
from torch import nn, autograd, optim
from torch.nn import functional as F
from torch.utils import data
import torch.distributed as dist
from torchvision import transforms, utils
from torchvision.io import write_video
from PIL import Image
from tqdm import tqdm
import util
import pdb
st = pdb.set_trace
try:
import wandb
except ImportError:
wandb = None
from model import Generator, Discriminator
from idinvert_pytorch.models.perceptual_model import VGG16
from dataset import MultiResolutionDataset, VideoFolderDataset
from distributed import (
get_rank,
synchronize,
reduce_loss_dict,
reduce_sum,
get_world_size,
)
from non_leaking import augment, AdaptiveAugment
def data_sampler(dataset, shuffle, distributed):
if distributed:
return data.distributed.DistributedSampler(dataset, shuffle=shuffle)
if shuffle:
return data.RandomSampler(dataset)
else:
return data.SequentialSampler(dataset)
def requires_grad(model, flag=True):
for p in model.parameters():
p.requires_grad = flag
def sample_data(loader):
# Endless iterator
while True:
for batch in loader:
yield batch
def set_grad_none(model, targets):
for n, p in model.named_parameters():
if n in targets:
p.grad = None
def accumulate_batches(data_iter, num):
samples = []
while num > 0:
data = next(data_iter)
samples.append(data)
num -= data.size(0)
samples = torch.cat(samples, dim=0)
if num < 0:
samples = samples[:num, ...]
return samples
def adjust_batch(data, group=1):
# Adjust batch size to multiple of group
# data is of shape [N, C, H, W]
batch = data.shape[0]
if batch % group == 0:
return data
batch_new = int(math.ceil(batch / group * 1.0) * group)
repeat_dims = [int(math.ceil(batch_new / batch * 1.0))] + [1] * (data.ndim - 1)
return data.repeat(*repeat_dims)
def encode(encoder, seq, args):
# seq is of shape [T, C, H, W]
T = seq.shape[0]
if T % args.stddev_group != 0:
seq = adjust_batch(seq, args.stddev_group)
latent = encoder(seq) # shape [N, n_latent, 512]
latent = latent[:T, ...]
else:
latent = encoder(seq) # shape [T, n_latent, 512]
return latent
@torch.no_grad()
def run(args, loader, encoder, generator, device):
if args.distributed:
e_module = encoder.module
g_module = generator.module
else:
e_module = encoder
g_module = generator
requires_grad(encoder, False)
requires_grad(generator, False)
encoder.eval()
generator.eval()
counter = 0
for data in tqdm(loader):
counter += 1
real_seq = data['frames']
real_seq = real_seq.to(device) # shape [2, T, 3, H, W]
seqA, seqB = real_seq[0, ...], real_seq[1, ...]
latA = encode(encoder, seqA, args)
latB = encode(encoder, seqB, args)
coA, moA = latA[0, ...], latA[1:, ...] - latA[0, ...]
coB, moB = latB[0, ...], latB[1:, ...] - latB[0, ...]
# recon
recA, _ = generator([coA + moA], input_is_latent=True, return_latents=False)
recB, _ = generator([coB + moB], input_is_latent=True, return_latents=False)
# swap
coAmoB, _ = generator([coA + moB], input_is_latent=True, return_latents=False)
coBmoA, _ = generator([coB + moA], input_is_latent=True, return_latents=False)
# orig
oriA = seqA[1:, ...]
oriB = seqB[1:, ...]
# save images and videos
T = seqA.shape[0]
utils.save_image(oriA, os.path.join(args.output_dir, f'{counter:03d}-img_origA.png'), nrow=T-1, normalize=True, value_range=(-1, 1))
utils.save_image(oriB, os.path.join(args.output_dir, f'{counter:03d}-img_origB.png'), nrow=T-1, normalize=True, value_range=(-1, 1))
utils.save_image(recA, os.path.join(args.output_dir, f'{counter:03d}-img_reconA.png'), nrow=T-1, normalize=True, value_range=(-1, 1))
utils.save_image(recB, os.path.join(args.output_dir, f'{counter:03d}-img_reconB.png'), nrow=T-1, normalize=True, value_range=(-1, 1))
utils.save_image(coAmoB, os.path.join(args.output_dir, f'{counter:03d}-img_coA+moB.png'), nrow=T-1, normalize=True, value_range=(-1, 1))
utils.save_image(coBmoA, os.path.join(args.output_dir, f'{counter:03d}-img_coB+moA.png'), nrow=T-1, normalize=True, value_range=(-1, 1))
util.save_video(oriA, os.path.join(args.output_dir, f'{counter:03d}-vid_origA.mp4'))
util.save_video(oriB, os.path.join(args.output_dir, f'{counter:03d}-vid_origB.mp4'))
util.save_video(recA, os.path.join(args.output_dir, f'{counter:03d}-vid_reconA.mp4'))
util.save_video(recB, os.path.join(args.output_dir, f'{counter:03d}-vid_reconB.mp4'))
util.save_video(coAmoB, os.path.join(args.output_dir, f'{counter:03d}-vid_coA+moB.mp4'))
util.save_video(coBmoA, os.path.join(args.output_dir, f'{counter:03d}-vid_coB+moA.mp4'))
if counter >= args.n_sample:
break
if __name__ == "__main__":
device = "cuda"
parser = argparse.ArgumentParser(description="StyleGAN2 encoder trainer")
parser.add_argument("--path", type=str, help="path to the lmdb dataset")
parser.add_argument("--cache", type=str, default='local.db')
parser.add_argument("--name", type=str, help="experiment name", default='default_exp')
parser.add_argument("--output_dir", type=str, default='samples/style_transfer')
parser.add_argument("--use_wscale", action='store_true', help="whether to use `wscale` layer in idinvert encoder")
parser.add_argument("--train_on_fake", action='store_true', help="train encoder on fake?")
parser.add_argument("--which_encoder", type=str, default='style')
parser.add_argument("--which_latent", type=str, default='w_tied')
parser.add_argument("--frame_num", type=int, default=50)
parser.add_argument("--frame_step", type=int, default=1)
parser.add_argument("--stddev_group", type=int, default=4)
parser.add_argument(
"--batch", type=int, default=16, help="batch sizes for each gpus"
)
parser.add_argument(
"--iter", type=int, default=800000, help="total training iterations"
)
parser.add_argument(
"--n_sample",
type=int,
default=64,
help="number of the samples generated during training",
)
parser.add_argument(
"--size", type=int, default=256, help="image sizes for the model"
)
parser.add_argument(
"--mixing", type=float, default=0.9, help="probability of latent code mixing"
)
parser.add_argument(
"--ckpt",
type=str,
default=None,
help="path to the checkpoints to resume training",
)
parser.add_argument(
"--channel_multiplier",
type=int,
default=2,
help="channel multiplier factor for the model. config-f = 2, else = 1",
)
parser.add_argument(
"--wandb", action="store_true", help="use weights and biases logging"
)
parser.add_argument(
"--local_rank", type=int, default=0, help="local rank for distributed training"
)
args = parser.parse_args()
n_gpu = int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1
args.distributed = n_gpu > 1
if args.distributed:
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(backend="nccl", init_method="env://")
synchronize()
args.n_latent = int(np.log2(args.size)) * 2 - 2 # used in Generator
args.latent = 512 # fixed, dim of w or z (same size)
if args.which_latent == 'w_plus':
args.latent_full = args.latent * args.n_latent
elif args.which_latent == 'w_tied':
args.latent_full = args.latent
else:
raise NotImplementedError
args.n_mlp = 8
args.start_iter = 0
# util.set_log_dir(args)
# util.print_args(parser, args)
g_ema = Generator(
args.size, args.latent, args.n_mlp, channel_multiplier=args.channel_multiplier
).to(device)
g_ema.eval()
if args.which_encoder == 'idinvert':
from idinvert_pytorch.models.stylegan_encoder_network import StyleGANEncoderNet
e_ema = StyleGANEncoderNet(resolution=args.size, w_space_dim=args.latent,
which_latent=args.which_latent, reshape_latent=True,
use_wscale=args.use_wscale).to(device)
else:
from model import Encoder
e_ema = Encoder(args.size, args.latent, channel_multiplier=args.channel_multiplier,
which_latent=args.which_latent, reshape_latent=True, stddev_group=args.stddev_group).to(device)
e_ema.eval()
if args.ckpt is not None:
print("load model:", args.ckpt)
ckpt = torch.load(args.ckpt, map_location=lambda storage, loc: storage)
g_ema.load_state_dict(ckpt["g_ema"])
e_ema.load_state_dict(ckpt["e_ema"])
transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(),
transforms.Resize(args.size),
transforms.CenterCrop(args.size),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
]
)
dataset = VideoFolderDataset(args.path, transform, mode='video', cache=args.cache,
frame_num=args.frame_num, frame_step=args.frame_step)
if len(dataset) == 0:
raise ValueError
loader = data.DataLoader(
dataset,
batch_size=2,
sampler=data_sampler(dataset, shuffle=True, distributed=args.distributed),
drop_last=True,
)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
args.stddev_group = min(args.stddev_group, args.batch)
run(args, loader, e_ema, g_ema, device)