"vscode:/vscode.git/clone" did not exist on "e38ee081a0495769e25766b894abe19bc8a6209e"
batch_load_scannet_data.py 4.28 KB
Newer Older
1
2
3
4
# Modified from
# https://github.com/facebookresearch/votenet/blob/master/scannet/batch_load_scannet_data.py
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""Batch mode in loading Scannet scenes with vertices and ground truth labels
liyinhao's avatar
liyinhao committed
5
6
7
8
for semantic and instance segmentations

Usage example: python ./batch_load_scannet_data.py
"""
9
import argparse
liyinhao's avatar
liyinhao committed
10
11
import datetime
import os
12
import os.path as osp
liyinhao's avatar
liyinhao committed
13
14
15
16
17
18
19
20
21
22

import numpy as np
from load_scannet_data import export

SCANNET_DIR = 'scans'
DONOTCARE_CLASS_IDS = np.array([])
OBJ_CLASS_IDS = np.array(
    [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39])


23
24
def export_one_scan(scan_name, output_filename_prefix, max_num_point,
                    label_map_file, scannet_dir):
25
26
27
28
29
30
31
    mesh_file = osp.join(scannet_dir, scan_name, scan_name + '_vh_clean_2.ply')
    agg_file = osp.join(scannet_dir, scan_name,
                        scan_name + '.aggregation.json')
    seg_file = osp.join(scannet_dir, scan_name,
                        scan_name + '_vh_clean_2.0.010000.segs.json')
    # includes axisAlignment info for the train set scans.
    meta_file = osp.join(scannet_dir, scan_name, f'{scan_name}.txt')
liyinhao's avatar
liyinhao committed
32
33
    mesh_vertices, semantic_labels, instance_labels, instance_bboxes, \
        instance2semantic = export(mesh_file, agg_file, seg_file,
34
                                   meta_file, label_map_file, None)
liyinhao's avatar
liyinhao committed
35
36
37
38
39
40
41

    mask = np.logical_not(np.in1d(semantic_labels, DONOTCARE_CLASS_IDS))
    mesh_vertices = mesh_vertices[mask, :]
    semantic_labels = semantic_labels[mask]
    instance_labels = instance_labels[mask]

    num_instances = len(np.unique(instance_labels))
42
    print(f'Num of instances: {num_instances}')
liyinhao's avatar
liyinhao committed
43
44
45
46
47
48

    bbox_mask = np.in1d(instance_bboxes[:, -1], OBJ_CLASS_IDS)
    instance_bboxes = instance_bboxes[bbox_mask, :]
    print('Num of care instances: ', instance_bboxes.shape[0])

    N = mesh_vertices.shape[0]
49
50
    if N > max_num_point:
        choices = np.random.choice(N, max_num_point, replace=False)
liyinhao's avatar
liyinhao committed
51
52
53
54
        mesh_vertices = mesh_vertices[choices, :]
        semantic_labels = semantic_labels[choices]
        instance_labels = instance_labels[choices]

55
56
57
58
    np.save(f'{output_filename_prefix}_vert.npy', mesh_vertices)
    np.save(f'{output_filename_prefix}_sem_label.npy', semantic_labels)
    np.save(f'{output_filename_prefix}_ins_label.npy', instance_labels)
    np.save(f'{output_filename_prefix}_bbox.npy', instance_bboxes)
liyinhao's avatar
liyinhao committed
59
60


61
62
63
def batch_export(max_num_point, output_folder, train_scan_names_file,
                 label_map_file, scannet_dir):
    if not os.path.exists(output_folder):
64
        print(f'Creating new data folder: {output_folder}')
65
        os.mkdir(output_folder)
liyinhao's avatar
liyinhao committed
66

67
68
    train_scan_names = [line.rstrip() for line in open(train_scan_names_file)]
    for scan_name in train_scan_names:
liyinhao's avatar
liyinhao committed
69
70
71
        print('-' * 20 + 'begin')
        print(datetime.datetime.now())
        print(scan_name)
72
73
        output_filename_prefix = osp.join(output_folder, scan_name)
        if osp.isfile(f'{output_filename_prefix}_vert.npy'):
liyinhao's avatar
liyinhao committed
74
75
76
77
            print('File already exists. skipping.')
            print('-' * 20 + 'done')
            continue
        try:
78
79
            export_one_scan(scan_name, output_filename_prefix, max_num_point,
                            label_map_file, scannet_dir)
liyinhao's avatar
liyinhao committed
80
81
82
83
84
        except Exception:
            print('Failed export scan: %s' % (scan_name))
        print('-' * 20 + 'done')


85
86
87
88
89
90
91
92
93
94
95
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--max_num_point',
        default=50000,
        help='The maximum number of the points.')
    parser.add_argument(
        '--output_folder',
        default='./scannet_train_instance_data',
        help='output folder of the result.')
    parser.add_argument(
yinchimaoliang's avatar
yinchimaoliang committed
96
        '--scannet_dir', default='scans', help='scannet data directory.')
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    parser.add_argument(
        '--label_map_file',
        default='meta_data/scannetv2-labels.combined.tsv',
        help='The path of label map file.')
    parser.add_argument(
        '--train_scan_names_file',
        default='meta_data/scannet_train.txt',
        help='The path of the file that stores the scan names.')
    args = parser.parse_args()
    batch_export(args.max_num_point, args.output_folder,
                 args.train_scan_names_file, args.label_map_file,
                 args.scannet_dir)


liyinhao's avatar
liyinhao committed
111
if __name__ == '__main__':
112
    main()