In [2]:
%pylab inline
pylab.rcParams['figure.figsize'] = (16,12)

import config
from cpp_wrapper.tree_based_regression import AlignmentMethod


Populating the interactive namespace from numpy and matplotlib

In [3]:
def readRegressionForestsModel(filename):
    f = open(filename, "r")
    T, N, D, L = (int(x) for x in f.readline().split())
    mean_shape = np.array([float(x) for x in f.readline().split()]).reshape(L, 2)
    
    residuals = []
    for t in range(T):
        residuals.append([])
        
        for l in range(L):
            residuals[t].append([])

            for i in range(N*(2**(D+1)-1)):
                f.readline()

            k = int(f.readline())

            for i in range(N*(2**D)):
                residuals[t][l].append(np.array([float(x) for x in f.readline().split()]).reshape(k,2))

    return mean_shape, np.array(residuals)

In [6]:
_, residuals = readRegressionForestsModel(os.path.join(config._300w_benchmark_path, "local_trees_regression_model_300w_68_landmarks.txt"))

In [7]:
for t in range(residuals.shape[0]):
    plot(range(1, residuals.shape[1]+1),np.mean(np.linalg.norm(residuals, axis=4), axis=(2,3))[t], markevery=5)
_ = xticks(range(0, residuals.shape[1]+1, 5))



In [9]:
mean_shape, residuals = readRegressionForestsModel(os.path.join(config._300w_benchmark_path, "lbf_regression_model_300w_68_landmarks.txt"))

In [10]:
for t in range(residuals.shape[0]):
    plot(range(1,residuals.shape[1]+1), np.mean(np.linalg.norm(residuals, axis=4), axis=(2,3))[t])
_ = xticks(range(0, residuals.shape[1]+1, 5))



In [13]:
def computeDensity(x, shape):
    distances = [np.linalg.norm(x - y)**2 for y in shape if (x != y).all()]
    sigma = 0.05
    return np.sum(np.exp(-0.5 * (np.asarray(distances)/sigma)**2) / (np.sqrt(2*np.pi)*sigma))
    

L = residuals.shape[1]
influence_curve = np.sum([np.sum(np.mean(np.linalg.norm(residuals, axis=4), axis=2)[:,:,[i for i in range(L) if i!=l]], axis=2)[:,l] for l in range(L)], axis=1)/(L-1)
self_influence_curve = np.sum([np.mean(np.linalg.norm(residuals, axis=4), axis=2)[:,l,l] for l in range(L)], axis=1)
#density = [1/np.linalg.norm(mean_shape-mean_shape[l]) for l in range(mean_shape.shape[0])]
density = [computeDensity(mean_shape[l], mean_shape) for l in range(mean_shape.shape[0])]

subplot(311)
plot(range(1,residuals.shape[1]+1), density)
_ = xticks(range(0, residuals.shape[1]+1, 5))
subplot(312)
plot(range(1,residuals.shape[1]+1), self_influence_curve)
_ = xticks(range(0, residuals.shape[1]+1, 5))
subplot(313)
plot(range(1,residuals.shape[1]+1), influence_curve)
_ = xticks(range(0, residuals.shape[1]+1, 5))



In [12]:
np.corrcoef(np.vstack((density, self_influence_curve, influence_curve)))


Out[12]:
array([[ 1.        , -0.80081266, -0.39545117],
       [-0.80081266,  1.        ,  0.24889274],
       [-0.39545117,  0.24889274,  1.        ]])

In [10]:
subplot(311)
plot(range(1,residuals.shape[1]+1), density)
subplot(312)
plot(range(1,residuals.shape[1]+1), influence_curve*density)
_ = xticks(range(0, residuals.shape[1]+1, 5))
subplot(313)
plot(range(1,residuals.shape[1]+1), np.divide(self_influence_curve, density))
_ = xticks(range(0, residuals.shape[1]+1, 5))



In [12]:
pylab.rcParams['figure.figsize'] = (18,150)

for i in range(0, residuals.shape[3]):
    subplot(residuals.shape[3],1,i+1)
    plot(range(1,residuals.shape[1]+1), np.mean(np.linalg.norm(residuals, axis=4), axis=(2))[0,i], label="Landmark #%d"%(i+1))
    axvline(i+1)
    legend(loc=4)
_ = xticks(range(0, residuals.shape[1]+1, 5))



In [ ]: