-
Notifications
You must be signed in to change notification settings - Fork 130
/
main.py
499 lines (439 loc) · 23.7 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import clip
from tqdm import tqdm
import kaolin.ops.mesh
import kaolin as kal
import torch
from neural_style_field import NeuralStyleField
from utils import device
from render import Renderer
from mesh import Mesh
from Normalization import MeshNormalizer
import numpy as np
import random
import copy
import torchvision
import os
from PIL import Image
import argparse
from pathlib import Path
from torchvision import transforms
def run_branched(args):
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
# Constrain all sources of randomness
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
# Load CLIP model
clip_model, preprocess = clip.load(args.clipmodel, device, jit=args.jit)
# Adjust output resolution depending on model type
res = 224
if args.clipmodel == "ViT-L/14@336px":
res = 336
if args.clipmodel == "RN50x4":
res = 288
if args.clipmodel == "RN50x16":
res = 384
if args.clipmodel == "RN50x64":
res = 448
objbase, extension = os.path.splitext(os.path.basename(args.obj_path))
# Check that isn't already done
if (not args.overwrite) and os.path.exists(os.path.join(args.output_dir, "loss.png")) and \
os.path.exists(os.path.join(args.output_dir, f"{objbase}_final.obj")):
print(f"Already done with {args.output_dir}")
exit()
elif args.overwrite and os.path.exists(os.path.join(args.output_dir, "loss.png")) and \
os.path.exists(os.path.join(args.output_dir, f"{objbase}_final.obj")):
import shutil
for filename in os.listdir(args.output_dir):
file_path = os.path.join(args.output_dir, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
render = Renderer(dim=(res, res))
mesh = Mesh(args.obj_path)
MeshNormalizer(mesh)()
prior_color = torch.full(size=(mesh.faces.shape[0], 3, 3), fill_value=0.5, device=device)
background = None
if args.background is not None:
assert len(args.background) == 3
background = torch.tensor(args.background).to(device)
losses = []
n_augs = args.n_augs
dir = args.output_dir
clip_normalizer = transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
# CLIP Transform
clip_transform = transforms.Compose([
transforms.Resize((res, res)),
clip_normalizer
])
# Augmentation settings
augment_transform = transforms.Compose([
transforms.RandomResizedCrop(res, scale=(1, 1)),
transforms.RandomPerspective(fill=1, p=0.8, distortion_scale=0.5),
clip_normalizer
])
# Augmentations for normal network
if args.cropforward :
curcrop = args.normmincrop
else:
curcrop = args.normmaxcrop
normaugment_transform = transforms.Compose([
transforms.RandomResizedCrop(res, scale=(curcrop, curcrop)),
transforms.RandomPerspective(fill=1, p=0.8, distortion_scale=0.5),
clip_normalizer
])
cropiter = 0
cropupdate = 0
if args.normmincrop < args.normmaxcrop and args.cropsteps > 0:
cropiter = round(args.n_iter / (args.cropsteps + 1))
cropupdate = (args.maxcrop - args.mincrop) / cropiter
if not args.cropforward:
cropupdate *= -1
# Displacement-only augmentations
displaugment_transform = transforms.Compose([
transforms.RandomResizedCrop(res, scale=(args.normmincrop, args.normmincrop)),
transforms.RandomPerspective(fill=1, p=0.8, distortion_scale=0.5),
clip_normalizer
])
normweight = 1.0
# MLP Settings
input_dim = 6 if args.input_normals else 3
if args.only_z:
input_dim = 1
mlp = NeuralStyleField(args.sigma, args.depth, args.width, 'gaussian', args.colordepth, args.normdepth,
args.normratio, args.clamp, args.normclamp, niter=args.n_iter,
progressive_encoding=args.pe, input_dim=input_dim, exclude=args.exclude).to(device)
mlp.reset_weights()
optim = torch.optim.Adam(mlp.parameters(), args.learning_rate, weight_decay=args.decay)
activate_scheduler = args.lr_decay < 1 and args.decay_step > 0 and not args.lr_plateau
if activate_scheduler:
lr_scheduler = torch.optim.lr_scheduler.StepLR(optim, step_size=args.decay_step, gamma=args.lr_decay)
if not args.no_prompt:
if args.prompt:
prompt = ' '.join(args.prompt)
prompt_token = clip.tokenize([prompt]).to(device)
encoded_text = clip_model.encode_text(prompt_token)
# Save prompt
with open(os.path.join(dir, prompt), "w") as f:
f.write("")
# Same with normprompt
norm_encoded = encoded_text
if args.normprompt is not None:
prompt = ' '.join(args.normprompt)
prompt_token = clip.tokenize([prompt]).to(device)
norm_encoded = clip_model.encode_text(prompt_token)
# Save prompt
with open(os.path.join(dir, f"NORM {prompt}"), "w") as f:
f.write("")
if args.image:
img = Image.open(args.image)
img = preprocess(img).to(device)
encoded_image = clip_model.encode_image(img.unsqueeze(0))
if args.no_prompt:
norm_encoded = encoded_image
loss_check = None
vertices = copy.deepcopy(mesh.vertices)
network_input = copy.deepcopy(vertices)
if args.symmetry == True:
network_input[:,2] = torch.abs(network_input[:,2])
if args.standardize == True:
# Each channel into z-score
network_input = (network_input - torch.mean(network_input, dim=0))/torch.std(network_input, dim=0)
for i in tqdm(range(args.n_iter)):
optim.zero_grad()
sampled_mesh = mesh
update_mesh(mlp, network_input, prior_color, sampled_mesh, vertices)
rendered_images, elev, azim = render.render_front_views(sampled_mesh, num_views=args.n_views,
show=args.show,
center_azim=args.frontview_center[0],
center_elev=args.frontview_center[1],
std=args.frontview_std,
return_views=True,
background=background)
# rendered_images = torch.stack([preprocess(transforms.ToPILImage()(image)) for image in rendered_images])
if n_augs == 0:
clip_image = clip_transform(rendered_images)
encoded_renders = clip_model.encode_image(clip_image)
if not args.no_prompt:
loss = torch.mean(torch.cosine_similarity(encoded_renders, encoded_text))
# Check augmentation steps
if args.cropsteps != 0 and cropupdate != 0 and i != 0 and i % args.cropsteps == 0:
curcrop += cropupdate
# print(curcrop)
normaugment_transform = transforms.Compose([
transforms.RandomResizedCrop(res, scale=(curcrop, curcrop)),
transforms.RandomPerspective(fill=1, p=0.8, distortion_scale=0.5),
clip_normalizer
])
if n_augs > 0:
loss = 0.0
for _ in range(n_augs):
augmented_image = augment_transform(rendered_images)
encoded_renders = clip_model.encode_image(augmented_image)
if not args.no_prompt:
if args.prompt:
if args.clipavg == "view":
if encoded_text.shape[0] > 1:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0),
torch.mean(encoded_text, dim=0), dim=0)
else:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0, keepdim=True),
encoded_text)
else:
loss -= torch.mean(torch.cosine_similarity(encoded_renders, encoded_text))
if args.image:
if encoded_image.shape[0] > 1:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0),
torch.mean(encoded_image, dim=0), dim=0)
else:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0, keepdim=True),
encoded_image)
# if args.image:
# loss -= torch.mean(torch.cosine_similarity(encoded_renders,encoded_image))
if args.splitnormloss:
for param in mlp.mlp_normal.parameters():
param.requires_grad = False
loss.backward(retain_graph=True)
# optim.step()
# with torch.no_grad():
# losses.append(loss.item())
# Normal augment transform
# loss = 0.0
if args.n_normaugs > 0:
normloss = 0.0
for _ in range(args.n_normaugs):
augmented_image = normaugment_transform(rendered_images)
encoded_renders = clip_model.encode_image(augmented_image)
if not args.no_prompt:
if args.prompt:
if args.clipavg == "view":
if norm_encoded.shape[0] > 1:
normloss -= normweight * torch.cosine_similarity(torch.mean(encoded_renders, dim=0),
torch.mean(norm_encoded, dim=0),
dim=0)
else:
normloss -= normweight * torch.cosine_similarity(
torch.mean(encoded_renders, dim=0, keepdim=True),
norm_encoded)
else:
normloss -= normweight * torch.mean(
torch.cosine_similarity(encoded_renders, norm_encoded))
if args.image:
if encoded_image.shape[0] > 1:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0),
torch.mean(encoded_image, dim=0), dim=0)
else:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0, keepdim=True),
encoded_image)
# if args.image:
# loss -= torch.mean(torch.cosine_similarity(encoded_renders,encoded_image))
if args.splitnormloss:
for param in mlp.mlp_normal.parameters():
param.requires_grad = True
if args.splitcolorloss:
for param in mlp.mlp_rgb.parameters():
param.requires_grad = False
if not args.no_prompt:
normloss.backward(retain_graph=True)
# Also run separate loss on the uncolored displacements
if args.geoloss:
default_color = torch.zeros(len(mesh.vertices), 3).to(device)
default_color[:, :] = torch.tensor([0.5, 0.5, 0.5]).to(device)
sampled_mesh.face_attributes = kaolin.ops.mesh.index_vertices_by_faces(default_color.unsqueeze(0),
sampled_mesh.faces)
geo_renders, elev, azim = render.render_front_views(sampled_mesh, num_views=args.n_views,
show=args.show,
center_azim=args.frontview_center[0],
center_elev=args.frontview_center[1],
std=args.frontview_std,
return_views=True,
background=background)
if args.n_normaugs > 0:
normloss = 0.0
### avgview != aug
for _ in range(args.n_normaugs):
augmented_image = displaugment_transform(geo_renders)
encoded_renders = clip_model.encode_image(augmented_image)
if norm_encoded.shape[0] > 1:
normloss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0),
torch.mean(norm_encoded, dim=0), dim=0)
else:
normloss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0, keepdim=True),
norm_encoded)
if args.image:
if encoded_image.shape[0] > 1:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0),
torch.mean(encoded_image, dim=0), dim=0)
else:
loss -= torch.cosine_similarity(torch.mean(encoded_renders, dim=0, keepdim=True),
encoded_image) # if args.image:
# loss -= torch.mean(torch.cosine_similarity(encoded_renders,encoded_image))
# if not args.no_prompt:
normloss.backward(retain_graph=True)
optim.step()
for param in mlp.mlp_normal.parameters():
param.requires_grad = True
for param in mlp.mlp_rgb.parameters():
param.requires_grad = True
if activate_scheduler:
lr_scheduler.step()
with torch.no_grad():
losses.append(loss.item())
# Adjust normweight if set
if args.decayfreq is not None:
if i % args.decayfreq == 0:
normweight *= args.cropdecay
if i % 100 == 0:
report_process(args, dir, i, loss, loss_check, losses, rendered_images)
export_final_results(args, dir, losses, mesh, mlp, network_input, vertices)
def report_process(args, dir, i, loss, loss_check, losses, rendered_images):
print('iter: {} loss: {}'.format(i, loss.item()))
torchvision.utils.save_image(rendered_images, os.path.join(dir, 'iter_{}.jpg'.format(i)))
if args.lr_plateau and loss_check is not None:
new_loss_check = np.mean(losses[-100:])
# If avg loss increased or plateaued then reduce LR
if new_loss_check >= loss_check:
for g in torch.optim.param_groups:
g['lr'] *= 0.5
loss_check = new_loss_check
elif args.lr_plateau and loss_check is None and len(losses) >= 100:
loss_check = np.mean(losses[-100:])
def export_final_results(args, dir, losses, mesh, mlp, network_input, vertices):
with torch.no_grad():
pred_rgb, pred_normal = mlp(network_input)
pred_rgb = pred_rgb.detach().cpu()
pred_normal = pred_normal.detach().cpu()
torch.save(pred_rgb, os.path.join(dir, f"colors_final.pt"))
torch.save(pred_normal, os.path.join(dir, f"normals_final.pt"))
base_color = torch.full(size=(mesh.vertices.shape[0], 3), fill_value=0.5)
final_color = torch.clamp(pred_rgb + base_color, 0, 1)
mesh.vertices = vertices.detach().cpu() + mesh.vertex_normals.detach().cpu() * pred_normal
objbase, extension = os.path.splitext(os.path.basename(args.obj_path))
mesh.export(os.path.join(dir, f"{objbase}_final.obj"), color=final_color)
# Run renders
if args.save_render:
save_rendered_results(args, dir, final_color, mesh)
# Save final losses
torch.save(torch.tensor(losses), os.path.join(dir, "losses.pt"))
def save_rendered_results(args, dir, final_color, mesh):
default_color = torch.full(size=(mesh.vertices.shape[0], 3), fill_value=0.5, device=device)
mesh.face_attributes = kaolin.ops.mesh.index_vertices_by_faces(default_color.unsqueeze(0),
mesh.faces.to(device))
kal_render = Renderer(
camera=kal.render.camera.generate_perspective_projection(np.pi / 4, 1280 / 720).to(device),
dim=(1280, 720))
MeshNormalizer(mesh)()
img, mask = kal_render.render_single_view(mesh, args.frontview_center[1], args.frontview_center[0],
radius=2.5,
background=torch.tensor([1, 1, 1]).to(device).float(),
return_mask=True)
img = img[0].cpu()
mask = mask[0].cpu()
# Manually add alpha channel using background color
alpha = torch.ones(img.shape[1], img.shape[2])
alpha[torch.where(mask == 0)] = 0
img = torch.cat((img, alpha.unsqueeze(0)), dim=0)
img = transforms.ToPILImage()(img)
img.save(os.path.join(dir, f"init_cluster.png"))
MeshNormalizer(mesh)()
# Vertex colorings
mesh.face_attributes = kaolin.ops.mesh.index_vertices_by_faces(final_color.unsqueeze(0).to(device),
mesh.faces.to(device))
img, mask = kal_render.render_single_view(mesh, args.frontview_center[1], args.frontview_center[0],
radius=2.5,
background=torch.tensor([1, 1, 1]).to(device).float(),
return_mask=True)
img = img[0].cpu()
mask = mask[0].cpu()
# Manually add alpha channel using background color
alpha = torch.ones(img.shape[1], img.shape[2])
alpha[torch.where(mask == 0)] = 0
img = torch.cat((img, alpha.unsqueeze(0)), dim=0)
img = transforms.ToPILImage()(img)
img.save(os.path.join(dir, f"final_cluster.png"))
def update_mesh(mlp, network_input, prior_color, sampled_mesh, vertices):
pred_rgb, pred_normal = mlp(network_input)
sampled_mesh.face_attributes = prior_color + kaolin.ops.mesh.index_vertices_by_faces(
pred_rgb.unsqueeze(0),
sampled_mesh.faces)
sampled_mesh.vertices = vertices + sampled_mesh.vertex_normals * pred_normal
MeshNormalizer(sampled_mesh)()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--obj_path', type=str, default='meshes/mesh1.obj')
parser.add_argument('--prompt', nargs="+", default='a pig with pants')
parser.add_argument('--normprompt', nargs="+", default=None)
parser.add_argument('--promptlist', nargs="+", default=None)
parser.add_argument('--normpromptlist', nargs="+", default=None)
parser.add_argument('--image', type=str, default=None)
parser.add_argument('--output_dir', type=str, default='round2/alpha5')
parser.add_argument('--traintype', type=str, default="shared")
parser.add_argument('--sigma', type=float, default=10.0)
parser.add_argument('--normsigma', type=float, default=10.0)
parser.add_argument('--depth', type=int, default=4)
parser.add_argument('--width', type=int, default=256)
parser.add_argument('--colordepth', type=int, default=2)
parser.add_argument('--normdepth', type=int, default=2)
parser.add_argument('--normwidth', type=int, default=256)
parser.add_argument('--learning_rate', type=float, default=0.0005)
parser.add_argument('--normal_learning_rate', type=float, default=0.0005)
parser.add_argument('--decay', type=float, default=0)
parser.add_argument('--lr_decay', type=float, default=1)
parser.add_argument('--lr_plateau', action='store_true')
parser.add_argument('--no_pe', dest='pe', default=True, action='store_false')
parser.add_argument('--decay_step', type=int, default=100)
parser.add_argument('--n_views', type=int, default=5)
parser.add_argument('--n_augs', type=int, default=0)
parser.add_argument('--n_normaugs', type=int, default=0)
parser.add_argument('--n_iter', type=int, default=6000)
parser.add_argument('--encoding', type=str, default='gaussian')
parser.add_argument('--normencoding', type=str, default='xyz')
parser.add_argument('--layernorm', action="store_true")
parser.add_argument('--run', type=str, default=None)
parser.add_argument('--gen', action='store_true')
parser.add_argument('--clamp', type=str, default="tanh")
parser.add_argument('--normclamp', type=str, default="tanh")
parser.add_argument('--normratio', type=float, default=0.1)
parser.add_argument('--frontview', action='store_true')
parser.add_argument('--no_prompt', default=False, action='store_true')
parser.add_argument('--exclude', type=int, default=0)
# Training settings
parser.add_argument('--frontview_std', type=float, default=8)
parser.add_argument('--frontview_center', nargs=2, type=float, default=[0., 0.])
parser.add_argument('--clipavg', type=str, default=None)
parser.add_argument('--geoloss', action="store_true")
parser.add_argument('--samplebary', action="store_true")
parser.add_argument('--promptviews', nargs="+", default=None)
parser.add_argument('--mincrop', type=float, default=1)
parser.add_argument('--maxcrop', type=float, default=1)
parser.add_argument('--normmincrop', type=float, default=0.1)
parser.add_argument('--normmaxcrop', type=float, default=0.1)
parser.add_argument('--splitnormloss', action="store_true")
parser.add_argument('--splitcolorloss', action="store_true")
parser.add_argument("--nonorm", action="store_true")
parser.add_argument('--cropsteps', type=int, default=0)
parser.add_argument('--cropforward', action='store_true')
parser.add_argument('--cropdecay', type=float, default=1.0)
parser.add_argument('--decayfreq', type=int, default=None)
parser.add_argument('--overwrite', action='store_true')
parser.add_argument('--show', action='store_true')
parser.add_argument('--background', nargs=3, type=float, default=None)
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--save_render', action="store_true")
parser.add_argument('--input_normals', default=False, action='store_true')
parser.add_argument('--symmetry', default=False, action='store_true')
parser.add_argument('--only_z', default=False, action='store_true')
parser.add_argument('--standardize', default=False, action='store_true')
# CLIP model settings
parser.add_argument('--clipmodel', type=str, default='ViT-B/32')
parser.add_argument('--jit', action="store_true")
args = parser.parse_args()
run_branched(args)