lfd.tpsopt package

Class inheritance diagram

Inheritance diagram of lfd.tpsopt.batchtps, lfd.tpsopt.clouds, lfd.tpsopt.culinalg_exts, lfd.tpsopt.precompute, lfd.tpsopt.registration, lfd.tpsopt.settings, lfd.tpsopt.tps, lfd.tpsopt.transformations

Submodules

lfd.tpsopt.batchtps module

class lfd.tpsopt.batchtps.GPUContext(bend_coefs=None)[source]

Bases: object

Class to contain GPU arrays

add_cld(name, proj_mats, offset_mats, cloud_xyz, kernel, scale_params, update_ptrs=False)[source]

adds a new cloud to our context for batch processing

bending_cost(b=0.001)[source]
bidir_tps_cost(other, bend_coef=1, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, T=0.005, norm_iters=10, sigma=1, return_components=False)[source]
check_cld(cloud_xyz)[source]
get_sol_params(cld)[source]
get_target_points(other, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, T=0.005, norm_iters=10)[source]

computes the target points for self and other using the current warped points for both

gram_mat_cost(sigma)[source]
mapping_cost(other, bend_coef=0.001, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, T=0.005, norm_iters=10)[source]

computes the error in the current mapping assumes that the target points have already been filled

read_h5(fname)[source]
reset_tps_params()[source]

sets the tps params to be identity

reset_warp_err()[source]
set_tps_params(vals)[source]
setup_tgt_ctx(cloud_xyz)[source]

returns a GPUContext where all the clouds are cloud_xyz and matched in length with this contex

assumes cloud_xyz is already downsampled and scaled

test_bending_cost(other, bend_coef=0.001, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, T=0.005, norm_iters=10)[source]
test_get_targ(other, T=0.005, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, norm_iters=10)[source]
test_init_corr(other, T=0.005, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01)[source]
test_mapping_cost(other, bend_coef=0.001, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, T=0.005, norm_iters=10)[source]
test_norm_corr(other, T=0.005, outlierprior=0.1, outlierfrac=0.01, outliercutoff=0.01, norm_iters=10)[source]
transform_points()[source]

computes the warp of self.pts under the current tps params

unit_test(other)[source]
update_ptrs()[source]
update_transform(b)[source]

computes the TPS associated with the current target pts

w_nd_ptrs = None

TPS PARAM FORMAT [ np.zeros(DATA_DIM) ] [ trans_d ] [1 x d] [ np.eye(DATA_DIM) ] = [ lin_dd ] = [d x d] [np.zeros((np.zeros, DATA_DIM))] [ w_nd ] [n x d]

class lfd.tpsopt.batchtps.Globals[source]
i = 9
streams = [<Mock name='mock()' id='140724508621392'>, <Mock name='mock()' id='140724508621648'>, <Mock name='mock()' id='140724507127440'>, <Mock name='mock()' id='140724508621776'>, <Mock name='mock()' id='140724508621904'>, <Mock name='mock()' id='140724508622032'>, <Mock name='mock()' id='140724508622160'>, <Mock name='mock()' id='140724508622288'>, <Mock name='mock()' id='140724508622416'>, <Mock name='mock()' id='140724508622544'>]
sync = False
class lfd.tpsopt.batchtps.SrcContext(bend_coefs=None)[source]

Bases: lfd.tpsopt.batchtps.GPUContext

specialized class to handle source clouds includes support for warped trajectories as well

add_cld(name, proj_mats, offset_mats, cloud_xyz, kernel, scale_params, r_traj, r_traj_K, l_traj, l_traj_K, update_ptrs=False)[source]

does the normal add, but also adds the trajectories

get_unscaled_trajs(other)[source]

transforms the trajectories with the current tps params unscales them assuming that other is the target assumes other is a TgtContext

read_h5(fname)[source]
test_get_unscaled_trajs(other)[source]
test_traj_cost(other)[source]
test_transform_trajs()[source]
traj_cost(tgt_seg, other)[source]
transform_trajs()[source]

computes the warp of l_traj and r_traj under current tps params

unit_test(other)[source]
update_ptrs()[source]
class lfd.tpsopt.batchtps.TgtContext(src_ctx)[source]

