-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_mesh.py
239 lines (169 loc) · 8.95 KB
/
test_mesh.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
import os
import sys
import argparse
from utils import dataloader
import torch
import torchvision
import cv2
from models.render import Render_SMPL,Render_TEX
from models.mesh import SMPL_Mesh,TEX_Mesh
from models.smpl import SMPL,load_smpl
from models.meshNet import MeshRefinementStage, MeshRefinementHead
from models.textureNet import TextureRefinementStage,discriminator
from utils.mesh_tools import write_obj
from utils.SSIM import SSIM
from utils import arguments
import random
from pytorch3d.io import save_obj
from pytorch3d.utils import ico_sphere
from pytorch3d.ops import sample_points_from_meshes
from pytorch3d.loss import (
chamfer_distance,
mesh_edge_loss,
mesh_laplacian_smoothing,
mesh_normal_consistency,
)
from pytorch3d.structures import Meshes
from pytorch3d.transforms import Transform3d
from torch.utils.tensorboard import SummaryWriter
from torch.optim import lr_scheduler
from torch.autograd import Variable
import datetime
import yaml
import numpy as np
from tqdm import tqdm
import config
import pdb
# Losses to smooth / regularize the mesh shape
def update_mesh_shape_prior_losses(mesh,loss):
# and (b) the edge length of the predicted mesh
#loss["edge"] = mesh_edge_loss(mesh)
edges_packed = mesh.edges_packed()
verts_packed = mesh.verts_packed()
verts_edges = verts_packed[edges_packed]
v0, v1 = verts_edges.unbind(1)
edge_size = ((v0 - v1).norm(dim=1, p=2)) - 0.1
m = torch.nn.ReLU()
loss["edge"] = (m(edge_size)).sum()
#loss["edge"] = mesh_edge_loss(mesh)
# mesh normal consistency
loss["normal"] = mesh_normal_consistency(mesh)
# mesh laplacian smoothing
loss["laplacian"] = mesh_laplacian_smoothing(mesh, method="uniform")
def draw_weights(model_name, model, summary, epoch):
if model_name == 'texture':
for i, (param_name, param) in enumerate(model.named_parameters()):
summary.add_histogram(f"{model_name}/channel_0", param[..., 0].flatten().data.cpu(), epoch)
summary.add_histogram(f"{model_name}/channel_1", param[..., 1].flatten().data.cpu(), epoch)
summary.add_histogram(f"{model_name}/channel_2", param[..., 2].flatten().data.cpu(), epoch)
if model_name == 'mesh':
for i, (param_name, param) in enumerate(model.named_parameters()):
try:
_, stage, _, gconv, weight, weight_type = tuple(param_name.split('.'))
summary.add_histogram(f"{model_name}/stage{stage}/gconv_{gconv}_{weight}.{weight_type}", param.data.cpu(), epoch)
except ValueError:
_, stage, _, weight_type = tuple(param_name.split('.'))
summary.add_histogram(f"{model_name}/stage{stage}/verts_offset.{weight_type}", param.data.cpu(), epoch)
def save_model(state_dict, path):
torch.save(state_dict, path)
def read_model(path, device):
return torch.load(path, map_location = device)
#python trainMesh.py -d /media/thiagoluange/SAMSUNG/ -w 1 -rss 256 -rsh 75 -b 1
#python trainMesh.py -d /media/thiagoluange/SAMSUNG/AIST/ -w 1 -rss 256 -rsh 512 -b 1 -st aist -g male -s d -p 04
def main():
## ARGS
args = arguments.get_args()
if(torch.cuda.is_available()):
device = torch.device("cuda:{}".format(args.device))
torch.cuda.set_device(device)
else:
device = torch.device("cpu")
print(f"RUNNING ON {device}")
## CREATE DATALOADER
#dataloaders = dataloader.get_dataloaders(args)
movements = ["box", "cone", "fusion", "hand", "jump", "rotate", "shake_hands", "simple_walk"]
dataloaders, dataset = dataloader.get_dataloaders(args, phase = "test", movements = movements, test = True)
#dataloader_test = dataloader.get_dataloaders(args, phase = "test")
## RECOVERING FIXED PARAMS
#dataset = dataloaders['test'].dataset
faces_mesh = torch.from_numpy(np.load(config.SMPL_FACES)).to(device)
output_path = os.path.join(args.output_path, args.source, args.person, "mesh_results")
os.makedirs(output_path, exist_ok = True)
f = dataset.f
'''
NOTE: Atualmente img_shape recebe o tamanho da imagem original.
Talvez tenha que mudar para tamanho da imagem cropada.
Seria pegar a menor dimensao para montar imagem quadrada? Thiago: Deve receber o tamanho original mesmo, ele que defini a camera
'''
img_shape = dataset.img_shape
## LOAD TEXTURE
txt_img = cv2.resize(cv2.imread(config.TEX_MAP, cv2.IMREAD_UNCHANGED), (512,512))
'''
NOTE: Nao sei o quanto o tamanho da textura vai influenciar na qualidade e no peso da rede
'''
txt_img = torch.from_numpy(txt_img[:,:,::-1].astype('float64')/255.0).to(device)
### LOADING MODELS
## LOAD MESH MODEL
with open("models/model_cfg.yaml", 'r') as cfg_file:
model_cfgs = yaml.safe_load(cfg_file)
model_cfgs["device"] = device
model_cfgs["batch_size"] = args.batch_size
model = MeshRefinementHead(model_cfgs).to(device)
if(args.pretrained_path_model is not None):
model.load_state_dict(read_model(args.pretrained_path_model, device))
print("loaded weights sucessfully")
## LOAD RENDER MODEL
if args.style == "mt":
my_render_hard = Render_SMPL(f, img_shape, args.render_size_hard, device, "hard").to(device)
else:
my_render_hard = Render_SMPL(f, img_shape, args.render_size_hard, device, "hard", eye=[[0,0,0]],at=[[0,0,-1]], up=[[0, 1, 0]]).to(device)
## MODELS OPTIMIZER
## MODELS TRAIN
model.eval()
#cv2.imwrite(checkpoint_path + "/image_step_%09d_in"%step + ".jpg", img_hard.permute(0, 2, 3, 1).cpu().detach().numpy()[0,face_posi[1] - 40:face_posi[1] + 40,face_posi[0] - 40:face_posi[0] + 40,-1::-1]*255)
step = 0
for idx, (vertices, trans, global_mat, f_now) in enumerate(tqdm(dataloaders['test'])):
############ Initialize optimizer mesh
trans = trans.to(device)
## CREATE MESH
vertices = [vert.to(device) for vert in vertices]
faces = [faces_mesh.to(device) for i in range(len(vertices))] ## O numero de amostras no batch sempre sera batch_size?
src_mesh = Meshes(verts=vertices, faces=faces).to(device)
with torch.no_grad():
# Deform the mesh
subdivide = False
deformed_mesh = model(src_mesh, subdivide)
it_size = int(deformed_mesh.verts_packed().shape[0]/len(vertices))
deformed_meshes = [deformed_mesh.verts_packed()[idx : idx + it_size] for idx in range(0, len(deformed_mesh.verts_packed()), it_size)]
tex_maps = []
for i in range(len(vertices)):
tex_maps.append(txt_img)
tex_map = torch.stack(tex_maps)
render_mesh = SMPL_Mesh(deformed_meshes, faces, tex_map, device)
S = torch.ones(f_now.shape[0],3)
for i in range(f_now.shape[0]):
S[i,2] = f/f_now[i]
images_predicted = my_render_hard(render_mesh.to(device), trans,global_mat,S)
predicted_silhouette = images_predicted[..., 3:].to(device)
rgb = images_predicted[..., :3]
seg = images_predicted[..., 3:]
for idx in range(images_predicted.shape[0]):
rgb_i = rgb.cpu().detach().numpy()[idx,:,:,-1::-1]*255
seg_i = seg.cpu().detach().numpy()[idx,:,:,-1::-1]*255
cv2.imwrite(os.path.join(output_path, "TEST_mesh{:05d}.jpg").format(step), rgb_i)
cv2.imwrite(os.path.join(output_path, "TEST_seg{:05d}.jpg").format(step), seg_i)
step += 1
os.system('ffmpeg -hide_banner -loglevel panic -framerate 30 -i {}/TEST_mesh%05d.jpg {}/{}.mp4'.format(output_path, output_path, "result_mesh"))
os.system('ffmpeg -hide_banner -loglevel panic -framerate 30 -i {}/TEST_seg%05d.jpg {}/{}.mp4'.format(output_path, output_path, "result_seg"))
#cv2.imwrite(checkpoint_path + "/image_step_%09d_in"%step + ".jpg", img_soft.permute(0, 2, 3, 1).cpu().detach().numpy()[0,:,:,-1::-1]*255)
#cv2.imwrite(checkpoint_path + "/image_step_%09d_in"%step + ".jpg", img_hard.permute(0, 2, 3, 1).cpu().detach().numpy()[0,face_posi[1] - 20:face_posi[1] + 20,face_posi[0] - 20:face_posi[0] + 20,-1::-1]*255)
#cv2.imwrite(checkpoint_path + "/seg_step_%09d_in"%step + ".png", seg_soft.permute(0, 2, 3, 1).detach().numpy()[0,:,:,:]*255)
#cv2.imwrite(checkpoint_path + "/image_step_%09d_out"%step + ".jpg", images_predicted[..., :3].cpu().detach().numpy()[0,:,:,-1::-1]*255)
#cv2.imwrite(checkpoint_path + "/seg_step_%09d_out"%step + ".png", images_predicted[..., 3:].detach().numpy()[0,:,:,:]*255)
#out_vert,out_faces = render_mesh.get_mesh_verts_faces(0)
#save_obj(checkpoint_path + "/model_%09d"%step + ".obj",out_vert,out_faces)
# Optimization step
## TEST PHASE
## VOLTAR IDENTACAO
if __name__ == "__main__":
main()