We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
An error occurred while creating Scannetv2 data
@Wuziyi616  I am trying to create the Scannet dataset as required, but an error occurred when I operate ```python tools/create_data.py scannet --root-path ./data/scannet --out-dir ./data/scannet --extra-tag scannet```. Here is the error message:
```
scannet info test file is saved to ./data/scannet/scannet_infos_test.pkl
Traceback (most recent call last):
  File "tools/create_data.py", line 300, in <module>
    workers=args.workers)
  File "tools/create_data.py", line 134, in scannet_data_prep
    root_path, info_prefix, out_dir, workers=workers)
  File "/scratch1/du014/Auxiliary_Code/mmdetection3d/tools/data_converter/indoor_converter.py", line 87, in create_indoor_info_file
    train_dataset.get_seg_infos()
  File "/scratch1/du014/Auxiliary_Code/mmdetection3d/tools/data_converter/scannet_data_utils.py", line 204, in get_seg_infos
    scene_idxs, label_weight = self.get_scene_idxs_and_label_weight()
  File "/scratch1/du014/Auxiliary_Code/mmdetection3d/tools/data_converter/scannet_data_utils.py", line 247, in get_scene_idxs_and_label_weight
    scene_idxs.extend([idx] * round(sample_prob[idx] * num_iter))
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
```

My environment:
- cuda 10.2
- python 3.6
- mmcv-full 1.3.7
- mmdet 2.11.0
- mmdet3d 0.14.0
- mmsegmentation 0.14.0
- numpy 1.18.0

