def semantic3d_sample_block(beg, bi, ri, rm, fs, fn, min_p, bsize, ds_stride): # from io_util import get_semantic3d_class_colors # colors=get_semantic3d_class_colors() lbls, pts = [], [] pn=0 for i in xrange(fn): points, labels = read_room_pkl('data/Semantic3D.Net/pkl/train/' + fs + '_{}.pkl'.format(i)) idxs = libPointUtil.gridDownsampleGPU(points, ds_stride, False) points = points[idxs] labels = labels[idxs] points[:, :3] = np.dot(points[:, :3], rm) points[:, :3] -= np.expand_dims(min_p, axis=0) x_cond = (points[:, 0] >= beg[0]) & (points[:, 0] < beg[0] + bsize) y_cond = (points[:, 1] >= beg[1]) & (points[:, 1] < beg[1] + bsize) cond = x_cond & y_cond pn+=np.sum(cond) pts.append(points[cond]) lbls.append(labels[cond]) if pn>1024: pts = np.concatenate(pts, axis=0) lbls = np.concatenate(lbls, axis=0) print 'block {} pn {}'.format(bi,pn) save_room_pkl('data/Semantic3D.Net/block/train/' + fs + '_{}_{}.pkl'.format(bi,ri), pts, lbls)
def semantic3d_test_to_block(): with open('cached/semantic3d_test_stems.txt','r') as f: lines=f.readlines() fns=[fn.strip('\n').split(' ')[0] for fn in lines] pns=[int(fn.strip('\n').split(' ')[1]) for fn in lines] for fn,pn in zip(fns,pns): all_data=[[] for _ in xrange(13)] for t in xrange(pn): fs=('data/Semantic3D.Net/pkl/test_presample/' + fn + '_{}.pkl'.format(t)) points,labels=read_room_pkl(fs) xyzs, rgbs, covars, lbls = sample_block(points,labels,ds_stride=sstride,block_size=bsize, block_stride=bstride,min_pn=min_pn,use_rescale=False, use_flip=False,use_rotate=False,covar_ds_stride=covar_ds_stride, covar_nn_size=covar_nn_size,gpu_gather=True) print 'block num {}'.format(len(xyzs)) data = normalize_block_hierarchy(xyzs, rgbs, covars, lbls, bsize=bsize, nr1=nr1, nr2=nr2, nr3=nr3, vc1=vc1, vc2=vc2,resample=True, jitter_color=True, resample_low=resample_ratio_low, resample_high=resample_ratio_high, max_pt_num=max_pt_num) for i in xrange(13): all_data[i]+=data[i] save_pkl('data/Semantic3D.Net/block/test/'+fn+'.pkl',all_data) print '{} done'.format(fn)
def semantic3d_process_block(filename, use_rescale=False, use_rotate=False, use_flip=False, use_resample=False, jitter_color=False, nr1=0.1,nr2=0.4,nr3=1.0, vc1=0.2,vc2=0.5, sstride=0.075, bsize=3.0, bstride=1.5, min_pn=1024, resample_ratio_low=0.8, resample_ratio_high=1.0, covar_ds_stride=0.03, covar_nn_size=0.1, max_pt_num=10240): points,labels=read_room_pkl(filename) # [n,6],[n,1] xyzs, rgbs, covars, lbls=sample_block(points,labels,sstride,bsize,bstride,min_pn=min_pn, use_rescale=use_rescale,use_flip=use_flip,use_rotate=use_rotate, covar_ds_stride=covar_ds_stride,covar_nn_size=covar_nn_size,gpu_gather=True) cxyzs, dxyzs, rgbs, covars, lbls, vlens, vlens_bgs, vcidxs, cidxs, nidxs, nidxs_bgs, nidxs_lens, block_mins = \ normalize_block_hierarchy(xyzs,rgbs,covars,lbls,bsize=bsize,nr1=nr1,nr2=nr2,nr3=nr3,vc1=vc1,vc2=vc2, resample=use_resample,jitter_color=jitter_color, resample_low=resample_ratio_low,resample_high=resample_ratio_high, max_pt_num=max_pt_num) return cxyzs,dxyzs,rgbs,covars,lbls,vlens,vlens_bgs,vcidxs,cidxs,nidxs,nidxs_bgs,nidxs_lens,block_mins
def test_labels(): import os fss,fns=read_semantic3d_pkl_stems() from io_util import get_semantic3d_class_colors colors=get_semantic3d_class_colors() for fn in os.listdir('data/Semantic3D.Net/block/train'): if fn.startswith(fss[6]) and fn.endswith('_0.pkl'): # or fn.endswith('_3.pkl')): points,labels=read_room_pkl('data/Semantic3D.Net/block/train/'+fn) idxs = libPointUtil.gridDownsampleGPU(points, 0.1, False) output_points('test_result/'+fn[:-4]+'.txt',points[idxs],colors[labels[idxs],:])
def semantic3d_process_block_v2(filename): points,labels=read_room_pkl(filename) # [n,6],[n,1] xyzs, rgbs, covars, lbls=sample_block(points,labels,sample_stride,block_size,block_stride,min_pn=min_point_num, use_rescale=True,use_flip=True,use_rotate=False, covar_ds_stride=covar_sample_stride,covar_nn_size=covar_neighbor_radius, gpu_gather=True) # normalize rgbs xyzs, rgbs, covars, lbls=normalize_semantic3d_block(xyzs,rgbs,covars,lbls,block_size, resample=True,resample_low=0.8,resample_high=1.0, jitter_color=True,jitter_val=2.5,max_pt_num=max_pt_num) return xyzs, rgbs, covars, lbls
def get_class_num(): train_list,test_list=get_block_train_test_split_ds() counts=np.zeros(13) for fs in test_list: points, labels = read_room_pkl('data/S3DIS/room_block_10_10_ds0.03/'+fs) for i in xrange(13): counts[i]+=np.sum(labels==i) for i in xrange(13): print counts[i]/counts[0]
def semantic3d_presample_block(): with open('cached/semantic3d_test_stems.txt','r') as f: fns=f.readlines() fns=[fn.strip('\n').split(' ')[0] for fn in fns] for fn in fns: fs=('data/Semantic3D.Net/pkl/test/' + fn + '.pkl') points,labels=read_room_pkl(fs) xyzs, rgbs, covars, lbls = sample_block(points,labels,ds_stride=0.03,block_size=50.0, block_stride=45.0,min_pn=128,use_rescale=False, use_flip=False,use_rotate=False,covar_ds_stride=0.01, covar_nn_size=0.1,gpu_gather=True) for t in xrange(len(xyzs)): points=np.concatenate([xyzs[t],rgbs[t]],axis=1) save_pkl('data/Semantic3D.Net/pkl/test_presample/' + fn + '_{}.pkl'.format(t),[points,lbls[t]])
def downsample_and_save(): train_list,test_list=get_block_train_test_split() idx=0 train_list+=test_list with open('cached/room_block_ds0.03_stems.txt','w') as f: for _ in xrange(5): for fs in train_list: points,labels=read_room_pkl('data/S3DIS/room_block_10_10/'+fs) points, labels=downsample(points,labels,0.03) names=fs.split('_') names[0]=str(idx) nfs='_'.join(names) f.write(nfs+'\n') ofs='data/S3DIS/room_block_10_10_ds0.03/'+nfs save_room_pkl(ofs,points,labels) idx+=1
def test_block(): import os fss,fns=read_semantic3d_pkl_stems() from draw_util import get_semantic3d_class_colors colors=get_semantic3d_class_colors() for fs in fss: all_points,all_labels=[],[] for fn in os.listdir('data/Semantic3D.Net/block/train'): if fn.startswith(fs) and fn.endswith('_0.pkl'): # or fn.endswith('_3.pkl')): points,labels=read_room_pkl('data/Semantic3D.Net/block/train/'+fn) idxs = libPointUtil.gridDownsampleGPU(points, 0.1, False) all_points.append(points[idxs]) all_labels.append(labels[idxs]) all_points = np.concatenate(all_points, axis=0) all_labels = np.concatenate(all_labels, axis=0) output_points('test_result/'+fs+'_labels.txt',all_points,colors[all_labels,:]) output_points('test_result/'+fs+'_colors.txt',all_points)
def get_intensity_distribution(): fss,fns=read_semantic3d_pkl_stems() intensities=[] for fs,fn in zip(fss,fns): for i in xrange(fn): points,labels=read_room_pkl('data/Semantic3D.Net/pkl/train/'+fs+'_{}.pkl'.format(i)) idxs=libPointUtil.gridDownsampleGPU(points,0.1,False) intensities.append(points[idxs,-1]) intensities=np.concatenate(intensities,axis=0) print np.min(intensities),np.max(intensities) print np.mean(intensities),np.std(intensities) import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt plt.hist(intensities,100) plt.savefig('test_result/intensities.png') plt.close()
def semantic3d_to_block(bsize=80.0,bstride=40.0,ds_stride=0.03): executor=ProcessPoolExecutor(4) fss,fns=read_semantic3d_pkl_stems() for fs,fn in zip(fss[3:],fns[3:]): for ri in xrange(6): rot_ang=np.pi/12.0*ri cosval = np.cos(rot_ang) sinval = np.sin(rot_ang) rot_m = np.array([[cosval, sinval, 0], [-sinval, cosval, 0], [0, 0, 1]], dtype=np.float32) min_p,max_p=[],[] for i in xrange(fn): points,labels=read_room_pkl('data/Semantic3D.Net/pkl/train/'+fs+'_{}.pkl'.format(i)) points[:, :3]=np.dot(points[:,:3],rot_m) min_p.append(np.min(points,axis=0)[:3]) max_p.append(np.max(points,axis=0)[:3]) min_p=np.min(np.asarray(min_p),axis=0) max_p=np.max(np.asarray(max_p),axis=0) x_list=get_list_without_back_sample(max_p[0]-min_p[0],bsize,bstride) y_list=get_list_without_back_sample(max_p[1]-min_p[1],bsize,bstride) beg_list=[] for x in x_list: for y in y_list: beg_list.append((x,y)) print 'fs {} block num {}'.format(fs,len(beg_list)) futures=[] for bi,beg in enumerate(beg_list): futures.append(executor.submit(semantic3d_sample_block,beg,bi,ri,rot_m,fs,fn,min_p,bsize,ds_stride)) # semantic3d_sample_block(beg,bi,fs,fn,min_p,bsize,ds_stride) for f in futures: f.result() print 'fs {} done'.format(fs)
qprobs = libPointUtil.interpolateProbsGPU(sxyzs, qxyzs, sprobs, nidxs, nidxs_lens, nidxs_bgs, ratio) return qprobs if __name__ == "__main__": fns, pns = get_semantic3d_testset() sess, pls, ops, feed_dict = build_session() all_preds, all_labels = [], [] for fn, pn in zip(fns, pns): begin = time.time() sxyzs, slbls, sprobs = eval_room_probs(fn, sess, pls, ops, feed_dict) points, labels = read_room_pkl('data/Semantic3D.Net/pkl/test/' + fn + '.pkl') qxyzs = np.ascontiguousarray(points[:, :3], np.float32) qn = qxyzs.shape[0] rn = 1000000 qrn = qn / rn if qn % rn != 0: qrn += 1 print 'qrn {} sxyzs num {}'.format(qrn, sxyzs.shape[0]) qprobs = [] for t in xrange(qrn): beg_idxs = t * rn end_idxs = min((t + 1) * rn, qn) qrprobs = interpolate(sxyzs, sprobs, qxyzs[beg_idxs:end_idxs]) print 'interpolate {} done'.format(t) qprobs.append(qrprobs)