def init_based_on_last_frame(this, lastResult): this.schedule, this.Q = init_schedule(this, this.A, this.nEdges) # Initialize the node data (particles, likelihood and max marginal value) this.b = [None]*this.nNodes this.new_b = [None]*this.nNodes for i in this.nodeIdx: #print ' ###*** nParticles ' +str(i) + ': ' + str(this.nParticles[i]) this.b[i] = {'x':lastResult.new_b[i]['x'][:,:this.nParticles[i]], 'L':np.zeros((this.nParticles[i])), 'value':np.zeros((this.nParticles[i]))} #print ' *** x: ' + str(this.b[i]['x'].shape) #print ' *** L: ' + str(this.b[i]['L'].shape) #print ' *** v: ' + str(this.b[i]['value'].shape) this.new_b[i] = {'x':lastResult.new_b[i]['x'][:,:this.nParticles[i]], 'L':np.zeros((this.nParticles[i])), 'value':np.zeros((this.nParticles[i]))} # Particles initialization init_particles_based_on_last_frame(this, lastResult) # Beliefs initialization for i in this.nodeIdx: this.b[i]['L'] = particles.compute_likelihood(this, i, this.b[i]['x']) this.b[i]['value'] = this.b[i]['L'] # Messages initialization init_messages(this) return
def compute_model_log_posterior(this): """" Computes the energy of the current sbm body model in the dpmp class (this.body) """ body = this.body # Compute particles and likelihood X = [None]*this.nNodes logL = np.zeros([this.nNodes]) for part in body.partSet: x = particles.get_from_sbm(this, body, part) X[part] = np.asarray([x]).T logL[part], D = particles.compute_likelihood(this, part, X[part], return_normals_cost=True) if this.verbose > 1: print 'part ' + this.body.names[part] + ' likelihood: ' + str(logL[part]) + ' normals cost: ' + str(D) logPair = np.zeros([body.nPairs]) for k, pair in enumerate(body.pairs): logPair[k] = compute_pairwise_stitch_potential(this, pair[0], pair[1], X[pair[0]], X[pair[1]]) if this.verbose > 1: print 'pair ' + this.body.names[pair[0]] + ' ' + this.body.names[pair[1]] + ' stitch :' + str(logPair[k]) logLs = np.sum(logL) # The pairwise stitch cost and the camera potentials are counted 2 times logP = np.sum(logPair)/2 logPos = logLs + logP return logPos, logLs, logP
def init(this): this.schedule, this.Q = init_schedule(this, this.A, this.nEdges) # Initialize the node data (particles, likelihood and max marginal value) this.b = [None]*this.nNodes this.new_b = [None]*this.nNodes for i in this.nodeIdx: this.b[i] = {'x':np.zeros((this.nodeDim[i],this.nParticles[i])), 'L':np.zeros((this.nParticles[i])), 'value':np.zeros((this.nParticles[i]))} this.new_b[i] = {'x':np.zeros((this.nodeDim[i],this.nParticles[i])), 'L':np.zeros((this.nParticles[i])), 'value':np.zeros((this.nParticles[i]))} # Particles initialization init_particles(this) # Beliefs initialization for i in this.nodeIdx: this.b[i]['L'] = particles.compute_likelihood(this, i, this.b[i]['x']) this.b[i]['value'] = this.b[i]['L'] # Messages initialization init_messages(this) return
def run(nParticles, nSteps, basePath, faustId, outputPath, isTest, params, code, seed, frameId, genderArgv, lastResult=None): np.random.seed(seed) nBtorso = 12 nB = 5 nBshape = 4 gender = 'male' sbmModel = "model_ho_male_5_reduced" if genderArgv == 'female': gender = 'female' sbmModel = "model_ho_female_5_reduced" b = sbm.Sbm(basePath) b.gender = gender d = dpmp.Dpmp(b, nBtorso, nB, nBshape, nParticles, 0, sbmModel) d.body.fixedShape = False d.compute_normals_cost = True d.select_msg = True d.probRandomWalk = 0.5 d.use_map_particle_for_rnd_walk = False d.LMsteps = 5 # Actually is 4 d.init_torso_location = np.zeros((3)) d.init_torso_rotation = np.zeros((3)) d.init_with_global_rotation = True d.springSigma = 0 d.display = -1 d.verbose = 1 d.likelihoodType = '3Dlikelihood' if d.verbose > 0: print 'MODEL ' + sbmModel print 'GENDER ' + d.body.gender # Inference parameters d.particle_genericSigma = params['genericSigma']; d.particle_rSigma = params['rSigma']; d.particle_tSigma = params['tSigma'] d.particle_posePCAsigmaScale = params['posePCAsigmaScale']; d.particle_shapePCAsigmaScale = params['shapePCAsigmaScale'] d.robustOffset = params['robustOffset']; d.robustGamma = params['robustGamma']; l_alphaNormal = params['l_alphaNormal'] l_alphaLoose = params['l_alphaLoose']; l_alphaVeryLoose = params['l_alphaVeryLoose']; s_alphaNormal = params['s_alphaNormal'] s_alphaLoose = params['s_alphaLoose']; s_alphaTight = params['s_alphaTight']; alphaRef = params['alphaRef'] # When to change parameters during inference if ADAPTIVE_WEIGHTS: fullModelStart = nSteps/4 refinementStart = 2*nSteps/4 greedyStart = 3*nSteps/4 else: fullModelStart = 1 refinementStart = nSteps+1 greedyStart = nSteps+1 # Load one example to use as test data load_mesh.load_FAUST_scan(d, basePath+faustId, isTest) # Inference lower_parts = np.array([2, 0, 5, 10, 12, 4, 1, 15]) logB = np.zeros((nSteps)) if ADAPTIVE_WEIGHTS: d.likelihoodAlpha[:] = l_alphaNormal d.likelihoodAlpha[lower_parts] = l_alphaVeryLoose d.stitchAlpha = s_alphaNormal*np.ones((d.nNodes, d.nNodes)) d.stitchAlpha[d.body.parts['ll_r'], d.body.parts['foot_r']] = s_alphaLoose d.stitchAlpha[d.body.parts['foot_r'], d.body.parts['ll_r']] = s_alphaLoose d.stitchAlpha[d.body.parts['ll_l'], d.body.parts['foot_l']] = s_alphaLoose d.stitchAlpha[d.body.parts['foot_l'], d.body.parts['ll_l']] = s_alphaLoose d.stitchAlpha[d.body.parts['la_r'], d.body.parts['hand_r']] = s_alphaLoose d.stitchAlpha[d.body.parts['hand_r'], d.body.parts['la_r']] = s_alphaLoose d.stitchAlpha[d.body.parts['la_l'], d.body.parts['hand_l']] = s_alphaLoose d.stitchAlpha[d.body.parts['hand_l'], d.body.parts['la_l']] = s_alphaLoose d.stitchAlpha[d.body.parts['ul_r'], d.body.parts['torso']] = s_alphaTight d.stitchAlpha[d.body.parts['torso'], d.body.parts['ul_r']] = s_alphaTight d.stitchAlpha[d.body.parts['ul_l'], d.body.parts['torso']] = s_alphaTight d.stitchAlpha[d.body.parts['torso'], d.body.parts['ul_l']] = s_alphaTight else: d.likelihoodAlpha[:] = l_alphaNormal d.stitchAlpha = s_alphaNormal*np.ones((d.nNodes, d.nNodes)) d.nSteps = nSteps for s in range(nSteps): d.step = s if ADAPTIVE_WEIGHTS: if s == fullModelStart: d.likelihoodAlpha[lower_parts] = l_alphaNormal d.likelihoodAlpha[d.body.parts['hand_r']] = l_alphaLoose d.likelihoodAlpha[d.body.parts['hand_l']] = l_alphaLoose d.likelihoodAlpha[d.body.parts['foot_r']] = l_alphaLoose d.likelihoodAlpha[d.body.parts['foot_l']] = l_alphaLoose d.stitchAlpha = s_alphaNormal*np.ones((d.nNodes, d.nNodes)) d.stitchAlpha[d.body.parts['ul_r'], d.body.parts['torso']] = s_alphaTight d.stitchAlpha[d.body.parts['torso'], d.body.parts['ul_r']] = s_alphaTight d.stitchAlpha[d.body.parts['ul_l'], d.body.parts['torso']] = s_alphaTight d.stitchAlpha[d.body.parts['torso'], d.body.parts['ul_l']] = s_alphaTight # Recompute the likelihood of the particles as I have changed the weight d.compute_normals_cost = True for v in d.nodeIdx: new_L = particles.compute_likelihood(d, v, d.b[v]['x']) d.b[v]['L'] = new_L.copy() # Refinement if s == refinementStart: d.particle_genericSigma = alphaRef*d.particle_genericSigma d.particle_rSigma = alphaRef*d.particle_rSigma d.particle_tSigma = alphaRef*d.particle_tSigma d.particle_posePCAsigmaScale = alphaRef*d.particle_posePCAsigmaScale d.particle_shapePCAsigmaScale = alphaRef*d.particle_shapePCAsigmaScale d.stitchAlpha = s_alphaTight*np.ones((d.nNodes, d.nNodes)) # Greedy resampling around the best solution if s == greedyStart: d.select_msg = False # Use m-best instead d.probRandomWalk = 1.0 d.use_map_particle_for_rnd_walk = True tic = time.time() logB[s] = run_DPMP_step(d, s, frameId, lastResult) toc = time.time() - tic #if d.display ==4: #show_all_particles(d, faustId, nParticles, s) if d.verbose > 0: #print str(s) + ' time to run DPMP step: ' + str(toc) logPos, logL, logP = compute_model_log_posterior(d) #print 'iter ' + str(s) + ' logPos= ' + str(logPos) + ' logL= ' + str(logL) + ' logP= ' + str(logP) #print str(s) + ': ' + str(logB[s]) #filename = 'faustID_' + faustId + '_' + str(seed) + '_' + str(s) + '.png' #if d.display > 0: #ba.show_me(d.body, scan=d.scanMesh, filename='dpmp_step_'+faustId + '_' +str(s)+'.png') # Show the solution #if d.display > 0: #filename = code + 'faustID_' + faustId + '_' + str(seed) + '.png' #ba.show_me(d.body, dbstop=False, scan=d.scanMesh, filename=filename) #if d.verbose > 0: #print 'negative energy at each iteration:' #print logB # Save the result as a single mesh v, f, joints, skeleton = ba.sbm_to_scape_mesh(d.body, d.scanCenter) mesh_data = {'v':v, 'f':f} filename = basePath+outputPath+'.pkl' #dpmp.save_dpmp(d, logB, params, mesh_data, filename) dpmp.show_result(filename) # Save in ply from my_mesh.mesh import myMesh m = myMesh(v=v, f=f, e=[]) filename = basePath+outputPath + '.ply' m.save_ply(filename) # My code starts: #print len(skeleton) m = myMesh(v=joints, f=[], e = skeleton) filename = basePath+outputPath +'_skeleton.ply' m.save_ply(filename) # My code ends: #save landMark file filename = basePath+outputPath + '.lnd' m.save_lnd(filename) return d
def run_DPMP_step(dpmp, s, frameId, lastResult=None): """ Run a step of diverse particle max product. @params: dpmp Is an object of class Dpmp @params: s Is the step index """ if dpmp.verbose >= 1: print 'PBP Iter ' + str(s) if s == 0 and frameId == 0: # Only do the initialization and run BP initBP.init(dpmp) update_all_messages.update(dpmp) elif s == 0: initBP.init_based_on_last_frame(dpmp, lastResult) update_all_messages.update(dpmp) else: # In each step we perform augment + BP + select + BP # Save the current number of particles nParticles_old = dpmp.nParticles.copy() # Define the augmentation strategy. Can be chosen per step, or a different strategy per particle (in this case # we do the selection later) if dpmp.proposal_per_particle: augment_type = None else: randVal = random.random() if randVal <= (1.0 - dpmp.probRandomWalk - dpmp.probUniform): augment_type = 'NBR' else: if randVal <= (1.0 - dpmp.probUniform): augment_type = 'RANDWALK' else: augment_type = 'UNIFORM' # Sample new particles (the are saved in dpmp.new_b) tic = time.time() augmentTypes = sample_particles.sample_particles(dpmp, augment_type) toc = time.time() if dpmp.verbose > 0: print 'time to sample new particles: ' + str(toc - tic) # Augment the partice set on each node (dpmp.b) tic = time.time() for v in dpmp.nodeIdx: dpmp.b[v]['x'] = np.hstack( (dpmp.b[v]['x'].copy(), dpmp.new_b[v]['x'].copy())) dpmp.nParticles[v] = dpmp.b[v]['x'].shape[1] new_L = particles.compute_likelihood(dpmp, v, dpmp.new_b[v]['x']) dpmp.b[v]['L'] = np.vstack( (dpmp.b[v]['L'].copy(), new_L.copy())).flatten() dpmp.b[v]['value'] = np.zeros((dpmp.nParticles[v])) toc = time.time() if dpmp.verbose > 0: print 'time for likelihood of augmented particles: ' + str(toc - tic) # Update the messages on the extended sets of particles update_all_messages.update(dpmp) # Select: we select into new_b the chosen particles from the extended set in b dpmp.nParticles = nParticles_old.copy() if dpmp.select_msg: I_accept = select_msg_faster(dpmp) else: if dpmp.verbose > 0: print 'select m-best!' I_accept = select_mbest(dpmp) if dpmp.verbose > 2: print 'Acceptance rates:' for v in dpmp.nodeIdx: print dpmp.body.names[v] + ': ' + str( np.sum(I_accept[v] >= dpmp.nParticles[v]) / (2.0 * dpmp.nParticles[v])) for v in dpmp.nodeIdx: dpmp.b[v]['value'] = dpmp.new_b[v]['value'].copy() dpmp.b[v]['L'] = dpmp.new_b[v]['L'].copy() dpmp.b[v]['x'] = dpmp.new_b[v]['x'].copy() update_all_messages.update(dpmp) logB = set_me_as_solution.fun(dpmp, 0) if dpmp.verbose > 1: print 'negative energy ' + str(logB) return logB
def run_DPMP_step(dpmp, s, frameId, lastResult = None): """ Run a step of diverse particle max product. @params: dpmp Is an object of class Dpmp @params: s Is the step index """ if dpmp.verbose >= 1: print 'PBP Iter ' + str(s) if s == 0 and frameId == 0: # Only do the initialization and run BP initBP.init(dpmp) update_all_messages.update(dpmp) elif s == 0: initBP.init_based_on_last_frame(dpmp, lastResult) update_all_messages.update(dpmp) else: # In each step we perform augment + BP + select + BP # Save the current number of particles nParticles_old = dpmp.nParticles.copy() # Define the augmentation strategy. Can be chosen per step, or a different strategy per particle (in this case # we do the selection later) if dpmp.proposal_per_particle: augment_type = None else: randVal = random.random() if randVal <= (1.0 - dpmp.probRandomWalk - dpmp.probUniform): augment_type = 'NBR' else: if randVal <= (1.0 - dpmp.probUniform): augment_type = 'RANDWALK' else: augment_type = 'UNIFORM' # Sample new particles (the are saved in dpmp.new_b) tic = time.time() augmentTypes = sample_particles.sample_particles(dpmp, augment_type) toc = time.time() if dpmp.verbose > 0: print 'time to sample new particles: ' + str(toc-tic) # Augment the partice set on each node (dpmp.b) tic = time.time() for v in dpmp.nodeIdx: dpmp.b[v]['x'] = np.hstack((dpmp.b[v]['x'].copy(), dpmp.new_b[v]['x'].copy())) dpmp.nParticles[v] = dpmp.b[v]['x'].shape[1] new_L = particles.compute_likelihood(dpmp, v, dpmp.new_b[v]['x']) dpmp.b[v]['L'] = np.vstack((dpmp.b[v]['L'].copy(), new_L.copy())).flatten() dpmp.b[v]['value'] = np.zeros((dpmp.nParticles[v])) toc = time.time() if dpmp.verbose > 0: print 'time for likelihood of augmented particles: ' + str(toc - tic) # Update the messages on the extended sets of particles update_all_messages.update(dpmp) # Select: we select into new_b the chosen particles from the extended set in b dpmp.nParticles = nParticles_old.copy() if dpmp.select_msg: I_accept = select_msg_faster(dpmp) else: if dpmp.verbose > 0: print 'select m-best!' I_accept = select_mbest(dpmp) if dpmp.verbose > 2: print 'Acceptance rates:' for v in dpmp.nodeIdx: print dpmp.body.names[v] + ': ' + str(np.sum(I_accept[v] >= dpmp.nParticles[v])/(2.0*dpmp.nParticles[v])) for v in dpmp.nodeIdx: dpmp.b[v]['value'] = dpmp.new_b[v]['value'].copy() dpmp.b[v]['L'] = dpmp.new_b[v]['L'].copy() dpmp.b[v]['x'] = dpmp.new_b[v]['x'].copy() update_all_messages.update(dpmp) logB = set_me_as_solution.fun(dpmp, 0) if dpmp.verbose > 1: print 'negative energy ' + str(logB) return logB