-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanonymize.py
233 lines (195 loc) · 12.9 KB
/
anonymize.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
import argparse
import torch
from torch.utils import data
from torch.optim.lr_scheduler import MultiStepLR
import os
import os.path as osp
from lib import DATASETS, CelebAHQ, DataParallelPassthrough, IDLoss, AttrLoss, LatentCode, tensor2image, anon_exp_dir
from models.load_generator import load_generator
from tqdm import tqdm
import json
def main():
"""Anonymize the images of a given real dataset.
Options:
-v, --verbose : set verbose mode on
--dataset : choose dataset (see lib/config.py:DATASETS.keys())
--dataset-root : choose dataset root directory (if none is given, lib/config.py:DATASETS[args.dataset] will be
used)
--subset : choose dataset subset ('train', 'val', 'test', 'train+val', 'train+val+test'), if applicable
--fake-nn-map : choose fake NN map file (created by `pair_nn.py`)
--latent-space : choose StyleGAN2's latent space ('W+' or 'S')
-m, --id-margin : set identity loss margin
--epochs : set number of training epochs
--optim : set optimizer ('sgd' or 'adam')
--lr : set (initial) learning rate
--lr-milestones : set learning rate scheduler milestones (list of floats in (0.0, 1.0)
--lr-gamma : set learning rate decay parameter
--lambda_id : set identity loss weighting parameter
--lambda_attr : set attribute loss weighting parameter
--cuda : use CUDA (default)
--no-cuda : do not use CUDA
References:
[1] Tov, Omer, et al. "Designing an encoder for stylegan image manipulation."
ACM Transactions on Graphics (TOG) 40.4 (2021): 1-14.
"""
parser = argparse.ArgumentParser("Anonymization script")
parser.add_argument('-v', '--verbose', action='store_true', help="verbose mode on")
parser.add_argument('--dataset', type=str, required=True, choices=DATASETS.keys(), help="choose real dataset")
parser.add_argument('--dataset-root', type=str, help="set dataset root directory")
parser.add_argument('--subset', type=str, default='train+val+test',
choices=('train', 'val', 'test', 'train+val', 'train+val+test'), help="choose dataset's subset")
parser.add_argument('--fake-nn-map', type=str, required=True, help="fake NN map file (created by `pair_nn.py`)")
parser.add_argument('--latent-space', type=str, default='W+', choices=('W+', 'S'), help="StyleGAN2's latent space")
parser.add_argument('-m', '--id-margin', type=float, default=0.0, help="identity loss margin")
parser.add_argument('--epochs', type=int, default=50, help="Number of anonymization steps")
parser.add_argument('--optim', type=str, default='adam', choices=('sgd', 'adam'),
help="set optimizer ('sgd' or 'adam')")
parser.add_argument('--lr', type=float, default=0.01, help="Learning rate")
parser.add_argument('--lr-milestones', nargs="+", default=[0.75, 0.9], help="learning rate scheduler milestones")
parser.add_argument('--lr-gamma', type=float, default=0.8, help="learning rate decay parameter")
parser.add_argument('--lambda-id', type=float, default=10.0, help="Scaling parameter of the ID loss")
parser.add_argument('--lambda-attr', type=float, default=0.1, help="Scaling parameter of the attribute loss")
parser.add_argument('--cuda', dest='cuda', action='store_true', help="use CUDA during training")
parser.add_argument('--no-cuda', dest='cuda', action='store_false', help="do NOT use CUDA during training")
parser.set_defaults(cuda=True)
# Parse given arguments
args = parser.parse_args()
# Set real dataset root dir
if args.dataset_root is None:
args.dataset_root = DATASETS[args.dataset]
####################################################################################################################
## ##
## [ Anonymized Dataset Directory ] ##
## ##
####################################################################################################################
args_dict = args.__dict__.copy()
out_dir = anon_exp_dir(args_dict)
if args.verbose:
print("#. Create dir for storing the anonymized {} dataset...".format(args.dataset))
print(" \\__{}".format(out_dir))
# Save experiment's arguments
del args_dict["lr_milestones"]
with open(osp.join(out_dir, 'args.json'), 'w') as args_json_file:
json.dump(args_dict, args_json_file)
####################################################################################################################
## ##
## [ CUDA ] ##
## ##
####################################################################################################################
use_cuda = False
multi_gpu = False
if torch.cuda.is_available():
if args.cuda:
use_cuda = True
if torch.cuda.device_count() > 1:
multi_gpu = True
else:
print("*** WARNING ***: It looks like you have a CUDA device, but aren't using CUDA.\n"
" Run with --cuda for optimal training speed.")
device = 'cuda' if use_cuda else 'cpu'
####################################################################################################################
## ##
## [ Pre-trained GAN Generator ] ##
## ##
####################################################################################################################
# Build GAN generator model and load with pre-trained weights
if args.verbose:
print("#. Build StyleGAN2 generator model G and load with pre-trained weights...")
G = load_generator(model_name='stylegan2_ffhq1024', latent_is_w=True, verbose=args.verbose).eval().to(device)
####################################################################################################################
## ##
## [ Data Loader ] ##
## ##
####################################################################################################################
if args.verbose:
print("#. Load {} dataset...".format(args.dataset))
####################################################################################################################
## [ CelebA-HQ ] ##
####################################################################################################################
if args.dataset == 'celebahq':
dataset = CelebAHQ(root_dir=args.dataset_root,
subset=args.subset,
fake_nn_map=args.fake_nn_map,
inv=True)
dataloader = data.DataLoader(dataset=dataset, batch_size=1, shuffle=False)
# Create output directory to save images
out_data_dir = osp.join(out_dir, 'data')
os.makedirs(out_data_dir, exist_ok=True)
# Create output directory to save latent codes
out_code_dir = osp.join(out_dir, 'latent_codes')
os.makedirs(out_code_dir, exist_ok=True)
####################################################################################################################
## [ Other Datasets ] ##
####################################################################################################################
else:
raise NotImplementedError
####################################################################################################################
## ##
## [ Losses ] ##
## ##
####################################################################################################################
id_criterion = IDLoss(id_margin=args.id_margin).eval().to(device)
attribute_loss = AttrLoss(feat_ext='farl').eval().to(device)
# Parallelize GAN's generator G
if multi_gpu:
G = DataParallelPassthrough(G)
####################################################################################################################
## ##
## [ Anonymization ] ##
## ##
####################################################################################################################
for data_idx, data_ in enumerate(
tqdm(dataloader, desc="#. Anonymize {} images".format(args.dataset) if args.verbose else '')):
# Get data
img_orig = data_[0]
img_orig_id = int(osp.basename(data_[2][0]).split('.')[0])
img_nn_code = data_[4]
img_recon_code = data_[6]
# Build anonymization latent code
latent_code = LatentCode(latent_code_real=img_recon_code, latent_code_fake_nn=img_nn_code, img_id=img_orig_id,
out_code_dir=out_code_dir, latent_space='W+')
latent_code.to(device)
# Count trainable parameters
# latent_code_trainable_parameters = sum(p.numel() for p in latent_code.parameters() if p.requires_grad)
# print("latent_code_trainable_parameters: {}".format(latent_code_trainable_parameters))
# Check whether anonymization latent code has already been optimized -- if so, continue with the next one
if not latent_code.do_optim():
continue
# Build optimizer
optimizer = None
if args.optim == 'sgd':
optimizer = torch.optim.SGD(params=latent_code.parameters(), lr=args.lr)
elif args.optim == 'adam':
optimizer = torch.optim.Adam(params=latent_code.parameters(), lr=args.lr)
# Set learning rate scheduler
lr_scheduler = MultiStepLR(optimizer=optimizer,
milestones=[int(m * args.epochs) for m in args.lr_milestones],
gamma=args.lr_gamma)
# Zero out gradients
G.zero_grad()
id_criterion.zero_grad()
attribute_loss.zero_grad()
# Training (anonymization) loop for the current batch of images / latent codes
for epoch in range(args.epochs):
# Clear gradients wrt parameters
optimizer.zero_grad()
# Generate anonymized image
img_anon = G(latent_code())
# Calculate identity and attribute preservation losses
id_loss = id_criterion(img_anon.to(device), img_orig.to(device))
att_loss = attribute_loss(img_orig.to(device), img_anon.to(device))
# Calculate total loss
loss = args.lambda_id * id_loss + args.lambda_attr * att_loss
# Back-propagation
loss.backward()
optimizer.step()
lr_scheduler.step()
# Store optimized anonymization latent codes
latent_code.save()
# Generate and save anonymized image
with torch.no_grad():
anonymized_image = G(latent_code())
tensor2image(anonymized_image.cpu(), adaptive=True).save(osp.join(out_data_dir, '{}.jpg'.format(img_orig_id)),
"JPEG", quality=75, subsampling=0, progressive=True)
if __name__ == '__main__':
main()