Bases: lfd.tpsopt.batchtps.GPUContext

specialized class to handle the case where we are mapping to a single target cloud –> only allocate GPU Memory once

add_cld(name, proj_mats, offset_mats, cloud_xyz, kernel, update_ptrs=False)[source]
set_cld(cld)[source]

sets the cloud for this appropriately won’t allocate any new memory

update_ptrs()[source]
lfd.tpsopt.batchtps.batch_tps_rpm_bij(src_ctx, tgt_ctx, T_init=0.1, T_final=0.005, outlierfrac=0.01, outlierprior=0.1, outliercutoff=0.01, em_iter=1, component_cost=False)[source]

computes tps rpm for the clouds in src and tgt in batch TODO: Fill out comment cleanly

lfd.tpsopt.batchtps.check_transform_pts(ctx, i=0)[source]
lfd.tpsopt.batchtps.check_update(ctx, b)[source]
lfd.tpsopt.batchtps.get_stream(i)[source]
lfd.tpsopt.batchtps.gpu_pad(x, shape, dtype=<Mock id='140724508621008'>)[source]
lfd.tpsopt.batchtps.parse_arguments()[source]
lfd.tpsopt.batchtps.sync(override=False)[source]
lfd.tpsopt.batchtps.test_batch_tps_rpm_bij(src_ctx, tgt_ctx, T_init=0.1, T_final=0.005, outlierfrac=0.01, outlierprior=0.1, outliercutoff=0.5, em_iter=1, test_ind=0)[source]

lfd.tpsopt.clouds module

lfd.tpsopt.clouds.XYZ_to_xy(X, Y, Z, f=535.0)[source]
lfd.tpsopt.clouds.depth_to_xyz(depth, f=535.0)[source]
lfd.tpsopt.clouds.downsample(xyz, v)[source]
lfd.tpsopt.clouds.xyZ_to_XY(x, y, Z, f=535.0)[source]

lfd.tpsopt.culinalg_exts module

lfd.tpsopt.culinalg_exts.batch_sum(a_arr_gpu, a_ptr_gpu)[source]

computes a sum of all of the arrays pointed to by a_arr_gpu and a_ptr_gpu

lfd.tpsopt.culinalg_exts.dot_batch(a_arr_gpu, b_arr_gpu, a_ptr_gpu, b_ptr_gpu, transa='N', transb='N')[source]
lfd.tpsopt.culinalg_exts.dot_batch_nocheck(a_arr_gpu, b_arr_gpu, c_arr_gpu, a_ptr_gpu, b_ptr_gpu, c_ptr_gpu, transa='N', transb='N', a=1, b=1, handle=None)[source]

Implementation of batched dot products using cuda.

a_arr_gpu : list of pycuda.gpuarray.GPUArray
Input array.
b_arr_gpu : list of pycuda.gpuarray.GPUArray
Input array.
c_arr_gpu : list of pycuda.gpuarray.GPUArray
Input/Output array.
a_ptr_gpu : pycuda.gpuarray.GPUArray
Array of pointers to arrays in a_arr_gpu
b_ptr_gpu : pycuda.gpuarray.GPUArray
Array of pointers to arrays in b_arr_gpu
c_ptr_gpu : pycuda.gpuarray.GPUArray
Array of pointers to arrays in c_arr_gpu
transa : char
If ‘T’, compute the product of the transpose of a_arr_gpu[i]. If ‘C’, compute the product of the Hermitian of a_arr_gpu[i].
transb : char
If ‘T’, compute the product of the transpose of b_arr_gpu[i]. If ‘C’, compute the product of the Hermitian of b_arr_gpu[i].
handle : int
CUBLAS context. If no context is specified, the default handle from scikits.cuda.misc._global_cublas_handle is used.

None. Output is stored into the arrays in c_arr_gpu

The input matrices must all contain elements of the same data type. All matrics in a list must have same size.

>>> import pycuda.driver
>>> import pycuda.autoinit
>>> from pycuda import gpuarray
>>> import numpy as np
>>> from scikits.cuda import linalg
>>> linalg.init()
>>> a_arr = [np.asarray(np.random.rand(4, 2), np.float32) for i in range(10)]
>>> b_arr = [np.asarray(np.random.rand(2, 2), np.float32) for i in range(10)]
>>> c_arr = [np.asarray(np.random.rand(4, 2), np.float32) for i in range(10)]
>>> a_arr_gpu = [gpuarray.to_gpu(a_gpu) for a_gpu in a_arr]
>>> b_arr_gpu = [gpuarray.to_gpu(b_gpu) for b_gpu in b_arr]
>>> c_arr_gpu = [gpuarray.to_gpu(c_gpu) for c_gpu in c_arr]
>>> a_ptr_gpu = gpuarray.to_gpu(np.asarray([int(a_gpu.gpudata) for a_gpu in a_arr_gpu]))
>>> b_ptr_gpu = gpuarray.to_gpu(np.asarray([int(b_gpu.gpudata) for b_gpu in b_arr_gpu]))
>>> c_ptr_gpu = gpuarray.to_gpu(np.asarray([int(c_gpu.gpudata) for c_gpu in c_arr_gpu]))
>>> linalg.dot_batch_nocheck(a_arr_gpu, b_arr_gpu, c_arr_gpu, a_ptr_gpu, b_ptr_gpu, c_ptr_gpu)
>>> for i in range(10):
...   print np.allclose(np.dot(a_arr[i], b_arr[i]) + c_arr[i], c_arr_gpu[i].get())
...
True
True
True
True
True
True
True
True
True
True
>>>
lfd.tpsopt.culinalg_exts.geam(a_gpu, b_gpu, c_gpu, transa='N', transb='N', alpha=1, beta=1, handle=None)[source]
lfd.tpsopt.culinalg_exts.gemm(a_gpu, b_gpu, c_gpu, transa='N', transb='N', alpha=1, beta=0, handle=None)[source]
lfd.tpsopt.culinalg_exts.get_gpu_ptrs(arr, (m, n)=(0, 0))[source]
lfd.tpsopt.culinalg_exts.m_dot_batch(*args)[source]

lfd.tpsopt.precompute module

lfd.tpsopt.precompute.batch_get_sol_params(x_nd, K_nn, bend_coefs, rot_coef)[source]
lfd.tpsopt.precompute.downsample_cloud(cloud_xyz)[source]
lfd.tpsopt.precompute.get_exact_solver(x_na, K_nn, bend_coefs, rot_coef)[source]

precomputes several of the matrix products needed to fit a TPS w/o the approximations for the batch computation

a TPS is fit by solving the system N’(Q’WQ +O_b)N z = -N’(Q’W’y - N’R) x = Nz

This function returns a tuple N, QN, N’O_bN, N’R where N’O_bN is a dict mapping the desired bending coefs to the appropriate product

lfd.tpsopt.precompute.get_opening_closing_inds(finger_traj)[source]
lfd.tpsopt.precompute.get_sol_params(x_na, K_nn, bend_coef, rot_coef)[source]

precomputes the linear operators to solve this linear system. only dependence on data is through the specified targets

all thats needed is to compute the righthand side and do a forward solve

lfd.tpsopt.precompute.gripper_joint2gripper_l_finger_joint_values(gripper_joint_vals)[source]

Only the %s_gripper_l_finger_joint%lr can be controlled (this is the joint returned by robot.GetManipulator({“l”:”leftarm”, “r”:”rightarm”}[lr]).GetGripperIndices()) The rest of the gripper joints (like %s_gripper_joint%lr) are mimiced and cannot be controlled directly

lfd.tpsopt.precompute.main()[source]
lfd.tpsopt.precompute.parse_arguments()[source]
lfd.tpsopt.precompute.test_batch_get_sol_params(f, bend_coefs, rot_coef, atol=1e-07, index=0)[source]

lfd.tpsopt.registration module

Register point clouds to each other

arrays are named like name_abc abc are subscripts and indicate the what that tensor index refers to

index name conventions:
m: test point index n: training point index a: input coordinate g: output coordinate d: gripper coordinate
lfd.tpsopt.registration.balance_matrix(prob_nm, max_iter, p, outlierfrac, r_N=None)[source]
lfd.tpsopt.registration.loglinspace(a, b, n)[source]

