-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo_segmentation.py
154 lines (117 loc) · 5.06 KB
/
demo_segmentation.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
import os
import pickle as pickle
import random
import sys
sys.path.append('.')
import time
import matplotlib.pyplot as plt
import numpy as np
import scipy.io
from Inference.BookKeeper import *
from Inference.SamplerState import *
from Models.ddCRP import *
from Models.MNIWlik import *
from Util.load_data import *
from Util.package_data import *
from Util.trace_plot import *
from Util.visualize_result import *
def demo_segmentation():
# Demo for mesh segmentation
# Reads from disk (./Data) meshes preprocessed into point clouds.
# Writes to disk (./Results) the resulting segmentation.
""" SET PARAMETERS
1) number of sampler iterations. Higher the better.
Recommended setting > 150. Reasonable segmentations can often
be achieved within the first 30 iterations. While these premature samples
do not correspond to samples from the posterior distribution, they might
be sufficient to produce reasonable results. The segmentation quality
gradually increases with increasing number of iterations."""
NUMITER = 50
""" SELECT DATASET TO ANALYZE
2) We include two sets of meshes from the TOSCA dataset (Centaur and Horse).
Each dataset consists of a single synthetic mesh in several different poses."""
DATASET = 'Centaur' #could also select 'Horse'
""" SET NOISE PARAMETER
3) Noise parameter, this controls the amount of deviation from the
predicted affine transformation.
Noise --- HIGHER values will result in LARGER segments."""
NOISESCALE = 9e-04
savedir = './Results'
if(not os.path.isdir(savedir)):
os.mkdir(savedir)
datapath = './Data'
# LOAD DATA
#load Mesh face neighbors, symmetric mapping along the midsagittal plane ,
#and mesh faces as point clouds.
[A,left_tris,pcf] = load_data(datapath,DATASET)
A = np.take(A,left_tris, axis=0)
A = np.take(A, left_tris, axis=1)
# create a ddCRP object.
ddcrp = ddCRP(float(A[0,2]), A.astype('float64'))
X = []
for i in range(0,1):
# reference coords.
X = np.insert(pcf.X, 3, 1, axis=1)
Y = pcf.Y # these are the coordinates of triangles in different body poses.
# visualize the reference mesh.
plt.figure()
ax = plt.axes(projection='3d')
ax.scatter(pcf.X[:,0],pcf.X[:,1],pcf.X[:,2], 'b')
plt.title('Reference mesh visualized as a point cloud')
plt.show(block=False)
plt.pause(2)
plt.close()
# view(94,10) axis equal
# time.sleep(3.0)
# plt.close('all')
data = package_data(X,Y,1)
# load precomputed ratio of gammas to speed up likelihood computation.
rofg = scipy.io.loadmat(os.path.join(datapath,'Ratio_of_Gammas.mat'))
rofg = np.asarray(rofg.get("Ratio_of_Gammas")[0][0][0])
#############Set hyper-parameters governing the affine likelihoods
#(See http://cs.brown.edu/~sghosh/papers/GhoshSudderthLoperBlack12NIPS.pdf for details)########
# Expected variance
S0 = NOISESCALE*np.identity(3)
n0 = 5
# mean transformation
M = np.asarray([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])
#PRECISON matrix. Higher = smaller var.
K = NOISESCALE*np.asarray([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0.1]])
# Matrix Normal Inverse Wishart likelihood object
mniw = MNIWlik(NOISESCALE,S0,n0,M,K,rofg)
###########################################################################
######## Initialize and Run Sampler########################################
if(not os.path.isfile(os.path.join(savedir,'MCMC_Chain.pkl'))): #only initialize and run if cached results don't exist
print('Gibbs Sampler will be run for %d iterations.\n' % NUMITER)
# Create sampler state object
sampler_state = SamplerState(data,NUMITER)
# Create bookkeeping object
bookkeeper = BookKeeper(sampler_state,savedir)
# Run Sampler
converged_sampler_state=sampler_state.run_sampler(ddcrp,mniw,data,bookkeeper,NUMITER)
pickle.dump(converged_sampler_state, open(os.path.join(savedir,'MCMC_Chain.pkl'),"wb"))
else:
print('Loading from cache...')
with open(os.path.join(savedir,'MCMC_Chain.pkl'), 'rb') as f:
converged_sampler_state = pickle.load(f)
###########################################################################
######################## Diagnostics ######################################
# joint lik trace
ll_c = np.full(NUMITER, np.nan)
ll = np.full(NUMITER, np.nan)
prior = np.full(NUMITER, np.nan)
for i in range(0,NUMITER):
(ll_c[i], ll[i], prior[i]) = trace_plot(i,mniw,data,converged_sampler_state,ddcrp.d2_mat)
plt.figure()
plt.plot(ll_c, 'r-')
plt.title('joint log-lik')
plt.show(block=False)
plt.savefig(os.path.join(savedir,'joint-log-lik.png'))
plt.pause(2)
plt.close()
MAP_SAMPLE = np.argmax(ll_c)
# visualize result
visualize_result(2,converged_sampler_state,MAP_SAMPLE,left_tris,DATASET)
###########################################################################
if __name__=="__main__":
demo_segmentation()