-
Notifications
You must be signed in to change notification settings - Fork 6
/
paint_it.py
309 lines (251 loc) · 12.2 KB
/
paint_it.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
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
from tqdm import tqdm
import torch
import numpy as np
import random
import math
import copy
import argparse
import torch.nn.functional as F
import warnings
warnings.simplefilter("ignore", UserWarning)
warnings.simplefilter("ignore", FutureWarning)
import time
from nvdiff_render.mesh import *
from nvdiff_render.render import *
from nvdiff_render.texture import *
from nvdiff_render.material import *
from nvdiff_render.obj import *
from utils import *
from dc_pbr import skip
from sd import StableDiffusion
glctx = dr.RasterizeCudaContext()
OBJECT_PATH = './data'
def parse_args():
parser = argparse.ArgumentParser()
# model
parser.add_argument('--decay', type=float, default=0) # weight decay
parser.add_argument('--lr_decay', type=float, default=0.9)
parser.add_argument('--lr_plateau', action='store_true')
parser.add_argument('--decay_step', type=int, default=100)
# training
parser.add_argument('--sd_max_grad_norm', type=float, default=10.0)
parser.add_argument('--n_iter', type=int, default=1500) # can be increased
parser.add_argument('--seed', type=int, default=2023)
parser.add_argument('--learning_rate', type=float, default=0.0005)
parser.add_argument('--sd_min', type=float, default=0.2)
parser.add_argument('--sd_max', type=float, default=0.98)
parser.add_argument('--sd_min_l', type=float, default=0.2)
parser.add_argument('--sd_min_r', type=float, default=0.3)
parser.add_argument('--sd_max_l', type=float, default=0.5)
parser.add_argument('--sd_max_r', type=float, default=0.98)
parser.add_argument('--bg', type=float, default=0.25)
parser.add_argument('--logging', type=eval, default=True, choices=[True, False])
parser.add_argument('--n_view', type=int, default=4)
parser.add_argument('--exp_name', type=str, default='debug')
parser.add_argument('--env_scale', type=float, default=2.0)
parser.add_argument('--envmap', type=str, default='data/irrmaps/mud_road_puresky_4k.hdr')
parser.add_argument('--log_freq', type=int, default=100)
parser.add_argument('--gd_scale', type=int, default=100)
parser.add_argument('--uv_res', type=int, default=512)
args = parser.parse_args()
args.kd_min = [0.0, 0.0, 0.0, 0.0] # Limits for kd
args.kd_max = [1.0, 1.0, 1.0, 1.0]
args.ks_min = [0.0, 0.08, 0.0] # Limits for ks
args.ks_max = [1.0, 1.0, 1.0]
args.nrm_min = [-0.1, -0.1, 0.0] # Limits for normal map
args.nrm_max = [0.1, 0.1, 1.0]
return args
def seed_all(args):
# Constrain all sources of randomness
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
def get_model(args):
# MLP Settings
input_depth = 3
net = skip(input_depth, 9,
num_channels_down=[128] * 5,
num_channels_up=[128] * 5,
num_channels_skip=[128] * 5,
filter_size_up=3, filter_size_down=3,
upsample_mode='nearest', filter_skip_size=1,
need_sigmoid=True, need_bias=True, pad='reflection', act_fun='LeakyReLU').type(torch.cuda.FloatTensor)
params = list(net.parameters())
lgt = light.load_env(args.envmap, scale=args.env_scale)
for p in lgt.parameters():
p.requires_grad = False
optim = torch.optim.Adam(params, 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)
return net, lgt, optim, activate_scheduler, lr_scheduler
def report_process(i, loss, exp_name):
full_loss = 0
log_message = f'[{exp_name}] iter: {i} '
for loss_type, loss_val in loss.items():
full_loss += loss_val
log_message += f'{loss_type}: {"%.3f" % loss_val} '
loss['L_all'] = full_loss
print(log_message)
def get_template_normal(h=512, w=512):
return torch.cat([torch.zeros((h, w, 1), device=device), torch.zeros((h, w, 1), device=device),
torch.ones((h, w, 1), device=device)], dim=-1)[None, ...]
def compute_sd_step(min, max, iter_frac):
step = (max - (max - min) * math.sqrt(iter_frac))
return step
def main(args, guidance):
exp_name = time.strftime('%Y%m%d', time.localtime()) + '_' + args.exp_name
output_dir = os.path.join('./logs', exp_name)
Path(output_dir).mkdir(parents=True, exist_ok=True)
# seed_all(args)
# Get text prompt and tokenize it
sd_prompt = ", ".join(
(f"a DSLR photo of {args.identity}", "best quality, high quality, extremely detailed, good geometry"))
# load obj and read uv information
args.obj_path = os.path.join(OBJECT_PATH, args.objaverse_id, 'mesh.obj')
obj_f_uv, obj_v_uv, obj_f, obj_v = load_obj_uv(obj_path=args.obj_path, device=device)
# initialize template mesh
mesh_t = Mesh(obj_v, obj_f, v_tex=obj_v_uv, t_tex_idx=obj_f_uv)
mesh_t = unit_size(mesh_t)
mesh_t = auto_normals(mesh_t)
mesh_t = compute_tangents(mesh_t)
input_uv_ = torch.randn((3, args.uv_res, args.uv_res), device=device)
# scale input
input_uv = (input_uv_ - torch.mean(input_uv_, dim=(1, 2)).reshape(-1, 1, 1)) / torch.std(input_uv_, dim=(1, 2)).reshape(-1, 1, 1)
network_input = copy.deepcopy(input_uv.unsqueeze(0))
# get model and optimizer
net, lgt, optim, activate_scheduler, lr_scheduler = get_model(args)
# get text embedding
neg_prompt = 'deformed, extra digit, fewer digits, cropped, worst quality, low quality, smoke'
text_z = []
for d in ['front', 'side', 'back', 'overhead']:
# construct dir-encoded text
text_z.append(guidance.get_text_embeds([f"{sd_prompt}, {d} view"], [neg_prompt], 1))
text_z = torch.stack(text_z, dim=0)
kd_min, kd_max = torch.tensor(args.kd_min, dtype=torch.float32, device='cuda'), torch.tensor(args.kd_max, dtype=torch.float32, device='cuda')
ks_min, ks_max = torch.tensor(args.ks_min, dtype=torch.float32, device='cuda'), torch.tensor(args.ks_max, dtype=torch.float32, device='cuda')
nrm_min, nrm_max = torch.tensor(args.nrm_min, dtype=torch.float32, device='cuda'), torch.tensor(args.nrm_max, dtype=torch.float32, device='cuda')
nrm_t = get_template_normal(h=args.uv_res, w=args.uv_res) # (512, 512, 3)
# Main training loop
for step in tqdm(range(args.n_iter + 1)):
cur_iter_frac = step / args.n_iter
losses = {}
optim.zero_grad()
# build mips
lgt.build_mips()
with torch.no_grad():
mesh = copy.deepcopy(mesh_t)
net_output = net(network_input) # [B, 9, H, W]
pred_tex = net_output.permute(0, 2, 3, 1)
pred_kd = pred_tex[..., :-6]
pred_ks = pred_tex[..., -6:-3]
pred_n = F.normalize((pred_tex[..., -3:] * 2.0 - 1.0) + nrm_t, dim=-1)
pred_material = Material({
'bsdf': 'pbr',
'kd': Texture2D(pred_kd, min_max=[kd_min, kd_max]),
'ks': Texture2D(pred_ks, min_max=[ks_min, ks_max]),
'normal': Texture2D(pred_n, min_max=[nrm_min, nrm_max])
})
pred_material['kd'].clamp_()
pred_material['ks'].clamp_()
pred_material['normal'].clamp_()
mesh.material = pred_material
cam = sample_view_obj(args.n_view, cam_radius=3.25)
buffers = render_mesh(glctx, mesh, cam['mvp'], cam['campos'], lgt, cam['resolution'],
spp=cam['spp'], msaa=True, background=None, bsdf='pbr')
pred_obj_rgb = buffers['shaded'][..., 0:3].permute(0, 3, 1, 2).contiguous()
pred_obj_ws = buffers['shaded'][..., 3].unsqueeze(1) # [B, 1, H, W]
obj_image = pred_obj_rgb * pred_obj_ws + (1 - pred_obj_ws) * args.bg
# SDS losses
all_pos, all_neg = [], []
#
text_z_iter = text_z[cam['direction']]
#
#
for emb in text_z_iter: # list of [2, S, -1]
pos, neg = emb.chunk(2) # [1, S, -1]
all_pos.append(pos)
all_neg.append(neg)
text_embedding = torch.cat(all_pos + all_neg, dim=0) # [2b, S, -1]
sd_min_step = compute_sd_step(args.sd_min_l, args.sd_min_r, cur_iter_frac)
sd_max_step = compute_sd_step(args.sd_max_l, args.sd_max_r, cur_iter_frac)
# # compute sds_loss for the body
sd_loss = guidance.batch_train_step(text_embedding, obj_image,
guidance_scale=args.gd_scale,
min_step=sd_min_step,
max_step=sd_max_step)
total_loss = sd_loss
losses['L_sds'] = sd_loss.item()
total_loss.backward()
torch.nn.utils.clip_grad_norm_(net.parameters(), max_norm=args.sd_max_grad_norm)
optim.step()
lr_scheduler.step()
if step % args.log_freq == 0 and args.logging:
with torch.no_grad():
report_process(step, losses, exp_name)
mtl_file = os.path.join(output_dir, 'mesh.mtl')
save_mtl(mtl_file, mesh.material, step=step)
torchvision.utils.save_image(obj_image[0], os.path.join(output_dir, f'obj_{step:04}.jpg'))
with torch.no_grad():
#
vis_mesh = copy.deepcopy(mesh_t)
final_pred = net(network_input)
final_tex = final_pred.permute(0, 2, 3, 1).contiguous()
final_kd = final_tex[..., :-6]
final_ks = final_tex[..., -6:-3]
final_n = F.normalize((final_tex[..., -3:] * 2.0 - 1.0) + nrm_t, dim=-1)
circle_n_view = 120
for elev in [-np.pi / 4, 0.0]:
final_cam = sample_circle_view(n_view=circle_n_view, elev=elev, cam_radius=3.25)
final_material = Material({
'bsdf': 'pbr',
'kd': Texture2D(final_kd, min_max=[kd_min, kd_max]),
'ks': Texture2D(final_ks, min_max=[ks_min, ks_max]),
'normal': Texture2D(final_n, min_max=[nrm_min, nrm_max])
})
final_material['kd'].clamp_()
final_material['ks'].clamp_()
final_material['normal'].clamp_()
vis_mesh.material = final_material
write_obj(output_dir, vis_mesh)
final_lgt = lgt
final_buffers = render_mesh(glctx, vis_mesh, final_cam['mvp'], final_cam['campos'], final_lgt,
final_cam['resolution'], spp=final_cam['spp'], msaa=True, background=None,
bsdf='pbr')
final_obj_rgb = final_buffers['shaded'][..., 0:3].permute(0, 3, 1, 2).contiguous()
final_obj_ws = final_buffers['shaded'][..., 3].unsqueeze(1) # [B, 1, H, W]
vis_mesh_img = final_obj_rgb * final_obj_ws + (1 - final_obj_ws) * 1 # white bg, float32, [B, 3, H, W]
# # save final front body image
if elev == 0.0:
os.makedirs(os.path.join(output_dir, 'view_front'), exist_ok=True)
else:
os.makedirs(os.path.join(output_dir, 'view_top'), exist_ok=True)
for idx in range(circle_n_view):
if idx == 0:
if elev == 0.0:
torchvision.utils.save_image(final_obj_rgb[idx], os.path.join(output_dir, "final_front.png"))
else:
torchvision.utils.save_image(final_obj_rgb[idx], os.path.join(output_dir, "final_top.png"))
if elev == 0.0:
torchvision.utils.save_image(vis_mesh_img[idx], os.path.join(output_dir, 'view_front', f'{idx:04}.png'))
else:
torchvision.utils.save_image(vis_mesh_img[idx], os.path.join(output_dir, 'view_top', f'{idx:04}.png'))
if __name__ == '__main__':
args = parse_args()
mesh_dicts = {
'9ce8ab24383c4c93b4c1c7c3848abc52': 'a pretzel',
}
# load stable-diffusion model
guidance = StableDiffusion(device, min=args.sd_min, max=args.sd_max)
guidance.eval()
for p in guidance.parameters():
p.requires_grad = False
# iterate through the renderpeople items
for obj_id, caption in mesh_dicts.items():
args.exp_name = '_'.join((caption.split(' ')[1:] + [obj_id[:6]]))
args.objaverse_id = obj_id
args.identity = caption
main(args, guidance)