Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

60x Speedup on IUV2FBC Function. Greatly speeds up converting a 2D person image to a 3D model. #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added DensePoseData/demo_data_2/INDS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added DensePoseData/demo_data_2/IUV.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added DensePoseData/demo_data_2/im.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions detectron/utils/densepose_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,69 @@ def barycentric_coordinates(self,P0, P1, P2, P):
#
return(1-(r+t),r,t)

def barycentric_coordinates_fast(self, P0, P1, P2, P):
# This is a merge of barycentric_coordinates_exists & barycentric_coordinates.
# Inputs are (n, 3) shaped.

u = P1 - P0 #u is (n,3)
v = P2 - P0 #v is (n,3)
w = P.T - P0 #w is (n,3)
#
vCrossW = np.cross(v, w) #result is (n,3)
vCrossU = np.cross(v, u) #result is (n,3)
A = np.einsum('nd,nd->n', vCrossW, vCrossU) # vector-wise dot product. Result shape is (n,)
#
uCrossW = np.cross(u, w)
uCrossV = - vCrossU
B = np.einsum('nd,nd->n', uCrossW, uCrossV)
#
sq_denoms = np.einsum('nd,nd->n', uCrossV, uCrossV) #result shape is (n,)
sq_rs = np.einsum('nd,nd->n', vCrossW, vCrossW)
sq_ts = np.einsum('nd,nd->n', uCrossW, uCrossW)
rs = np.sqrt(sq_rs / sq_denoms) #result shape is (n,)
ts = np.sqrt(sq_ts / sq_denoms)
#
results = [None] * P0.shape[0]
for i in range(len(results)):
if not (A[i] < 0 or B[i] < 0):
if ((rs[i] <= 1) and (ts[i] <= 1) and (rs[i] + ts[i] <= 1)):
results[i] = (1 - (rs[i] + ts[i]) , rs[i], ts[i])
return results

def IUV2FBC_fast( self, I_point , U_point, V_point):
P = np.array([ U_point , V_point , 0 ])

FaceIndicesNow = np.where( self.FaceIndices == I_point )

FacesNow = self.FacesDensePose[FaceIndicesNow]
#
P_0 = np.vstack( (self.U_norm[FacesNow][:,0], self.V_norm[FacesNow][:,0], np.zeros(self.U_norm[FacesNow][:,0].shape))).transpose()
P_1 = np.vstack( (self.U_norm[FacesNow][:,1], self.V_norm[FacesNow][:,1], np.zeros(self.U_norm[FacesNow][:,1].shape))).transpose()
P_2 = np.vstack( (self.U_norm[FacesNow][:,2], self.V_norm[FacesNow][:,2], np.zeros(self.U_norm[FacesNow][:,2].shape))).transpose()
#
bcs = self.barycentric_coordinates_fast(P_0, P_1, P_2, P)
for i, bc in enumerate(bcs):
if bc is not None:
bc1,bc2,bc3 = bc
return(FaceIndicesNow[0][i],bc1,bc2,bc3)
#
# If the found UV is not inside any faces, select the vertex that is closest!
#
D1 = scipy.spatial.distance.cdist( np.array( [U_point,V_point])[np.newaxis,:] , P_0[:,0:2]).squeeze()
D2 = scipy.spatial.distance.cdist( np.array( [U_point,V_point])[np.newaxis,:] , P_1[:,0:2]).squeeze()
D3 = scipy.spatial.distance.cdist( np.array( [U_point,V_point])[np.newaxis,:] , P_2[:,0:2]).squeeze()
#
minD1 = D1.min()
minD2 = D2.min()
minD3 = D3.min()
#
if((minD1< minD2) & (minD1< minD3)):
return( FaceIndicesNow[0][np.argmin(D1)] , 1.,0.,0. )
elif((minD2< minD1) & (minD2< minD3)):
return( FaceIndicesNow[0][np.argmin(D2)] , 0.,1.,0. )
else:
return( FaceIndicesNow[0][np.argmin(D3)] , 0.,0.,1. )

def IUV2FBC( self, I_point , U_point, V_point):
P = [ U_point , V_point , 0 ]
FaceIndicesNow = np.where( self.FaceIndices == I_point )
Expand Down
367 changes: 367 additions & 0 deletions notebooks/DensePose-Fast-IUV-2-XYZ.ipynb

Large diffs are not rendered by default.