def test_neighborhood(): # pt_num=np.random.randint(20000,50000) pt_num=10240 pts=np.random.uniform(0,1,[pt_num,3]) pts=np.asarray(pts,dtype=np.float32) idxs=np.random.choice(pt_num,100,False) idxs=np.asarray(idxs,np.int32) for i in xrange(10000): nidxs=libPointUtil.findNeighborRadiusGPU(pts,idxs,0.1,30)
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 test_concat_non_center(): import libPointUtil pts=np.random.uniform(-1.0,1.0,[20,3]) pts=np.asarray(pts,dtype=np.float32) nidxs=libPointUtil.findNeighborRadiusGPU(pts,1.0) nidxs_lens=np.asarray([len(idxs) for idxs in nidxs],dtype=np.int32) nidxs_bgs=compute_nidxs_bgs(nidxs_lens) nidxs=np.concatenate(nidxs,axis=0) nidxs=np.asarray(nidxs,dtype=np.int32) pts=np.asarray(pts,np.float64) ofeats=np_concat_non_center_forward(pts,nidxs,nidxs_lens,nidxs_bgs) dofeats=np.random.uniform(-1,1,ofeats.shape) difeats=np_concat_non_center_backward(dofeats,nidxs,nidxs_lens,nidxs_bgs) f=lambda ifeats: np_concat_non_center_forward(ifeats,nidxs,nidxs_lens,nidxs_bgs) difeats_num=eval_numerical_gradient_array(f,pts,dofeats) print pts print ofeats print 'diff {} '.format(np.max(difeats-difeats_num))
def tesst_concat_non_center_scatter(): pts = np.random.uniform(-1.0, 1.0, [20, 3]) pts = np.asarray(pts, dtype=np.float32) nidxs = libPointUtil.findNeighborRadiusGPU(pts, 1.0) nidxs_lens = np.asarray([len(idxs) for idxs in nidxs], dtype=np.int32) nidxs_bgs = compute_nidxs_bgs(nidxs_lens) nidxs = np.concatenate(nidxs, axis=0) nidxs = np.asarray(nidxs, dtype=np.int32) pts = np.asarray(pts, np.float64) ofeats = np_concat_non_center_forward(pts, nidxs, nidxs_lens, nidxs_bgs) dofeats = np.random.uniform(-1, 1, ofeats.shape) difeats = np_concat_non_center_backward(dofeats, nidxs, nidxs_lens, nidxs_bgs) ifeats = np.ascontiguousarray(pts, np.float32) dofeats = np.ascontiguousarray(dofeats, np.float32) ofeats_tf, difeats_tf = eval_concat_non_center_forward( ifeats, nidxs, nidxs_lens, nidxs_bgs, dofeats) print 'forward diff {}'.format(np.max(ofeats_tf - ofeats)) print 'backward diff {}'.format(np.max(np.abs(difeats_tf - difeats))) print np.mean(difeats_tf)
def test_covars(): pts, _ = read_room_h5('data/S3DIS/room/16_Area_1_office_15.h5') ds_idxs, _ = libPointUtil.gridDownsampleGPU(pts, 0.05, True) pts = pts[ds_idxs, :] spts = np.ascontiguousarray(pts[:, :3]) nidxs = libPointUtil.findNeighborRadiusGPU(spts, 0.2) nidxs_lens = np.asarray([len(idxs) for idxs in nidxs], dtype=np.int32) nidxs_bgs = compute_nidxs_bgs(nidxs_lens) nidxs = np.concatenate(nidxs, axis=0) begin = time.time() for i in xrange(10000): covars = libPointUtil.computeCovarsGPU(spts, nidxs, nidxs_lens, nidxs_bgs) print 'cost {} s'.format(time.time() - begin) from sklearn.cluster import KMeans kmeans = KMeans(5) preds = kmeans.fit_predict(covars) colors = np.random.randint(0, 255, [5, 3]) output_points('test_result/cluster.txt', pts, colors[preds, :])
def sample_block_scannet(points, labels, ds_stride, block_size, block_stride, min_pn, use_rescale=False, use_flip=False, use_rotate=False, covar_nn_size=0.1): xyzs = np.ascontiguousarray(points[:, :3]) min_xyzs = np.min(xyzs, axis=0, keepdims=True) max_xyzs = np.max(xyzs, axis=0, keepdims=True) # flip if use_flip: if random.random() < 0.5: xyzs = swap_xy(xyzs) min_xyzs = swap_xy(min_xyzs) max_xyzs = swap_xy(max_xyzs) if random.random() < 0.5: xyzs = flip(xyzs, axis=0) min_xyzs[:, 0], max_xyzs[:, 0] = -max_xyzs[:, 0], -min_xyzs[:, 0] if random.random() < 0.5: xyzs = flip(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]) 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 xyzs = rotate(xyzs, angle) min_xyzs = np.min(xyzs, axis=0, keepdims=True) max_xyzs = np.max(xyzs, axis=0, keepdims=True) ds_idxs = libPointUtil.gridDownsampleGPU(xyzs, ds_stride, False) covar_nidxs = libPointUtil.findNeighborRadiusGPU(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(xyzs, covar_nidxs, covar_nidxs_lens, covar_nidxs_bgs) xyzs = xyzs[ds_idxs, :] lbls = labels[ds_idxs] xyzs -= min_xyzs idxs = uniform_sample_block(xyzs, block_size, block_stride, normalized=True, min_pn=min_pn) xyzs += min_xyzs xyzs, covars, lbls = fetch_subset([xyzs, covars, lbls], idxs) return xyzs, covars, lbls
return vals if __name__ == "__main__": pts,_=read_room_h5('data/S3DIS/room/16_Area_1_office_15.h5') ds_idxs,_=libPointUtil.gridDownsampleGPU(pts,0.05,True) pts=pts[ds_idxs,:] block_idxs=libPointUtil.sampleRotatedBlockGPU(pts,1.5,3.0,0.0) block_idxs=[idxs for idxs in block_idxs if len(idxs)>2048] print 'mean block pt_num: {}'.format(np.mean(np.asarray([len(idxs) for idxs in block_idxs]))) bid=np.random.randint(0,len(block_idxs)) pts=pts[block_idxs[bid],:] spts=np.ascontiguousarray(pts[:,:3]) nidxs=libPointUtil.findNeighborRadiusGPU(spts,0.08) nidxs_lens=np.asarray([len(idxs) for idxs in nidxs],dtype=np.int32) nidxs_bgs=compute_nidxs_bgs(nidxs_lens) cidxs=compute_cidxs(nidxs_lens) nidxs=np.concatenate(nidxs,axis=0) print 'pn*n: {}'.format(nidxs.shape) print 'pn: {}'.format(nidxs_bgs.shape) print 'avg n: {}'.format(float(nidxs.shape[0])/nidxs_bgs.shape[0]) pmiu=neighbor_anchors() pmiu=pmiu.transpose() print 'pmiu {}'.format(pmiu.shape) dpfeats=np.random.uniform(-1.0,1.0,[pts.shape[0],final_dims]) dpfeats=np.asarray(dpfeats,dtype=np.float32) vals=eval_conv_xyz_feats(pts[:,:3],pts[:,3:],nidxs,nidxs_lens,nidxs_bgs,pmiu,dpfeats,cidxs)