-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation_test.py
144 lines (113 loc) · 5.09 KB
/
generation_test.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
# generate motion from noise
# check denoise performence
import json
import torch
import numpy as np
import mmap
import trimesh
from utils import dist_util
from data_loaders.get_data import get_dataset
from utils.model_util import create_unconditioned_model_and_diffusion
from scipy.spatial.transform import Rotation as R
import os
from model.cfg_sampler import ClassifierFreeSampleModel
torch.backends.cudnn.enabled = False
exp_name = "text_conditioned/"
path = "./save/" + exp_name
with open("preProcessing/default_options_dataset.json", "r") as outfile:
opt = json.load(outfile)
# evecs
print("loading evecs")
with open("data/evecs_4096.bin", "r+b") as f:
mm = mmap.mmap(f.fileno(), 0)
evecs = torch.tensor(np.frombuffer(
mm[:], dtype=np.float32)).view(6890, 4096).to(opt["device"])
evecs = evecs[:, :opt["nb_freqs"]]
path_faces = "data/faces.bin"
with open(path_faces, "r+b") as f:
mm = mmap.mmap(f.fileno(), 0)
smpl_faces = np.frombuffer(mm[:], dtype=np.intc).reshape(-1, 3)
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
with open(path + "args.json", "r") as outfile:
args = dotdict(json.load(outfile))
args.batch_size = 10
scaler = 2 # scale factor for guidance
def get_result(model, diffusion, shape, model_kwargs=None):
data = diffusion.p_sample_loop(model, shape, clip_denoised=False, model_kwargs=model_kwargs)
return data.to("cpu").detach().numpy()
def get_result_classifier(model, diffusion, shape, cond_fn, model_kwargs=None):
data = diffusion.p_sample_loop(model, shape, clip_denoised=False, cond_fn=cond_fn, model_kwargs=model_kwargs)
return data.to("cpu").detach().numpy()
def get_x0_result_iter(model, diffusion, data, t):
t = torch.tensor(t).to("cuda")
data = torch.tensor(data).to("cuda").unsqueeze(0)
noise = torch.randn_like(data)
with torch.no_grad():
while torch.all(t>=0):
data_t = diffusion.q_sample(data, t, noise)
data = model(data_t, t)
t -= 1
return data[0].to("cpu").detach().numpy()
def result2mesh(data):
meshes = np.matmul(evecs.cpu().numpy(), data)
return meshes
def training_perform():
mean = np.load("data/datasets/text_conditioned/mean.npy")
std = np.load("data/datasets/text_conditioned/std.npy")
max = np.load("data/datasets/text_conditioned/max.npy")
min = np.load("data/datasets/text_conditioned/min.npy")
tpose_mean = torch.tensor(np.load("data/classifier/coef_mean.npy"))
tpose_std = torch.tensor(np.load("data/classifier/coef_std.npy"))
mean_std = torch.tensor(np.stack([mean, std]))
mean_max = torch.tensor(np.stack([mean,np.maximum(max, -min)]))
if args.cuda:
mean_std = [ele.to("cuda") for ele in mean_std]
model, diffusion = create_unconditioned_model_and_diffusion(args, mean_std)
model = ClassifierFreeSampleModel(model, scaler)
# model.model.tpose_ae.load_state_dict(dist_util.load_state_dict(
# "save/autoencoder/model0099.pt", map_location=dist_util.dev()
# ))
# model.tpose_ae.requires_grad_(False)
if args.cuda:
model.to("cuda")
# load checkpoints
model.model.load_my_state_dict(
dist_util.load_state_dict(
path + "model000100000.pt", map_location=dist_util.dev()
)
)
model.eval()
# train female_notrans_50 male_5000 male_notrans_500 male_jump_12000
# male_walk_20000 female_return_40000 val_female_2000 val_male_11000
with open("data/datasets/text_conditioned/Tunconditioned.bin", "r+b") as f:
tposes = mmap.mmap(f.fileno(), 0)
tpose = torch.frombuffer(tposes, dtype=torch.float32).view(-1,1024,3)
target = torch.stack([(tpose[0]-tpose_mean)/tpose_std for i in range(args.batch_size)]).to("cuda")
# target = target[(None,)*2].repeat(args.batch_size,1,1,1)
# text conditioning signal
text = ["a person walks forward" for i in range(args.batch_size)]
textcond = [1 for i in range(args.batch_size)]
lenbatch = torch.tensor([200 for i in range(args.batch_size)]).to('cuda')
maskbatch = torch.logical_not(torch.zeros(args.batch_size, 200)).to('cuda')
cond = {'y': {'mask': maskbatch, 'lengths': lenbatch, 'tpose': target}}
cond['y'].update({'text': text})
cond['y'].update({'textcond': torch.tensor(textcond).to("cuda")})
cond['y'].update({'shapecond': torch.ones_like(cond['y']['textcond'])})
# original generation process
result = get_result(model, diffusion, (args.batch_size,200,1026,3), model_kwargs=cond)
rot = result[:,:,-2,:]
trans = result[:,:,-1,:]
res_mesh = result[:,:,:-2,:]
rec_verts = np.matmul(evecs.cpu().numpy(), res_mesh*std+mean)
save_path = "render/"+exp_name
os.makedirs(save_path, exist_ok=True)
for i in range(args.batch_size):
rot_mat = R.from_rotvec(rot[i]).as_matrix()
for j in range(200):
_ = trimesh.Trimesh(np.matmul(rot_mat[j], rec_verts[i,j].T).T+trans[i,j], smpl_faces).export(save_path+"/test_"+str(scaler)+"_"+str(i)+"_"+str(j)+".obj")
if __name__ == "__main__":
training_perform()