Commit d3564d6d authored by liyinhao's avatar liyinhao
Browse files

changed based on third comment

parent 7a872356
import os import os
import pickle
from pathlib import Path
import mmcv
from tools.data_converter.scannet_data_utils import ScanNetData from tools.data_converter.scannet_data_utils import ScanNetData
def create_scannet_info_file(data_path, pkl_prefix='scannet', save_path=None): def create_scannet_info_file(data_path, pkl_prefix='scannet', save_path=None):
'''
Create scannet information file.
Get information of the raw data and save it to the pkl file.
Args:
data_path (str): Path of the data.
pkl_prefix (str): Prefix ofr the pkl to be saved. Default: 'scannet'. # noqa: E501
save_path (str): Path of the pkl to be saved. Default: None.
Returns:
None
'''
assert os.path.exists(data_path) assert os.path.exists(data_path)
if save_path is None: if save_path is None:
save_path = Path(data_path) save_path = data_path
else: else:
save_path = Path(save_path) save_path = save_path
assert os.path.exists(save_path) assert os.path.exists(save_path)
train_filename = save_path / f'{pkl_prefix}_infos_train.pkl' train_filename = os.path.join(save_path, f'{pkl_prefix}_infos_train.pkl')
val_filename = save_path / f'{pkl_prefix}_infos_val.pkl' val_filename = os.path.join(save_path, f'{pkl_prefix}_infos_val.pkl')
train_dataset = ScanNetData(root_path=data_path, split='train') train_dataset = ScanNetData(root_path=data_path, split='train')
val_dataset = ScanNetData(root_path=data_path, split='val') val_dataset = ScanNetData(root_path=data_path, split='val')
scannet_infos_train = train_dataset.get_scannet_infos(has_label=True) scannet_infos_train = train_dataset.get_scannet_infos(has_label=True)
with open(train_filename, 'wb') as f: with open(train_filename, 'wb') as f:
pickle.dump(scannet_infos_train, f) mmcv.dump(scannet_infos_train, f, 'pkl')
print('Scannet info train file is saved to %s' % train_filename) print(f'Scannet info train file is saved to {train_filename}')
scannet_infos_val = val_dataset.get_scannet_infos(has_label=True) scannet_infos_val = val_dataset.get_scannet_infos(has_label=True)
with open(val_filename, 'wb') as f: with open(val_filename, 'wb') as f:
pickle.dump(scannet_infos_val, f) mmcv.dump(scannet_infos_val, f, 'pkl')
print('Scannet info val file is saved to %s' % val_filename) print(f'Scannet info val file is saved to {val_filename}')
if __name__ == '__main__':
create_scannet_info_file(
data_path='./data/scannet', save_path='./data/scannet')
import concurrent.futures as futures
import os import os
import numpy as np import numpy as np
class ScanNetData(object): class ScanNetData(object):
''' Load and parse object data ''' '''
ScanNet Data
Generate scannet infos for scannet_converter
Args:
root_path (str): Root path of the raw data
split (str): Set split type of the data. Default: 'train'.
'''
def __init__(self, root_path, split='train'): def __init__(self, root_path, split='train'):
self.root_dir = root_path self.root_dir = root_path
...@@ -26,7 +35,7 @@ class ScanNetData(object): ...@@ -26,7 +35,7 @@ class ScanNetData(object):
} }
assert split in ['train', 'val', 'test'] assert split in ['train', 'val', 'test']
split_dir = os.path.join(self.root_dir, 'meta_data', split_dir = os.path.join(self.root_dir, 'meta_data',
'scannetv2_%s.txt' % split) f'scannetv2_{split}.txt')
self.sample_id_list = [x.strip() for x in open(split_dir).readlines() self.sample_id_list = [x.strip() for x in open(split_dir).readlines()
] if os.path.exists(split_dir) else None ] if os.path.exists(split_dir) else None
...@@ -35,7 +44,7 @@ class ScanNetData(object): ...@@ -35,7 +44,7 @@ class ScanNetData(object):
def get_box_label(self, idx): def get_box_label(self, idx):
box_file = os.path.join(self.root_dir, 'scannet_train_instance_data', box_file = os.path.join(self.root_dir, 'scannet_train_instance_data',
'%s_bbox.npy' % idx) f'{idx}_bbox.npy')
assert os.path.exists(box_file) assert os.path.exists(box_file)
return np.load(box_file) return np.load(box_file)
...@@ -43,10 +52,22 @@ class ScanNetData(object): ...@@ -43,10 +52,22 @@ class ScanNetData(object):
num_workers=4, num_workers=4,
has_label=True, has_label=True,
sample_id_list=None): sample_id_list=None):
import concurrent.futures as futures '''
Get scannet infos.
This method gets information from the raw data.
Args:
num_workers (int): Number of threads to be used. Default: 4.
has_label (bool): Whether the data has label. Default: True.
sample_id_list (List[int]): Index list of the sample. Default: None. # noqa: E501
Returns:
infos (List[dict]): Information of the raw data.
'''
def process_single_scene(sample_idx): def process_single_scene(sample_idx):
print('%s sample_idx: %s' % (self.split, sample_idx)) print(f'{self.split} sample_idx: {sample_idx}')
info = dict() info = dict()
pc_info = {'num_features': 6, 'lidar_idx': sample_idx} pc_info = {'num_features': 6, 'lidar_idx': sample_idx}
info['point_cloud'] = pc_info info['point_cloud'] = pc_info
......
import os import os
import pickle
from pathlib import Path
import mmcv
from tools.data_converter.sunrgbd_data_utils import SUNRGBDData from tools.data_converter.sunrgbd_data_utils import SUNRGBDData
...@@ -9,28 +8,37 @@ def create_sunrgbd_info_file(data_path, ...@@ -9,28 +8,37 @@ def create_sunrgbd_info_file(data_path,
pkl_prefix='sunrgbd', pkl_prefix='sunrgbd',
save_path=None, save_path=None,
use_v1=False): use_v1=False):
'''
Create sunrgbd information file.
Get information of the raw data and save it to the pkl file.
Args:
data_path (str): Path of the data.
pkl_prefix (str): Prefix ofr the pkl to be saved. Default: 'sunrgbd'.
save_path (str): Path of the pkl to be saved. Default: None.
use_v1 (bool): Whether to use v1. Default: False.
Returns:
None
'''
assert os.path.exists(data_path) assert os.path.exists(data_path)
if save_path is None: if save_path is None:
save_path = Path(data_path) save_path = data_path
else: else:
save_path = Path(save_path) save_path = save_path
assert os.path.exists(save_path) assert os.path.exists(save_path)
train_filename = save_path / f'{pkl_prefix}_infos_train.pkl' train_filename = os.path.join(save_path, f'{pkl_prefix}_infos_train.pkl')
val_filename = save_path / f'{pkl_prefix}_infos_val.pkl' val_filename = os.path.join(save_path, f'{pkl_prefix}_infos_val.pkl')
train_dataset = SUNRGBDData( train_dataset = SUNRGBDData(
root_path=data_path, split='train', use_v1=use_v1) root_path=data_path, split='train', use_v1=use_v1)
val_dataset = SUNRGBDData(root_path=data_path, split='val', use_v1=use_v1) val_dataset = SUNRGBDData(root_path=data_path, split='val', use_v1=use_v1)
sunrgbd_infos_train = train_dataset.get_sunrgbd_infos(has_label=True) sunrgbd_infos_train = train_dataset.get_sunrgbd_infos(has_label=True)
with open(train_filename, 'wb') as f: with open(train_filename, 'wb') as f:
pickle.dump(sunrgbd_infos_train, f) mmcv.dump(sunrgbd_infos_train, f, 'pkl')
print('Sunrgbd info train file is saved to %s' % train_filename) print(f'Sunrgbd info train file is saved to {train_filename}')
sunrgbd_infos_val = val_dataset.get_sunrgbd_infos(has_label=True) sunrgbd_infos_val = val_dataset.get_sunrgbd_infos(has_label=True)
with open(val_filename, 'wb') as f: with open(val_filename, 'wb') as f:
pickle.dump(sunrgbd_infos_val, f) mmcv.dump(sunrgbd_infos_val, f, 'pkl')
print('Sunrgbd info val file is saved to %s' % val_filename) print(f'Sunrgbd info val file is saved to {val_filename}')
if __name__ == '__main__':
create_sunrgbd_info_file(
data_path='./data/sunrgbd/sunrgbd_trainval',
save_path='./data/sunrgbd')
import concurrent.futures as futures
import os import os
import cv2 import cv2
...@@ -5,12 +6,24 @@ import numpy as np ...@@ -5,12 +6,24 @@ import numpy as np
import scipy.io as sio import scipy.io as sio
def random_sampling(pc, num_sample, replace=None, return_choices=False): def random_sampling(pc, num_samples, replace=None, return_choices=False):
""" Input is NxC, output is num_samplexC '''
""" Random Sampling.
Sampling point cloud to num_samples points.
Args:
pc (ndarray): Point cloud.
num_samples (int): The number of samples.
replace (bool): Whether the sample is with or without replacement.
return_choices (bool): Whether to return choices.
Returns:
pc (ndarray): Point cloud after sampling.
'''
if replace is None: if replace is None:
replace = (pc.shape[0] < num_sample) replace = (pc.shape[0] < num_samples)
choices = np.random.choice(pc.shape[0], num_sample, replace=replace) choices = np.random.choice(pc.shape[0], num_samples, replace=replace)
if return_choices: if return_choices:
return pc[choices], choices return pc[choices], choices
else: else:
...@@ -44,7 +57,16 @@ class SUNRGBDInstance(object): ...@@ -44,7 +57,16 @@ class SUNRGBDInstance(object):
class SUNRGBDData(object): class SUNRGBDData(object):
''' Load and parse object data ''' '''
SUNRGBD Data
Generate scannet infos for sunrgbd_converter
Args:
root_path (str): Root path of the raw data.
split (str): Set split type of the data. Default: 'train'.
use_v1 (bool): Whether to use v1. Default: False.
'''
def __init__(self, root_path, split='train', use_v1=False): def __init__(self, root_path, split='train', use_v1=False):
self.root_dir = root_path self.root_dir = root_path
...@@ -60,7 +82,7 @@ class SUNRGBDData(object): ...@@ -60,7 +82,7 @@ class SUNRGBDData(object):
for label in range(len(self.classes)) for label in range(len(self.classes))
} }
assert split in ['train', 'val', 'test'] assert split in ['train', 'val', 'test']
split_dir = os.path.join(self.root_dir, '%s_data_idx.txt' % split) split_dir = os.path.join(self.root_dir, f'{split}_data_idx.txt')
self.sample_id_list = [ self.sample_id_list = [
int(x.strip()) for x in open(split_dir).readlines() int(x.strip()) for x in open(split_dir).readlines()
] if os.path.exists(split_dir) else None ] if os.path.exists(split_dir) else None
...@@ -77,7 +99,7 @@ class SUNRGBDData(object): ...@@ -77,7 +99,7 @@ class SUNRGBDData(object):
return len(self.sample_id_list) return len(self.sample_id_list)
def get_image(self, idx): def get_image(self, idx):
img_filename = os.path.join(self.image_dir, '%06d.jpg' % (idx)) img_filename = os.path.join(self.image_dir, f'{idx:06d}.jpg')
return cv2.imread(img_filename) return cv2.imread(img_filename)
def get_image_shape(self, idx): def get_image_shape(self, idx):
...@@ -85,12 +107,12 @@ class SUNRGBDData(object): ...@@ -85,12 +107,12 @@ class SUNRGBDData(object):
return np.array(image.shape[:2], dtype=np.int32) return np.array(image.shape[:2], dtype=np.int32)
def get_depth(self, idx): def get_depth(self, idx):
depth_filename = os.path.join(self.depth_dir, '%06d.mat' % (idx)) depth_filename = os.path.join(self.depth_dir, f'{idx:06d}.mat')
depth = sio.loadmat(depth_filename)['instance'] depth = sio.loadmat(depth_filename)['instance']
return depth return depth
def get_calibration(self, idx): def get_calibration(self, idx):
calib_filepath = os.path.join(self.calib_dir, '%06d.txt' % (idx)) calib_filepath = os.path.join(self.calib_dir, f'{idx:06d}.txt')
lines = [line.rstrip() for line in open(calib_filepath)] lines = [line.rstrip() for line in open(calib_filepath)]
Rt = np.array([float(x) for x in lines[0].split(' ')]) Rt = np.array([float(x) for x in lines[0].split(' ')])
Rt = np.reshape(Rt, (3, 3), order='F') Rt = np.reshape(Rt, (3, 3), order='F')
...@@ -98,7 +120,7 @@ class SUNRGBDData(object): ...@@ -98,7 +120,7 @@ class SUNRGBDData(object):
return K, Rt return K, Rt
def get_label_objects(self, idx): def get_label_objects(self, idx):
label_filename = os.path.join(self.label_dir, '%06d.txt' % (idx)) label_filename = os.path.join(self.label_dir, f'{idx:06d}.txt')
lines = [line.rstrip() for line in open(label_filename)] lines = [line.rstrip() for line in open(label_filename)]
objects = [SUNRGBDInstance(line) for line in lines] objects = [SUNRGBDInstance(line) for line in lines]
return objects return objects
...@@ -107,10 +129,22 @@ class SUNRGBDData(object): ...@@ -107,10 +129,22 @@ class SUNRGBDData(object):
num_workers=4, num_workers=4,
has_label=True, has_label=True,
sample_id_list=None): sample_id_list=None):
import concurrent.futures as futures '''
Get sunrgbd infos.
This method gets information from the raw data.
Args:
num_workers (int): Number of threads to be used. Default: 4.
has_label (bool): Whether the data has label. Default: True.
sample_id_list (List[int]): Index list of the sample. Default: None. # noqa: E501
Returns:
infos (List[dict]): Information of the raw data.
'''
def process_single_scene(sample_idx): def process_single_scene(sample_idx):
print('%s sample_idx: %s' % (self.split, sample_idx)) print(f'{self.split} sample_idx: {sample_idx}')
# convert depth to points # convert depth to points
SAMPLE_NUM = 50000 SAMPLE_NUM = 50000
pc_upright_depth = self.get_depth(sample_idx) pc_upright_depth = self.get_depth(sample_idx)
...@@ -118,13 +152,13 @@ class SUNRGBDData(object): ...@@ -118,13 +152,13 @@ class SUNRGBDData(object):
pc_upright_depth_subsampled = random_sampling( pc_upright_depth_subsampled = random_sampling(
pc_upright_depth, SAMPLE_NUM) pc_upright_depth, SAMPLE_NUM)
np.savez_compressed( np.savez_compressed(
os.path.join(self.root_dir, 'lidar', '%06d.npz' % sample_idx), os.path.join(self.root_dir, 'lidar', f'{sample_idx:06d}.npz'),
pc=pc_upright_depth_subsampled) pc=pc_upright_depth_subsampled)
info = dict() info = dict()
pc_info = {'num_features': 6, 'lidar_idx': sample_idx} pc_info = {'num_features': 6, 'lidar_idx': sample_idx}
info['point_cloud'] = pc_info info['point_cloud'] = pc_info
img_name = os.path.join(self.image_dir, '%06d.jpg' % (sample_idx)) img_name = os.path.join(self.image_dir, f'{sample_idx:06d}')
img_path = os.path.join(self.image_dir, img_name) img_path = os.path.join(self.image_dir, img_name)
image_info = { image_info = {
'image_idx': sample_idx, 'image_idx': sample_idx,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment