def normalize(xyzs, rgbs, covars, lbls, offset_z, bsize=3.0): bn = len(xyzs) block_mins = [] block_nidxs = [] block_nlens = [] block_nbegs = [] # t=0 for bid in xrange(bn): min_xyz = np.min(xyzs[bid], axis=0, keepdims=True) min_xyz[:, 2] = offset_z min_xyz[:, :2] += bsize / 2.0 xyzs[bid] -= min_xyz block_mins.append(min_xyz) rgbs[bid][:, :3] -= 128 rgbs[bid][:, :3] /= 128 # sub mean div std if rgbs[bid].shape[1] > 3: rgbs[bid][:, 3] -= -1164.05 rgbs[bid][:, 3] /= 600.0 nidxs = libPointUtil.findNeighborRadiusCPU(xyzs[bid], radius) nlens = np.asarray([len(idxs) for idxs in nidxs], dtype=np.int32) nbegs = compute_nidxs_bgs(nlens) nidxs = np.concatenate(nidxs, axis=0) block_nidxs.append(nidxs) block_nlens.append(nlens) block_nbegs.append(nbegs) return xyzs, rgbs, covars, lbls, block_mins, block_nidxs, block_nlens, block_nbegs
def normalize_model_hierarchy(xyzs, use_rotate=False, nr1=0.05, nr2=0.1, nr3=0.3, vc1=0.1, vc2=0.3): #xyzs [b,1024,3] n = xyzs.shape[0] cxyzs,dxyzs,vlens,vlens_bgs,vcidxs,\ cidxs,nidxs,nidxs_bgs,nidxs_lens=[],[],[],[],[],[],[],[],[] covars = [] for i in xrange(n): # bg=time.time() if use_rotate: ang = np.random.uniform(0, np.pi * 2) xyzs[i] = rotate(xyzs[i], ang) nidxs1 = libPointUtil.findNeighborRadiusCPU(xyzs[i], nr1, 15) nidxs_lens1 = np.ascontiguousarray([len(idxs) for idxs in nidxs1], np.int32) nidxs_bgs1 = compute_nidxs_bgs(nidxs_lens1) nidxs1 = np.ascontiguousarray(np.concatenate(nidxs1, axis=0), dtype=np.int32) covar = libPointUtil.computeCovarsGPU(xyzs[i], nidxs1, nidxs_lens1, nidxs_bgs1) cxyz1, dxyz1, vlens1, cxyz2, dxyz2, vlens2, cxyz3, feats_list=\ build_hierarchy(xyzs[i],[covar],vc1,vc2) covar = feats_list[0] covars.append(covar) # rescale to unit cube dxyz1 /= vc1 dxyz2 /= vc2 # bg=time.time() vlens_bgs1 = compute_nidxs_bgs(vlens1) vlens_bgs2 = compute_nidxs_bgs(vlens2) vcidxs1 = compute_cidxs(vlens1) vcidxs2 = compute_cidxs(vlens2) nidxs1, nidxs_lens1, nidxs_bgs1, cidxs1 = point2ndixs(cxyz1, nr1) nidxs2, nidxs_lens2, nidxs_bgs2, cidxs2 = point2ndixs(cxyz2, nr2) nidxs3, nidxs_lens3, nidxs_bgs3, cidxs3 = point2ndixs(cxyz3, nr3) _append([ cxyzs, dxyzs, vlens, vlens_bgs, vcidxs, cidxs, nidxs, nidxs_bgs, nidxs_lens ], [[cxyz1, cxyz2, cxyz3], [dxyz1, dxyz2], [vlens1, vlens2], [vlens_bgs1, vlens_bgs2], [vcidxs1, vcidxs2], [cidxs1, cidxs2, cidxs3], [nidxs1, nidxs2, nidxs3], [nidxs_bgs1, nidxs_bgs2, nidxs_bgs3], [nidxs_lens1, nidxs_lens2, nidxs_lens3]]) # print '{} cost {} s'.format(i,time.time()-bg) return cxyzs, dxyzs, covars, vlens, vlens_bgs, vcidxs, cidxs, nidxs, nidxs_bgs, nidxs_lens
def test_nn(): xyzs = np.random.uniform(-1, 1, [1024, 3]) xyzs = np.asarray(xyzs, np.float32) covar_nidxs = libPointUtil.findNeighborRadiusCPU(xyzs, radius) for k in xrange(1024): for pi, pt in enumerate(xyzs): if np.sum((pt - xyzs[k])**2, axis=0) < radius**2: assert pi in covar_nidxs[k] for ni in covar_nidxs[k]: assert np.sum((xyzs[ni] - xyzs[k])**2, axis=0) < radius**2
def compute_covar(xyzs, radius): covar_nidxs = libPointUtil.findNeighborRadiusCPU(xyzs, radius) covar_nidxs_lens = np.ascontiguousarray( [len(idxs) for idxs in covar_nidxs], np.int32) covar_nidxs_bgs = compute_nidxs_bgs(covar_nidxs_lens) covar_nidxs = np.ascontiguousarray(np.concatenate(covar_nidxs, axis=0), dtype=np.int32) covars = libPointUtil.computeCovarsGPU(xyzs, covar_nidxs, covar_nidxs_lens, covar_nidxs_bgs) return covars
def point2ndixs(pts, radius): leafsize = 10 if pts.shape[0] > 100 else 1 if pts.shape[0] > 8000: nidxs = libPointUtil.findNeighborRadiusGPU(pts, radius, leafsize) else: nidxs = libPointUtil.findNeighborRadiusCPU(pts, radius, leafsize) nidxs_lens = [len(nidx) for nidx in nidxs] nidxs_lens = np.asarray(nidxs_lens, dtype=np.int32) nidxs = np.concatenate(nidxs, axis=0) nidxs_bgs = compute_nidxs_bgs(nidxs_lens) cindxs = compute_cidxs(nidxs_lens) return nidxs, nidxs_lens, nidxs_bgs, cindxs
def sample_block(points, labels, ds_stride, block_size, block_stride, min_pn, use_rescale=False, use_flip=False, use_rotate=False, covar_ds_stride=0.03, covar_nn_size=0.1, gpu_gather=False): xyzs = np.ascontiguousarray(points[:, :3]) rgbs = np.ascontiguousarray(points[:, 3:]) min_xyzs = np.min(xyzs, axis=0, keepdims=True) max_xyzs = np.max(xyzs, axis=0, keepdims=True) covar_ds_idxs = libPointUtil.gridDownsampleGPU(xyzs, covar_ds_stride, False) covar_ds_xyzs = np.ascontiguousarray(xyzs[covar_ds_idxs, :]) # flip if use_flip: if random.random() < 0.5: covar_ds_xyzs = swap_xy(covar_ds_xyzs) min_xyzs = swap_xy(min_xyzs) max_xyzs = swap_xy(max_xyzs) if random.random() < 0.5: covar_ds_xyzs = flip(covar_ds_xyzs, axis=0) min_xyzs[:, 0], max_xyzs[:, 0] = -max_xyzs[:, 0], -min_xyzs[:, 0] if random.random() < 0.5: covar_ds_xyzs = flip(covar_ds_xyzs, axis=1) min_xyzs[:, 1], max_xyzs[:, 1] = -max_xyzs[:, 1], -min_xyzs[:, 1] # rescale if use_rescale: rescale = np.random.uniform(0.9, 1.1, [1, 3]) covar_ds_xyzs[:, :3] *= rescale min_xyzs *= rescale max_xyzs *= rescale # rotate if use_rotate: if random.random() > 0.3: angle = random.random() * np.pi / 2.0 covar_ds_xyzs = rotate(covar_ds_xyzs, angle) ds_idxs = libPointUtil.gridDownsampleGPU(covar_ds_xyzs, ds_stride, False) covar_nidxs = libPointUtil.findNeighborRadiusCPU(covar_ds_xyzs, ds_idxs, covar_nn_size) covar_nidxs_lens = np.ascontiguousarray( [len(idxs) for idxs in covar_nidxs], np.int32) covar_nidxs_bgs = compute_nidxs_bgs(covar_nidxs_lens) covar_nidxs = np.ascontiguousarray(np.concatenate(covar_nidxs, axis=0), dtype=np.int32) covars = libPointUtil.computeCovarsGPU(covar_ds_xyzs, covar_nidxs, covar_nidxs_lens, covar_nidxs_bgs) xyzs = covar_ds_xyzs[ds_idxs, :] rgbs = rgbs[covar_ds_idxs, :][ds_idxs, :] lbls = labels[covar_ds_idxs][ds_idxs] if gpu_gather: xyzs -= min_xyzs idxs = uniform_sample_block_gpu(xyzs, block_size, block_stride, min_pn=min_pn) xyzs += min_xyzs else: xyzs -= min_xyzs idxs = uniform_sample_block(xyzs, block_size, block_stride, normalized=True, min_pn=min_pn) xyzs += min_xyzs xyzs, rgbs, covars, lbls = fetch_subset([xyzs, rgbs, covars, lbls], idxs) return xyzs, rgbs, covars, lbls
def sample_context_block(tfs, points, labels, global_points, ds_stride, block_size, block_stride, min_pn, use_rescale=False, use_flip=False, covar_ds_stride=0.03, covar_nn_size=0.1, context_len=50.0): xyzs=np.ascontiguousarray(points[:,:3]) rgbs=np.ascontiguousarray(points[:,3:]) min_xyz=np.min(xyzs,axis=0,keepdims=True) max_xyz=np.max(xyzs,axis=0,keepdims=True) covar_ds_idxs=libPointUtil.gridDownsampleGPU(xyzs, covar_ds_stride, False) covar_ds_xyzs=np.ascontiguousarray(xyzs[covar_ds_idxs,:]) # flip if use_flip: if random.random()<0.5: covar_ds_xyzs=swap_xy(covar_ds_xyzs) global_points=swap_xy(global_points) min_xyz=swap_xy(min_xyz) max_xyz=swap_xy(max_xyz) if random.random()<0.5: covar_ds_xyzs=flip(covar_ds_xyzs,axis=0) global_points=flip(global_points,axis=0) min_xyz[:,0],max_xyz[:,0]=-max_xyz[:,0],-min_xyz[:,0] if random.random()<0.5: covar_ds_xyzs=flip(covar_ds_xyzs,axis=1) global_points=flip(global_points,axis=1) min_xyz[:,1],max_xyz[:,1]=-max_xyz[:,1],-min_xyz[:,1] # rescale if use_rescale: rescale=np.random.uniform(0.9,1.1,[1,3]) covar_ds_xyzs[:,:3]*=rescale global_points[:,:3]*=rescale min_xyz*=rescale max_xyz*=rescale ds_idxs=libPointUtil.gridDownsampleGPU(covar_ds_xyzs,ds_stride,False) # compute covar covar_nidxs=libPointUtil.findNeighborRadiusCPU(covar_ds_xyzs,ds_idxs,covar_nn_size) covar_nidxs_lens=np.ascontiguousarray([len(idxs) for idxs in covar_nidxs],np.int32) covar_nidxs_bgs=compute_nidxs_bgs(covar_nidxs_lens) covar_nidxs=np.ascontiguousarray(np.concatenate(covar_nidxs,axis=0),dtype=np.int32) covars=libPointUtil.computeCovarsGPU(covar_ds_xyzs,covar_nidxs,covar_nidxs_lens,covar_nidxs_bgs) xyzs=covar_ds_xyzs[ds_idxs,:] rgbs=rgbs[covar_ds_idxs,:][ds_idxs,:] lbls=labels[covar_ds_idxs][ds_idxs] xyzs-=min_xyz idxs=uniform_sample_block(xyzs,block_size,block_stride,min_pn=min_pn) xyzs+=min_xyz xyzs, rgbs, covars, lbls=fetch_subset([xyzs,rgbs,covars,lbls],idxs) context_xyzs=compute_context_xyzs(global_points,xyzs,context_len=context_len) for ci,ctx_xyz in enumerate(context_xyzs): if ctx_xyz.shape[0]==0: print '!!!! error {}'.format(tfs) raise RuntimeError context_idxs=compute_context_idxs(context_xyzs,xyzs) return xyzs, rgbs, covars, lbls, context_xyzs, context_idxs