Can anyone help me????

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `tools/data_converter/scannet_data_utils.py`
Content:
```
1 import mmcv
2 import numpy as np
3 from concurrent import futures as futures
4 from os import path as osp
5 
6 
7 class ScanNetData(object):
8     """ScanNet data.
9 
10     Generate scannet infos for scannet_converter.
11 
12     Args:
13         root_path (str): Root path of the raw data.
14         split (str): Set split type of the data. Default: 'train'.
15     """
16 
17     def __init__(self, root_path, split='train'):
18         self.root_dir = root_path
19         self.split = split
20         self.split_dir = osp.join(root_path)
21         self.classes = [
22             'cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window',
23             'bookshelf', 'picture', 'counter', 'desk', 'curtain',
24             'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub',
25             'garbagebin'
26         ]
27         self.cat2label = {cat: self.classes.index(cat) for cat in self.classes}
28         self.label2cat = {self.cat2label[t]: t for t in self.cat2label}
29         self.cat_ids = np.array(
30             [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39])
31         self.cat_ids2class = {
32             nyu40id: i
33             for i, nyu40id in enumerate(list(self.cat_ids))
34         }
35         assert split in ['train', 'val', 'test']
36         split_file = osp.join(self.root_dir, 'meta_data',
37                               f'scannetv2_{split}.txt')
38         mmcv.check_file_exist(split_file)
39         self.sample_id_list = mmcv.list_from_file(split_file)
40         self.test_mode = (split == 'test')
41 
42     def __len__(self):
43         return len(self.sample_id_list)
44 
45     def get_aligned_box_label(self, idx):
46         box_file = osp.join(self.root_dir, 'scannet_instance_data',
47                             f'{idx}_aligned_bbox.npy')
48         mmcv.check_file_exist(box_file)
49         return np.load(box_file)
50 
51     def get_unaligned_box_label(self, idx):
52         box_file = osp.join(self.root_dir, 'scannet_instance_data',
53                             f'{idx}_unaligned_bbox.npy')
54         mmcv.check_file_exist(box_file)
55         return np.load(box_file)
56 
57     def get_axis_align_matrix(self, idx):
58         matrix_file = osp.join(self.root_dir, 'scannet_instance_data',
59                                f'{idx}_axis_align_matrix.npy')
60         mmcv.check_file_exist(matrix_file)
61         return np.load(matrix_file)
62 
63     def get_infos(self, num_workers=4, has_label=True, sample_id_list=None):
64         """Get data infos.
65 
66         This method gets information from the raw data.
67 
68         Args:
69             num_workers (int): Number of threads to be used. Default: 4.
70             has_label (bool): Whether the data has label. Default: True.
71             sample_id_list (list[int]): Index list of the sample.
72                 Default: None.
73 
74         Returns:
75             infos (list[dict]): Information of the raw data.
76         """
77 
78         def process_single_scene(sample_idx):
79             print(f'{self.split} sample_idx: {sample_idx}')
80             info = dict()
81             pc_info = {'num_features': 6, 'lidar_idx': sample_idx}
82             info['point_cloud'] = pc_info
83             pts_filename = osp.join(self.root_dir, 'scannet_instance_data',
84                                     f'{sample_idx}_vert.npy')
85             points = np.load(pts_filename)
86             mmcv.mkdir_or_exist(osp.join(self.root_dir, 'points'))
87             points.tofile(
88                 osp.join(self.root_dir, 'points', f'{sample_idx}.bin'))
89             info['pts_path'] = osp.join('points', f'{sample_idx}.bin')
90 
91             if not self.test_mode:
92                 pts_instance_mask_path = osp.join(
93                     self.root_dir, 'scannet_instance_data',
94                     f'{sample_idx}_ins_label.npy')
95                 pts_semantic_mask_path = osp.join(
96                     self.root_dir, 'scannet_instance_data',
97                     f'{sample_idx}_sem_label.npy')
98 
99                 pts_instance_mask = np.load(pts_instance_mask_path).astype(
100                     np.long)
101                 pts_semantic_mask = np.load(pts_semantic_mask_path).astype(
102                     np.long)
103 
104                 mmcv.mkdir_or_exist(osp.join(self.root_dir, 'instance_mask'))
105                 mmcv.mkdir_or_exist(osp.join(self.root_dir, 'semantic_mask'))
106 
107                 pts_instance_mask.tofile(
108                     osp.join(self.root_dir, 'instance_mask',
109                              f'{sample_idx}.bin'))
110                 pts_semantic_mask.tofile(
111                     osp.join(self.root_dir, 'semantic_mask',
112                              f'{sample_idx}.bin'))
113 
114                 info['pts_instance_mask_path'] = osp.join(
115                     'instance_mask', f'{sample_idx}.bin')
116                 info['pts_semantic_mask_path'] = osp.join(
117                     'semantic_mask', f'{sample_idx}.bin')
118 
119             if has_label:
120                 annotations = {}
121                 # box is of shape [k, 6 + class]
122                 aligned_box_label = self.get_aligned_box_label(sample_idx)
123                 unaligned_box_label = self.get_unaligned_box_label(sample_idx)
124                 annotations['gt_num'] = aligned_box_label.shape[0]
125                 if annotations['gt_num'] != 0:
126                     aligned_box = aligned_box_label[:, :-1]  # k, 6
127                     unaligned_box = unaligned_box_label[:, :-1]
128                     classes = aligned_box_label[:, -1]  # k
129                     annotations['name'] = np.array([
130                         self.label2cat[self.cat_ids2class[classes[i]]]
131                         for i in range(annotations['gt_num'])
132                     ])
133                     # default names are given to aligned bbox for compatibility
134                     # we also save unaligned bbox info with marked names
135                     annotations['location'] = aligned_box[:, :3]
136                     annotations['dimensions'] = aligned_box[:, 3:6]
137                     annotations['gt_boxes_upright_depth'] = aligned_box
138                     annotations['unaligned_location'] = unaligned_box[:, :3]
139                     annotations['unaligned_dimensions'] = unaligned_box[:, 3:6]
140                     annotations[
141                         'unaligned_gt_boxes_upright_depth'] = unaligned_box
142                     annotations['index'] = np.arange(
143                         annotations['gt_num'], dtype=np.int32)
144                     annotations['class'] = np.array([
145                         self.cat_ids2class[classes[i]]
146                         for i in range(annotations['gt_num'])
147                     ])
148                 axis_align_matrix = self.get_axis_align_matrix(sample_idx)
149                 annotations['axis_align_matrix'] = axis_align_matrix  # 4x4
150                 info['annos'] = annotations
151             return info
152 
153         sample_id_list = sample_id_list if sample_id_list is not None \
154             else self.sample_id_list
155         with futures.ThreadPoolExecutor(num_workers) as executor:
156             infos = executor.map(process_single_scene, sample_id_list)
157         return list(infos)
158 
159 
160 class ScanNetSegData(object):
161     """ScanNet dataset used to generate infos for semantic segmentation task.
162 
163     Args:
164         data_root (str): Root path of the raw data.
165         ann_file (str): The generated scannet infos.
166         split (str): Set split type of the data. Default: 'train'.
167         num_points (int): Number of points in each data input. Default: 8192.
168         label_weight_func (function): Function to compute the label weight.
169             Default: None.
170     """
171 
172     def __init__(self,
173                  data_root,
174                  ann_file,
175                  split='train',
176                  num_points=8192,
177                  label_weight_func=None):
178         self.data_root = data_root
179         self.data_infos = mmcv.load(ann_file)
180         self.split = split
181         assert split in ['train', 'val', 'test']
182         self.num_points = num_points
183 
184         self.all_ids = np.arange(41)  # all possible ids
185         self.cat_ids = np.array([
186             1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36,
187             39
188         ])  # used for seg task
189         self.ignore_index = len(self.cat_ids)
190 
191         self.cat_id2class = np.ones((self.all_ids.shape[0],), dtype=np.int) * \
192             self.ignore_index
193         for i, cat_id in enumerate(self.cat_ids):
194             self.cat_id2class[cat_id] = i
195 
196         # label weighting function is taken from
197         # https://github.com/charlesq34/pointnet2/blob/master/scannet/scannet_dataset.py#L24
198         self.label_weight_func = (lambda x: 1.0 / np.log(1.2 + x)) if \
199             label_weight_func is None else label_weight_func
200 
201     def get_seg_infos(self):
202         if self.split == 'test':
203             return
204         scene_idxs, label_weight = self.get_scene_idxs_and_label_weight()
205         save_folder = osp.join(self.data_root, 'seg_info')
206         mmcv.mkdir_or_exist(save_folder)
207         np.save(
208             osp.join(save_folder, f'{self.split}_resampled_scene_idxs.npy'),
209             scene_idxs)
210         np.save(
211             osp.join(save_folder, f'{self.split}_label_weight.npy'),
212             label_weight)
213         print(f'{self.split} resampled scene index and label weight saved')
214 
215     def _convert_to_label(self, mask):
216         """Convert class_id in loaded segmentation mask to label."""
217         if isinstance(mask, str):
218             if mask.endswith('npy'):
219                 mask = np.load(mask)
220             else:
221                 mask = np.fromfile(mask, dtype=np.long)
222         label = self.cat_id2class[mask]
223         return label
224 
225     def get_scene_idxs_and_label_weight(self):
226         """Compute scene_idxs for data sampling and label weight for loss \
227         calculation.
228 
229         We sample more times for scenes with more points. Label_weight is
230         inversely proportional to number of class points.
231         """
232         num_classes = len(self.cat_ids)
233         num_point_all = []
234         label_weight = np.zeros((num_classes + 1, ))  # ignore_index
235         for data_info in self.data_infos:
236             label = self._convert_to_label(
237                 osp.join(self.data_root, data_info['pts_semantic_mask_path']))
238             num_point_all.append(label.shape[0])
239             class_count, _ = np.histogram(label, range(num_classes + 2))
240             label_weight += class_count
241 
242         # repeat scene_idx for num_scene_point // num_sample_point times
243         sample_prob = np.array(num_point_all) / float(np.sum(num_point_all))
244         num_iter = int(np.sum(num_point_all) / float(self.num_points))
245         scene_idxs = []
246         for idx in range(len(self.data_infos)):
247             scene_idxs.extend([idx] * round(sample_prob[idx] * num_iter))
248         scene_idxs = np.array(scene_idxs).astype(np.int32)
249 
250         # calculate label weight, adopted from PointNet++
251         label_weight = label_weight[:-1].astype(np.float32)
252         label_weight = label_weight / label_weight.sum()
253         label_weight = self.label_weight_func(label_weight).astype(np.float32)
254 
255         return scene_idxs, label_weight
256
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/tools/data_converter/scannet_data_utils.py b/tools/data_converter/scannet_data_utils.py
--- a/tools/data_converter/scannet_data_utils.py
+++ b/tools/data_converter/scannet_data_utils.py
@@ -244,7 +244,7 @@
         num_iter = int(np.sum(num_point_all) / float(self.num_points))
         scene_idxs = []
         for idx in range(len(self.data_infos)):
-            scene_idxs.extend([idx] * round(sample_prob[idx] * num_iter))
+            scene_idxs.extend([idx] * int(round(sample_prob[idx] * num_iter)))
         scene_idxs = np.array(scene_idxs).astype(np.int32)
 
         # calculate label weight, adopted from PointNet++

{"golden_diff": "diff --git a/tools/data_converter/scannet_data_utils.py b/tools/data_converter/scannet_data_utils.py\n--- a/tools/data_converter/scannet_data_utils.py\n+++ b/tools/data_converter/scannet_data_utils.py\n@@ -244,7 +244,7 @@\n         num_iter = int(np.sum(num_point_all) / float(self.num_points))\n         scene_idxs = []\n         for idx in range(len(self.data_infos)):\n-            scene_idxs.extend([idx] * round(sample_prob[idx] * num_iter))\n+            scene_idxs.extend([idx] * int(round(sample_prob[idx] * num_iter)))\n         scene_idxs = np.array(scene_idxs).astype(np.int32)\n \n         # calculate label weight, adopted from PointNet++\n", "issue": "An error occurred while creating Scannetv2 data\n@Wuziyi616  I am trying to create the Scannet dataset as required, but an error occurred when I operate ```python tools/create_data.py scannet --root-path ./data/scannet --out-dir ./data/scannet --extra-tag scannet```. Here is the error message:\r\n```\r\nscannet info test file is saved to ./data/scannet/scannet_infos_test.pkl\r\nTraceback (most recent call last):\r\n  File \"tools/create_data.py\", line 300, in <module>\r\n    workers=args.workers)\r\n  File \"tools/create_data.py\", line 134, in scannet_data_prep\r\n    root_path, info_prefix, out_dir, workers=workers)\r\n  File \"/scratch1/du014/Auxiliary_Code/mmdetection3d/tools/data_converter/indoor_converter.py\", line 87, in create_indoor_info_file\r\n    train_dataset.get_seg_infos()\r\n  File \"/scratch1/du014/Auxiliary_Code/mmdetection3d/tools/data_converter/scannet_data_utils.py\", line 204, in get_seg_infos\r\n    scene_idxs, label_weight = self.get_scene_idxs_and_label_weight()\r\n  File \"/scratch1/du014/Auxiliary_Code/mmdetection3d/tools/data_converter/scannet_data_utils.py\", line 247, in get_scene_idxs_and_label_weight\r\n    scene_idxs.extend([idx] * round(sample_prob[idx] * num_iter))\r\nTypeError: can't multiply sequence by non-int of type 'numpy.float64'\r\n```\r\n\r\nMy environment:\r\n- cuda 10.2\r\n- python 3.6\r\n- mmcv-full 1.3.7\r\n- mmdet 2.11.0\r\n- mmdet3d 0.14.0\r\n- mmsegmentation 0.14.0\r\n- numpy 1.18.0\r\n\r\nCan anyone help me????\n", "before_files": [{"content": "import mmcv\nimport numpy as np\nfrom concurrent import futures as futures\nfrom os import path as osp\n\n\nclass ScanNetData(object):\n    \"\"\"ScanNet data.\n\n    Generate scannet infos for scannet_converter.\n\n    Args:\n        root_path (str): Root path of the raw data.\n        split (str): Set split type of the data. Default: 'train'.\n    \"\"\"\n\n    def __init__(self, root_path, split='train'):\n        self.root_dir = root_path\n        self.split = split\n        self.split_dir = osp.join(root_path)\n        self.classes = [\n            'cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window',\n            'bookshelf', 'picture', 'counter', 'desk', 'curtain',\n            'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub',\n            'garbagebin'\n        ]\n        self.cat2label = {cat: self.classes.index(cat) for cat in self.classes}\n        self.label2cat = {self.cat2label[t]: t for t in self.cat2label}\n        self.cat_ids = np.array(\n            [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39])\n        self.cat_ids2class = {\n            nyu40id: i\n            for i, nyu40id in enumerate(list(self.cat_ids))\n        }\n        assert split in ['train', 'val', 'test']\n        split_file = osp.join(self.root_dir, 'meta_data',\n                              f'scannetv2_{split}.txt')\n        mmcv.check_file_exist(split_file)\n        self.sample_id_list = mmcv.list_from_file(split_file)\n        self.test_mode = (split == 'test')\n\n    def __len__(self):\n        return len(self.sample_id_list)\n\n    def get_aligned_box_label(self, idx):\n        box_file = osp.join(self.root_dir, 'scannet_instance_data',\n                            f'{idx}_aligned_bbox.npy')\n        mmcv.check_file_exist(box_file)\n        return np.load(box_file)\n\n    def get_unaligned_box_label(self, idx):\n        box_file = osp.join(self.root_dir, 'scannet_instance_data',\n                            f'{idx}_unaligned_bbox.npy')\n        mmcv.check_file_exist(box_file)\n        return np.load(box_file)\n\n    def get_axis_align_matrix(self, idx):\n        matrix_file = osp.join(self.root_dir, 'scannet_instance_data',\n                               f'{idx}_axis_align_matrix.npy')\n        mmcv.check_file_exist(matrix_file)\n        return np.load(matrix_file)\n\n    def get_infos(self, num_workers=4, has_label=True, sample_id_list=None):\n        \"\"\"Get data infos.\n\n        This method gets information from the raw data.\n\n        Args:\n            num_workers (int): Number of threads to be used. Default: 4.\n            has_label (bool): Whether the data has label. Default: True.\n            sample_id_list (list[int]): Index list of the sample.\n                Default: None.\n\n        Returns:\n            infos (list[dict]): Information of the raw data.\n        \"\"\"\n\n        def process_single_scene(sample_idx):\n            print(f'{self.split} sample_idx: {sample_idx}')\n            info = dict()\n            pc_info = {'num_features': 6, 'lidar_idx': sample_idx}\n            info['point_cloud'] = pc_info\n            pts_filename = osp.join(self.root_dir, 'scannet_instance_data',\n                                    f'{sample_idx}_vert.npy')\n            points = np.load(pts_filename)\n            mmcv.mkdir_or_exist(osp.join(self.root_dir, 'points'))\n            points.tofile(\n                osp.join(self.root_dir, 'points', f'{sample_idx}.bin'))\n            info['pts_path'] = osp.join('points', f'{sample_idx}.bin')\n\n            if not self.test_mode:\n                pts_instance_mask_path = osp.join(\n                    self.root_dir, 'scannet_instance_data',\n                    f'{sample_idx}_ins_label.npy')\n                pts_semantic_mask_path = osp.join(\n                    self.root_dir, 'scannet_instance_data',\n                    f'{sample_idx}_sem_label.npy')\n\n                pts_instance_mask = np.load(pts_instance_mask_path).astype(\n                    np.long)\n                pts_semantic_mask = np.load(pts_semantic_mask_path).astype(\n                    np.long)\n\n                mmcv.mkdir_or_exist(osp.join(self.root_dir, 'instance_mask'))\n                mmcv.mkdir_or_exist(osp.join(self.root_dir, 'semantic_mask'))\n\n                pts_instance_mask.tofile(\n                    osp.join(self.root_dir, 'instance_mask',\n                             f'{sample_idx}.bin'))\n                pts_semantic_mask.tofile(\n                    osp.join(self.root_dir, 'semantic_mask',\n                             f'{sample_idx}.bin'))\n\n                info['pts_instance_mask_path'] = osp.join(\n                    'instance_mask', f'{sample_idx}.bin')\n                info['pts_semantic_mask_path'] = osp.join(\n                    'semantic_mask', f'{sample_idx}.bin')\n\n            if has_label:\n                annotations = {}\n                # box is of shape [k, 6 + class]\n                aligned_box_label = self.get_aligned_box_label(sample_idx)\n                unaligned_box_label = self.get_unaligned_box_label(sample_idx)\n                annotations['gt_num'] = aligned_box_label.shape[0]\n                if annotations['gt_num'] != 0:\n                    aligned_box = aligned_box_label[:, :-1]  # k, 6\n                    unaligned_box = unaligned_box_label[:, :-1]\n                    classes = aligned_box_label[:, -1]  # k\n                    annotations['name'] = np.array([\n                        self.label2cat[self.cat_ids2class[classes[i]]]\n                        for i in range(annotations['gt_num'])\n                    ])\n                    # default names are given to aligned bbox for compatibility\n                    # we also save unaligned bbox info with marked names\n                    annotations['location'] = aligned_box[:, :3]\n                    annotations['dimensions'] = aligned_box[:, 3:6]\n                    annotations['gt_boxes_upright_depth'] = aligned_box\n                    annotations['unaligned_location'] = unaligned_box[:, :3]\n                    annotations['unaligned_dimensions'] = unaligned_box[:, 3:6]\n                    annotations[\n                        'unaligned_gt_boxes_upright_depth'] = unaligned_box\n                    annotations['index'] = np.arange(\n                        annotations['gt_num'], dtype=np.int32)\n                    annotations['class'] = np.array([\n                        self.cat_ids2class[classes[i]]\n                        for i in range(annotations['gt_num'])\n                    ])\n                axis_align_matrix = self.get_axis_align_matrix(sample_idx)\n                annotations['axis_align_matrix'] = axis_align_matrix  # 4x4\n                info['annos'] = annotations\n            return info\n\n        sample_id_list = sample_id_list if sample_id_list is not None \\\n            else self.sample_id_list\n        with futures.ThreadPoolExecutor(num_workers) as executor:\n            infos = executor.map(process_single_scene, sample_id_list)\n        return list(infos)\n\n\nclass ScanNetSegData(object):\n    \"\"\"ScanNet dataset used to generate infos for semantic segmentation task.\n\n    Args:\n        data_root (str): Root path of the raw data.\n        ann_file (str): The generated scannet infos.\n        split (str): Set split type of the data. Default: 'train'.\n        num_points (int): Number of points in each data input. Default: 8192.\n        label_weight_func (function): Function to compute the label weight.\n            Default: None.\n    \"\"\"\n\n    def __init__(self,\n                 data_root,\n                 ann_file,\n                 split='train',\n                 num_points=8192,\n                 label_weight_func=None):\n        self.data_root = data_root\n        self.data_infos = mmcv.load(ann_file)\n        self.split = split\n        assert split in ['train', 'val', 'test']\n        self.num_points = num_points\n\n        self.all_ids = np.arange(41)  # all possible ids\n        self.cat_ids = np.array([\n            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36,\n            39\n        ])  # used for seg task\n        self.ignore_index = len(self.cat_ids)\n\n        self.cat_id2class = np.ones((self.all_ids.shape[0],), dtype=np.int) * \\\n            self.ignore_index\n        for i, cat_id in enumerate(self.cat_ids):\n            self.cat_id2class[cat_id] = i\n\n        # label weighting function is taken from\n        # https://github.com/charlesq34/pointnet2/blob/master/scannet/scannet_dataset.py#L24\n        self.label_weight_func = (lambda x: 1.0 / np.log(1.2 + x)) if \\\n            label_weight_func is None else label_weight_func\n\n    def get_seg_infos(self):\n        if self.split == 'test':\n            return\n        scene_idxs, label_weight = self.get_scene_idxs_and_label_weight()\n        save_folder = osp.join(self.data_root, 'seg_info')\n        mmcv.mkdir_or_exist(save_folder)\n        np.save(\n            osp.join(save_folder, f'{self.split}_resampled_scene_idxs.npy'),\n            scene_idxs)\n        np.save(\n            osp.join(save_folder, f'{self.split}_label_weight.npy'),\n            label_weight)\n        print(f'{self.split} resampled scene index and label weight saved')\n\n    def _convert_to_label(self, mask):\n        \"\"\"Convert class_id in loaded segmentation mask to label.\"\"\"\n        if isinstance(mask, str):\n            if mask.endswith('npy'):\n                mask = np.load(mask)\n            else:\n                mask = np.fromfile(mask, dtype=np.long)\n        label = self.cat_id2class[mask]\n        return label\n\n    def get_scene_idxs_and_label_weight(self):\n        \"\"\"Compute scene_idxs for data sampling and label weight for loss \\\n        calculation.\n\n        We sample more times for scenes with more points. Label_weight is\n        inversely proportional to number of class points.\n        \"\"\"\n        num_classes = len(self.cat_ids)\n        num_point_all = []\n        label_weight = np.zeros((num_classes + 1, ))  # ignore_index\n        for data_info in self.data_infos:\n            label = self._convert_to_label(\n                osp.join(self.data_root, data_info['pts_semantic_mask_path']))\n            num_point_all.append(label.shape[0])\n            class_count, _ = np.histogram(label, range(num_classes + 2))\n            label_weight += class_count\n\n        # repeat scene_idx for num_scene_point // num_sample_point times\n        sample_prob = np.array(num_point_all) / float(np.sum(num_point_all))\n        num_iter = int(np.sum(num_point_all) / float(self.num_points))\n        scene_idxs = []\n        for idx in range(len(self.data_infos)):\n            scene_idxs.extend([idx] * round(sample_prob[idx] * num_iter))\n        scene_idxs = np.array(scene_idxs).astype(np.int32)\n\n        # calculate label weight, adopted from PointNet++\n        label_weight = label_weight[:-1].astype(np.float32)\n        label_weight = label_weight / label_weight.sum()\n        label_weight = self.label_weight_func(label_weight).astype(np.float32)\n\n        return scene_idxs, label_weight\n", "path": "tools/data_converter/scannet_data_utils.py"}], "after_files": [{"content": "import mmcv\nimport numpy as np\nfrom concurrent import futures as futures\nfrom os import path as osp\n\n\nclass ScanNetData(object):\n    \"\"\"ScanNet data.\n\n    Generate scannet infos for scannet_converter.\n\n    Args:\n        root_path (str): Root path of the raw data.\n        split (str): Set split type of the data. Default: 'train'.\n    \"\"\"\n\n    def __init__(self, root_path, split='train'):\n        self.root_dir = root_path\n        self.split = split\n        self.split_dir = osp.join(root_path)\n        self.classes = [\n            'cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window',\n            'bookshelf', 'picture', 'counter', 'desk', 'curtain',\n            'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub',\n            'garbagebin'\n        ]\n        self.cat2label = {cat: self.classes.index(cat) for cat in self.classes}\n        self.label2cat = {self.cat2label[t]: t for t in self.cat2label}\n        self.cat_ids = np.array(\n            [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39])\n        self.cat_ids2class = {\n            nyu40id: i\n            for i, nyu40id in enumerate(list(self.cat_ids))\n        }\n        assert split in ['train', 'val', 'test']\n        split_file = osp.join(self.root_dir, 'meta_data',\n                              f'scannetv2_{split}.txt')\n        mmcv.check_file_exist(split_file)\n        self.sample_id_list = mmcv.list_from_file(split_file)\n        self.test_mode = (split == 'test')\n\n    def __len__(self):\n        return len(self.sample_id_list)\n\n    def get_aligned_box_label(self, idx):\n        box_file = osp.join(self.root_dir, 'scannet_instance_data',\n                            f'{idx}_aligned_bbox.npy')\n        mmcv.check_file_exist(box_file)\n        return np.load(box_file)\n\n    def get_unaligned_box_label(self, idx):\n        box_file = osp.join(self.root_dir, 'scannet_instance_data',\n                            f'{idx}_unaligned_bbox.npy')\n        mmcv.check_file_exist(box_file)\n        return np.load(box_file)\n\n    def get_axis_align_matrix(self, idx):\n        matrix_file = osp.join(self.root_dir, 'scannet_instance_data',\n                               f'{idx}_axis_align_matrix.npy')\n        mmcv.check_file_exist(matrix_file)\n        return np.load(matrix_file)\n\n    def get_infos(self, num_workers=4, has_label=True, sample_id_list=None):\n        \"\"\"Get data infos.\n\n        This method gets information from the raw data.\n\n        Args:\n            num_workers (int): Number of threads to be used. Default: 4.\n            has_label (bool): Whether the data has label. Default: True.\n            sample_id_list (list[int]): Index list of the sample.\n                Default: None.\n\n        Returns:\n            infos (list[dict]): Information of the raw data.\n        \"\"\"\n\n        def process_single_scene(sample_idx):\n            print(f'{self.split} sample_idx: {sample_idx}')\n            info = dict()\n            pc_info = {'num_features': 6, 'lidar_idx': sample_idx}\n            info['point_cloud'] = pc_info\n            pts_filename = osp.join(self.root_dir, 'scannet_instance_data',\n                                    f'{sample_idx}_vert.npy')\n            points = np.load(pts_filename)\n            mmcv.mkdir_or_exist(osp.join(self.root_dir, 'points'))\n            points.tofile(\n                osp.join(self.root_dir, 'points', f'{sample_idx}.bin'))\n            info['pts_path'] = osp.join('points', f'{sample_idx}.bin')\n\n            if not self.test_mode:\n                pts_instance_mask_path = osp.join(\n                    self.root_dir, 'scannet_instance_data',\n                    f'{sample_idx}_ins_label.npy')\n                pts_semantic_mask_path = osp.join(\n                    self.root_dir, 'scannet_instance_data',\n                    f'{sample_idx}_sem_label.npy')\n\n                pts_instance_mask = np.load(pts_instance_mask_path).astype(\n                    np.long)\n                pts_semantic_mask = np.load(pts_semantic_mask_path).astype(\n                    np.long)\n\n                mmcv.mkdir_or_exist(osp.join(self.root_dir, 'instance_mask'))\n                mmcv.mkdir_or_exist(osp.join(self.root_dir, 'semantic_mask'))\n\n                pts_instance_mask.tofile(\n                    osp.join(self.root_dir, 'instance_mask',\n                             f'{sample_idx}.bin'))\n                pts_semantic_mask.tofile(\n                    osp.join(self.root_dir, 'semantic_mask',\n                             f'{sample_idx}.bin'))\n\n                info['pts_instance_mask_path'] = osp.join(\n                    'instance_mask', f'{sample_idx}.bin')\n                info['pts_semantic_mask_path'] = osp.join(\n                    'semantic_mask', f'{sample_idx}.bin')\n\n            if has_label:\n                annotations = {}\n                # box is of shape [k, 6 + class]\n                aligned_box_label = self.get_aligned_box_label(sample_idx)\n                unaligned_box_label = self.get_unaligned_box_label(sample_idx)\n                annotations['gt_num'] = aligned_box_label.shape[0]\n                if annotations['gt_num'] != 0:\n                    aligned_box = aligned_box_label[:, :-1]  # k, 6\n                    unaligned_box = unaligned_box_label[:, :-1]\n                    classes = aligned_box_label[:, -1]  # k\n                    annotations['name'] = np.array([\n                        self.label2cat[self.cat_ids2class[classes[i]]]\n                        for i in range(annotations['gt_num'])\n                    ])\n                    # default names are given to aligned bbox for compatibility\n                    # we also save unaligned bbox info with marked names\n                    annotations['location'] = aligned_box[:, :3]\n                    annotations['dimensions'] = aligned_box[:, 3:6]\n                    annotations['gt_boxes_upright_depth'] = aligned_box\n                    annotations['unaligned_location'] = unaligned_box[:, :3]\n                    annotations['unaligned_dimensions'] = unaligned_box[:, 3:6]\n                    annotations[\n                        'unaligned_gt_boxes_upright_depth'] = unaligned_box\n                    annotations['index'] = np.arange(\n                        annotations['gt_num'], dtype=np.int32)\n                    annotations['class'] = np.array([\n                        self.cat_ids2class[classes[i]]\n                        for i in range(annotations['gt_num'])\n                    ])\n                axis_align_matrix = self.get_axis_align_matrix(sample_idx)\n                annotations['axis_align_matrix'] = axis_align_matrix  # 4x4\n                info['annos'] = annotations\n            return info\n\n        sample_id_list = sample_id_list if sample_id_list is not None \\\n            else self.sample_id_list\n        with futures.ThreadPoolExecutor(num_workers) as executor:\n            infos = executor.map(process_single_scene, sample_id_list)\n        return list(infos)\n\n\nclass ScanNetSegData(object):\n    \"\"\"ScanNet dataset used to generate infos for semantic segmentation task.\n\n    Args:\n        data_root (str): Root path of the raw data.\n        ann_file (str): The generated scannet infos.\n        split (str): Set split type of the data. Default: 'train'.\n        num_points (int): Number of points in each data input. Default: 8192.\n        label_weight_func (function): Function to compute the label weight.\n            Default: None.\n    \"\"\"\n\n    def __init__(self,\n                 data_root,\n                 ann_file,\n                 split='train',\n                 num_points=8192,\n                 label_weight_func=None):\n        self.data_root = data_root\n        self.data_infos = mmcv.load(ann_file)\n        self.split = split\n        assert split in ['train', 'val', 'test']\n        self.num_points = num_points\n\n        self.all_ids = np.arange(41)  # all possible ids\n        self.cat_ids = np.array([\n            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36,\n            39\n        ])  # used for seg task\n        self.ignore_index = len(self.cat_ids)\n\n        self.cat_id2class = np.ones((self.all_ids.shape[0],), dtype=np.int) * \\\n            self.ignore_index\n        for i, cat_id in enumerate(self.cat_ids):\n            self.cat_id2class[cat_id] = i\n\n        # label weighting function is taken from\n        # https://github.com/charlesq34/pointnet2/blob/master/scannet/scannet_dataset.py#L24\n        self.label_weight_func = (lambda x: 1.0 / np.log(1.2 + x)) if \\\n            label_weight_func is None else label_weight_func\n\n    def get_seg_infos(self):\n        if self.split == 'test':\n            return\n        scene_idxs, label_weight = self.get_scene_idxs_and_label_weight()\n        save_folder = osp.join(self.data_root, 'seg_info')\n        mmcv.mkdir_or_exist(save_folder)\n        np.save(\n            osp.join(save_folder, f'{self.split}_resampled_scene_idxs.npy'),\n            scene_idxs)\n        np.save(\n            osp.join(save_folder, f'{self.split}_label_weight.npy'),\n            label_weight)\n        print(f'{self.split} resampled scene index and label weight saved')\n\n    def _convert_to_label(self, mask):\n        \"\"\"Convert class_id in loaded segmentation mask to label.\"\"\"\n        if isinstance(mask, str):\n            if mask.endswith('npy'):\n                mask = np.load(mask)\n            else:\n                mask = np.fromfile(mask, dtype=np.long)\n        label = self.cat_id2class[mask]\n        return label\n\n    def get_scene_idxs_and_label_weight(self):\n        \"\"\"Compute scene_idxs for data sampling and label weight for loss \\\n        calculation.\n\n        We sample more times for scenes with more points. Label_weight is\n        inversely proportional to number of class points.\n        \"\"\"\n        num_classes = len(self.cat_ids)\n        num_point_all = []\n        label_weight = np.zeros((num_classes + 1, ))  # ignore_index\n        for data_info in self.data_infos:\n            label = self._convert_to_label(\n                osp.join(self.data_root, data_info['pts_semantic_mask_path']))\n            num_point_all.append(label.shape[0])\n            class_count, _ = np.histogram(label, range(num_classes + 2))\n            label_weight += class_count\n\n        # repeat scene_idx for num_scene_point // num_sample_point times\n        sample_prob = np.array(num_point_all) / float(np.sum(num_point_all))\n        num_iter = int(np.sum(num_point_all) / float(self.num_points))\n        scene_idxs = []\n        for idx in range(len(self.data_infos)):\n            scene_idxs.extend([idx] * int(round(sample_prob[idx] * num_iter)))\n        scene_idxs = np.array(scene_idxs).astype(np.int32)\n\n        # calculate label weight, adopted from PointNet++\n        label_weight = label_weight[:-1].astype(np.float32)\n        label_weight = label_weight / label_weight.sum()\n        label_weight = self.label_weight_func(label_weight).astype(np.float32)\n\n        return scene_idxs, label_weight\n", "path": "tools/data_converter/scannet_data_utils.py"}]}