new files

This commit is contained in:
2022-10-19 18:45:17 +00:00
parent 5881f330f6
commit 4d6296946a
11 changed files with 111618 additions and 33591 deletions
+107
View File
@@ -0,0 +1,107 @@
import os
import numpy as np
os.environ["CUDA_VISIBLE_DEVICES"] = '2'
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
import plotly.graph_objects as go
import plotly.express as ex
from custom_utils.smpl import SMPL
from custom_utils.custom_utils import m2uv_array, _maskFaces
is_skirt = False
smpl = SMPL(orig_smpl=False)
smpl_orig = SMPL(orig_smpl=True)
def get_faces_orig_smpl():
faces = np.load('custom_utils/smpl_faces_orig.npy')
return faces
def filter_uv_map():
uv_map = np.load('custom_utils/stretched_uv_map_' + ("skirt" if is_skirt == 1 else "3") + '.npy')
m2uv = np.load('custom_utils/m2uv' + ("_skirt" if is_skirt == 1 else "") + '.npy', allow_pickle=True).item()
# m2uv = m2uv_array(m2uv)
m2uv_orig = { key: m2uv[key] for key in m2uv.keys() if key < 3638 }
all_ids = []
for k in m2uv_orig.keys():
all_ids.extend(list(m2uv_orig[k]))
all_ids = np.asarray(all_ids)
uv_map_orig = np.asarray([uv_map[i] for i in range(uv_map.shape[0]) if i in all_ids])
mapping = [i for i in range(uv_map.shape[0]) if i in all_ids]
new_m2uv = dict()
for key in m2uv_orig.keys():
elems = list(m2uv_orig[key])
new_elem = set()
for elem in elems:
new_elem.add(mapping.index(elem))
new_m2uv[key] = new_elem
np.save('custom_utils/stretched_uv_map_3_orig.npy', uv_map_orig)
np.save('custom_utils/m2uv_orig.npy', new_m2uv)
print()
def test():
from custom_utils.custom_utils import _maskFaces
pose = np.zeros((1, 72), dtype=np.float32)
shape = np.zeros((1, 11), dtype=np.float32)
body = smpl_orig.run(smpl_orig.run_rodrigues(pose), shape, remove=True)[0].numpy()[0]
x = body
faces = get_faces_orig_smpl()
y = x
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=x[:,0],
y=x[:,1],
z=x[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
go.Mesh3d(
# 8 vertices of a cube
x=y[:,0],
y=y[:,1],
z=y[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1.5,1.5],),
yaxis = dict(nticks=4, range=[-1.5,1.5],),
zaxis = dict(nticks=4, range=[-1.5,1.5],),)
)
fig.show()
if __name__ == '__main__':
# test()
filter_uv_map()
+18
View File
@@ -0,0 +1,18 @@
import os
if __name__ == '__main__':
folder = '/home/workspace/local-data/cape/train/male'
out_folder = '/home/workspace/local-data/cape_processed/train/male'
ids = os.listdir(folder)
count = 0
step = 1
while count < len(ids):
# os.system('nohup python MeshUtils/curvature_labels.py -f ' + folder + ' -s ' + str(count) + ' -e ' + str(count + step) + ' -n ' + 'curv_train_' + str(count) + '_' + str(count+step) + ' > curv_train_' + str(count) + '_' + str(count+step) + '.out' + ' 2>&1 &')
os.system('nohup python curvature_labels_cape.py -if ' + folder + ' -of ' + out_folder + ' -s ' + str(count) + ' -e ' + str(count + step) + ' -n ' + 'curv_gen_2_cape_train_male_' + str(count) + '_' + str(count+step) + ' > nohup/curv_gen_2_cape_train_male_' + str(count) + '_' + str(count+step) + '.out' + ' 2>&1 &')
# os.system('python Data/convert_to_uv.py -f ' + folder + ' -s ' + str(count) + ' -e ' + str(count + step) + '')
count += step
+309
View File
@@ -0,0 +1,309 @@
import os
import sys
import tqdm
import argparse
import random
import string
import numpy as np
import trimesh as tm
import cmapy as cm
import scipy as sp
import scipy.io as sio
import plotly.graph_objects as go
from pyntcloud import PyntCloud
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import floyd_warshall, dijkstra
from custom_utils.supersmpl_io import readPC2, readPC2Frame, writePC2, readOBJ
from custom_utils.custom_utils import _maskFaces
# from FeatureLines.superSMPL.smpl import SMPL
def get_body():
smpl = SMPL('FeatureLines/superSMPL')
pose = np.zeros((1, 72), dtype=np.float32)
beta = np.zeros((1, 11), dtype=np.float32)
R = smpl.run_rodrigues(pose)
T, _, _ = smpl.run(R, beta, True)
return np.squeeze(T)
def get_skirt_body():
body = readOBJ('FeatureLines/superSMPL_skirt.obj')
return body[0]
def export_to_ply(mesh, file):
ply = tm.exchange.ply.export_ply(mesh, encoding='binary', vertex_normal=True, include_attributes=True)
output_file = open(file, "wb+")
output_file.write(ply)
output_file.close()
def get_closest_n_points(verts, idx, n):
dist = sp.spatial.distance.cdist(verts[idx, :], verts)
dist[dist == 0] = 1e9
idx_sort = np.argsort(dist, axis=-1)
closest = idx_sort[:, :n]
return closest
def histedges_equalN(x, nbin):
npt = len(x)
return np.interp(np.linspace(0, npt, nbin + 1),
np.arange(npt),
np.sort(x))
def k_neighbours(k):
body = get_skirt_body()
adj = np.ones((14475, 14475)) * 9999 # ???
diagonal = np.linspace(0, 14474, 14475, dtype=np.int32)
m = tm.Trimesh(body, F, process=False, validate=False)
edges = m.edges_unique
lengths = m.edges_unique_length
adj[edges[:, 0], edges[:, 1]] = lengths
adj[edges[:, 1], edges[:, 0]] = lengths
adj[diagonal, diagonal] = 0
adj = csr_matrix(adj)
dist = dijkstra(adj, directed=False, unweighted=False, limit=0.5)
np.save('MeshUtils/dijkstra_skirt.npy', dist)
# dist = floydWarshall(adj, 14475)
# idx_sort = np.argsort(dist, axis=-1)
# closest = idx_sort[:, :k]
pass
def generate_curvatures(folder, neighbourhood):
ids = os.listdir(folder)[int(args.start):int(args.end)]
curvatures_all = []
for id in tqdm.tqdm(ids):
garment_dir = os.path.join(folder, id)
if not os.path.isdir(garment_dir):
continue
# if os.path.exists(garment_dir + '/curvature_25_raw.npy'):
# continue
curvatures_garment = []
garments = [f.replace('.pc16','') for f in os.listdir(garment_dir) if f.find('_') == -1 and f != 'info.mat' and not f.endswith('.obj')]
# assert len (garments) == 1
if len (garments) != 1:
continue
garment = garments[0]
bodies = readPC2(os.path.join(folder, id, 'Body_posed.pc16'), True)['V']
offsetss = readPC2(os.path.join(folder, id, garment + '_registered.pc16'), True)['V']
mask = np.squeeze(sio.loadmat(os.path.join(garment_dir, garment + '_mask.mat'))['mask'].astype(bool))
_, faces = _maskFaces(bodies[0, :, :], F, mask)
v = bodies[0, mask, :] + offsetss[0, mask, :]
m = tm.Trimesh(v, faces, process=False, validate=False)
outliers = np.array([len(v) for v in m.vertex_neighbors]) == 0
outliers = np.nonzero(outliers)[0]
outliers_n_neighbours = get_closest_n_points(v, outliers, 2)
outliers_faces = np.concatenate((outliers[:, np.newaxis], outliers_n_neighbours), axis=-1)
faces = np.concatenate((faces, outliers_faces), axis=0)
step = 0
for body, offsets in zip(bodies, offsetss):
verts = body + offsets
verts = verts[mask, :]
m = tm.Trimesh(verts, faces, process=False, validate=False)
export_to_ply(m, temp_file)
cloud = PyntCloud.from_file(temp_file)
# cloud.plot(mesh=True)
k_neighbors = np.argsort(dist[mask, :][:, mask], axis=-1)[:, :neighbourhood] #cloud.get_neighbors(k=neighbourhood)
ev = cloud.add_scalar_field("eigen_values", k_neighbors=k_neighbors)
cloud.add_scalar_field("curvature", ev=ev)
curvature = np.array(cloud.points["curvature(" + str(neighbourhood+1) + ")"])
curvatures_garment.append(curvature)
print(step)
step += 1
# curvatures_all.append(np.array(curvatures_garment))
np.save(garment_dir + '/curvature_25_raw.npy', np.array(curvatures_garment))
return curvatures_all
def get_bins(values, nr_of_bins):
flattened = []
for c in values:
flattened.extend(c.flatten())
flattened = np.array(flattened)
bins = histedges_equalN(flattened, nr_of_bins)
return bins
def generate_curvature_labels(folder, bins, curvatures):
ids = os.listdir(folder)#[:10]
labels = np.linspace(0, bins.shape[0] - 2, bins.shape[0] - 1)
i = 0
for id in tqdm.tqdm(ids):
garment_dir = os.path.join(folder, id)
if not os.path.isdir(garment_dir):
continue
garments = [f.replace('.pc16','') for f in os.listdir(garment_dir) if f.find('_') == -1 and f != 'info.mat' and not f.endswith('.obj')]
# assert len (garments) == 1
if len (garments) != 1:
continue
garment = garments[0]
mask = np.squeeze(sio.loadmat(os.path.join(garment_dir, garment + '_mask.mat'))['mask'].astype(bool))
curvature = curvatures[i]
i += 1
curvature_label = np.zeros((curvature.shape[0], curvature.shape[1]))
frame = 0
for frame in range(curvature.shape[0]):
frame_labels = np.zeros((curvature.shape[1]), dtype=np.float32)
for j in range(bins.shape[0] - 2):
frame_labels[(curvature[frame, :] >= bins[j]) * (curvature[frame, :] < bins[j+1])] = labels[j]
frame_labels[curvature[frame, :] >= bins[-2]] = labels[-1]
curvature_label[frame, :] = frame_labels
frame += 1
unmasked_label = np.zeros((curvature.shape[0], 14475), dtype=np.float32)
unmasked_label[:, mask] = curvature_label
np.save(os.path.join(garment_dir, "curvature_label_" + str(nr_of_labels) + "_" + str(neighbourhood) + ".npy"), unmasked_label)
# writePC2(os.path.join(garment_dir, "curvature_label_" + str(nr_of_labels) + "_" + str(neighbourhood) + ".pc16"), unmasked_label, True)
def visualize_curvature_labels(folder):
ids = os.listdir(folder)#[:10]
for id in tqdm.tqdm(ids):
garment_dir = os.path.join(folder, id)
if not os.path.isdir(garment_dir):
continue
garments = [f.replace('.pc16','') for f in os.listdir(garment_dir) if f.find('_') == -1 and f != 'info.mat' and not f.endswith('.obj')]
# assert len (garments) == 1
if len (garments) != 1:
continue
garment = garments[0]
frame = 10
bodies = readPC2Frame(os.path.join(folder, id, 'Body_posed.pc16'), frame, True)
offsetss = readPC2Frame(os.path.join(folder, id, garment + '_registered.pc16'), frame, True)
# curvature_labels = readPC2Frame(os.path.join(folder, id, 'curvature_label_6_25.pc16'), frame, True)[:, 0]
curvature_labels = np.load(os.path.join(folder, id, 'curvature_label_6_25.npy'), allow_pickle=True)[frame, :]
mask = np.squeeze(sio.loadmat(os.path.join(garment_dir, garment + '_mask.mat'))['mask'].astype(bool))
verts = offsetss
verts, faces = _maskFaces(verts, F, mask)
# m = tm.Trimesh(verts, faces, process=False, validate=False)
# outliers = np.array([len(v) for v in m.vertex_neighbors]) == 0
# outliers = np.nonzero(outliers)[0]
# outliers_n_neighbours = get_closest_n_points(verts, outliers, 2)
# outliers_faces = np.concatenate((outliers[:, np.newaxis], outliers_n_neighbours), axis=-1)
# faces = np.concatenate((faces, outliers_faces), axis=0)
colors = np.array([cm.color('jet', 1 - i) for i in np.linspace(0, 1, int(curvature_labels.max()+1))])
colors_binned = np.zeros((curvature_labels.shape[0], 3))
for i in range(int(curvature_labels.max())+1):
print(np.count_nonzero(curvature_labels == i))
colors_binned[curvature_labels == i, :] = colors[i]
colors_binned = colors_binned[mask, :]
# m = tm.Trimesh(verts, faces, process=False, validate=False, vertex_colors=colors_binned)
# m.show()
x = verts
meshes = [go.Mesh3d(
x=x[:,0],
y=x[:,2],
z=x[:,1],
i = faces[:,0],
j = faces[:,1],
k = faces[:,2],
vertexcolor=colors_binned
),]
fig = go.Figure(data=meshes)
fig.update_layout(scene_aspectmode='manual', scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1,1],),
yaxis = dict(nticks=4, range=[-1,1],),
zaxis = dict(nticks=4, range=[-1,1],),)
)
fig.show()
print()
def read_raw_curvatures(folder):
ids = os.listdir(folder)[int(args.start):int(args.end)]
curvatures_all = []
for id in tqdm.tqdm(ids):
garment_dir = os.path.join(folder, id)
if not os.path.isdir(garment_dir):
continue
garments = [f.replace('.pc16','') for f in os.listdir(garment_dir) if f.find('_') == -1 and f != 'info.mat' and not f.endswith('.obj')]
# assert len (garments) == 1
if len (garments) != 1:
continue
curv = np.load(garment_dir + '/curvature_25_raw.npy')
curvatures_all.append(curv)
return curvatures_all
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder", help = "process the given folder", default=r'/home/workspace/data/dataset/test')
parser.add_argument("-s", "--start", help = "start of range", default=0)
parser.add_argument("-e", "--end", help = "end of range", default=1500)
parser.add_argument("-n", "--name", help = "end of range", default="lhunor")
args = parser.parse_args()
F = np.load("FeatureLines/superSMPL/smpl_faces.npy")
# k_neighbours(k=25)
folder = args.folder
nr_of_labels = 6
neighbourhood = 25
temp_file = 'temp/' + ''.join(random.choice(string.ascii_lowercase) for i in range(10)) + '.ply'
dist = np.load('MeshUtils/dijsktra.npy')
# dist = np.load('MeshUtils/dijkstra_skirt.npy')
generate_curvatures(folder, neighbourhood)
# curvatures = read_raw_curvatures(folder)
# bins = get_bins(curvatures, nr_of_labels)
# np.save('train_bins_skirt.npy', bins)
# bins = np.load('train_bins.npy')
# generate_curvature_labels(folder, bins, curvatures)
# visualize_curvature_labels(folder)
pass
-33591
View File
File diff suppressed because it is too large Load Diff
+309
View File
@@ -0,0 +1,309 @@
import os
import sys
import time
import argparse
import numpy as np
import scipy.io as sio
import scipy as sp
import plotly.graph_objects as go
from scipy.spatial.transform import Rotation
from tqdm import tqdm
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
from custom_utils.smpl import SMPL
from custom_utils.supersmpl_io import readPC2
# run = wandb.init(config=tf.compat.v1.flags.FLAGS, sync_tensorboard=True, project='unpose', name='new_smpl_2', notes='')
# artifact = wandb.Artifact('code', type='code')
# artifact.add_file('Data/unpose.py')
# artifact.add_file('Data/verify_unposed.py')
# artifact.add_file('FeatureLines/superSMPL/smpl.py')
# artifact.add_file('FeatureLines/superSMPL/smpl_2.py')
# run.log_artifact(artifact)
def read_garment(folder, id):
min_body = np.load(os.path.join(folder, id, id + '_minimal.npy'))
# data = np.load(os.path.join('/home/workspace/data/dataset/cape_release', 'sequences', id))
return min_body
def plot_3d(mesh_orig, mesh_pred, unposed, faces):
_mesh_pred = mesh_pred.copy()
_unposed = unposed.copy()
_mesh_pred[:,1] += 1
_unposed[:,1] += 3
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=mesh_orig[:,0],
y=mesh_orig[:,1],
z=mesh_orig[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
go.Mesh3d(
# 8 vertices of a cube
x=_mesh_pred[:,0],
y=_mesh_pred[:,1],
z=_mesh_pred[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2],
),
go.Mesh3d(
# 8 vertices of a cube
x=_unposed[:,0],
y=_unposed[:,1],
z=_unposed[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2],
# vertexcolor=collision_color
)
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=2, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1,1],),
yaxis = dict(nticks=4, range=[-1,3],),
zaxis = dict(nticks=4, range=[-1,1],),)
)
return fig
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder", help = "end of range", default='/home/workspace/data/dataset/cape_release/minimal_body_shape')
parser.add_argument("-s", "--start", help = "start of range", default=0)
parser.add_argument("-e", "--end", help = "end of range", default=200)
parser.add_argument("-n", "--name", help = "end of range", default='lhunor')
args = parser.parse_args()
F = np.load('/home/workspace/data/dataset/cape_release/misc/smpl_tris.npy')
gender_table = np.load('/home/workspace/data/dataset/cape_release/misc/subj_genders.pkl', allow_pickle=True)
folder = args.folder
smpl = SMPL()
iterations = 10000
ids = os.listdir(folder)[int(args.start):int(args.end)]
for id in tqdm(ids):
if not os.path.isdir(os.path.join(folder, id)):
continue
min_body = read_garment(folder, id)
pose = tf.constant(np.zeros((1, 72)), dtype=np.float32)
R = smpl.run_rodrigues(pose)
# lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(10.0, decay_steps=40000, decay_rate=0.1, staircase=False)
optimizer = tf.keras.optimizers.SGD(learning_rate=25.0)
betas = tf.Variable(np.zeros((1, 10), np.float32))
gender = np.zeros((1,1), np.float32) if gender_table[id] == 'female' else np.ones((1,1), np.float32)
@tf.function
def shaping(shape):
_shape = tf.concat((shape, gender), axis=-1)
result = smpl.run(R, _shape, remove=False)[0][0, :6890, :]
return result
@tf.function
def error():
pred = shaping(betas)
loss = tf.reduce_mean(tf.math.abs(pred - min_body))
return loss
start = time.perf_counter()
euclid_error = 10000
while True:
optimizer.minimize(error, [betas])
i = optimizer.iterations.numpy()
if i % 100 == 0:
pred = shaping(betas)
# loss = tf.reduce_mean(tf.reduce_sum(tf.math.abs(pred - posed) ** 2, axis=-1))
loss = tf.reduce_mean(tf.math.abs(pred - min_body))
euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((min_body - pred) ** 2, axis=-1)))
print('id: ' + id + ' iteration: ' + str(i) + ' loss: ' + str(loss.numpy()) + ' error: ' + str(euclid_error.numpy()) + ' time: ' + str(time.perf_counter() - start))
start = time.perf_counter()
if euclid_error < 0.0001:
break
if euclid_error < 0.0005 and i > 20000:
break
if euclid_error < 0.001 and i > 40000:
break
if i > 60000:
break
x = min_body
y = pred
faces = F
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=x[:,0],
y=x[:,1],
z=x[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
go.Mesh3d(
# 8 vertices of a cube
x=y[:,0],
y=y[:,1],
z=y[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1.5,1.5],),
yaxis = dict(nticks=4, range=[-1.5,1.5],),
zaxis = dict(nticks=4, range=[-1.5,1.5],),)
)
fig.show()
continue
offset = 1
for frame in range(posed.shape[0]):
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(10.0, decay_steps=10000, decay_rate=0.1, staircase=False)
optimizer = tf.keras.optimizers.SGD(learning_rate=lr_schedule)
unposed = tf.Variable(np.random.rand(offset, posed.shape[1], 3).astype(np.float32) * 2 - 1)
# unposed = tf.Variable(init)
@tf.function
def skinning(verts):
# verts = smpl.run(R[frame:frame+offset], tf.tile(shape[tf.newaxis,:], [offset,1]))[0].numpy()[:,mask,:]
# start = time.perf_counter()
result = smpl.run_garment(R[frame:frame+offset], tf.tile(shape[tf.newaxis,:], [offset,1]), verts, filter_idx=mask_idx)
# print(time.perf_counter() - start)
# start = time.perf_counter()
# result_2 = smpl_2.run_garment(R[frame:frame+offset], shape[:10], gender, verts, filter_idx=mask_idx)
# print(time.perf_counter() - start)
# test = np.abs(result.numpy() - result_2.numpy()) > 1e-4
# x = np.sum(test, axis=-1).sum(-1).sum(-1)
# euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((result - result_2) ** 2, axis=-1)))
# batch = 25
# _, faces = _maskFaces(np.zeros((14475,3)), F, mask)
# go.Figure(data=[
# go.Mesh3d(
# # 8 vertices of a cube
# x=result[batch,:,0],
# y=result[batch,:,1],
# z=result[batch,:,2],
# # i, j and k give the vertices of triangles
# i = faces[:,0],
# j = faces[:,1],
# k = faces[:,2]
# ),
# go.Mesh3d(
# # 8 vertices of a cube
# x=result_2[batch,:,0],
# y=result_2[batch,:,1],
# z=result_2[batch,:,2],
# # i, j and k give the vertices of triangles
# i = faces[:,0],
# j = faces[:,1],
# k = faces[:,2],
# )]).show()
return result
@tf.function
def error():
pred = skinning(unposed)
loss = tf.reduce_mean(tf.math.abs(pred - posed[frame]))
return loss
start = time.perf_counter()
euclid_error = 10000
for i in range(iterations):
optimizer.minimize(error, [unposed])
if i % 50 == 0:
pred = skinning(unposed)
# loss = tf.reduce_mean(tf.reduce_sum(tf.math.abs(pred - posed) ** 2, axis=-1))
loss = tf.reduce_mean(tf.math.abs(pred - posed[frame]))
euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((posed[frame] - pred) ** 2, axis=-1)))
print('id: ' + id + ' frame: ' + str(frame) + ' iteration: ' + str(i) + ' loss: ' + str(loss.numpy()) + ' error: ' + str(euclid_error.numpy()) + ' time: ' + str(time.perf_counter() - start))
start = time.perf_counter()
if euclid_error < 0.001:
break
result = np.zeros((14475, 3), dtype = np.float32)
result[mask,:] = unposed.numpy()
np.savez_compressed(os.path.join(folder, id, 'mesh_unposed', id + '_' + str(frame) + '.npz'), mesh_unposed=result)
init = unposed
# pred, unposed = pred.numpy(), unposed.numpy()
# _, faces = _maskFaces(np.zeros((14475,3)), F, mask)
# k = 0
# plot_3d(posed[k], pred[k], unposed[k], faces).show()
# print()
+279
View File
@@ -0,0 +1,279 @@
import os
import sys
import time
import argparse
import numpy as np
import scipy.io as sio
import scipy as sp
import plotly.graph_objects as go
from scipy.spatial.transform import Rotation
from tqdm import tqdm
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
from custom_utils.smpl import SMPL
from custom_utils.supersmpl_io import readPC2
# run = wandb.init(config=tf.compat.v1.flags.FLAGS, sync_tensorboard=True, project='unpose', name='new_smpl_2', notes='')
# artifact = wandb.Artifact('code', type='code')
# artifact.add_file('Data/unpose.py')
# artifact.add_file('Data/verify_unposed.py')
# artifact.add_file('FeatureLines/superSMPL/smpl.py')
# artifact.add_file('FeatureLines/superSMPL/smpl_2.py')
# run.log_artifact(artifact)
def read_garment(folder, id):
min_body = np.load(os.path.join(folder, id, id + '_minimal.npy'))
return min_body
def plot_3d(mesh_orig, mesh_pred, unposed, faces):
_mesh_pred = mesh_pred.copy()
_unposed = unposed.copy()
_mesh_pred[:,1] += 1
_unposed[:,1] += 3
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=mesh_orig[:,0],
y=mesh_orig[:,1],
z=mesh_orig[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
go.Mesh3d(
# 8 vertices of a cube
x=_mesh_pred[:,0],
y=_mesh_pred[:,1],
z=_mesh_pred[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2],
),
go.Mesh3d(
# 8 vertices of a cube
x=_unposed[:,0],
y=_unposed[:,1],
z=_unposed[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2],
# vertexcolor=collision_color
)
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=2, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1,1],),
yaxis = dict(nticks=4, range=[-1,3],),
zaxis = dict(nticks=4, range=[-1,1],),)
)
return fig
def verify():
folder = '/home/workspace/data/dataset/cape_release/minimal_body_shape'
F = np.load('/home/workspace/data/dataset/cape_release/misc/smpl_tris.npy')
gender_table = np.load('/home/workspace/data/dataset/cape_release/misc/subj_genders.pkl', allow_pickle=True)
smpl = SMPL(orig_smpl=True)
ids = os.listdir(folder)
for id in tqdm(ids):
if not os.path.isdir(os.path.join(folder, id)):
continue
orig_body = read_garment(folder, id)
pose = tf.constant(np.zeros((1, 72)), dtype=np.float32)
R = smpl.run_rodrigues(pose)
shape = np.load(os.path.join(folder, id, 'betas.npy'))
gender = [0] if gender_table[id] == 'female' else [1]
shape = np.concatenate((shape, gender)).astype(np.float32)
new_body = smpl.run(R, shape[np.newaxis,:], remove=False)[0][0]
euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((orig_body[smpl.to_keep,:] - new_body.numpy()[smpl.to_keep,:]) ** 2, axis=-1))).numpy()
x = orig_body
y = new_body
faces = F
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=x[:,0],
y=x[:,1],
z=x[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
go.Mesh3d(
# 8 vertices of a cube
x=y[:,0],
y=y[:,1],
z=y[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1.5,1.5],),
yaxis = dict(nticks=4, range=[-1.5,1.5],),
zaxis = dict(nticks=4, range=[-1.5,1.5],),)
)
fig.show()
print()
if __name__ == '__main__':
verify()
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder", help = "end of range", default='/home/workspace/data/dataset/cape_release/minimal_body_shape')
parser.add_argument("-s", "--start", help = "start of range", default=0)
parser.add_argument("-e", "--end", help = "end of range", default=200)
parser.add_argument("-n", "--name", help = "end of range", default='lhunor')
args = parser.parse_args()
F = np.load('/home/workspace/data/dataset/cape_release/misc/smpl_tris.npy')
F = np.load('custom_utils/smpl_faces_orig.npy')
gender_table = np.load('/home/workspace/data/dataset/cape_release/misc/subj_genders.pkl', allow_pickle=True)
folder = args.folder
smpl = SMPL(orig_smpl=True)
iterations = 10000
ids = os.listdir(folder)[int(args.start):int(args.end)]
for id in tqdm(ids):
if not os.path.isdir(os.path.join(folder, id)):
continue
min_body = read_garment(folder, id)
min_body = min_body[smpl.to_keep, :]
pose = tf.constant(np.zeros((1, 72)), dtype=np.float32)
R = smpl.run_rodrigues(pose)
zero_body = smpl.run(R, np.zeros((1, 11), np.float32), remove=False)[0]
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(1.0, decay_steps=40000, decay_rate=0.1, staircase=False)
optimizer = tf.keras.optimizers.SGD(learning_rate=1.0)
betas = tf.Variable(np.zeros((1, 10), np.float32))
# gender = np.zeros((1,1), np.float32) #F
gender = 0 if gender_table[id] == 'female' else 1
@tf.function
def shaping(shape):
result = (tf.tensordot(shape, smpl.shapedirs[gender], axes=[[1], [2]]) + tf.reshape(smpl.v_template[gender], shape=(1,6890,3)))[0]
result = tf.gather(result, smpl.to_keep, axis=0)
return result
@tf.function
def error():
pred = shaping(betas)
loss = tf.reduce_mean(tf.math.abs(pred - min_body))
return loss
start = time.perf_counter()
euclid_error = 10000
while True:
optimizer.minimize(error, [betas])
i = optimizer.iterations.numpy()
if i % 100 == 0:
pred = shaping(betas)
# loss = tf.reduce_mean(tf.reduce_sum(tf.math.abs(pred - posed) ** 2, axis=-1))
loss = tf.reduce_mean(tf.math.abs(pred - min_body))
euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((min_body - pred) ** 2, axis=-1)))
print('id: ' + id + ' iteration: ' + str(i) + ' loss: ' + str(loss.numpy()) + ' error: ' + str(euclid_error.numpy()) + ' time: ' + str(time.perf_counter() - start))
start = time.perf_counter()
if euclid_error < 0.0001:
break
if euclid_error < 0.0005 and i > 20000:
break
if euclid_error < 0.001 and i > 40000:
break
if i > 60000:
break
x = min_body
y = pred
faces = F
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=x[:,0],
y=x[:,1],
z=x[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
go.Mesh3d(
# 8 vertices of a cube
x=y[:,0],
y=y[:,1],
z=y[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1.5,1.5],),
yaxis = dict(nticks=4, range=[-1.5,1.5],),
zaxis = dict(nticks=4, range=[-1.5,1.5],),)
)
fig.show()
np.save(os.path.join(folder, id, 'betas_2.npy'), betas[0])
continue
+112
View File
@@ -0,0 +1,112 @@
import sys
import numpy as np
import pickle as pkl
from scipy import sparse
from time import time
from custom_utils.supersmpl_io import writeOBJ, readOBJ
def makeGraph(F):
G = {}
for f in F:
N = len(f)
for i,v in enumerate(f):
if not v in G: G[v] = {}
G[v][f[(i+1) % N]] = 1
return G
def extrapolateMissings(M, G):
for i in range(6890, M.shape[0]):
if np.isnan(M[i]).all():
M[i] = neighAvg(M, G, i)
elif np.isnan(M[i]).any():
print("HOW?")
print(M[i])
if np.isnan(M).any(): extrapolateMissings(M, G)
def neighAvg(M, G, i):
neigh = set(G[i].keys())
avg = 0
count = 0
for n in neigh:
if not np.isnan(M[n]).any():
avg += M[n]
count += 1
if count: return avg / count
return np.nan
if __name__ == '__main__':
g = 'f'
# Read Super topology
V, F, Vt, Ft = readOBJ('super_f_avg.obj')
N = V.shape[0]
G = makeGraph(F)
# Read PKL
print("Loading .pkl file")
with open('custom_data/model_' + g + '.pkl', 'rb') as f:
M = pkl.load(f, encoding='latin1')
# Fields
# print("Checking fields and shapes")
# for k in M:
# print(k, M[k].shape)
# Faces
print("New faces...")
M['f'] = F
# J_regressor (24, 6890)
# Sparse matrix, just change shape to add zeros
print("New regressor")
A = np.zeros((24, N), np.float32)
A[:,:6890] = M['J_regressor'].todense()
M['J_regressor'] = sparse.csr_matrix(A)
# Weights (6890, 24)
print("New weights")
start = time()
A = np.empty((N, 24), np.float32)
A[:] = np.nan
A[:6890] = M['weights'][:6890]
extrapolateMissings(A, G)
M['weights'] = A
print(time() - start)
# Posedirs (6890, 3, 207)
print("New posedirs")
start = time()
A = np.empty((N, 3, 207), np.float32)
A[:] = np.nan
A[:6890] = M['posedirs'][:6890]
extrapolateMissings(A, G)
M['posedirs'] = A
print(time() - start)
# v_tempalte (6890, 3)
print("New template")
start = time()
A = np.empty((N, 3), np.float32)
A[:] = np.nan
A[:6890] = M['v_template'][:6890]
extrapolateMissings(A, G)
M['v_template'] = A
print(time() - start)
# Shapedirs (6890, 3, 10)
print("New shapedirs")
start = time()
A = np.empty((N, 3, 10), np.float32)
A[:] = np.nan
A[:6890] = M['shapedirs'][:6890]
extrapolateMissings(A, G)
M['shapedirs'] = A
print(time() - start)
# Dump
print("Saving new model")
with open('super_model_' + g + '.pkl', 'wb') as f:
pkl.dump(M, f)
+92
View File
@@ -0,0 +1,92 @@
import os
import numpy as np
import numpy as np
import pickle as pkl
from scipy import sparse
from time import time
import plotly.graph_objects as go
from custom_utils.supersmpl_io import writeOBJ, readOBJ
def makeGraph(F):
G = {}
for f in F:
N = len(f)
for i,v in enumerate(f):
if not v in G: G[v] = {}
G[v][f[(i+1) % N]] = 1
return G
def extrapolateMissings(M, G):
for i in range(6890, M.shape[0]):
if np.isnan(M[i]).all():
M[i] = neighAvg(M, G, i)
elif np.isnan(M[i]).any():
print("HOW?")
print(M[i])
if np.isnan(M).any(): extrapolateMissings(M, G)
def neighAvg(M, G, i):
neigh = set(G[i].keys())
avg = 0
count = 0
for n in neigh:
if not np.isnan(M[n]).any():
avg += M[n]
count += 1
if count: return avg / count
return np.nan
def convert(smpl, G):
supersmpl = np.empty((27578, 3), np.float32)
supersmpl[:] = np.nan
supersmpl[:6890] = smpl
extrapolateMissings(supersmpl, G)
return supersmpl
if __name__ == '__main__':
g = 'f'
# Read Super topology
V, F, Vt, Ft = readOBJ('super_' + g + '_avg.obj')
G = makeGraph(F)
folder = '/home/workspace/data/dataset/cape/cape_release'
test = np.load(os.path.join('/home/workspace/data/dataset/cape_zips/03383/shortlong_ATUsquat/shortlong_ATUsquat.000105.npz'))
faces = np.load(os.path.join(folder, 'misc/smpl_tris.npy'))
min_body = np.load(os.path.join(folder, 'minimal_body_shape/03383/03383_minimal.npy'))
offsets_cano = test['v_cano'] - min_body
supersmpl = convert(test['v_posed'], G)
x = supersmpl
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=x[:,0],
y=x[:,1],
z=x[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1,1],),
yaxis = dict(nticks=4, range=[-1,1],),
zaxis = dict(nticks=4, range=[-1,1],),)
)
fig.show()
print()
+82
View File
@@ -0,0 +1,82 @@
import os
import sys
os.environ["CUDA_VISIBLE_DEVICES"] = '2'
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
import numpy as np
import plotly.graph_objects as go
from custom_utils.smpl import SMPL
from custom_utils.supersmpl_io import writeOBJ
from custom_utils.custom_utils import _maskFaces
def getAdjMatrix(faces):
adj = np.zeros((np.max(faces[:])+1, np.max(faces[:])+1))
for face in faces:
adj[face[0], face[1]] = 1
adj[face[1], face[0]] = 1
adj[face[2], face[1]] = 1
adj[face[0], face[2]] = 1
adj[face[1], face[2]] = 1
adj[face[2], face[0]] = 1
return adj
if __name__ == '__main__':
smpl = SMPL()
pose = np.zeros((1, 72), dtype=np.float32)
shape = np.zeros((1, 11), dtype=np.float32)
body = smpl.run(smpl.run_rodrigues(pose), shape, remove=False)[0].numpy()[0]
faces = np.load(os.path.join('/home/workspace/data/dataset/cape/cape_release', 'misc/smpl_tris.npy')).astype(np.int32)
mask = np.zeros((27578), dtype=bool)
mask[:6890] = True
verts, faces = _maskFaces(body, faces, mask)
adj = getAdjMatrix(faces.astype(np.int32))
adj = np.triu(adj)
edges = np.stack(np.nonzero(adj)).transpose()
x = body[:6890]
fig = go.Figure(data=[
go.Mesh3d(
# 8 vertices of a cube
x=x[:,0],
y=x[:,1],
z=x[:,2],
# i, j and k give the vertices of triangles
i = faces[:,0],
j = faces[:,1],
k = faces[:,2]
),
])
fig.update_layout(scene_aspectmode='manual',
scene_aspectratio=dict(x=1, y=1, z=1))
fig.update_layout(
scene = dict(
xaxis = dict(nticks=4, range=[-1,1],),
yaxis = dict(nticks=4, range=[-1,1],),
zaxis = dict(nticks=4, range=[-1,1],),)
)
fig.show()
writeOBJ('temp/smpl_orig_f.obj', x, faces)
print()
+55155
View File
File diff suppressed because it is too large Load Diff
+55155
View File
File diff suppressed because it is too large Load Diff