n numbers between a to b (inclusive) with constant ratio between consecutive numbers

lfd.tpsopt.registration.registration_cost(xyz0, xyz1, f_p_mats=None, f_o_mats=None, b_p_mats=None, b_o_mats=None)[source]
lfd.tpsopt.registration.tps_rpm_bij(x_nd, y_md, fsolve, gsolve, n_iter=20, reg_init=0.1, reg_final=0.001, rad_init=0.1, rad_final=0.005, rot_reg=0.001, outlierprior=0.1, outlierfrac=0.2, vis_cost_xy=None, return_corr=False, check_solver=False)[source]

tps-rpm algorithm mostly as described by chui and rangaran reg_init/reg_final: regularization on curvature rad_init/rad_final: radius for correspondence calculation (meters) plotting: 0 means don’t plot. integer n means plot every n iterations

lfd.tpsopt.registration.unit_boxify(x_na)[source]
lfd.tpsopt.registration.unscale_tps(f, src_params, targ_params)[source]

Only works in 3d!!

lfd.tpsopt.settings module

lfd.tpsopt.tps module

Functions for fitting and applying thin plate spline transformations

lfd.tpsopt.tps.nan2zero(x)[source]
lfd.tpsopt.tps.solve_eqp1(H, f, A)[source]

solve equality-constrained qp min tr(x’Hx) + sum(f’x) s.t. Ax = 0

lfd.tpsopt.tps.tps_apply_kernel(distmat, dim)[source]
if d=2:
k(r) = 4 * r^2 log(r)
d=3:
k(r) = -r

import numpy as np, scipy.spatial.distance as ssd x = np.random.rand(100,2) d=ssd.squareform(ssd.pdist(x)) print np.clip(np.linalg.eigvalsh( 4 * d**2 * log(d+1e-9) ),0,inf).mean() print np.clip(np.linalg.eigvalsh(-d),0,inf).mean()

