-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoeff_to_shape.py
236 lines (174 loc) · 6.86 KB
/
coeff_to_shape.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
"""
Convert the 3DMM coefficients to shapes
"""
import sys
import gc
import os
import torch
from tqdm import tqdm
from scipy.io import loadmat
import numpy as np
import argparse
import face_alignment
import cv2
import matplotlib.pyplot as plt
import scipy.io as sio
from array import array
# From models.py
import torch
import torch.nn as nn
import numpy as np
from pytorch3d.structures import Meshes
from pytorch3d.renderer import (
look_at_view_transform,
FoVPerspectiveCameras,
PointLights,
RasterizationSettings,
MeshRenderer,
MeshRasterizer,
SoftPhongShader,
TexturesVertex,
blending
)
import torchvision.transforms as transforms
from PIL import Image
import torch.nn.functional as F
import pickle
import open3d as o3d
from time import time
# Setup PyTorch
if torch.cuda.is_available():
device = torch.device("cuda:0")
torch.cuda.set_device(device)
print('CUDA is available')
else:
device = torch.device("cpu")
print('CUDA is not available')
sys.path.append('../')
from models.ReconModel import ReconModel
########################
# Load BFM model
########################
with open('../BFM/bfm09.pkl', 'rb') as f:
bfm = pickle.load(f)
bfm['idBase'] = bfm['idBase'][...,:80] # only use 80 coefficients
bfm['texBase'] = bfm['texBase'][...,:80] # only use 80 coefficients
bfm['exBase'] = bfm['exBase'][...,:64] # only use 64 coefficients
print('BFM model loaded')
TAR_SIZE = 224 # size for rendering window
model = ReconModel(bfm, img_size=TAR_SIZE)
model.eval()
model.cuda()
def load_coefficients(coeff_path):
matlab = sio.loadmat(coeff_path)
id_ = np.reshape(matlab['id'], (1,-1))
tex_ = np.reshape(matlab['tex'], (1,-1))
exp_ = np.reshape(matlab['exp'], (1,-1))
rot_ = np.reshape(matlab['angle'], (1,-1))
trans_ = np.reshape(matlab['trans'], (1,-1))
gamma_ = np.reshape(matlab['gamma'], (1,-1))
return id_ , tex_ , exp_ , rot_ , trans_, gamma_
shape_save_path = '../datasets/CelebA/raw_bfm_shape/'
albedo_save_path = '../datasets/CelebA/raw_bfm_albedo/'
color_save_path = '../datasets/CelebA/raw_bfm_color/'
try:
os.mkdir(shape_save_path)
except:
pass
try:
os.mkdir(albedo_save_path)
except:
pass
try:
os.mkdir(color_save_path)
except:
pass
shape_save_path_ = shape_save_path + '{}.npy'
albedo_save_path_ = albedo_save_path + '{}.npy'
color_save_path_ = color_save_path + '{}.npy'
#####################
## CelebA dataset
img_index = 0
# Natural expression
exp_tensor = torch.zeros((1,64), dtype=torch.float32, requires_grad=False, device='cuda')
for fname in tqdm(os.listdir('../datasets/CelebA/images224x224/')):
if fname.endswith('.jpg'):
img_index = fname[:-4]
img_path = '../datasets/CelebA/images224x224/{}.jpg'.format(img_index)
coeff_path = '../datasets/CelebA/bfm_fitting_coeffs/{}.mat'.format(img_index)
shape_save_path = shape_save_path_.format(img_index)
albedo_save_path = albedo_save_path_.format(img_index)
color_save_path = color_save_path_.format(img_index)
## Load the deep3dmm(2019) fitted bfm coefficients
id_ , tex_ , exp_ , rot_ , trans_, gamma_ = load_coefficients(coeff_path)
id_tensor = torch.tensor(id_, dtype=torch.float32, requires_grad=False, device='cuda')
tex_tensor = torch.tensor(tex_, dtype=torch.float32, requires_grad=False, device='cuda')
rot_tensor = torch.tensor(rot_, dtype=torch.float32, requires_grad=False, device='cuda')
gamma_tensor = torch.tensor(gamma_, dtype=torch.float32, requires_grad=False, device='cuda')
face_shape = model.Shape_formation(id_coeff=id_tensor, ex_coeff=exp_tensor)
face_texture = model.Texture_formation(tex_coeff=tex_tensor) # albedo only
face_norm = model.Compute_norm(face_shape)
rotation = model.Compute_rotation_matrix(rot_tensor)
face_norm_r = face_norm.bmm(rotation)
face_color = model.Illumination_layer(face_texture, face_norm_r, gamma_tensor)
# Save as Numpy array
np.save(shape_save_path, face_shape.detach().cpu().numpy())
np.save(albedo_save_path, face_texture.detach().cpu().numpy())
np.save(color_save_path, face_color.detach().cpu().numpy())
# Release CUDA memory
del id_tensor, tex_tensor, face_shape, face_texture
torch.cuda.empty_cache()
# Release RAM
gc.collect()
#####################
## FFHQ dataset
shape_save_path = '../datasets/FFHQ/raw_bfm_shape/'
albedo_save_path = '../datasets/FFHQ/raw_bfm_albedo/'
color_save_path = '../datasets/FFHQ/raw_bfm_color/'
try:
os.mkdir(shape_save_path)
except:
pass
try:
os.mkdir(albedo_save_path)
except:
pass
try:
os.mkdir(color_save_path)
except:
pass
shape_save_path_ = shape_save_path + '{}.npy'
albedo_save_path_ = albedo_save_path + '{}.npy'
color_save_path_ = color_save_path + '{}.npy'
img_index = 0
# Natural expression
exp_tensor = torch.zeros((1,64), dtype=torch.float32, requires_grad=False, device='cuda')
for fname in tqdm(os.listdir('../datasets/FFHQ/images224x224/')):
if fname.endswith('.png'):
img_index = fname[:-4]
img_path = '../datasets/FFHQ/images224x224/{}.jpg'.format(img_index)
coeff_path = '../datasets/FFHQ/bfm_fitting_coeffs/{}.mat'.format(img_index)
shape_save_path = shape_save_path_.format(img_index)
albedo_save_path = albedo_save_path_.format(img_index)
color_save_path = color_save_path_.format(img_index)
## Load the deep3dmm(2019) fitted bfm coefficients
id_ , tex_ , exp_ , rot_ , trans_, gamma_ = load_coefficients(coeff_path)
id_tensor = torch.tensor(id_, dtype=torch.float32, requires_grad=False, device='cuda')
tex_tensor = torch.tensor(tex_, dtype=torch.float32, requires_grad=False, device='cuda')
rot_tensor = torch.tensor(rot_, dtype=torch.float32, requires_grad=False, device='cuda')
gamma_tensor = torch.tensor(gamma_, dtype=torch.float32, requires_grad=False, device='cuda')
face_shape = model.Shape_formation(id_coeff=id_tensor, ex_coeff=exp_tensor)
face_texture = model.Texture_formation(tex_coeff=tex_tensor) # albedo only
face_norm = model.Compute_norm(face_shape)
rotation = model.Compute_rotation_matrix(rot_tensor)
face_norm_r = face_norm.bmm(rotation)
face_color = model.Illumination_layer(face_texture, face_norm_r, gamma_tensor)
# Save as Numpy array
np.save(shape_save_path, face_shape.detach().cpu().numpy())
np.save(albedo_save_path, face_texture.detach().cpu().numpy())
np.save(color_save_path, face_color.detach().cpu().numpy())
# Release CUDA memory
del id_tensor, tex_tensor, face_shape, face_texture
torch.cuda.empty_cache()
# Release RAM
gc.collect()