forked from jiamings/d2c
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
212 lines (184 loc) · 6.71 KB
/
main.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
import argparse
import os
import torch
import torch.distributed as dist
import torchvision
import torchvision.datasets as tdatasets
import torchvision.transforms as transforms
import tqdm
import yaml
from d2c import D2C, BoundaryInterpolationLayer
def dict2namespace(config):
namespace = argparse.Namespace()
for key, value in config.items():
if isinstance(value, dict):
new_value = dict2namespace(value)
else:
new_value = value
setattr(namespace, key, new_value)
return namespace
def parse_args_and_config():
parser = argparse.ArgumentParser(description=globals()["__doc__"])
parser.add_argument("config", type=str, help="Path to the config file")
parser.add_argument(
"action",
type=str,
help="Action to perform with D2C",
choices=["manipulation", "sample_uncond"],
)
parser.add_argument("--d2c_path", type=str, required=True, help='D2C model location.')
parser.add_argument('--backend', type=str, default='nccl')
parser.add_argument('--init_method', type=str, default="tcp://localhost:10002")
args, _ = parser.parse_known_args()
if args.action == "manipulation":
parser.add_argument(
"--boundary_path",
type=str,
required=True,
help="Path to save the interpolation model on the latents (attribute specific).",
)
parser.add_argument(
"--image_dir",
type=str,
required=True,
help="Image directory that stores images to manipulate (in PyTorch ImageFolder format).",
)
parser.add_argument(
"--step",
type=float,
default=0.0,
help="Step size taken in manipulation, depends on attribute.",
)
parser.add_argument(
"--postprocess_steps",
type=int,
default=51,
help="Noise added in postprocess with DDIM, default value is fine.",
)
parser.add_argument(
"--postprocess_skip",
type=int,
default=50,
help="Denoising skip size with DDIM for acceleration, default value is fine.",
)
parser.add_argument(
"--batch_size", type=int, default=4, help="Batch size for the D2C model."
)
parser.add_argument(
"--save_location",
type=str,
default="results/default",
help="Save edited and/or original images at this location.",
)
parser.add_argument(
"--save_originals",
action="store_true",
help="If true, also save original images.",
)
args = parser.parse_args()
if args.action == "sample_uncond":
parser.add_argument(
"--num_batches",
type=int,
default=1,
help="Number of batches used in sampling, total number of images sampled is num_batches * batch_size.",
)
parser.add_argument(
"--batch_size", type=int, default=4, help="Batch size for the D2C model."
)
parser.add_argument(
"--skip", type=int, default=1, help="Denoising skip step size with DDIM."
)
parser.add_argument(
"--save_location",
type=str,
default="results/default",
help="Save samples at this location.",
)
args = parser.parse_args()
with open(os.path.join("configs", args.config + ".yml"), "r") as f:
config = yaml.safe_load(f)
new_config = dict2namespace(config)
return args, new_config
def manipulation(args, config):
print(f"Saving to {args.save_location} ...")
if not os.path.isdir(os.path.join(args.save_location, "edited")):
os.makedirs(os.path.join(args.save_location, "edited"))
if args.save_originals and not os.path.isdir(
os.path.join(args.save_location, "originals")
):
print("Original images are also saved.")
os.makedirs(os.path.join(args.save_location, "originals"))
resolution = config.autoencoder.resolution
val_dataset = tdatasets.ImageFolder(
root=args.image_dir,
transform=transforms.Compose(
[transforms.Resize((resolution, resolution)), transforms.ToTensor()]
),
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
args.batch_size,
shuffle=False,
num_workers=4,
pin_memory=True,
drop_last=False,
)
model = D2C(args, config)
state_dict = torch.load(args.d2c_path)
model.load_state_dict(state_dict)
model.eval()
r_model = BoundaryInterpolationLayer(1, *model.latent_size).cuda()
r_model.load_state_dict(torch.load(args.boundary_path))
count = 0
with torch.no_grad():
for x, _ in tqdm.tqdm(val_loader):
x = x.cuda()
z = model.image_to_latent(x)
z_ = model.manipulate_latent(z, r_model, args.step)
z_ = model.postprocess_latent(
z_, range(0, args.postprocess_steps, args.postprocess_skip)
)
x_ = model.latent_to_image(z_)
for j in range(0, x_.size(0)):
torchvision.utils.save_image(
x_[j : j + 1],
os.path.join(args.save_location, f"edited/{count}.png"),
padding=0,
)
if args.save_originals:
torchvision.utils.save_image(
x[j : j + 1],
os.path.join(args.save_location, f"originals/{count}.png"),
padding=0,
)
count = count + 1
def sample_uncond(args, config):
print(f"Saving to {args.save_location} ...")
if not os.path.isdir(args.save_location):
os.makedirs(args.save_location)
model = D2C(args, config)
state_dict = torch.load(args.d2c_path)
model.load_state_dict(state_dict)
model.eval()
count = 0
with torch.no_grad():
for i in tqdm.tqdm(range(0, args.num_batches)):
z = model.sample_latent(args.batch_size, args.skip)
x = model.latent_to_image(z)
for j in range(0, args.batch_size):
torchvision.utils.save_image(
x[j : j + 1],
os.path.join(args.save_location, f"{count}.png"),
padding=0,
)
count = count + 1
if __name__ == "__main__":
args, config = parse_args_and_config()
dist.init_process_group(
args.backend, init_method=args.init_method, world_size=1, rank=0
)
if args.action == "manipulation":
manipulation(args, config)
if args.action == "sample_uncond":
sample_uncond(args, config)