Note the actual coefficients (from http://www.geometrictools.com/Documentation/ThinPlateSplines.pdf) d=2: 1/(8*sqrt(pi)) = 0.070523697943469535 d=3: gamma(-.5)/(16*pi**1.5) = -0.039284682964880184

lfd.tpsopt.tps.tps_cost(lin_ag, trans_g, w_ng, x_na, y_ng, bend_coef, K_nn=None, return_tuple=False, wt_n=None)[source]

XXX doesn’t include rotation cost

lfd.tpsopt.tps.tps_cost_regrot(lin_ag, trans_g, w_ng, x_na, y_ng, bend_coef, rfunc, K_nn=None, wt_n=None)[source]

(Y-KA-XB-1C)’ W (Y-KA-XB-1C) + tr(A’KA) + r(B) subject to A’(X 1) = 0

lfd.tpsopt.tps.tps_eval(x_ma, lin_ag, trans_g, w_ng, x_na)[source]
lfd.tpsopt.tps.tps_fit(x_na, y_ng, bend_coef, rot_coef, wt_n=None, K_nn=None)[source]
lfd.tpsopt.tps.tps_fit2(x_na, y_ng, bend_coef, rot_coef, wt_n=None)[source]
lfd.tpsopt.tps.tps_fit3(x_na, y_ng, bend_coef, rot_coef, wt_n)[source]
lfd.tpsopt.tps.tps_fit_fixedrot(x_na, y_ng, bend_coef, lin_ag, K_nn=None, wt_n=None)[source]

minimize (Y-KA-XB-1C)’W(Y-KA-XB-1C) + tr(A’KA) + r(B)

lfd.tpsopt.tps.tps_fit_regrot(x_na, y_ng, bend_coef, rfunc, wt_n=None, max_iter=1, inner_max_iter=100, rgrad=None, l_init=None)[source]

minimize (Y-KA-XB-1C)’ W (Y-KA-XB-1C) + tr(A’KA) + r(B) subject to A’(X 1) = 0

lfd.tpsopt.tps.tps_grad(x_ma, lin_ag, _trans_g, w_ng, x_na)[source]
lfd.tpsopt.tps.tps_kernel_matrix(x_na)[source]
lfd.tpsopt.tps.tps_kernel_matrix2(x_na, y_ma)[source]
lfd.tpsopt.tps.tps_nr_cost_eval(lin_ag, trans_g, w_ng, x_na, y_ng, xnr_ma, bend_coef, nr_coef, K_nn=None, return_tuple=False)[source]
lfd.tpsopt.tps.tps_nr_cost_eval_general(lin_ag, trans_g, w_eg, x_ea, y_ng, nr_ma, bend_coef, nr_coef, K_ee=None, return_tuple=True)[source]
lfd.tpsopt.tps.tps_nr_err(x_ma, lin_ag, trans_g, w_ng, x_na)[source]

green’s strain

lfd.tpsopt.tps.tps_nr_fit(x_na, y_ng, bend_coef, nr_ma, nr_coef, method='newton')[source]
lfd.tpsopt.tps.tps_nr_fit_enhanced(x_na, y_ng, bend_coef, nr_ma, nr_coef)[source]
lfd.tpsopt.tps.tps_nr_grad(x_ma, lin_ag, _trans_g, w_ng, x_na, return_tuple=False)[source]

gradient of green’s strain

lfd.tpsopt.transformations module

class lfd.tpsopt.transformations.Affine(lin_ag, trans_g)[source]

Bases: lfd.tpsopt.transformations.Transformation

compute_jacobian(x_ma)[source]
transform_points(x_ma)[source]
class lfd.tpsopt.transformations.Composition(fs)[source]

Bases: lfd.tpsopt.transformations.Transformation

compute_jacobian(x_ma)[source]
transform_points(x_ma)[source]
class lfd.tpsopt.transformations.EmptySolver(max_N, bend_coefs)[source]

Bases: object

pre-allocates the GPU space needed to get a new solver efficiently computes solution params and returns a TPSSolver

get_solver(x_na, K_nn, bend_coefs, rot_coef)[source]
class lfd.tpsopt.transformations.NoGPUEmptySolver(max_N, bend_coefs)[source]

Bases: object

computes solution params and returns a NoGPUTPSSolver

get_solver(x_na, K_nn, bend_coefs, rot_coef)[source]
class lfd.tpsopt.transformations.NoGPUTPSSolver(bend_coefs, N, QN, NON, NR, x_nd, K_nn, rot_coef)[source]

Bases: object

class to fit a thin plate spline to data using precomputed matrix products

static get_solvers(h5file)[source]
solve(wt_n, y_nd, bend_coef, rot_coef, f_res)[source]
class lfd.tpsopt.transformations.TPSSolver(bend_coefs, N, QN, NON, NR, x_nd, K_nn, rot_coef, QN_gpu=None, WQN_gpu=None, NON_gpu=None, NHN_gpu=None)[source]

Bases: object

class to fit a thin plate spline to data using precomputed matrix products

static get_solvers(h5file)[source]
initialize_solver(b, wt_n)[source]
solve(wt_n, y_nd, bend_coef, rot_coef, f_res)[source]
class lfd.tpsopt.transformations.ThinPlateSpline(d=3)[source]

Bases: lfd.tpsopt.transformations.Transformation

members:
x_na: centers of basis functions w_ng: lin_ag: transpose of linear part, so you take x_na.dot(lin_ag) trans_g: translation part
compute_jacobian(x_ma)[source]
transform_points(x_ma)[source]
class lfd.tpsopt.transformations.Transformation[source]

Bases: object

Object oriented interface for transformations R^d -> R^d

compute_jacobian(x_ma)[source]
compute_numerical_jacobian(x_d, epsilon=0.0001)[source]

numerical jacobian

transform_bases(x_ma, rot_mad, orthogonalize=True, orth_method='cross')[source]

orthogonalize: none, svd, qr

transform_hmats(hmat_mAD)[source]

Transform (D+1) x (D+1) homogenius matrices

transform_points(x_ma)[source]
lfd.tpsopt.transformations.fit_ThinPlateSpline(x_na, y_ng, bend_coef=0.1, rot_coef=1e-05, wt_n=None)[source]

x_na: source cloud y_nd: target cloud smoothing: penalize non-affine part angular_spring: penalize rotation wt_n: weight the points

lfd.tpsopt.transformations.set_ThinPlateSpline(f, x_na, theta)[source]