Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dcnv3
Commits
df3c64a9
Commit
df3c64a9
authored
Apr 17, 2023
by
zhiqi-li
Browse files
support occupancy prediction
parent
bdd98bcb
Changes
160
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
3618 additions
and
0 deletions
+3618
-0
autonomous_driving/occupancy_prediction/tools/create_data.py
autonomous_driving/occupancy_prediction/tools/create_data.py
+206
-0
autonomous_driving/occupancy_prediction/tools/data_converter/__init__.py
...ing/occupancy_prediction/tools/data_converter/__init__.py
+1
-0
autonomous_driving/occupancy_prediction/tools/data_converter/create_gt_database.py
...ncy_prediction/tools/data_converter/create_gt_database.py
+338
-0
autonomous_driving/occupancy_prediction/tools/data_converter/nuscenes_converter.py
...ncy_prediction/tools/data_converter/nuscenes_converter.py
+672
-0
autonomous_driving/occupancy_prediction/tools/data_converter/nuscenes_occ_converter.py
...prediction/tools/data_converter/nuscenes_occ_converter.py
+700
-0
autonomous_driving/occupancy_prediction/tools/dist_test.sh
autonomous_driving/occupancy_prediction/tools/dist_test.sh
+10
-0
autonomous_driving/occupancy_prediction/tools/dist_train.sh
autonomous_driving/occupancy_prediction/tools/dist_train.sh
+20
-0
autonomous_driving/occupancy_prediction/tools/fp16/dist_train.sh
...ous_driving/occupancy_prediction/tools/fp16/dist_train.sh
+9
-0
autonomous_driving/occupancy_prediction/tools/fp16/train.py
autonomous_driving/occupancy_prediction/tools/fp16/train.py
+271
-0
autonomous_driving/occupancy_prediction/tools/misc/browse_dataset.py
...driving/occupancy_prediction/tools/misc/browse_dataset.py
+240
-0
autonomous_driving/occupancy_prediction/tools/misc/fuse_conv_bn.py
...s_driving/occupancy_prediction/tools/misc/fuse_conv_bn.py
+67
-0
autonomous_driving/occupancy_prediction/tools/misc/print_config.py
...s_driving/occupancy_prediction/tools/misc/print_config.py
+26
-0
autonomous_driving/occupancy_prediction/tools/misc/visualize_results.py
...ving/occupancy_prediction/tools/misc/visualize_results.py
+49
-0
autonomous_driving/occupancy_prediction/tools/model_converters/convert_votenet_checkpoints.py
...ion/tools/model_converters/convert_votenet_checkpoints.py
+152
-0
autonomous_driving/occupancy_prediction/tools/model_converters/publish_model.py
...upancy_prediction/tools/model_converters/publish_model.py
+35
-0
autonomous_driving/occupancy_prediction/tools/model_converters/regnet2mmdet.py
...cupancy_prediction/tools/model_converters/regnet2mmdet.py
+89
-0
autonomous_driving/occupancy_prediction/tools/slurm_train.sh
autonomous_driving/occupancy_prediction/tools/slurm_train.sh
+24
-0
autonomous_driving/occupancy_prediction/tools/test.py
autonomous_driving/occupancy_prediction/tools/test.py
+266
-0
autonomous_driving/occupancy_prediction/tools/train.py
autonomous_driving/occupancy_prediction/tools/train.py
+260
-0
autonomous_driving/occupancy_prediction/utils/vis.py
autonomous_driving/occupancy_prediction/utils/vis.py
+183
-0
No files found.
autonomous_driving/occupancy_prediction/tools/create_data.py
0 → 100644
View file @
df3c64a9
# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------
# Modified by Xiaoyu Tian
# ---------------------------------------------
from
data_converter.create_gt_database
import
create_groundtruth_database
from
data_converter
import
nuscenes_converter
as
nuscenes_converter
from
data_converter
import
nuscenes_occ_converter
as
occ_converter
import
argparse
from
os
import
path
as
osp
import
sys
sys
.
path
.
append
(
'.'
)
def
nuscenes_data_prep
(
root_path
,
can_bus_root_path
,
info_prefix
,
version
,
dataset_name
,
out_dir
,
max_sweeps
=
10
):
"""Prepare data related to nuScenes dataset.
Related data consists of '.pkl' files recording basic infos,
2D annotations and groundtruth database.
Args:
root_path (str): Path of dataset root.
info_prefix (str): The prefix of info filenames.
version (str): Dataset version.
dataset_name (str): The dataset class name.
out_dir (str): Output directory of the groundtruth database info.
max_sweeps (int): Number of input consecutive frames. Default: 10
"""
nuscenes_converter
.
create_nuscenes_infos
(
root_path
,
out_dir
,
can_bus_root_path
,
info_prefix
,
version
=
version
,
max_sweeps
=
max_sweeps
)
if
version
==
'v1.0-test'
:
info_test_path
=
osp
.
join
(
out_dir
,
f
'
{
info_prefix
}
_infos_temporal_test.pkl'
)
nuscenes_converter
.
export_2d_annotation
(
root_path
,
info_test_path
,
version
=
version
)
else
:
info_train_path
=
osp
.
join
(
out_dir
,
f
'
{
info_prefix
}
_infos_temporal_train.pkl'
)
info_val_path
=
osp
.
join
(
out_dir
,
f
'
{
info_prefix
}
_infos_temporal_val.pkl'
)
nuscenes_converter
.
export_2d_annotation
(
root_path
,
info_train_path
,
version
=
version
)
nuscenes_converter
.
export_2d_annotation
(
root_path
,
info_val_path
,
version
=
version
)
# create_groundtruth_database(dataset_name, root_path, info_prefix,
# f'{out_dir}/{info_prefix}_infos_train.pkl')
def
occ_nuscenes_data_prep
(
root_path
,
occ_path
,
can_bus_root_path
,
info_prefix
,
version
,
dataset_name
,
out_dir
,
max_sweeps
=
10
):
"""Prepare occ data related to nuScenes dataset.
Related data consists of '.pkl' files recording basic infos,
2D annotations and groundtruth database.
Args:
root_path (str): Path of dataset root.
info_prefix (str): The prefix of info filenames.
version (str): Dataset version.
dataset_name (str): The dataset class name.
out_dir (str): Output directory of the groundtruth database info.
max_sweeps (int): Number of input consecutive frames. Default: 10
"""
occ_converter
.
create_nuscenes_occ_infos
(
root_path
,
occ_path
,
out_dir
,
can_bus_root_path
,
info_prefix
,
version
=
version
,
max_sweeps
=
max_sweeps
)
# if version == 'v1.0-test':
# info_test_path = osp.join(
# out_dir, f'{info_prefix}_infos_temporal_test.pkl')
# nuscenes_converter.export_2d_annotation(
# root_path, info_test_path, version=version)
# else:
# info_train_path = osp.join(
# out_dir, f'{info_prefix}_infos_temporal_train.pkl')
# info_val_path = osp.join(
# out_dir, f'{info_prefix}_infos_temporal_val.pkl')
# nuscenes_converter.export_2d_annotation(
# root_path, info_train_path, version=version)
# nuscenes_converter.export_2d_annotation(
# root_path, info_val_path, version=version)
# create_groundtruth_database(dataset_name, root_path, info_prefix,
# f'{out_dir}/{info_prefix}_infos_train.pkl')
parser
=
argparse
.
ArgumentParser
(
description
=
'Data converter arg parser'
)
parser
.
add_argument
(
'dataset'
,
metavar
=
'kitti'
,
help
=
'name of the dataset'
)
parser
.
add_argument
(
'--root-path'
,
type
=
str
,
default
=
'./data/kitti'
,
help
=
'specify the root path of dataset'
)
parser
.
add_argument
(
'--occ-path'
,
type
=
str
,
default
=
'./data/occ'
,
help
=
'specify the occ path of dataset'
)
parser
.
add_argument
(
'--canbus'
,
type
=
str
,
default
=
'./data'
,
help
=
'specify the root path of nuScenes canbus'
)
parser
.
add_argument
(
'--version'
,
type
=
str
,
default
=
'v1.0'
,
required
=
False
,
help
=
'specify the dataset version, no need for kitti'
)
parser
.
add_argument
(
'--max-sweeps'
,
type
=
int
,
default
=
10
,
required
=
False
,
help
=
'specify sweeps of lidar per example'
)
parser
.
add_argument
(
'--out-dir'
,
type
=
str
,
default
=
'./data/kitti'
,
required
=
'False'
,
help
=
'name of info pkl'
)
parser
.
add_argument
(
'--extra-tag'
,
type
=
str
,
default
=
'kitti'
)
parser
.
add_argument
(
'--workers'
,
type
=
int
,
default
=
4
,
help
=
'number of threads to be used'
)
args
=
parser
.
parse_args
()
if
__name__
==
'__main__'
:
if
args
.
dataset
==
'nuscenes'
and
args
.
version
!=
'v1.0-mini'
:
train_version
=
f
'
{
args
.
version
}
-trainval'
nuscenes_data_prep
(
root_path
=
args
.
root_path
,
can_bus_root_path
=
args
.
canbus
,
info_prefix
=
args
.
extra_tag
,
version
=
train_version
,
dataset_name
=
'NuScenesDataset'
,
out_dir
=
args
.
out_dir
,
max_sweeps
=
args
.
max_sweeps
)
test_version
=
f
'
{
args
.
version
}
-test'
nuscenes_data_prep
(
root_path
=
args
.
root_path
,
can_bus_root_path
=
args
.
canbus
,
info_prefix
=
args
.
extra_tag
,
version
=
test_version
,
dataset_name
=
'NuScenesDataset'
,
out_dir
=
args
.
out_dir
,
max_sweeps
=
args
.
max_sweeps
)
elif
args
.
dataset
==
'nuscenes'
and
args
.
version
==
'v1.0-mini'
:
train_version
=
f
'
{
args
.
version
}
'
nuscenes_data_prep
(
root_path
=
args
.
root_path
,
can_bus_root_path
=
args
.
canbus
,
info_prefix
=
args
.
extra_tag
,
version
=
train_version
,
dataset_name
=
'NuScenesDataset'
,
out_dir
=
args
.
out_dir
,
max_sweeps
=
args
.
max_sweeps
)
elif
args
.
dataset
==
'occ'
and
args
.
version
!=
'v1.0-mini'
:
train_version
=
f
'
{
args
.
version
}
'
occ_nuscenes_data_prep
(
root_path
=
args
.
root_path
,
occ_path
=
args
.
occ_path
,
can_bus_root_path
=
args
.
canbus
,
info_prefix
=
args
.
extra_tag
,
version
=
train_version
,
dataset_name
=
'NuScenesDataset'
,
out_dir
=
args
.
out_dir
,
max_sweeps
=
args
.
max_sweeps
)
# test_version = f'{args.version}-test'
# nuscenes_data_prep(
# root_path=args.root_path,
# can_bus_root_path=args.canbus,
# info_prefix=args.extra_tag,
# version=test_version,
# dataset_name='NuScenesDataset',
# out_dir=args.out_dir,
# max_sweeps=args.max_sweeps)
elif
args
.
dataset
==
'occ'
and
args
.
version
==
'v1.0-mini'
:
train_version
=
f
'
{
args
.
version
}
'
occ_nuscenes_data_prep
(
root_path
=
args
.
root_path
,
occ_path
=
args
.
occ_path
,
can_bus_root_path
=
args
.
canbus
,
info_prefix
=
args
.
extra_tag
,
version
=
train_version
,
dataset_name
=
'NuScenesDataset'
,
out_dir
=
args
.
out_dir
,
max_sweeps
=
args
.
max_sweeps
)
autonomous_driving/occupancy_prediction/tools/data_converter/__init__.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
autonomous_driving/occupancy_prediction/tools/data_converter/create_gt_database.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
mmcv
import
numpy
as
np
import
pickle
from
mmcv
import
track_iter_progress
from
mmcv.ops
import
roi_align
from
os
import
path
as
osp
from
pycocotools
import
mask
as
maskUtils
from
pycocotools.coco
import
COCO
from
mmdet3d.core.bbox
import
box_np_ops
as
box_np_ops
from
mmdet3d.datasets
import
build_dataset
from
mmdet.core.evaluation.bbox_overlaps
import
bbox_overlaps
def
_poly2mask
(
mask_ann
,
img_h
,
img_w
):
if
isinstance
(
mask_ann
,
list
):
# polygon -- a single object might consist of multiple parts
# we merge all parts into one mask rle code
rles
=
maskUtils
.
frPyObjects
(
mask_ann
,
img_h
,
img_w
)
rle
=
maskUtils
.
merge
(
rles
)
elif
isinstance
(
mask_ann
[
'counts'
],
list
):
# uncompressed RLE
rle
=
maskUtils
.
frPyObjects
(
mask_ann
,
img_h
,
img_w
)
else
:
# rle
rle
=
mask_ann
mask
=
maskUtils
.
decode
(
rle
)
return
mask
def
_parse_coco_ann_info
(
ann_info
):
gt_bboxes
=
[]
gt_labels
=
[]
gt_bboxes_ignore
=
[]
gt_masks_ann
=
[]
for
i
,
ann
in
enumerate
(
ann_info
):
if
ann
.
get
(
'ignore'
,
False
):
continue
x1
,
y1
,
w
,
h
=
ann
[
'bbox'
]
if
ann
[
'area'
]
<=
0
:
continue
bbox
=
[
x1
,
y1
,
x1
+
w
,
y1
+
h
]
if
ann
.
get
(
'iscrowd'
,
False
):
gt_bboxes_ignore
.
append
(
bbox
)
else
:
gt_bboxes
.
append
(
bbox
)
gt_masks_ann
.
append
(
ann
[
'segmentation'
])
if
gt_bboxes
:
gt_bboxes
=
np
.
array
(
gt_bboxes
,
dtype
=
np
.
float32
)
gt_labels
=
np
.
array
(
gt_labels
,
dtype
=
np
.
int64
)
else
:
gt_bboxes
=
np
.
zeros
((
0
,
4
),
dtype
=
np
.
float32
)
gt_labels
=
np
.
array
([],
dtype
=
np
.
int64
)
if
gt_bboxes_ignore
:
gt_bboxes_ignore
=
np
.
array
(
gt_bboxes_ignore
,
dtype
=
np
.
float32
)
else
:
gt_bboxes_ignore
=
np
.
zeros
((
0
,
4
),
dtype
=
np
.
float32
)
ann
=
dict
(
bboxes
=
gt_bboxes
,
bboxes_ignore
=
gt_bboxes_ignore
,
masks
=
gt_masks_ann
)
return
ann
def
crop_image_patch_v2
(
pos_proposals
,
pos_assigned_gt_inds
,
gt_masks
):
import
torch
from
torch.nn.modules.utils
import
_pair
device
=
pos_proposals
.
device
num_pos
=
pos_proposals
.
size
(
0
)
fake_inds
=
(
torch
.
arange
(
num_pos
,
device
=
device
).
to
(
dtype
=
pos_proposals
.
dtype
)[:,
None
])
rois
=
torch
.
cat
([
fake_inds
,
pos_proposals
],
dim
=
1
)
# Nx5
mask_size
=
_pair
(
28
)
rois
=
rois
.
to
(
device
=
device
)
gt_masks_th
=
(
torch
.
from_numpy
(
gt_masks
).
to
(
device
).
index_select
(
0
,
pos_assigned_gt_inds
).
to
(
dtype
=
rois
.
dtype
))
# Use RoIAlign could apparently accelerate the training (~0.1s/iter)
targets
=
(
roi_align
(
gt_masks_th
,
rois
,
mask_size
[::
-
1
],
1.0
,
0
,
True
).
squeeze
(
1
))
return
targets
def
crop_image_patch
(
pos_proposals
,
gt_masks
,
pos_assigned_gt_inds
,
org_img
):
num_pos
=
pos_proposals
.
shape
[
0
]
masks
=
[]
img_patches
=
[]
for
i
in
range
(
num_pos
):
gt_mask
=
gt_masks
[
pos_assigned_gt_inds
[
i
]]
bbox
=
pos_proposals
[
i
,
:].
astype
(
np
.
int32
)
x1
,
y1
,
x2
,
y2
=
bbox
w
=
np
.
maximum
(
x2
-
x1
+
1
,
1
)
h
=
np
.
maximum
(
y2
-
y1
+
1
,
1
)
mask_patch
=
gt_mask
[
y1
:
y1
+
h
,
x1
:
x1
+
w
]
masked_img
=
gt_mask
[...,
None
]
*
org_img
img_patch
=
masked_img
[
y1
:
y1
+
h
,
x1
:
x1
+
w
]
img_patches
.
append
(
img_patch
)
masks
.
append
(
mask_patch
)
return
img_patches
,
masks
def
create_groundtruth_database
(
dataset_class_name
,
data_path
,
info_prefix
,
info_path
=
None
,
mask_anno_path
=
None
,
used_classes
=
None
,
database_save_path
=
None
,
db_info_save_path
=
None
,
relative_path
=
True
,
add_rgb
=
False
,
lidar_only
=
False
,
bev_only
=
False
,
coors_range
=
None
,
with_mask
=
False
):
"""Given the raw data, generate the ground truth database.
Args:
dataset_class_name (str): Name of the input dataset.
data_path (str): Path of the data.
info_prefix (str): Prefix of the info file.
info_path (str): Path of the info file.
Default: None.
mask_anno_path (str): Path of the mask_anno.
Default: None.
used_classes (list[str]): Classes have been used.
Default: None.
database_save_path (str): Path to save database.
Default: None.
db_info_save_path (str): Path to save db_info.
Default: None.
relative_path (bool): Whether to use relative path.
Default: True.
with_mask (bool): Whether to use mask.
Default: False.
"""
print
(
f
'Create GT Database of
{
dataset_class_name
}
'
)
dataset_cfg
=
dict
(
type
=
dataset_class_name
,
data_root
=
data_path
,
ann_file
=
info_path
)
if
dataset_class_name
==
'KittiDataset'
:
file_client_args
=
dict
(
backend
=
'disk'
)
dataset_cfg
.
update
(
test_mode
=
False
,
split
=
'training'
,
modality
=
dict
(
use_lidar
=
True
,
use_depth
=
False
,
use_lidar_intensity
=
True
,
use_camera
=
with_mask
,
),
pipeline
=
[
dict
(
type
=
'LoadPointsFromFile'
,
coord_type
=
'LIDAR'
,
load_dim
=
4
,
use_dim
=
4
,
file_client_args
=
file_client_args
),
dict
(
type
=
'LoadAnnotations3D'
,
with_bbox_3d
=
True
,
with_label_3d
=
True
,
file_client_args
=
file_client_args
)
])
elif
dataset_class_name
==
'NuScenesDataset'
:
dataset_cfg
.
update
(
use_valid_flag
=
True
,
pipeline
=
[
dict
(
type
=
'LoadPointsFromFile'
,
coord_type
=
'LIDAR'
,
load_dim
=
5
,
use_dim
=
5
),
dict
(
type
=
'LoadPointsFromMultiSweeps'
,
sweeps_num
=
10
,
use_dim
=
[
0
,
1
,
2
,
3
,
4
],
pad_empty_sweeps
=
True
,
remove_close
=
True
),
dict
(
type
=
'LoadAnnotations3D'
,
with_bbox_3d
=
True
,
with_label_3d
=
True
)
])
elif
dataset_class_name
==
'WaymoDataset'
:
file_client_args
=
dict
(
backend
=
'disk'
)
dataset_cfg
.
update
(
test_mode
=
False
,
split
=
'training'
,
modality
=
dict
(
use_lidar
=
True
,
use_depth
=
False
,
use_lidar_intensity
=
True
,
use_camera
=
False
,
),
pipeline
=
[
dict
(
type
=
'LoadPointsFromFile'
,
coord_type
=
'LIDAR'
,
load_dim
=
6
,
use_dim
=
5
,
file_client_args
=
file_client_args
),
dict
(
type
=
'LoadAnnotations3D'
,
with_bbox_3d
=
True
,
with_label_3d
=
True
,
file_client_args
=
file_client_args
)
])
dataset
=
build_dataset
(
dataset_cfg
)
if
database_save_path
is
None
:
database_save_path
=
osp
.
join
(
data_path
,
f
'
{
info_prefix
}
_gt_database'
)
if
db_info_save_path
is
None
:
db_info_save_path
=
osp
.
join
(
data_path
,
f
'
{
info_prefix
}
_dbinfos_train.pkl'
)
mmcv
.
mkdir_or_exist
(
database_save_path
)
all_db_infos
=
dict
()
if
with_mask
:
coco
=
COCO
(
osp
.
join
(
data_path
,
mask_anno_path
))
imgIds
=
coco
.
getImgIds
()
file2id
=
dict
()
for
i
in
imgIds
:
info
=
coco
.
loadImgs
([
i
])[
0
]
file2id
.
update
({
info
[
'file_name'
]:
i
})
group_counter
=
0
for
j
in
track_iter_progress
(
list
(
range
(
len
(
dataset
)))):
input_dict
=
dataset
.
get_data_info
(
j
)
dataset
.
pre_pipeline
(
input_dict
)
example
=
dataset
.
pipeline
(
input_dict
)
annos
=
example
[
'ann_info'
]
image_idx
=
example
[
'sample_idx'
]
points
=
example
[
'points'
].
tensor
.
numpy
()
gt_boxes_3d
=
annos
[
'gt_bboxes_3d'
].
tensor
.
numpy
()
names
=
annos
[
'gt_names'
]
group_dict
=
dict
()
if
'group_ids'
in
annos
:
group_ids
=
annos
[
'group_ids'
]
else
:
group_ids
=
np
.
arange
(
gt_boxes_3d
.
shape
[
0
],
dtype
=
np
.
int64
)
difficulty
=
np
.
zeros
(
gt_boxes_3d
.
shape
[
0
],
dtype
=
np
.
int32
)
if
'difficulty'
in
annos
:
difficulty
=
annos
[
'difficulty'
]
num_obj
=
gt_boxes_3d
.
shape
[
0
]
point_indices
=
box_np_ops
.
points_in_rbbox
(
points
,
gt_boxes_3d
)
if
with_mask
:
# prepare masks
gt_boxes
=
annos
[
'gt_bboxes'
]
img_path
=
osp
.
split
(
example
[
'img_info'
][
'filename'
])[
-
1
]
if
img_path
not
in
file2id
.
keys
():
print
(
f
'skip image
{
img_path
}
for empty mask'
)
continue
img_id
=
file2id
[
img_path
]
kins_annIds
=
coco
.
getAnnIds
(
imgIds
=
img_id
)
kins_raw_info
=
coco
.
loadAnns
(
kins_annIds
)
kins_ann_info
=
_parse_coco_ann_info
(
kins_raw_info
)
h
,
w
=
annos
[
'img_shape'
][:
2
]
gt_masks
=
[
_poly2mask
(
mask
,
h
,
w
)
for
mask
in
kins_ann_info
[
'masks'
]
]
# get mask inds based on iou mapping
bbox_iou
=
bbox_overlaps
(
kins_ann_info
[
'bboxes'
],
gt_boxes
)
mask_inds
=
bbox_iou
.
argmax
(
axis
=
0
)
valid_inds
=
(
bbox_iou
.
max
(
axis
=
0
)
>
0.5
)
# mask the image
# use more precise crop when it is ready
# object_img_patches = np.ascontiguousarray(
# np.stack(object_img_patches, axis=0).transpose(0, 3, 1, 2))
# crop image patches using roi_align
# object_img_patches = crop_image_patch_v2(
# torch.Tensor(gt_boxes),
# torch.Tensor(mask_inds).long(), object_img_patches)
object_img_patches
,
object_masks
=
crop_image_patch
(
gt_boxes
,
gt_masks
,
mask_inds
,
annos
[
'img'
])
for
i
in
range
(
num_obj
):
filename
=
f
'
{
image_idx
}
_
{
names
[
i
]
}
_
{
i
}
.bin'
abs_filepath
=
osp
.
join
(
database_save_path
,
filename
)
rel_filepath
=
osp
.
join
(
f
'
{
info_prefix
}
_gt_database'
,
filename
)
# save point clouds and image patches for each object
gt_points
=
points
[
point_indices
[:,
i
]]
gt_points
[:,
:
3
]
-=
gt_boxes_3d
[
i
,
:
3
]
if
with_mask
:
if
object_masks
[
i
].
sum
()
==
0
or
not
valid_inds
[
i
]:
# Skip object for empty or invalid mask
continue
img_patch_path
=
abs_filepath
+
'.png'
mask_patch_path
=
abs_filepath
+
'.mask.png'
mmcv
.
imwrite
(
object_img_patches
[
i
],
img_patch_path
)
mmcv
.
imwrite
(
object_masks
[
i
],
mask_patch_path
)
with
open
(
abs_filepath
,
'w'
)
as
f
:
gt_points
.
tofile
(
f
)
if
(
used_classes
is
None
)
or
names
[
i
]
in
used_classes
:
db_info
=
{
'name'
:
names
[
i
],
'path'
:
rel_filepath
,
'image_idx'
:
image_idx
,
'gt_idx'
:
i
,
'box3d_lidar'
:
gt_boxes_3d
[
i
],
'num_points_in_gt'
:
gt_points
.
shape
[
0
],
'difficulty'
:
difficulty
[
i
],
}
local_group_id
=
group_ids
[
i
]
# if local_group_id >= 0:
if
local_group_id
not
in
group_dict
:
group_dict
[
local_group_id
]
=
group_counter
group_counter
+=
1
db_info
[
'group_id'
]
=
group_dict
[
local_group_id
]
if
'score'
in
annos
:
db_info
[
'score'
]
=
annos
[
'score'
][
i
]
if
with_mask
:
db_info
.
update
({
'box2d_camera'
:
gt_boxes
[
i
]})
if
names
[
i
]
in
all_db_infos
:
all_db_infos
[
names
[
i
]].
append
(
db_info
)
else
:
all_db_infos
[
names
[
i
]]
=
[
db_info
]
for
k
,
v
in
all_db_infos
.
items
():
print
(
f
'load
{
len
(
v
)
}
{
k
}
database infos'
)
with
open
(
db_info_save_path
,
'wb'
)
as
f
:
pickle
.
dump
(
all_db_infos
,
f
)
autonomous_driving/occupancy_prediction/tools/data_converter/nuscenes_converter.py
0 → 100644
View file @
df3c64a9
# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------
# Modified by Xiaoyu Tian
# ---------------------------------------------
import
mmcv
import
numpy
as
np
import
os
from
collections
import
OrderedDict
from
nuscenes.nuscenes
import
NuScenes
from
nuscenes.utils.geometry_utils
import
view_points
from
os
import
path
as
osp
from
pyquaternion
import
Quaternion
from
shapely.geometry
import
MultiPoint
,
box
from
typing
import
List
,
Tuple
,
Union
from
mmdet3d.core.bbox.box_np_ops
import
points_cam2img
from
mmdet3d.datasets
import
NuScenesDataset
nus_categories
=
(
'car'
,
'truck'
,
'trailer'
,
'bus'
,
'construction_vehicle'
,
'bicycle'
,
'motorcycle'
,
'pedestrian'
,
'traffic_cone'
,
'barrier'
)
nus_attributes
=
(
'cycle.with_rider'
,
'cycle.without_rider'
,
'pedestrian.moving'
,
'pedestrian.standing'
,
'pedestrian.sitting_lying_down'
,
'vehicle.moving'
,
'vehicle.parked'
,
'vehicle.stopped'
,
'None'
)
def
create_nuscenes_infos
(
root_path
,
out_path
,
can_bus_root_path
,
info_prefix
,
version
=
'v1.0-trainval'
,
max_sweeps
=
10
):
"""Create info file of nuscene dataset.
Given the raw data, generate its related info file in pkl format.
Args:
root_path (str): Path of the data root.
info_prefix (str): Prefix of the info file to be generated.
version (str): Version of the data.
Default: 'v1.0-trainval'
max_sweeps (int): Max number of sweeps.
Default: 10
"""
from
nuscenes.nuscenes
import
NuScenes
from
nuscenes.can_bus.can_bus_api
import
NuScenesCanBus
print
(
version
,
root_path
)
nusc
=
NuScenes
(
version
=
version
,
dataroot
=
root_path
,
verbose
=
True
)
nusc_can_bus
=
NuScenesCanBus
(
dataroot
=
can_bus_root_path
)
from
nuscenes.utils
import
splits
available_vers
=
[
'v1.0-trainval'
,
'v1.0-test'
,
'v1.0-mini'
]
assert
version
in
available_vers
if
version
==
'v1.0-trainval'
:
train_scenes
=
splits
.
train
val_scenes
=
splits
.
val
elif
version
==
'v1.0-test'
:
train_scenes
=
splits
.
test
val_scenes
=
[]
elif
version
==
'v1.0-mini'
:
train_scenes
=
splits
.
mini_train
val_scenes
=
splits
.
mini_val
else
:
raise
ValueError
(
'unknown'
)
# filter existing scenes.
available_scenes
=
get_available_scenes
(
nusc
)
available_scene_names
=
[
s
[
'name'
]
for
s
in
available_scenes
]
train_scenes
=
list
(
filter
(
lambda
x
:
x
in
available_scene_names
,
train_scenes
))
val_scenes
=
list
(
filter
(
lambda
x
:
x
in
available_scene_names
,
val_scenes
))
train_scenes
=
set
([
available_scenes
[
available_scene_names
.
index
(
s
)][
'token'
]
for
s
in
train_scenes
])
val_scenes
=
set
([
available_scenes
[
available_scene_names
.
index
(
s
)][
'token'
]
for
s
in
val_scenes
])
test
=
'test'
in
version
if
test
:
print
(
'test scene: {}'
.
format
(
len
(
train_scenes
)))
else
:
print
(
'train scene: {}, val scene: {}'
.
format
(
len
(
train_scenes
),
len
(
val_scenes
)))
train_nusc_infos
,
val_nusc_infos
=
_fill_trainval_infos
(
nusc
,
nusc_can_bus
,
train_scenes
,
val_scenes
,
test
,
max_sweeps
=
max_sweeps
)
metadata
=
dict
(
version
=
version
)
if
test
:
print
(
'test sample: {}'
.
format
(
len
(
train_nusc_infos
)))
data
=
dict
(
infos
=
train_nusc_infos
,
metadata
=
metadata
)
info_path
=
osp
.
join
(
out_path
,
'{}_infos_temporal_test.pkl'
.
format
(
info_prefix
))
mmcv
.
dump
(
data
,
info_path
)
else
:
print
(
'train sample: {}, val sample: {}'
.
format
(
len
(
train_nusc_infos
),
len
(
val_nusc_infos
)))
data
=
dict
(
infos
=
train_nusc_infos
,
metadata
=
metadata
)
info_path
=
osp
.
join
(
out_path
,
'{}_infos_temporal_train.pkl'
.
format
(
info_prefix
))
mmcv
.
dump
(
data
,
info_path
)
data
[
'infos'
]
=
val_nusc_infos
info_val_path
=
osp
.
join
(
out_path
,
'{}_infos_temporal_val.pkl'
.
format
(
info_prefix
))
mmcv
.
dump
(
data
,
info_val_path
)
def
get_available_scenes
(
nusc
):
"""Get available scenes from the input nuscenes class.
Given the raw data, get the information of available scenes for
further info generation.
Args:
nusc (class): Dataset class in the nuScenes dataset.
Returns:
available_scenes (list[dict]): List of basic information for the
available scenes.
"""
available_scenes
=
[]
print
(
'total scene num: {}'
.
format
(
len
(
nusc
.
scene
)))
for
scene
in
nusc
.
scene
:
scene_token
=
scene
[
'token'
]
scene_rec
=
nusc
.
get
(
'scene'
,
scene_token
)
sample_rec
=
nusc
.
get
(
'sample'
,
scene_rec
[
'first_sample_token'
])
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample_rec
[
'data'
][
'LIDAR_TOP'
])
has_more_frames
=
True
scene_not_exist
=
False
while
has_more_frames
:
lidar_path
,
boxes
,
_
=
nusc
.
get_sample_data
(
sd_rec
[
'token'
])
lidar_path
=
str
(
lidar_path
)
if
os
.
getcwd
()
in
lidar_path
:
# path from lyftdataset is absolute path
lidar_path
=
lidar_path
.
split
(
f
'
{
os
.
getcwd
()
}
/'
)[
-
1
]
# relative path
if
not
mmcv
.
is_filepath
(
lidar_path
):
scene_not_exist
=
True
break
else
:
break
if
scene_not_exist
:
continue
available_scenes
.
append
(
scene
)
print
(
'exist scene num: {}'
.
format
(
len
(
available_scenes
)))
return
available_scenes
def
_get_can_bus_info
(
nusc
,
nusc_can_bus
,
sample
):
scene_name
=
nusc
.
get
(
'scene'
,
sample
[
'scene_token'
])[
'name'
]
sample_timestamp
=
sample
[
'timestamp'
]
try
:
pose_list
=
nusc_can_bus
.
get_messages
(
scene_name
,
'pose'
)
except
:
return
np
.
zeros
(
18
)
# server scenes do not have can bus information.
can_bus
=
[]
# during each scene, the first timestamp of can_bus may be large than the first sample's timestamp
last_pose
=
pose_list
[
0
]
for
i
,
pose
in
enumerate
(
pose_list
):
if
pose
[
'utime'
]
>
sample_timestamp
:
break
last_pose
=
pose
_
=
last_pose
.
pop
(
'utime'
)
# useless
pos
=
last_pose
.
pop
(
'pos'
)
rotation
=
last_pose
.
pop
(
'orientation'
)
can_bus
.
extend
(
pos
)
can_bus
.
extend
(
rotation
)
for
key
in
last_pose
.
keys
():
can_bus
.
extend
(
pose
[
key
])
# 16 elements
can_bus
.
extend
([
0.
,
0.
])
return
np
.
array
(
can_bus
)
def
_fill_trainval_infos
(
nusc
,
nusc_can_bus
,
train_scenes
,
val_scenes
,
test
=
False
,
max_sweeps
=
10
):
"""Generate the train/val infos from the raw data.
Args:
nusc (:obj:`NuScenes`): Dataset class in the nuScenes dataset.
train_scenes (list[str]): Basic information of training scenes.
val_scenes (list[str]): Basic information of validation scenes.
test (bool): Whether use the test mode. In the test mode, no
annotations can be accessed. Default: False.
max_sweeps (int): Max number of sweeps. Default: 10.
Returns:
tuple[list[dict]]: Information of training set and validation set
that will be saved to the info file.
"""
train_nusc_infos
=
[]
val_nusc_infos
=
[]
frame_idx
=
0
for
sample
in
mmcv
.
track_iter_progress
(
nusc
.
sample
):
lidar_token
=
sample
[
'data'
][
'LIDAR_TOP'
]
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample
[
'data'
][
'LIDAR_TOP'
])
cs_record
=
nusc
.
get
(
'calibrated_sensor'
,
sd_rec
[
'calibrated_sensor_token'
])
pose_record
=
nusc
.
get
(
'ego_pose'
,
sd_rec
[
'ego_pose_token'
])
lidar_path
,
boxes
,
_
=
nusc
.
get_sample_data
(
lidar_token
)
mmcv
.
check_file_exist
(
lidar_path
)
can_bus
=
_get_can_bus_info
(
nusc
,
nusc_can_bus
,
sample
)
##
info
=
{
'lidar_path'
:
lidar_path
,
'token'
:
sample
[
'token'
],
'prev'
:
sample
[
'prev'
],
'next'
:
sample
[
'next'
],
'can_bus'
:
can_bus
,
'frame_idx'
:
frame_idx
,
# temporal related info
'sweeps'
:
[],
'cams'
:
dict
(),
'scene_token'
:
sample
[
'scene_token'
],
# temporal related info
'lidar2ego_translation'
:
cs_record
[
'translation'
],
'lidar2ego_rotation'
:
cs_record
[
'rotation'
],
'ego2global_translation'
:
pose_record
[
'translation'
],
'ego2global_rotation'
:
pose_record
[
'rotation'
],
'timestamp'
:
sample
[
'timestamp'
],
}
if
sample
[
'next'
]
==
''
:
frame_idx
=
0
else
:
frame_idx
+=
1
l2e_r
=
info
[
'lidar2ego_rotation'
]
l2e_t
=
info
[
'lidar2ego_translation'
]
e2g_r
=
info
[
'ego2global_rotation'
]
e2g_t
=
info
[
'ego2global_translation'
]
l2e_r_mat
=
Quaternion
(
l2e_r
).
rotation_matrix
e2g_r_mat
=
Quaternion
(
e2g_r
).
rotation_matrix
# obtain 6 image's information per frame
camera_types
=
[
'CAM_FRONT'
,
'CAM_FRONT_RIGHT'
,
'CAM_FRONT_LEFT'
,
'CAM_BACK'
,
'CAM_BACK_LEFT'
,
'CAM_BACK_RIGHT'
,
]
for
cam
in
camera_types
:
cam_token
=
sample
[
'data'
][
cam
]
cam_path
,
_
,
cam_intrinsic
=
nusc
.
get_sample_data
(
cam_token
)
cam_info
=
obtain_sensor2top
(
nusc
,
cam_token
,
l2e_t
,
l2e_r_mat
,
e2g_t
,
e2g_r_mat
,
cam
)
cam_info
.
update
(
cam_intrinsic
=
cam_intrinsic
)
info
[
'cams'
].
update
({
cam
:
cam_info
})
# obtain sweeps for a single key-frame
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample
[
'data'
][
'LIDAR_TOP'
])
sweeps
=
[]
while
len
(
sweeps
)
<
max_sweeps
:
if
not
sd_rec
[
'prev'
]
==
''
:
sweep
=
obtain_sensor2top
(
nusc
,
sd_rec
[
'prev'
],
l2e_t
,
l2e_r_mat
,
e2g_t
,
e2g_r_mat
,
'lidar'
)
sweeps
.
append
(
sweep
)
sd_rec
=
nusc
.
get
(
'sample_data'
,
sd_rec
[
'prev'
])
else
:
break
info
[
'sweeps'
]
=
sweeps
# obtain annotation
if
not
test
:
annotations
=
[
nusc
.
get
(
'sample_annotation'
,
token
)
for
token
in
sample
[
'anns'
]
]
locs
=
np
.
array
([
b
.
center
for
b
in
boxes
]).
reshape
(
-
1
,
3
)
dims
=
np
.
array
([
b
.
wlh
for
b
in
boxes
]).
reshape
(
-
1
,
3
)
rots
=
np
.
array
([
b
.
orientation
.
yaw_pitch_roll
[
0
]
for
b
in
boxes
]).
reshape
(
-
1
,
1
)
velocity
=
np
.
array
(
[
nusc
.
box_velocity
(
token
)[:
2
]
for
token
in
sample
[
'anns'
]])
valid_flag
=
np
.
array
(
[(
anno
[
'num_lidar_pts'
]
+
anno
[
'num_radar_pts'
])
>
0
for
anno
in
annotations
],
dtype
=
bool
).
reshape
(
-
1
)
# convert velo from global to lidar
for
i
in
range
(
len
(
boxes
)):
velo
=
np
.
array
([
*
velocity
[
i
],
0.0
])
velo
=
velo
@
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
velocity
[
i
]
=
velo
[:
2
]
names
=
[
b
.
name
for
b
in
boxes
]
for
i
in
range
(
len
(
names
)):
if
names
[
i
]
in
NuScenesDataset
.
NameMapping
:
names
[
i
]
=
NuScenesDataset
.
NameMapping
[
names
[
i
]]
names
=
np
.
array
(
names
)
# we need to convert rot to SECOND format.
gt_boxes
=
np
.
concatenate
([
locs
,
dims
,
-
rots
-
np
.
pi
/
2
],
axis
=
1
)
assert
len
(
gt_boxes
)
==
len
(
annotations
),
f
'
{
len
(
gt_boxes
)
}
,
{
len
(
annotations
)
}
'
info
[
'gt_boxes'
]
=
gt_boxes
info
[
'gt_names'
]
=
names
info
[
'gt_velocity'
]
=
velocity
.
reshape
(
-
1
,
2
)
info
[
'num_lidar_pts'
]
=
np
.
array
(
[
a
[
'num_lidar_pts'
]
for
a
in
annotations
])
info
[
'num_radar_pts'
]
=
np
.
array
(
[
a
[
'num_radar_pts'
]
for
a
in
annotations
])
info
[
'valid_flag'
]
=
valid_flag
if
sample
[
'scene_token'
]
in
train_scenes
:
train_nusc_infos
.
append
(
info
)
else
:
val_nusc_infos
.
append
(
info
)
return
train_nusc_infos
,
val_nusc_infos
def
obtain_sensor2top
(
nusc
,
sensor_token
,
l2e_t
,
l2e_r_mat
,
e2g_t
,
e2g_r_mat
,
sensor_type
=
'lidar'
):
"""Obtain the info with RT matric from general sensor to Top LiDAR.
Args:
nusc (class): Dataset class in the nuScenes dataset.
sensor_token (str): Sample data token corresponding to the
specific sensor type.
l2e_t (np.ndarray): Translation from lidar to ego in shape (1, 3).
l2e_r_mat (np.ndarray): Rotation matrix from lidar to ego
in shape (3, 3).
e2g_t (np.ndarray): Translation from ego to global in shape (1, 3).
e2g_r_mat (np.ndarray): Rotation matrix from ego to global
in shape (3, 3).
sensor_type (str): Sensor to calibrate. Default: 'lidar'.
Returns:
sweep (dict): Sweep information after transformation.
"""
sd_rec
=
nusc
.
get
(
'sample_data'
,
sensor_token
)
cs_record
=
nusc
.
get
(
'calibrated_sensor'
,
sd_rec
[
'calibrated_sensor_token'
])
pose_record
=
nusc
.
get
(
'ego_pose'
,
sd_rec
[
'ego_pose_token'
])
data_path
=
str
(
nusc
.
get_sample_data_path
(
sd_rec
[
'token'
]))
if
os
.
getcwd
()
in
data_path
:
# path from lyftdataset is absolute path
data_path
=
data_path
.
split
(
f
'
{
os
.
getcwd
()
}
/'
)[
-
1
]
# relative path
sweep
=
{
'data_path'
:
data_path
,
'type'
:
sensor_type
,
'sample_data_token'
:
sd_rec
[
'token'
],
'sensor2ego_translation'
:
cs_record
[
'translation'
],
'sensor2ego_rotation'
:
cs_record
[
'rotation'
],
'ego2global_translation'
:
pose_record
[
'translation'
],
'ego2global_rotation'
:
pose_record
[
'rotation'
],
'timestamp'
:
sd_rec
[
'timestamp'
]
}
l2e_r_s
=
sweep
[
'sensor2ego_rotation'
]
l2e_t_s
=
sweep
[
'sensor2ego_translation'
]
e2g_r_s
=
sweep
[
'ego2global_rotation'
]
e2g_t_s
=
sweep
[
'ego2global_translation'
]
# obtain the RT from sensor to Top LiDAR
# sweep->ego->global->ego'->lidar
l2e_r_s_mat
=
Quaternion
(
l2e_r_s
).
rotation_matrix
e2g_r_s_mat
=
Quaternion
(
e2g_r_s
).
rotation_matrix
R
=
(
l2e_r_s_mat
.
T
@
e2g_r_s_mat
.
T
)
@
(
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
)
T
=
(
l2e_t_s
@
e2g_r_s_mat
.
T
+
e2g_t_s
)
@
(
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
)
T
-=
e2g_t
@
(
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
)
+
l2e_t
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
sweep
[
'sensor2lidar_rotation'
]
=
R
.
T
# points @ R.T + T
sweep
[
'sensor2lidar_translation'
]
=
T
return
sweep
def
export_2d_annotation
(
root_path
,
info_path
,
version
,
mono3d
=
True
):
"""Export 2d annotation from the info file and raw data.
Args:
root_path (str): Root path of the raw data.
info_path (str): Path of the info file.
version (str): Dataset version.
mono3d (bool): Whether to export mono3d annotation. Default: True.
"""
# get bbox annotations for camera
camera_types
=
[
'CAM_FRONT'
,
'CAM_FRONT_RIGHT'
,
'CAM_FRONT_LEFT'
,
'CAM_BACK'
,
'CAM_BACK_LEFT'
,
'CAM_BACK_RIGHT'
,
]
nusc_infos
=
mmcv
.
load
(
info_path
)[
'infos'
]
nusc
=
NuScenes
(
version
=
version
,
dataroot
=
root_path
,
verbose
=
True
)
# info_2d_list = []
cat2Ids
=
[
dict
(
id
=
nus_categories
.
index
(
cat_name
),
name
=
cat_name
)
for
cat_name
in
nus_categories
]
coco_ann_id
=
0
coco_2d_dict
=
dict
(
annotations
=
[],
images
=
[],
categories
=
cat2Ids
)
for
info
in
mmcv
.
track_iter_progress
(
nusc_infos
):
for
cam
in
camera_types
:
cam_info
=
info
[
'cams'
][
cam
]
coco_infos
=
get_2d_boxes
(
nusc
,
cam_info
[
'sample_data_token'
],
visibilities
=
[
''
,
'1'
,
'2'
,
'3'
,
'4'
],
mono3d
=
mono3d
)
(
height
,
width
,
_
)
=
mmcv
.
imread
(
cam_info
[
'data_path'
]).
shape
coco_2d_dict
[
'images'
].
append
(
dict
(
file_name
=
cam_info
[
'data_path'
].
split
(
'data/nuscenes/'
)
[
-
1
],
id
=
cam_info
[
'sample_data_token'
],
token
=
info
[
'token'
],
cam2ego_rotation
=
cam_info
[
'sensor2ego_rotation'
],
cam2ego_translation
=
cam_info
[
'sensor2ego_translation'
],
ego2global_rotation
=
info
[
'ego2global_rotation'
],
ego2global_translation
=
info
[
'ego2global_translation'
],
cam_intrinsic
=
cam_info
[
'cam_intrinsic'
],
width
=
width
,
height
=
height
))
for
coco_info
in
coco_infos
:
if
coco_info
is
None
:
continue
# add an empty key for coco format
coco_info
[
'segmentation'
]
=
[]
coco_info
[
'id'
]
=
coco_ann_id
coco_2d_dict
[
'annotations'
].
append
(
coco_info
)
coco_ann_id
+=
1
if
mono3d
:
json_prefix
=
f
'
{
info_path
[:
-
4
]
}
_mono3d'
else
:
json_prefix
=
f
'
{
info_path
[:
-
4
]
}
'
mmcv
.
dump
(
coco_2d_dict
,
f
'
{
json_prefix
}
.coco.json'
)
def
get_2d_boxes
(
nusc
,
sample_data_token
:
str
,
visibilities
:
List
[
str
],
mono3d
=
True
):
"""Get the 2D annotation records for a given `sample_data_token`.
Args:
sample_data_token (str): Sample data token belonging to a camera
\
keyframe.
visibilities (list[str]): Visibility filter.
mono3d (bool): Whether to get boxes with mono3d annotation.
Return:
list[dict]: List of 2D annotation record that belongs to the input
`sample_data_token`.
"""
# Get the sample data and the sample corresponding to that sample data.
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample_data_token
)
assert
sd_rec
[
'sensor_modality'
]
==
'camera'
,
'Error: get_2d_boxes only works'
\
' for camera sample_data!'
if
not
sd_rec
[
'is_key_frame'
]:
raise
ValueError
(
'The 2D re-projections are available only for keyframes.'
)
s_rec
=
nusc
.
get
(
'sample'
,
sd_rec
[
'sample_token'
])
# Get the calibrated sensor and ego pose
# record to get the transformation matrices.
cs_rec
=
nusc
.
get
(
'calibrated_sensor'
,
sd_rec
[
'calibrated_sensor_token'
])
pose_rec
=
nusc
.
get
(
'ego_pose'
,
sd_rec
[
'ego_pose_token'
])
camera_intrinsic
=
np
.
array
(
cs_rec
[
'camera_intrinsic'
])
# Get all the annotation with the specified visibilties.
ann_recs
=
[
nusc
.
get
(
'sample_annotation'
,
token
)
for
token
in
s_rec
[
'anns'
]
]
ann_recs
=
[
ann_rec
for
ann_rec
in
ann_recs
if
(
ann_rec
[
'visibility_token'
]
in
visibilities
)
]
repro_recs
=
[]
for
ann_rec
in
ann_recs
:
# Augment sample_annotation with token information.
ann_rec
[
'sample_annotation_token'
]
=
ann_rec
[
'token'
]
ann_rec
[
'sample_data_token'
]
=
sample_data_token
# Get the box in global coordinates.
box
=
nusc
.
get_box
(
ann_rec
[
'token'
])
# Move them to the ego-pose frame.
box
.
translate
(
-
np
.
array
(
pose_rec
[
'translation'
]))
box
.
rotate
(
Quaternion
(
pose_rec
[
'rotation'
]).
inverse
)
# Move them to the calibrated sensor frame.
box
.
translate
(
-
np
.
array
(
cs_rec
[
'translation'
]))
box
.
rotate
(
Quaternion
(
cs_rec
[
'rotation'
]).
inverse
)
# Filter out the corners that are not in front of the calibrated
# sensor.
corners_3d
=
box
.
corners
()
in_front
=
np
.
argwhere
(
corners_3d
[
2
,
:]
>
0
).
flatten
()
corners_3d
=
corners_3d
[:,
in_front
]
# Project 3d box to 2d.
corner_coords
=
view_points
(
corners_3d
,
camera_intrinsic
,
True
).
T
[:,
:
2
].
tolist
()
# Keep only corners that fall within the image.
final_coords
=
post_process_coords
(
corner_coords
)
# Skip if the convex hull of the re-projected corners
# does not intersect the image canvas.
if
final_coords
is
None
:
continue
else
:
min_x
,
min_y
,
max_x
,
max_y
=
final_coords
# Generate dictionary record to be included in the .json file.
repro_rec
=
generate_record
(
ann_rec
,
min_x
,
min_y
,
max_x
,
max_y
,
sample_data_token
,
sd_rec
[
'filename'
])
# If mono3d=True, add 3D annotations in camera coordinates
if
mono3d
and
(
repro_rec
is
not
None
):
loc
=
box
.
center
.
tolist
()
dim
=
box
.
wlh
dim
[[
0
,
1
,
2
]]
=
dim
[[
1
,
2
,
0
]]
# convert wlh to our lhw
dim
=
dim
.
tolist
()
rot
=
box
.
orientation
.
yaw_pitch_roll
[
0
]
rot
=
[
-
rot
]
# convert the rot to our cam coordinate
global_velo2d
=
nusc
.
box_velocity
(
box
.
token
)[:
2
]
global_velo3d
=
np
.
array
([
*
global_velo2d
,
0.0
])
e2g_r_mat
=
Quaternion
(
pose_rec
[
'rotation'
]).
rotation_matrix
c2e_r_mat
=
Quaternion
(
cs_rec
[
'rotation'
]).
rotation_matrix
cam_velo3d
=
global_velo3d
@
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
c2e_r_mat
).
T
velo
=
cam_velo3d
[
0
::
2
].
tolist
()
repro_rec
[
'bbox_cam3d'
]
=
loc
+
dim
+
rot
repro_rec
[
'velo_cam3d'
]
=
velo
center3d
=
np
.
array
(
loc
).
reshape
([
1
,
3
])
center2d
=
points_cam2img
(
center3d
,
camera_intrinsic
,
with_depth
=
True
)
repro_rec
[
'center2d'
]
=
center2d
.
squeeze
().
tolist
()
# normalized center2D + depth
# if samples with depth < 0 will be removed
if
repro_rec
[
'center2d'
][
2
]
<=
0
:
continue
ann_token
=
nusc
.
get
(
'sample_annotation'
,
box
.
token
)[
'attribute_tokens'
]
if
len
(
ann_token
)
==
0
:
attr_name
=
'None'
else
:
attr_name
=
nusc
.
get
(
'attribute'
,
ann_token
[
0
])[
'name'
]
attr_id
=
nus_attributes
.
index
(
attr_name
)
repro_rec
[
'attribute_name'
]
=
attr_name
repro_rec
[
'attribute_id'
]
=
attr_id
repro_recs
.
append
(
repro_rec
)
return
repro_recs
def
post_process_coords
(
corner_coords
:
List
,
imsize
:
Tuple
[
int
,
int
]
=
(
1600
,
900
)
)
->
Union
[
Tuple
[
float
,
float
,
float
,
float
],
None
]:
"""Get the intersection of the convex hull of the reprojected bbox corners
and the image canvas, return None if no intersection.
Args:
corner_coords (list[int]): Corner coordinates of reprojected
bounding box.
imsize (tuple[int]): Size of the image canvas.
Return:
tuple [float]: Intersection of the convex hull of the 2D box
corners and the image canvas.
"""
polygon_from_2d_box
=
MultiPoint
(
corner_coords
).
convex_hull
img_canvas
=
box
(
0
,
0
,
imsize
[
0
],
imsize
[
1
])
if
polygon_from_2d_box
.
intersects
(
img_canvas
):
img_intersection
=
polygon_from_2d_box
.
intersection
(
img_canvas
)
intersection_coords
=
np
.
array
(
[
coord
for
coord
in
img_intersection
.
exterior
.
coords
])
min_x
=
min
(
intersection_coords
[:,
0
])
min_y
=
min
(
intersection_coords
[:,
1
])
max_x
=
max
(
intersection_coords
[:,
0
])
max_y
=
max
(
intersection_coords
[:,
1
])
return
min_x
,
min_y
,
max_x
,
max_y
else
:
return
None
def
generate_record
(
ann_rec
:
dict
,
x1
:
float
,
y1
:
float
,
x2
:
float
,
y2
:
float
,
sample_data_token
:
str
,
filename
:
str
)
->
OrderedDict
:
"""Generate one 2D annotation record given various informations on top of
the 2D bounding box coordinates.
Args:
ann_rec (dict): Original 3d annotation record.
x1 (float): Minimum value of the x coordinate.
y1 (float): Minimum value of the y coordinate.
x2 (float): Maximum value of the x coordinate.
y2 (float): Maximum value of the y coordinate.
sample_data_token (str): Sample data token.
filename (str):The corresponding image file where the annotation
is present.
Returns:
dict: A sample 2D annotation record.
- file_name (str): flie name
- image_id (str): sample data token
- area (float): 2d box area
- category_name (str): category name
- category_id (int): category id
- bbox (list[float]): left x, top y, dx, dy of 2d box
- iscrowd (int): whether the area is crowd
"""
repro_rec
=
OrderedDict
()
repro_rec
[
'sample_data_token'
]
=
sample_data_token
coco_rec
=
dict
()
relevant_keys
=
[
'attribute_tokens'
,
'category_name'
,
'instance_token'
,
'next'
,
'num_lidar_pts'
,
'num_radar_pts'
,
'prev'
,
'sample_annotation_token'
,
'sample_data_token'
,
'visibility_token'
,
]
for
key
,
value
in
ann_rec
.
items
():
if
key
in
relevant_keys
:
repro_rec
[
key
]
=
value
repro_rec
[
'bbox_corners'
]
=
[
x1
,
y1
,
x2
,
y2
]
repro_rec
[
'filename'
]
=
filename
coco_rec
[
'file_name'
]
=
filename
coco_rec
[
'image_id'
]
=
sample_data_token
coco_rec
[
'area'
]
=
(
y2
-
y1
)
*
(
x2
-
x1
)
if
repro_rec
[
'category_name'
]
not
in
NuScenesDataset
.
NameMapping
:
return
None
cat_name
=
NuScenesDataset
.
NameMapping
[
repro_rec
[
'category_name'
]]
coco_rec
[
'category_name'
]
=
cat_name
coco_rec
[
'category_id'
]
=
nus_categories
.
index
(
cat_name
)
coco_rec
[
'bbox'
]
=
[
x1
,
y1
,
x2
-
x1
,
y2
-
y1
]
coco_rec
[
'iscrowd'
]
=
0
return
coco_rec
autonomous_driving/occupancy_prediction/tools/data_converter/nuscenes_occ_converter.py
0 → 100644
View file @
df3c64a9
# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------
# Modified by Xiaoyu Tian
# ---------------------------------------------
import
mmcv
import
numpy
as
np
import
os
from
collections
import
OrderedDict
from
nuscenes.nuscenes
import
NuScenes
from
nuscenes.utils.geometry_utils
import
view_points
from
os
import
path
as
osp
from
pyquaternion
import
Quaternion
from
shapely.geometry
import
MultiPoint
,
box
from
typing
import
List
,
Tuple
,
Union
from
mmdet3d.core.bbox.box_np_ops
import
points_cam2img
from
mmdet3d.datasets
import
NuScenesDataset
import
simplejson
as
json
nus_categories
=
(
'car'
,
'truck'
,
'trailer'
,
'bus'
,
'construction_vehicle'
,
'bicycle'
,
'motorcycle'
,
'pedestrian'
,
'traffic_cone'
,
'barrier'
)
nus_attributes
=
(
'cycle.with_rider'
,
'cycle.without_rider'
,
'pedestrian.moving'
,
'pedestrian.standing'
,
'pedestrian.sitting_lying_down'
,
'vehicle.moving'
,
'vehicle.parked'
,
'vehicle.stopped'
,
'None'
)
def
create_nuscenes_occ_infos
(
root_path
,
occ_path
,
out_path
,
can_bus_root_path
,
info_prefix
,
version
=
'v1.0-trainval'
,
max_sweeps
=
10
):
"""Create info file of nuscene dataset.
Given the raw data, generate its related info file in pkl format.
Args:
root_path (str): Path of the data root.
info_prefix (str): Prefix of the info file to be generated.
version (str): Version of the data.
Default: 'v1.0-trainval'
max_sweeps (int): Max number of sweeps.
Default: 10
"""
from
nuscenes.nuscenes
import
NuScenes
from
nuscenes.can_bus.can_bus_api
import
NuScenesCanBus
print
(
version
,
root_path
)
nusc
=
NuScenes
(
version
=
version
,
dataroot
=
root_path
,
verbose
=
True
)
nusc_can_bus
=
NuScenesCanBus
(
dataroot
=
can_bus_root_path
)
print
(
type
(
nusc_can_bus
))
from
nuscenes.utils
import
splits
available_vers
=
[
'v1.0-trainval'
,
'v1.0-test'
,
'v1.0-mini'
]
assert
version
in
available_vers
with
open
(
os
.
path
.
join
(
occ_path
,
'annotations.json'
),
'r'
)
as
f
:
occ_anno
=
json
.
load
(
f
)
if
version
==
'v1.0-trainval'
:
train_scenes
=
splits
.
train
val_scenes
=
splits
.
val
elif
version
==
'v1.0-test'
:
train_scenes
=
splits
.
test
val_scenes
=
[]
elif
version
==
'v1.0-mini'
:
train_scenes
=
splits
.
mini_train
val_scenes
=
splits
.
mini_val
else
:
raise
ValueError
(
'unknown'
)
# filter existing scenes.
available_scenes
=
get_available_scenes
(
nusc
)
available_scene_names
=
[
s
[
'name'
]
for
s
in
available_scenes
]
train_scenes
=
list
(
filter
(
lambda
x
:
x
in
available_scene_names
,
train_scenes
))
val_scenes
=
list
(
filter
(
lambda
x
:
x
in
available_scene_names
,
val_scenes
))
train_scenes
=
set
([
available_scenes
[
available_scene_names
.
index
(
s
)][
'token'
]
for
s
in
train_scenes
])
val_scenes
=
set
([
available_scenes
[
available_scene_names
.
index
(
s
)][
'token'
]
for
s
in
val_scenes
])
token2name
=
dict
()
for
scene
in
nusc
.
scene
:
token2name
[
scene
[
'token'
]]
=
scene
[
'name'
]
test
=
'test'
in
version
if
test
:
print
(
'test scene: {}'
.
format
(
len
(
train_scenes
)))
else
:
print
(
'train scene: {}, val scene: {}'
.
format
(
len
(
train_scenes
),
len
(
val_scenes
)))
train_nusc_infos
,
val_nusc_infos
=
_fill_occ_trainval_infos
(
nusc
,
occ_anno
,
token2name
,
nusc_can_bus
,
train_scenes
,
val_scenes
,
test
,
max_sweeps
=
max_sweeps
)
metadata
=
dict
(
version
=
version
)
if
test
:
print
(
'test sample: {}'
.
format
(
len
(
train_nusc_infos
)))
data
=
dict
(
infos
=
train_nusc_infos
,
metadata
=
metadata
)
info_path
=
osp
.
join
(
out_path
,
'{}_infos_temporal_test.pkl'
.
format
(
info_prefix
))
mmcv
.
dump
(
data
,
info_path
)
else
:
print
(
'train sample: {}, val sample: {}'
.
format
(
len
(
train_nusc_infos
),
len
(
val_nusc_infos
)))
data
=
dict
(
infos
=
train_nusc_infos
,
metadata
=
metadata
)
info_path
=
osp
.
join
(
out_path
,
'{}_infos_temporal_train.pkl'
.
format
(
info_prefix
))
mmcv
.
dump
(
data
,
info_path
)
data
[
'infos'
]
=
val_nusc_infos
info_val_path
=
osp
.
join
(
out_path
,
'{}_infos_temporal_val.pkl'
.
format
(
info_prefix
))
mmcv
.
dump
(
data
,
info_val_path
)
def
get_available_scenes
(
nusc
):
"""Get available scenes from the input nuscenes class.
Given the raw data, get the information of available scenes for
further info generation.
Args:
nusc (class): Dataset class in the nuScenes dataset.
Returns:
available_scenes (list[dict]): List of basic information for the
available scenes.
"""
available_scenes
=
[]
print
(
'total scene num: {}'
.
format
(
len
(
nusc
.
scene
)))
for
scene
in
nusc
.
scene
:
scene_token
=
scene
[
'token'
]
scene_rec
=
nusc
.
get
(
'scene'
,
scene_token
)
sample_rec
=
nusc
.
get
(
'sample'
,
scene_rec
[
'first_sample_token'
])
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample_rec
[
'data'
][
'LIDAR_TOP'
])
has_more_frames
=
True
scene_not_exist
=
False
while
has_more_frames
:
lidar_path
,
boxes
,
_
=
nusc
.
get_sample_data
(
sd_rec
[
'token'
])
lidar_path
=
str
(
lidar_path
)
if
os
.
getcwd
()
in
lidar_path
:
# path from lyftdataset is absolute path
lidar_path
=
lidar_path
.
split
(
f
'
{
os
.
getcwd
()
}
/'
)[
-
1
]
# relative path
if
not
mmcv
.
is_filepath
(
lidar_path
):
scene_not_exist
=
True
break
else
:
break
if
scene_not_exist
:
continue
available_scenes
.
append
(
scene
)
print
(
'exist scene num: {}'
.
format
(
len
(
available_scenes
)))
return
available_scenes
def
_get_can_bus_info
(
nusc
,
nusc_can_bus
,
sample
):
scene_name
=
nusc
.
get
(
'scene'
,
sample
[
'scene_token'
])[
'name'
]
sample_timestamp
=
sample
[
'timestamp'
]
try
:
pose_list
=
nusc_can_bus
.
get_messages
(
scene_name
,
'pose'
)
except
:
return
np
.
zeros
(
18
)
# server scenes do not have can bus information.
can_bus
=
[]
# during each scene, the first timestamp of can_bus may be large than the first sample's timestamp
last_pose
=
pose_list
[
0
]
for
i
,
pose
in
enumerate
(
pose_list
):
if
pose
[
'utime'
]
>
sample_timestamp
:
break
last_pose
=
pose
_
=
last_pose
.
pop
(
'utime'
)
# useless
pos
=
last_pose
.
pop
(
'pos'
)
rotation
=
last_pose
.
pop
(
'orientation'
)
can_bus
.
extend
(
pos
)
can_bus
.
extend
(
rotation
)
for
key
in
last_pose
.
keys
():
can_bus
.
extend
(
pose
[
key
])
# 16 elements
can_bus
.
extend
([
0.
,
0.
])
return
np
.
array
(
can_bus
)
def
_fill_occ_trainval_infos
(
nusc
,
occ_anno
,
token2name
,
nusc_can_bus
,
train_scenes
,
val_scenes
,
test
=
False
,
max_sweeps
=
10
):
"""Generate the train/val infos from the raw data.
Args:
nusc (:obj:`NuScenes`): Dataset class in the nuScenes dataset.
train_scenes (list[str]): Basic information of training scenes.
val_scenes (list[str]): Basic information of validation scenes.
test (bool): Whether use the test mode. In the test mode, no
annotations can be accessed. Default: False.
max_sweeps (int): Max number of sweeps. Default: 10.
Returns:
tuple[list[dict]]: Information of training set and validation set
that will be saved to the info file.
"""
train_nusc_infos
=
[]
val_nusc_infos
=
[]
frame_idx
=
0
scene_infos
=
occ_anno
[
'scene_infos'
]
for
sample
in
mmcv
.
track_iter_progress
(
nusc
.
sample
):
lidar_token
=
sample
[
'data'
][
'LIDAR_TOP'
]
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample
[
'data'
][
'LIDAR_TOP'
])
scene_token
=
sample
[
'scene_token'
]
scene_name
=
token2name
[
scene_token
]
sample_token
=
sd_rec
[
'sample_token'
]
if
sample_token
in
scene_infos
[
scene_name
].
keys
():
occ_sample
=
scene_infos
[
scene_name
][
sample_token
]
else
:
continue
cs_record
=
nusc
.
get
(
'calibrated_sensor'
,
sd_rec
[
'calibrated_sensor_token'
])
pose_record
=
nusc
.
get
(
'ego_pose'
,
sd_rec
[
'ego_pose_token'
])
lidar_path
,
boxes
,
_
=
nusc
.
get_sample_data
(
lidar_token
)
# mmcv.check_file_exist(lidar_path)
can_bus
=
_get_can_bus_info
(
nusc
,
nusc_can_bus
,
sample
)
##
info
=
{
'lidar_path'
:
lidar_path
,
'token'
:
sample
[
'token'
],
'prev'
:
sample
[
'prev'
],
'next'
:
sample
[
'next'
],
'can_bus'
:
can_bus
,
'frame_idx'
:
frame_idx
,
# temporal related info
'sweeps'
:
[],
'cams'
:
dict
(),
'scene_token'
:
sample
[
'scene_token'
],
# temporal related info
'lidar2ego_translation'
:
cs_record
[
'translation'
],
'lidar2ego_rotation'
:
cs_record
[
'rotation'
],
'ego2global_translation'
:
pose_record
[
'translation'
],
'ego2global_rotation'
:
pose_record
[
'rotation'
],
'timestamp'
:
sample
[
'timestamp'
],
}
info
[
'occ_gt_path'
]
=
occ_sample
[
'gt_path'
]
if
sample
[
'next'
]
==
''
:
frame_idx
=
0
else
:
frame_idx
+=
1
l2e_r
=
info
[
'lidar2ego_rotation'
]
l2e_t
=
info
[
'lidar2ego_translation'
]
e2g_r
=
info
[
'ego2global_rotation'
]
e2g_t
=
info
[
'ego2global_translation'
]
l2e_r_mat
=
Quaternion
(
l2e_r
).
rotation_matrix
e2g_r_mat
=
Quaternion
(
e2g_r
).
rotation_matrix
# obtain 6 image's information per frame
camera_types
=
[
'CAM_FRONT'
,
'CAM_FRONT_RIGHT'
,
'CAM_FRONT_LEFT'
,
'CAM_BACK'
,
'CAM_BACK_LEFT'
,
'CAM_BACK_RIGHT'
,
]
for
cam
in
camera_types
:
cam_token
=
sample
[
'data'
][
cam
]
cam_path
,
_
,
cam_intrinsic
=
nusc
.
get_sample_data
(
cam_token
)
cam_info
=
obtain_sensor2top
(
nusc
,
cam_token
,
l2e_t
,
l2e_r_mat
,
e2g_t
,
e2g_r_mat
,
cam
)
cam_info
.
update
(
cam_intrinsic
=
cam_intrinsic
)
info
[
'cams'
].
update
({
cam
:
cam_info
})
# obtain sweeps for a single key-frame
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample
[
'data'
][
'LIDAR_TOP'
])
sweeps
=
[]
while
len
(
sweeps
)
<
max_sweeps
:
if
not
sd_rec
[
'prev'
]
==
''
:
sweep
=
obtain_sensor2top
(
nusc
,
sd_rec
[
'prev'
],
l2e_t
,
l2e_r_mat
,
e2g_t
,
e2g_r_mat
,
'lidar'
)
sweeps
.
append
(
sweep
)
sd_rec
=
nusc
.
get
(
'sample_data'
,
sd_rec
[
'prev'
])
else
:
break
info
[
'sweeps'
]
=
sweeps
# obtain annotation
if
not
test
:
annotations
=
[
nusc
.
get
(
'sample_annotation'
,
token
)
for
token
in
sample
[
'anns'
]
]
locs
=
np
.
array
([
b
.
center
for
b
in
boxes
]).
reshape
(
-
1
,
3
)
dims
=
np
.
array
([
b
.
wlh
for
b
in
boxes
]).
reshape
(
-
1
,
3
)
rots
=
np
.
array
([
b
.
orientation
.
yaw_pitch_roll
[
0
]
for
b
in
boxes
]).
reshape
(
-
1
,
1
)
velocity
=
np
.
array
(
[
nusc
.
box_velocity
(
token
)[:
2
]
for
token
in
sample
[
'anns'
]])
valid_flag
=
np
.
array
(
[(
anno
[
'num_lidar_pts'
]
+
anno
[
'num_radar_pts'
])
>
0
for
anno
in
annotations
],
dtype
=
bool
).
reshape
(
-
1
)
# convert velo from global to lidar
for
i
in
range
(
len
(
boxes
)):
velo
=
np
.
array
([
*
velocity
[
i
],
0.0
])
velo
=
velo
@
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
velocity
[
i
]
=
velo
[:
2
]
names
=
[
b
.
name
for
b
in
boxes
]
for
i
in
range
(
len
(
names
)):
if
names
[
i
]
in
NuScenesDataset
.
NameMapping
:
names
[
i
]
=
NuScenesDataset
.
NameMapping
[
names
[
i
]]
names
=
np
.
array
(
names
)
# we need to convert rot to SECOND format.
gt_boxes
=
np
.
concatenate
([
locs
,
dims
,
-
rots
-
np
.
pi
/
2
],
axis
=
1
)
assert
len
(
gt_boxes
)
==
len
(
annotations
),
f
'
{
len
(
gt_boxes
)
}
,
{
len
(
annotations
)
}
'
info
[
'gt_boxes'
]
=
gt_boxes
info
[
'gt_names'
]
=
names
info
[
'gt_velocity'
]
=
velocity
.
reshape
(
-
1
,
2
)
info
[
'num_lidar_pts'
]
=
np
.
array
(
[
a
[
'num_lidar_pts'
]
for
a
in
annotations
])
info
[
'num_radar_pts'
]
=
np
.
array
(
[
a
[
'num_radar_pts'
]
for
a
in
annotations
])
info
[
'valid_flag'
]
=
valid_flag
if
sample
[
'scene_token'
]
in
train_scenes
:
train_nusc_infos
.
append
(
info
)
else
:
val_nusc_infos
.
append
(
info
)
return
train_nusc_infos
,
val_nusc_infos
def
obtain_sensor2top
(
nusc
,
sensor_token
,
l2e_t
,
l2e_r_mat
,
e2g_t
,
e2g_r_mat
,
sensor_type
=
'lidar'
):
"""Obtain the info with RT matric from general sensor to Top LiDAR.
Args:
nusc (class): Dataset class in the nuScenes dataset.
sensor_token (str): Sample data token corresponding to the
specific sensor type.
l2e_t (np.ndarray): Translation from lidar to ego in shape (1, 3).
l2e_r_mat (np.ndarray): Rotation matrix from lidar to ego
in shape (3, 3).
e2g_t (np.ndarray): Translation from ego to global in shape (1, 3).
e2g_r_mat (np.ndarray): Rotation matrix from ego to global
in shape (3, 3).
sensor_type (str): Sensor to calibrate. Default: 'lidar'.
Returns:
sweep (dict): Sweep information after transformation.
"""
sd_rec
=
nusc
.
get
(
'sample_data'
,
sensor_token
)
cs_record
=
nusc
.
get
(
'calibrated_sensor'
,
sd_rec
[
'calibrated_sensor_token'
])
pose_record
=
nusc
.
get
(
'ego_pose'
,
sd_rec
[
'ego_pose_token'
])
data_path
=
str
(
nusc
.
get_sample_data_path
(
sd_rec
[
'token'
]))
if
os
.
getcwd
()
in
data_path
:
# path from lyftdataset is absolute path
data_path
=
data_path
.
split
(
f
'
{
os
.
getcwd
()
}
/'
)[
-
1
]
# relative path
sweep
=
{
'data_path'
:
data_path
,
'type'
:
sensor_type
,
'sample_data_token'
:
sd_rec
[
'token'
],
'sensor2ego_translation'
:
cs_record
[
'translation'
],
'sensor2ego_rotation'
:
cs_record
[
'rotation'
],
'ego2global_translation'
:
pose_record
[
'translation'
],
'ego2global_rotation'
:
pose_record
[
'rotation'
],
'timestamp'
:
sd_rec
[
'timestamp'
]
}
l2e_r_s
=
sweep
[
'sensor2ego_rotation'
]
l2e_t_s
=
sweep
[
'sensor2ego_translation'
]
e2g_r_s
=
sweep
[
'ego2global_rotation'
]
e2g_t_s
=
sweep
[
'ego2global_translation'
]
# obtain the RT from sensor to Top LiDAR
# sweep->ego->global->ego'->lidar
l2e_r_s_mat
=
Quaternion
(
l2e_r_s
).
rotation_matrix
e2g_r_s_mat
=
Quaternion
(
e2g_r_s
).
rotation_matrix
R
=
(
l2e_r_s_mat
.
T
@
e2g_r_s_mat
.
T
)
@
(
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
)
T
=
(
l2e_t_s
@
e2g_r_s_mat
.
T
+
e2g_t_s
)
@
(
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
)
T
-=
e2g_t
@
(
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
)
+
l2e_t
@
np
.
linalg
.
inv
(
l2e_r_mat
).
T
sweep
[
'sensor2lidar_rotation'
]
=
R
.
T
# points @ R.T + T
sweep
[
'sensor2lidar_translation'
]
=
T
return
sweep
def
export_2d_annotation
(
root_path
,
info_path
,
version
,
mono3d
=
True
):
"""Export 2d annotation from the info file and raw data.
Args:
root_path (str): Root path of the raw data.
info_path (str): Path of the info file.
version (str): Dataset version.
mono3d (bool): Whether to export mono3d annotation. Default: True.
"""
# get bbox annotations for camera
camera_types
=
[
'CAM_FRONT'
,
'CAM_FRONT_RIGHT'
,
'CAM_FRONT_LEFT'
,
'CAM_BACK'
,
'CAM_BACK_LEFT'
,
'CAM_BACK_RIGHT'
,
]
nusc_infos
=
mmcv
.
load
(
info_path
)[
'infos'
]
nusc
=
NuScenes
(
version
=
version
,
dataroot
=
root_path
,
verbose
=
True
)
# info_2d_list = []
cat2Ids
=
[
dict
(
id
=
nus_categories
.
index
(
cat_name
),
name
=
cat_name
)
for
cat_name
in
nus_categories
]
coco_ann_id
=
0
coco_2d_dict
=
dict
(
annotations
=
[],
images
=
[],
categories
=
cat2Ids
)
for
info
in
mmcv
.
track_iter_progress
(
nusc_infos
):
for
cam
in
camera_types
:
cam_info
=
info
[
'cams'
][
cam
]
coco_infos
=
get_2d_boxes
(
nusc
,
cam_info
[
'sample_data_token'
],
visibilities
=
[
''
,
'1'
,
'2'
,
'3'
,
'4'
],
mono3d
=
mono3d
)
(
height
,
width
,
_
)
=
mmcv
.
imread
(
cam_info
[
'data_path'
]).
shape
coco_2d_dict
[
'images'
].
append
(
dict
(
file_name
=
cam_info
[
'data_path'
].
split
(
'data/nuscenes/'
)
[
-
1
],
id
=
cam_info
[
'sample_data_token'
],
token
=
info
[
'token'
],
cam2ego_rotation
=
cam_info
[
'sensor2ego_rotation'
],
cam2ego_translation
=
cam_info
[
'sensor2ego_translation'
],
ego2global_rotation
=
info
[
'ego2global_rotation'
],
ego2global_translation
=
info
[
'ego2global_translation'
],
cam_intrinsic
=
cam_info
[
'cam_intrinsic'
],
width
=
width
,
height
=
height
))
for
coco_info
in
coco_infos
:
if
coco_info
is
None
:
continue
# add an empty key for coco format
coco_info
[
'segmentation'
]
=
[]
coco_info
[
'id'
]
=
coco_ann_id
coco_2d_dict
[
'annotations'
].
append
(
coco_info
)
coco_ann_id
+=
1
if
mono3d
:
json_prefix
=
f
'
{
info_path
[:
-
4
]
}
_mono3d'
else
:
json_prefix
=
f
'
{
info_path
[:
-
4
]
}
'
mmcv
.
dump
(
coco_2d_dict
,
f
'
{
json_prefix
}
.coco.json'
)
def
get_2d_boxes
(
nusc
,
sample_data_token
:
str
,
visibilities
:
List
[
str
],
mono3d
=
True
):
"""Get the 2D annotation records for a given `sample_data_token`.
Args:
sample_data_token (str): Sample data token belonging to a camera
\
keyframe.
visibilities (list[str]): Visibility filter.
mono3d (bool): Whether to get boxes with mono3d annotation.
Return:
list[dict]: List of 2D annotation record that belongs to the input
`sample_data_token`.
"""
# Get the sample data and the sample corresponding to that sample data.
sd_rec
=
nusc
.
get
(
'sample_data'
,
sample_data_token
)
assert
sd_rec
[
'sensor_modality'
]
==
'camera'
,
'Error: get_2d_boxes only works'
\
' for camera sample_data!'
if
not
sd_rec
[
'is_key_frame'
]:
raise
ValueError
(
'The 2D re-projections are available only for keyframes.'
)
s_rec
=
nusc
.
get
(
'sample'
,
sd_rec
[
'sample_token'
])
# Get the calibrated sensor and ego pose
# record to get the transformation matrices.
cs_rec
=
nusc
.
get
(
'calibrated_sensor'
,
sd_rec
[
'calibrated_sensor_token'
])
pose_rec
=
nusc
.
get
(
'ego_pose'
,
sd_rec
[
'ego_pose_token'
])
camera_intrinsic
=
np
.
array
(
cs_rec
[
'camera_intrinsic'
])
# Get all the annotation with the specified visibilties.
ann_recs
=
[
nusc
.
get
(
'sample_annotation'
,
token
)
for
token
in
s_rec
[
'anns'
]
]
ann_recs
=
[
ann_rec
for
ann_rec
in
ann_recs
if
(
ann_rec
[
'visibility_token'
]
in
visibilities
)
]
repro_recs
=
[]
for
ann_rec
in
ann_recs
:
# Augment sample_annotation with token information.
ann_rec
[
'sample_annotation_token'
]
=
ann_rec
[
'token'
]
ann_rec
[
'sample_data_token'
]
=
sample_data_token
# Get the box in global coordinates.
box
=
nusc
.
get_box
(
ann_rec
[
'token'
])
# Move them to the ego-pose frame.
box
.
translate
(
-
np
.
array
(
pose_rec
[
'translation'
]))
box
.
rotate
(
Quaternion
(
pose_rec
[
'rotation'
]).
inverse
)
# Move them to the calibrated sensor frame.
box
.
translate
(
-
np
.
array
(
cs_rec
[
'translation'
]))
box
.
rotate
(
Quaternion
(
cs_rec
[
'rotation'
]).
inverse
)
# Filter out the corners that are not in front of the calibrated
# sensor.
corners_3d
=
box
.
corners
()
in_front
=
np
.
argwhere
(
corners_3d
[
2
,
:]
>
0
).
flatten
()
corners_3d
=
corners_3d
[:,
in_front
]
# Project 3d box to 2d.
corner_coords
=
view_points
(
corners_3d
,
camera_intrinsic
,
True
).
T
[:,
:
2
].
tolist
()
# Keep only corners that fall within the image.
final_coords
=
post_process_coords
(
corner_coords
)
# Skip if the convex hull of the re-projected corners
# does not intersect the image canvas.
if
final_coords
is
None
:
continue
else
:
min_x
,
min_y
,
max_x
,
max_y
=
final_coords
# Generate dictionary record to be included in the .json file.
repro_rec
=
generate_record
(
ann_rec
,
min_x
,
min_y
,
max_x
,
max_y
,
sample_data_token
,
sd_rec
[
'filename'
])
# If mono3d=True, add 3D annotations in camera coordinates
if
mono3d
and
(
repro_rec
is
not
None
):
loc
=
box
.
center
.
tolist
()
dim
=
box
.
wlh
dim
[[
0
,
1
,
2
]]
=
dim
[[
1
,
2
,
0
]]
# convert wlh to our lhw
dim
=
dim
.
tolist
()
rot
=
box
.
orientation
.
yaw_pitch_roll
[
0
]
rot
=
[
-
rot
]
# convert the rot to our cam coordinate
global_velo2d
=
nusc
.
box_velocity
(
box
.
token
)[:
2
]
global_velo3d
=
np
.
array
([
*
global_velo2d
,
0.0
])
e2g_r_mat
=
Quaternion
(
pose_rec
[
'rotation'
]).
rotation_matrix
c2e_r_mat
=
Quaternion
(
cs_rec
[
'rotation'
]).
rotation_matrix
cam_velo3d
=
global_velo3d
@
np
.
linalg
.
inv
(
e2g_r_mat
).
T
@
np
.
linalg
.
inv
(
c2e_r_mat
).
T
velo
=
cam_velo3d
[
0
::
2
].
tolist
()
repro_rec
[
'bbox_cam3d'
]
=
loc
+
dim
+
rot
repro_rec
[
'velo_cam3d'
]
=
velo
center3d
=
np
.
array
(
loc
).
reshape
([
1
,
3
])
center2d
=
points_cam2img
(
center3d
,
camera_intrinsic
,
with_depth
=
True
)
repro_rec
[
'center2d'
]
=
center2d
.
squeeze
().
tolist
()
# normalized center2D + depth
# if samples with depth < 0 will be removed
if
repro_rec
[
'center2d'
][
2
]
<=
0
:
continue
ann_token
=
nusc
.
get
(
'sample_annotation'
,
box
.
token
)[
'attribute_tokens'
]
if
len
(
ann_token
)
==
0
:
attr_name
=
'None'
else
:
attr_name
=
nusc
.
get
(
'attribute'
,
ann_token
[
0
])[
'name'
]
attr_id
=
nus_attributes
.
index
(
attr_name
)
repro_rec
[
'attribute_name'
]
=
attr_name
repro_rec
[
'attribute_id'
]
=
attr_id
repro_recs
.
append
(
repro_rec
)
return
repro_recs
def
post_process_coords
(
corner_coords
:
List
,
imsize
:
Tuple
[
int
,
int
]
=
(
1600
,
900
)
)
->
Union
[
Tuple
[
float
,
float
,
float
,
float
],
None
]:
"""Get the intersection of the convex hull of the reprojected bbox corners
and the image canvas, return None if no intersection.
Args:
corner_coords (list[int]): Corner coordinates of reprojected
bounding box.
imsize (tuple[int]): Size of the image canvas.
Return:
tuple [float]: Intersection of the convex hull of the 2D box
corners and the image canvas.
"""
polygon_from_2d_box
=
MultiPoint
(
corner_coords
).
convex_hull
img_canvas
=
box
(
0
,
0
,
imsize
[
0
],
imsize
[
1
])
if
polygon_from_2d_box
.
intersects
(
img_canvas
):
img_intersection
=
polygon_from_2d_box
.
intersection
(
img_canvas
)
intersection_coords
=
np
.
array
(
[
coord
for
coord
in
img_intersection
.
exterior
.
coords
])
min_x
=
min
(
intersection_coords
[:,
0
])
min_y
=
min
(
intersection_coords
[:,
1
])
max_x
=
max
(
intersection_coords
[:,
0
])
max_y
=
max
(
intersection_coords
[:,
1
])
return
min_x
,
min_y
,
max_x
,
max_y
else
:
return
None
def
generate_record
(
ann_rec
:
dict
,
x1
:
float
,
y1
:
float
,
x2
:
float
,
y2
:
float
,
sample_data_token
:
str
,
filename
:
str
)
->
OrderedDict
:
"""Generate one 2D annotation record given various informations on top of
the 2D bounding box coordinates.
Args:
ann_rec (dict): Original 3d annotation record.
x1 (float): Minimum value of the x coordinate.
y1 (float): Minimum value of the y coordinate.
x2 (float): Maximum value of the x coordinate.
y2 (float): Maximum value of the y coordinate.
sample_data_token (str): Sample data token.
filename (str):The corresponding image file where the annotation
is present.
Returns:
dict: A sample 2D annotation record.
- file_name (str): flie name
- image_id (str): sample data token
- area (float): 2d box area
- category_name (str): category name
- category_id (int): category id
- bbox (list[float]): left x, top y, dx, dy of 2d box
- iscrowd (int): whether the area is crowd
"""
repro_rec
=
OrderedDict
()
repro_rec
[
'sample_data_token'
]
=
sample_data_token
coco_rec
=
dict
()
relevant_keys
=
[
'attribute_tokens'
,
'category_name'
,
'instance_token'
,
'next'
,
'num_lidar_pts'
,
'num_radar_pts'
,
'prev'
,
'sample_annotation_token'
,
'sample_data_token'
,
'visibility_token'
,
]
for
key
,
value
in
ann_rec
.
items
():
if
key
in
relevant_keys
:
repro_rec
[
key
]
=
value
repro_rec
[
'bbox_corners'
]
=
[
x1
,
y1
,
x2
,
y2
]
repro_rec
[
'filename'
]
=
filename
coco_rec
[
'file_name'
]
=
filename
coco_rec
[
'image_id'
]
=
sample_data_token
coco_rec
[
'area'
]
=
(
y2
-
y1
)
*
(
x2
-
x1
)
if
repro_rec
[
'category_name'
]
not
in
NuScenesDataset
.
NameMapping
:
return
None
cat_name
=
NuScenesDataset
.
NameMapping
[
repro_rec
[
'category_name'
]]
coco_rec
[
'category_name'
]
=
cat_name
coco_rec
[
'category_id'
]
=
nus_categories
.
index
(
cat_name
)
coco_rec
[
'bbox'
]
=
[
x1
,
y1
,
x2
-
x1
,
y2
-
y1
]
coco_rec
[
'iscrowd'
]
=
0
return
coco_rec
autonomous_driving/occupancy_prediction/tools/dist_test.sh
0 → 100644
View file @
df3c64a9
#!/usr/bin/env bash
CONFIG
=
$1
CHECKPOINT
=
$2
GPUS
=
$3
PORT
=
${
PORT
:-
29503
}
PYTHONPATH
=
"
$(
dirname
$0
)
/.."
:
$PYTHONPATH
\
python
-m
torch.distributed.launch
--nproc_per_node
=
$GPUS
--master_port
=
$PORT
\
$(
dirname
"
$0
"
)
/test.py
$CONFIG
$CHECKPOINT
--launcher
pytorch
${
@
:4
}
--eval
bbox
autonomous_driving/occupancy_prediction/tools/dist_train.sh
0 → 100755
View file @
df3c64a9
#!/usr/bin/env bash
CONFIG
=
$1
GPUS
=
$2
NNODES
=
${
NNODES
:-
1
}
NODE_RANK
=
${
NODE_RANK
:-
0
}
PORT
=
${
PORT
:-
29500
}
MASTER_ADDR
=
${
MASTER_ADDR
:-
"127.0.0.1"
}
PYTHONPATH
=
"
$(
dirname
$0
)
/.."
:
$PYTHONPATH
\
python
-m
torch.distributed.launch
\
--nnodes
=
$NNODES
\
--node_rank
=
$NODE_RANK
\
--master_addr
=
$MASTER_ADDR
\
--nproc_per_node
=
$GPUS
\
--master_port
=
$PORT
\
$(
dirname
"
$0
"
)
/train.py
\
$CONFIG
\
--deterministic
\
--launcher
pytorch
${
@
:3
}
\ No newline at end of file
autonomous_driving/occupancy_prediction/tools/fp16/dist_train.sh
0 → 100644
View file @
df3c64a9
#!/usr/bin/env bash
CONFIG
=
$1
GPUS
=
$2
PORT
=
${
PORT
:-
28508
}
PYTHONPATH
=
"
$(
dirname
$0
)
/.."
:
$PYTHONPATH
\
python
-m
torch.distributed.launch
--nproc_per_node
=
$GPUS
--master_port
=
$PORT
\
$(
dirname
"
$0
"
)
/train.py
$CONFIG
--launcher
pytorch
${
@
:3
}
--deterministic
autonomous_driving/occupancy_prediction/tools/fp16/train.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
from
__future__
import
division
import
argparse
import
copy
import
mmcv
import
os
import
time
import
torch
import
warnings
from
mmcv
import
Config
,
DictAction
from
mmcv.runner
import
get_dist_info
,
init_dist
,
wrap_fp16_model
from
os
import
path
as
osp
from
mmdet
import
__version__
as
mmdet_version
from
mmdet3d
import
__version__
as
mmdet3d_version
#from mmdet3d.apis import train_model
from
mmdet3d.datasets
import
build_dataset
from
mmdet3d.models
import
build_model
from
mmdet3d.utils
import
collect_env
,
get_root_logger
from
mmdet.apis
import
set_random_seed
from
mmseg
import
__version__
as
mmseg_version
from
mmcv.utils
import
TORCH_VERSION
,
digit_version
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Train a detector'
)
parser
.
add_argument
(
'config'
,
help
=
'train config file path'
)
parser
.
add_argument
(
'--work-dir'
,
help
=
'the dir to save logs and models'
)
parser
.
add_argument
(
'--resume-from'
,
help
=
'the checkpoint file to resume from'
)
parser
.
add_argument
(
'--no-validate'
,
action
=
'store_true'
,
help
=
'whether not to evaluate the checkpoint during training'
)
group_gpus
=
parser
.
add_mutually_exclusive_group
()
group_gpus
.
add_argument
(
'--gpus'
,
type
=
int
,
help
=
'number of gpus to use '
'(only applicable to non-distributed training)'
)
group_gpus
.
add_argument
(
'--gpu-ids'
,
type
=
int
,
nargs
=
'+'
,
help
=
'ids of gpus to use '
'(only applicable to non-distributed training)'
)
parser
.
add_argument
(
'--seed'
,
type
=
int
,
default
=
0
,
help
=
'random seed'
)
parser
.
add_argument
(
'--deterministic'
,
action
=
'store_true'
,
help
=
'whether to set deterministic options for CUDNN backend.'
)
parser
.
add_argument
(
'--options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file (deprecate), '
'change to --cfg-options instead.'
)
parser
.
add_argument
(
'--cfg-options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.'
)
parser
.
add_argument
(
'--launcher'
,
choices
=
[
'none'
,
'pytorch'
,
'slurm'
,
'mpi'
],
default
=
'none'
,
help
=
'job launcher'
)
parser
.
add_argument
(
'--local_rank'
,
type
=
int
,
default
=
0
)
parser
.
add_argument
(
'--autoscale-lr'
,
action
=
'store_true'
,
help
=
'automatically scale lr with the number of gpus'
)
args
=
parser
.
parse_args
()
if
'LOCAL_RANK'
not
in
os
.
environ
:
os
.
environ
[
'LOCAL_RANK'
]
=
str
(
args
.
local_rank
)
if
args
.
options
and
args
.
cfg_options
:
raise
ValueError
(
'--options and --cfg-options cannot be both specified, '
'--options is deprecated in favor of --cfg-options'
)
if
args
.
options
:
warnings
.
warn
(
'--options is deprecated in favor of --cfg-options'
)
args
.
cfg_options
=
args
.
options
return
args
def
main
():
args
=
parse_args
()
cfg
=
Config
.
fromfile
(
args
.
config
)
if
args
.
cfg_options
is
not
None
:
cfg
.
merge_from_dict
(
args
.
cfg_options
)
# import modules from string list.
if
cfg
.
get
(
'custom_imports'
,
None
):
from
mmcv.utils
import
import_modules_from_strings
import_modules_from_strings
(
**
cfg
[
'custom_imports'
])
# import modules from plguin/xx, registry will be updated
if
hasattr
(
cfg
,
'plugin'
):
if
cfg
.
plugin
:
import
importlib
if
hasattr
(
cfg
,
'plugin_dir'
):
plugin_dir
=
cfg
.
plugin_dir
_module_dir
=
os
.
path
.
dirname
(
plugin_dir
)
_module_dir
=
_module_dir
.
split
(
'/'
)
_module_path
=
_module_dir
[
0
]
for
m
in
_module_dir
[
1
:]:
_module_path
=
_module_path
+
'.'
+
m
print
(
_module_path
)
plg_lib
=
importlib
.
import_module
(
_module_path
)
else
:
# import dir is the dirpath for the config file
_module_dir
=
os
.
path
.
dirname
(
args
.
config
)
_module_dir
=
_module_dir
.
split
(
'/'
)
_module_path
=
_module_dir
[
0
]
for
m
in
_module_dir
[
1
:]:
_module_path
=
_module_path
+
'.'
+
m
print
(
_module_path
)
plg_lib
=
importlib
.
import_module
(
_module_path
)
from
projects.mmdet3d_plugin.bevformer.apis
import
custom_train_model
# set cudnn_benchmark
if
cfg
.
get
(
'cudnn_benchmark'
,
False
):
torch
.
backends
.
cudnn
.
benchmark
=
True
# work_dir is determined in this priority: CLI > segment in file > filename
if
args
.
work_dir
is
not
None
:
# update configs according to CLI args if args.work_dir is not None
cfg
.
work_dir
=
args
.
work_dir
elif
cfg
.
get
(
'work_dir'
,
None
)
is
None
:
# use config filename as default work_dir if cfg.work_dir is None
cfg
.
work_dir
=
osp
.
join
(
'./work_dirs'
,
osp
.
splitext
(
osp
.
basename
(
args
.
config
))[
0
])
#if args.resume_from is not None:
if
args
.
resume_from
is
not
None
and
osp
.
isfile
(
args
.
resume_from
):
cfg
.
resume_from
=
args
.
resume_from
if
args
.
gpu_ids
is
not
None
:
cfg
.
gpu_ids
=
args
.
gpu_ids
else
:
cfg
.
gpu_ids
=
range
(
1
)
if
args
.
gpus
is
None
else
range
(
args
.
gpus
)
if
digit_version
(
TORCH_VERSION
)
!=
digit_version
(
'1.8.1'
):
cfg
.
optimizer
[
'type'
]
=
'AdamW'
if
args
.
autoscale_lr
:
# apply the linear scaling rule (https://arxiv.org/abs/1706.02677)
cfg
.
optimizer
[
'lr'
]
=
cfg
.
optimizer
[
'lr'
]
*
len
(
cfg
.
gpu_ids
)
/
8
# init distributed env first, since logger depends on the dist info.
if
args
.
launcher
==
'none'
:
assert
False
,
'DOT NOT SUPPORT!!!'
distributed
=
False
else
:
distributed
=
True
init_dist
(
args
.
launcher
,
**
cfg
.
dist_params
)
# re-set gpu_ids with distributed training mode
_
,
world_size
=
get_dist_info
()
cfg
.
gpu_ids
=
range
(
world_size
)
# create work_dir
mmcv
.
mkdir_or_exist
(
osp
.
abspath
(
cfg
.
work_dir
))
# dump config
cfg
.
dump
(
osp
.
join
(
cfg
.
work_dir
,
osp
.
basename
(
args
.
config
)))
# init the logger before other steps
timestamp
=
time
.
strftime
(
'%Y%m%d_%H%M%S'
,
time
.
localtime
())
log_file
=
osp
.
join
(
cfg
.
work_dir
,
f
'
{
timestamp
}
.log'
)
# specify logger name, if we still use 'mmdet', the output info will be
# filtered and won't be saved in the log_file
# TODO: ugly workaround to judge whether we are training det or seg model
if
cfg
.
model
.
type
in
[
'EncoderDecoder3D'
]:
logger_name
=
'mmseg'
else
:
logger_name
=
'mmdet'
logger
=
get_root_logger
(
log_file
=
log_file
,
log_level
=
cfg
.
log_level
,
name
=
logger_name
)
# init the meta dict to record some important information such as
# environment info and seed, which will be logged
meta
=
dict
()
# log env info
env_info_dict
=
collect_env
()
env_info
=
'
\n
'
.
join
([(
f
'
{
k
}
:
{
v
}
'
)
for
k
,
v
in
env_info_dict
.
items
()])
dash_line
=
'-'
*
60
+
'
\n
'
logger
.
info
(
'Environment info:
\n
'
+
dash_line
+
env_info
+
'
\n
'
+
dash_line
)
meta
[
'env_info'
]
=
env_info
meta
[
'config'
]
=
cfg
.
pretty_text
# log some basic info
logger
.
info
(
f
'Distributed training:
{
distributed
}
'
)
logger
.
info
(
f
'Config:
\n
{
cfg
.
pretty_text
}
'
)
# set random seeds
if
args
.
seed
is
not
None
:
logger
.
info
(
f
'Set random seed to
{
args
.
seed
}
, '
f
'deterministic:
{
args
.
deterministic
}
'
)
set_random_seed
(
args
.
seed
,
deterministic
=
args
.
deterministic
)
cfg
.
seed
=
args
.
seed
meta
[
'seed'
]
=
args
.
seed
meta
[
'exp_name'
]
=
osp
.
basename
(
args
.
config
)
model
=
build_model
(
cfg
.
model
,
train_cfg
=
cfg
.
get
(
'train_cfg'
),
test_cfg
=
cfg
.
get
(
'test_cfg'
))
model
.
init_weights
()
eval_model_config
=
copy
.
deepcopy
(
cfg
.
model
)
eval_model
=
build_model
(
eval_model_config
,
train_cfg
=
cfg
.
get
(
'train_cfg'
),
test_cfg
=
cfg
.
get
(
'test_cfg'
))
fp16_cfg
=
cfg
.
get
(
'fp16'
,
None
)
if
fp16_cfg
is
not
None
:
wrap_fp16_model
(
eval_model
)
#eval_model.init_weights()
eval_model
.
load_state_dict
(
model
.
state_dict
())
logger
.
info
(
f
'Model:
\n
{
model
}
'
)
from
projects.mmdet3d_plugin.datasets
import
custom_build_dataset
datasets
=
[
custom_build_dataset
(
cfg
.
data
.
train
)]
if
len
(
cfg
.
workflow
)
==
2
:
val_dataset
=
copy
.
deepcopy
(
cfg
.
data
.
val
)
# in case we use a dataset wrapper
if
'dataset'
in
cfg
.
data
.
train
:
val_dataset
.
pipeline
=
cfg
.
data
.
train
.
dataset
.
pipeline
else
:
val_dataset
.
pipeline
=
cfg
.
data
.
train
.
pipeline
# set test_mode=False here in deep copied config
# which do not affect AP/AR calculation later
# refer to https://mmdetection3d.readthedocs.io/en/latest/tutorials/customize_runtime.html#customize-workflow # noqa
val_dataset
.
test_mode
=
False
datasets
.
append
(
custom_build_dataset
(
val_dataset
))
if
cfg
.
checkpoint_config
is
not
None
:
# save mmdet version, config file content and class names in
# checkpoints as meta data
cfg
.
checkpoint_config
.
meta
=
dict
(
mmdet_version
=
mmdet_version
,
mmseg_version
=
mmseg_version
,
mmdet3d_version
=
mmdet3d_version
,
config
=
cfg
.
pretty_text
,
CLASSES
=
datasets
[
0
].
CLASSES
,
PALETTE
=
datasets
[
0
].
PALETTE
# for segmentors
if
hasattr
(
datasets
[
0
],
'PALETTE'
)
else
None
)
# add an attribute for visualization convenience
model
.
CLASSES
=
datasets
[
0
].
CLASSES
custom_train_model
(
model
,
datasets
,
cfg
,
eval_model
=
eval_model
,
distributed
=
distributed
,
validate
=
(
not
args
.
no_validate
),
timestamp
=
timestamp
,
meta
=
meta
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/misc/browse_dataset.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
import
numpy
as
np
import
warnings
from
mmcv
import
Config
,
DictAction
,
mkdir_or_exist
,
track_iter_progress
from
os
import
path
as
osp
from
mmdet3d.core.bbox
import
(
Box3DMode
,
CameraInstance3DBoxes
,
Coord3DMode
,
DepthInstance3DBoxes
,
LiDARInstance3DBoxes
)
from
mmdet3d.core.visualizer
import
(
show_multi_modality_result
,
show_result
,
show_seg_result
)
from
mmdet3d.datasets
import
build_dataset
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Browse a dataset'
)
parser
.
add_argument
(
'config'
,
help
=
'train config file path'
)
parser
.
add_argument
(
'--skip-type'
,
type
=
str
,
nargs
=
'+'
,
default
=
[
'Normalize'
],
help
=
'skip some useless pipeline'
)
parser
.
add_argument
(
'--output-dir'
,
default
=
None
,
type
=
str
,
help
=
'If there is no display interface, you can save it'
)
parser
.
add_argument
(
'--task'
,
type
=
str
,
choices
=
[
'det'
,
'seg'
,
'multi_modality-det'
,
'mono-det'
],
help
=
'Determine the visualization method depending on the task.'
)
parser
.
add_argument
(
'--online'
,
action
=
'store_true'
,
help
=
'Whether to perform online visualization. Note that you often '
'need a monitor to do so.'
)
parser
.
add_argument
(
'--cfg-options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.'
)
args
=
parser
.
parse_args
()
return
args
def
build_data_cfg
(
config_path
,
skip_type
,
cfg_options
):
"""Build data config for loading visualization data."""
cfg
=
Config
.
fromfile
(
config_path
)
if
cfg_options
is
not
None
:
cfg
.
merge_from_dict
(
cfg_options
)
# import modules from string list.
if
cfg
.
get
(
'custom_imports'
,
None
):
from
mmcv.utils
import
import_modules_from_strings
import_modules_from_strings
(
**
cfg
[
'custom_imports'
])
# extract inner dataset of `RepeatDataset` as `cfg.data.train`
# so we don't need to worry about it later
if
cfg
.
data
.
train
[
'type'
]
==
'RepeatDataset'
:
cfg
.
data
.
train
=
cfg
.
data
.
train
.
dataset
# use only first dataset for `ConcatDataset`
if
cfg
.
data
.
train
[
'type'
]
==
'ConcatDataset'
:
cfg
.
data
.
train
=
cfg
.
data
.
train
.
datasets
[
0
]
train_data_cfg
=
cfg
.
data
.
train
# eval_pipeline purely consists of loading functions
# use eval_pipeline for data loading
train_data_cfg
[
'pipeline'
]
=
[
x
for
x
in
cfg
.
eval_pipeline
if
x
[
'type'
]
not
in
skip_type
]
return
cfg
def
to_depth_mode
(
points
,
bboxes
):
"""Convert points and bboxes to Depth Coord and Depth Box mode."""
if
points
is
not
None
:
points
=
Coord3DMode
.
convert_point
(
points
.
copy
(),
Coord3DMode
.
LIDAR
,
Coord3DMode
.
DEPTH
)
if
bboxes
is
not
None
:
bboxes
=
Box3DMode
.
convert
(
bboxes
.
clone
(),
Box3DMode
.
LIDAR
,
Box3DMode
.
DEPTH
)
return
points
,
bboxes
def
show_det_data
(
idx
,
dataset
,
out_dir
,
filename
,
show
=
False
):
"""Visualize 3D point cloud and 3D bboxes."""
example
=
dataset
.
prepare_train_data
(
idx
)
points
=
example
[
'points'
].
_data
.
numpy
()
gt_bboxes
=
dataset
.
get_ann_info
(
idx
)[
'gt_bboxes_3d'
].
tensor
if
dataset
.
box_mode_3d
!=
Box3DMode
.
DEPTH
:
points
,
gt_bboxes
=
to_depth_mode
(
points
,
gt_bboxes
)
show_result
(
points
,
gt_bboxes
.
clone
(),
None
,
out_dir
,
filename
,
show
=
show
,
snapshot
=
True
)
def
show_seg_data
(
idx
,
dataset
,
out_dir
,
filename
,
show
=
False
):
"""Visualize 3D point cloud and segmentation mask."""
example
=
dataset
.
prepare_train_data
(
idx
)
points
=
example
[
'points'
].
_data
.
numpy
()
gt_seg
=
example
[
'pts_semantic_mask'
].
_data
.
numpy
()
show_seg_result
(
points
,
gt_seg
.
copy
(),
None
,
out_dir
,
filename
,
np
.
array
(
dataset
.
PALETTE
),
dataset
.
ignore_index
,
show
=
show
,
snapshot
=
True
)
def
show_proj_bbox_img
(
idx
,
dataset
,
out_dir
,
filename
,
show
=
False
,
is_nus_mono
=
False
):
"""Visualize 3D bboxes on 2D image by projection."""
try
:
example
=
dataset
.
prepare_train_data
(
idx
)
except
AttributeError
:
# for Mono-3D datasets
example
=
dataset
.
prepare_train_img
(
idx
)
gt_bboxes
=
dataset
.
get_ann_info
(
idx
)[
'gt_bboxes_3d'
]
img_metas
=
example
[
'img_metas'
].
_data
img
=
example
[
'img'
].
_data
.
numpy
()
# need to transpose channel to first dim
img
=
img
.
transpose
(
1
,
2
,
0
)
# no 3D gt bboxes, just show img
if
gt_bboxes
.
tensor
.
shape
[
0
]
==
0
:
gt_bboxes
=
None
if
isinstance
(
gt_bboxes
,
DepthInstance3DBoxes
):
show_multi_modality_result
(
img
,
gt_bboxes
,
None
,
None
,
out_dir
,
filename
,
box_mode
=
'depth'
,
img_metas
=
img_metas
,
show
=
show
)
elif
isinstance
(
gt_bboxes
,
LiDARInstance3DBoxes
):
show_multi_modality_result
(
img
,
gt_bboxes
,
None
,
img_metas
[
'lidar2img'
],
out_dir
,
filename
,
box_mode
=
'lidar'
,
img_metas
=
img_metas
,
show
=
show
)
elif
isinstance
(
gt_bboxes
,
CameraInstance3DBoxes
):
show_multi_modality_result
(
img
,
gt_bboxes
,
None
,
img_metas
[
'cam2img'
],
out_dir
,
filename
,
box_mode
=
'camera'
,
img_metas
=
img_metas
,
show
=
show
)
else
:
# can't project, just show img
warnings
.
warn
(
f
'unrecognized gt box type
{
type
(
gt_bboxes
)
}
, only show image'
)
show_multi_modality_result
(
img
,
None
,
None
,
None
,
out_dir
,
filename
,
show
=
show
)
def
main
():
args
=
parse_args
()
if
args
.
output_dir
is
not
None
:
mkdir_or_exist
(
args
.
output_dir
)
cfg
=
build_data_cfg
(
args
.
config
,
args
.
skip_type
,
args
.
cfg_options
)
try
:
dataset
=
build_dataset
(
cfg
.
data
.
train
,
default_args
=
dict
(
filter_empty_gt
=
False
))
except
TypeError
:
# seg dataset doesn't have `filter_empty_gt` key
dataset
=
build_dataset
(
cfg
.
data
.
train
)
data_infos
=
dataset
.
data_infos
dataset_type
=
cfg
.
dataset_type
# configure visualization mode
vis_task
=
args
.
task
# 'det', 'seg', 'multi_modality-det', 'mono-det'
for
idx
,
data_info
in
enumerate
(
track_iter_progress
(
data_infos
)):
if
dataset_type
in
[
'KittiDataset'
,
'WaymoDataset'
]:
data_path
=
data_info
[
'point_cloud'
][
'velodyne_path'
]
elif
dataset_type
in
[
'ScanNetDataset'
,
'SUNRGBDDataset'
,
'ScanNetSegDataset'
,
'S3DISSegDataset'
,
'S3DISDataset'
]:
data_path
=
data_info
[
'pts_path'
]
elif
dataset_type
in
[
'NuScenesDataset'
,
'LyftDataset'
]:
data_path
=
data_info
[
'lidar_path'
]
elif
dataset_type
in
[
'NuScenesMonoDataset'
]:
data_path
=
data_info
[
'file_name'
]
else
:
raise
NotImplementedError
(
f
'unsupported dataset type
{
dataset_type
}
'
)
file_name
=
osp
.
splitext
(
osp
.
basename
(
data_path
))[
0
]
if
vis_task
in
[
'det'
,
'multi_modality-det'
]:
# show 3D bboxes on 3D point clouds
show_det_data
(
idx
,
dataset
,
args
.
output_dir
,
file_name
,
show
=
args
.
online
)
if
vis_task
in
[
'multi_modality-det'
,
'mono-det'
]:
# project 3D bboxes to 2D image
show_proj_bbox_img
(
idx
,
dataset
,
args
.
output_dir
,
file_name
,
show
=
args
.
online
,
is_nus_mono
=
(
dataset_type
==
'NuScenesMonoDataset'
))
elif
vis_task
in
[
'seg'
]:
# show 3D segmentation mask on 3D point clouds
show_seg_data
(
idx
,
dataset
,
args
.
output_dir
,
file_name
,
show
=
args
.
online
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/misc/fuse_conv_bn.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
import
torch
from
mmcv.runner
import
save_checkpoint
from
torch
import
nn
as
nn
from
mmdet.apis
import
init_model
def
fuse_conv_bn
(
conv
,
bn
):
"""During inference, the functionary of batch norm layers is turned off but
only the mean and var alone channels are used, which exposes the chance to
fuse it with the preceding conv layers to save computations and simplify
network structures."""
conv_w
=
conv
.
weight
conv_b
=
conv
.
bias
if
conv
.
bias
is
not
None
else
torch
.
zeros_like
(
bn
.
running_mean
)
factor
=
bn
.
weight
/
torch
.
sqrt
(
bn
.
running_var
+
bn
.
eps
)
conv
.
weight
=
nn
.
Parameter
(
conv_w
*
factor
.
reshape
([
conv
.
out_channels
,
1
,
1
,
1
]))
conv
.
bias
=
nn
.
Parameter
((
conv_b
-
bn
.
running_mean
)
*
factor
+
bn
.
bias
)
return
conv
def
fuse_module
(
m
):
last_conv
=
None
last_conv_name
=
None
for
name
,
child
in
m
.
named_children
():
if
isinstance
(
child
,
(
nn
.
BatchNorm2d
,
nn
.
SyncBatchNorm
)):
if
last_conv
is
None
:
# only fuse BN that is after Conv
continue
fused_conv
=
fuse_conv_bn
(
last_conv
,
child
)
m
.
_modules
[
last_conv_name
]
=
fused_conv
# To reduce changes, set BN as Identity instead of deleting it.
m
.
_modules
[
name
]
=
nn
.
Identity
()
last_conv
=
None
elif
isinstance
(
child
,
nn
.
Conv2d
):
last_conv
=
child
last_conv_name
=
name
else
:
fuse_module
(
child
)
return
m
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'fuse Conv and BN layers in a model'
)
parser
.
add_argument
(
'config'
,
help
=
'config file path'
)
parser
.
add_argument
(
'checkpoint'
,
help
=
'checkpoint file path'
)
parser
.
add_argument
(
'out'
,
help
=
'output path of the converted model'
)
args
=
parser
.
parse_args
()
return
args
def
main
():
args
=
parse_args
()
# build the model from a config file and a checkpoint file
model
=
init_model
(
args
.
config
,
args
.
checkpoint
)
# fuse conv and bn layers of the model
fused_model
=
fuse_module
(
model
)
save_checkpoint
(
fused_model
,
args
.
out
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/misc/print_config.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
from
mmcv
import
Config
,
DictAction
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Print the whole config'
)
parser
.
add_argument
(
'config'
,
help
=
'config file path'
)
parser
.
add_argument
(
'--options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'arguments in dict'
)
args
=
parser
.
parse_args
()
return
args
def
main
():
args
=
parse_args
()
cfg
=
Config
.
fromfile
(
args
.
config
)
if
args
.
options
is
not
None
:
cfg
.
merge_from_dict
(
args
.
options
)
print
(
f
'Config:
\n
{
cfg
.
pretty_text
}
'
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/misc/visualize_results.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
import
mmcv
from
mmcv
import
Config
from
mmdet3d.datasets
import
build_dataset
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'MMDet3D visualize the results'
)
parser
.
add_argument
(
'config'
,
help
=
'test config file path'
)
parser
.
add_argument
(
'--result'
,
help
=
'results file in pickle format'
)
parser
.
add_argument
(
'--show-dir'
,
help
=
'directory where visualize results will be saved'
)
args
=
parser
.
parse_args
()
return
args
def
main
():
args
=
parse_args
()
if
args
.
result
is
not
None
and
\
not
args
.
result
.
endswith
((
'.pkl'
,
'.pickle'
)):
raise
ValueError
(
'The results file must be a pkl file.'
)
cfg
=
Config
.
fromfile
(
args
.
config
)
cfg
.
data
.
test
.
test_mode
=
True
# build the dataset
dataset
=
build_dataset
(
cfg
.
data
.
test
)
results
=
mmcv
.
load
(
args
.
result
)
if
getattr
(
dataset
,
'show'
,
None
)
is
not
None
:
# data loading pipeline for showing
eval_pipeline
=
cfg
.
get
(
'eval_pipeline'
,
{})
if
eval_pipeline
:
dataset
.
show
(
results
,
args
.
show_dir
,
pipeline
=
eval_pipeline
)
else
:
dataset
.
show
(
results
,
args
.
show_dir
)
# use default pipeline
else
:
raise
NotImplementedError
(
'Show is not implemented for dataset {}!'
.
format
(
type
(
dataset
).
__name__
))
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/model_converters/convert_votenet_checkpoints.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
import
tempfile
import
torch
from
mmcv
import
Config
from
mmcv.runner
import
load_state_dict
from
mmdet3d.models
import
build_detector
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'MMDet3D upgrade model version(before v0.6.0) of VoteNet'
)
parser
.
add_argument
(
'checkpoint'
,
help
=
'checkpoint file'
)
parser
.
add_argument
(
'--out'
,
help
=
'path of the output checkpoint file'
)
args
=
parser
.
parse_args
()
return
args
def
parse_config
(
config_strings
):
"""Parse config from strings.
Args:
config_strings (string): strings of model config.
Returns:
Config: model config
"""
temp_file
=
tempfile
.
NamedTemporaryFile
()
config_path
=
f
'
{
temp_file
.
name
}
.py'
with
open
(
config_path
,
'w'
)
as
f
:
f
.
write
(
config_strings
)
config
=
Config
.
fromfile
(
config_path
)
# Update backbone config
if
'pool_mod'
in
config
.
model
.
backbone
:
config
.
model
.
backbone
.
pop
(
'pool_mod'
)
if
'sa_cfg'
not
in
config
.
model
.
backbone
:
config
.
model
.
backbone
[
'sa_cfg'
]
=
dict
(
type
=
'PointSAModule'
,
pool_mod
=
'max'
,
use_xyz
=
True
,
normalize_xyz
=
True
)
if
'type'
not
in
config
.
model
.
bbox_head
.
vote_aggregation_cfg
:
config
.
model
.
bbox_head
.
vote_aggregation_cfg
[
'type'
]
=
'PointSAModule'
# Update bbox_head config
if
'pred_layer_cfg'
not
in
config
.
model
.
bbox_head
:
config
.
model
.
bbox_head
[
'pred_layer_cfg'
]
=
dict
(
in_channels
=
128
,
shared_conv_channels
=
(
128
,
128
),
bias
=
True
)
if
'feat_channels'
in
config
.
model
.
bbox_head
:
config
.
model
.
bbox_head
.
pop
(
'feat_channels'
)
if
'vote_moudule_cfg'
in
config
.
model
.
bbox_head
:
config
.
model
.
bbox_head
[
'vote_module_cfg'
]
=
config
.
model
.
bbox_head
.
pop
(
'vote_moudule_cfg'
)
if
config
.
model
.
bbox_head
.
vote_aggregation_cfg
.
use_xyz
:
config
.
model
.
bbox_head
.
vote_aggregation_cfg
.
mlp_channels
[
0
]
-=
3
temp_file
.
close
()
return
config
def
main
():
"""Convert keys in checkpoints for VoteNet.
There can be some breaking changes during the development of mmdetection3d,
and this tool is used for upgrading checkpoints trained with old versions
(before v0.6.0) to the latest one.
"""
args
=
parse_args
()
checkpoint
=
torch
.
load
(
args
.
checkpoint
)
cfg
=
parse_config
(
checkpoint
[
'meta'
][
'config'
])
# Build the model and load checkpoint
model
=
build_detector
(
cfg
.
model
,
train_cfg
=
cfg
.
get
(
'train_cfg'
),
test_cfg
=
cfg
.
get
(
'test_cfg'
))
orig_ckpt
=
checkpoint
[
'state_dict'
]
converted_ckpt
=
orig_ckpt
.
copy
()
if
cfg
[
'dataset_type'
]
==
'ScanNetDataset'
:
NUM_CLASSES
=
18
elif
cfg
[
'dataset_type'
]
==
'SUNRGBDDataset'
:
NUM_CLASSES
=
10
else
:
raise
NotImplementedError
RENAME_PREFIX
=
{
'bbox_head.conv_pred.0'
:
'bbox_head.conv_pred.shared_convs.layer0'
,
'bbox_head.conv_pred.1'
:
'bbox_head.conv_pred.shared_convs.layer1'
}
DEL_KEYS
=
[
'bbox_head.conv_pred.0.bn.num_batches_tracked'
,
'bbox_head.conv_pred.1.bn.num_batches_tracked'
]
EXTRACT_KEYS
=
{
'bbox_head.conv_pred.conv_cls.weight'
:
(
'bbox_head.conv_pred.conv_out.weight'
,
[(
0
,
2
),
(
-
NUM_CLASSES
,
-
1
)]),
'bbox_head.conv_pred.conv_cls.bias'
:
(
'bbox_head.conv_pred.conv_out.bias'
,
[(
0
,
2
),
(
-
NUM_CLASSES
,
-
1
)]),
'bbox_head.conv_pred.conv_reg.weight'
:
(
'bbox_head.conv_pred.conv_out.weight'
,
[(
2
,
-
NUM_CLASSES
)]),
'bbox_head.conv_pred.conv_reg.bias'
:
(
'bbox_head.conv_pred.conv_out.bias'
,
[(
2
,
-
NUM_CLASSES
)])
}
# Delete some useless keys
for
key
in
DEL_KEYS
:
converted_ckpt
.
pop
(
key
)
# Rename keys with specific prefix
RENAME_KEYS
=
dict
()
for
old_key
in
converted_ckpt
.
keys
():
for
rename_prefix
in
RENAME_PREFIX
.
keys
():
if
rename_prefix
in
old_key
:
new_key
=
old_key
.
replace
(
rename_prefix
,
RENAME_PREFIX
[
rename_prefix
])
RENAME_KEYS
[
new_key
]
=
old_key
for
new_key
,
old_key
in
RENAME_KEYS
.
items
():
converted_ckpt
[
new_key
]
=
converted_ckpt
.
pop
(
old_key
)
# Extract weights and rename the keys
for
new_key
,
(
old_key
,
indices
)
in
EXTRACT_KEYS
.
items
():
cur_layers
=
orig_ckpt
[
old_key
]
converted_layers
=
[]
for
(
start
,
end
)
in
indices
:
if
end
!=
-
1
:
converted_layers
.
append
(
cur_layers
[
start
:
end
])
else
:
converted_layers
.
append
(
cur_layers
[
start
:])
converted_layers
=
torch
.
cat
(
converted_layers
,
0
)
converted_ckpt
[
new_key
]
=
converted_layers
if
old_key
in
converted_ckpt
.
keys
():
converted_ckpt
.
pop
(
old_key
)
# Check the converted checkpoint by loading to the model
load_state_dict
(
model
,
converted_ckpt
,
strict
=
True
)
checkpoint
[
'state_dict'
]
=
converted_ckpt
torch
.
save
(
checkpoint
,
args
.
out
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/model_converters/publish_model.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
import
subprocess
import
torch
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Process a checkpoint to be published'
)
parser
.
add_argument
(
'in_file'
,
help
=
'input checkpoint filename'
)
parser
.
add_argument
(
'out_file'
,
help
=
'output checkpoint filename'
)
args
=
parser
.
parse_args
()
return
args
def
process_checkpoint
(
in_file
,
out_file
):
checkpoint
=
torch
.
load
(
in_file
,
map_location
=
'cpu'
)
# remove optimizer for smaller file size
if
'optimizer'
in
checkpoint
:
del
checkpoint
[
'optimizer'
]
# if it is necessary to remove some sensitive data in checkpoint['meta'],
# add the code here.
torch
.
save
(
checkpoint
,
out_file
)
sha
=
subprocess
.
check_output
([
'sha256sum'
,
out_file
]).
decode
()
final_file
=
out_file
.
rstrip
(
'.pth'
)
+
'-{}.pth'
.
format
(
sha
[:
8
])
subprocess
.
Popen
([
'mv'
,
out_file
,
final_file
])
def
main
():
args
=
parse_args
()
process_checkpoint
(
args
.
in_file
,
args
.
out_file
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/model_converters/regnet2mmdet.py
0 → 100644
View file @
df3c64a9
# Copyright (c) OpenMMLab. All rights reserved.
import
argparse
import
torch
from
collections
import
OrderedDict
def
convert_stem
(
model_key
,
model_weight
,
state_dict
,
converted_names
):
new_key
=
model_key
.
replace
(
'stem.conv'
,
'conv1'
)
new_key
=
new_key
.
replace
(
'stem.bn'
,
'bn1'
)
state_dict
[
new_key
]
=
model_weight
converted_names
.
add
(
model_key
)
print
(
f
'Convert
{
model_key
}
to
{
new_key
}
'
)
def
convert_head
(
model_key
,
model_weight
,
state_dict
,
converted_names
):
new_key
=
model_key
.
replace
(
'head.fc'
,
'fc'
)
state_dict
[
new_key
]
=
model_weight
converted_names
.
add
(
model_key
)
print
(
f
'Convert
{
model_key
}
to
{
new_key
}
'
)
def
convert_reslayer
(
model_key
,
model_weight
,
state_dict
,
converted_names
):
split_keys
=
model_key
.
split
(
'.'
)
layer
,
block
,
module
=
split_keys
[:
3
]
block_id
=
int
(
block
[
1
:])
layer_name
=
f
'layer
{
int
(
layer
[
1
:])
}
'
block_name
=
f
'
{
block_id
-
1
}
'
if
block_id
==
1
and
module
==
'bn'
:
new_key
=
f
'
{
layer_name
}
.
{
block_name
}
.downsample.1.
{
split_keys
[
-
1
]
}
'
elif
block_id
==
1
and
module
==
'proj'
:
new_key
=
f
'
{
layer_name
}
.
{
block_name
}
.downsample.0.
{
split_keys
[
-
1
]
}
'
elif
module
==
'f'
:
if
split_keys
[
3
]
==
'a_bn'
:
module_name
=
'bn1'
elif
split_keys
[
3
]
==
'b_bn'
:
module_name
=
'bn2'
elif
split_keys
[
3
]
==
'c_bn'
:
module_name
=
'bn3'
elif
split_keys
[
3
]
==
'a'
:
module_name
=
'conv1'
elif
split_keys
[
3
]
==
'b'
:
module_name
=
'conv2'
elif
split_keys
[
3
]
==
'c'
:
module_name
=
'conv3'
new_key
=
f
'
{
layer_name
}
.
{
block_name
}
.
{
module_name
}
.
{
split_keys
[
-
1
]
}
'
else
:
raise
ValueError
(
f
'Unsupported conversion of key
{
model_key
}
'
)
print
(
f
'Convert
{
model_key
}
to
{
new_key
}
'
)
state_dict
[
new_key
]
=
model_weight
converted_names
.
add
(
model_key
)
def
convert
(
src
,
dst
):
"""Convert keys in pycls pretrained RegNet models to mmdet style."""
# load caffe model
regnet_model
=
torch
.
load
(
src
)
blobs
=
regnet_model
[
'model_state'
]
# convert to pytorch style
state_dict
=
OrderedDict
()
converted_names
=
set
()
for
key
,
weight
in
blobs
.
items
():
if
'stem'
in
key
:
convert_stem
(
key
,
weight
,
state_dict
,
converted_names
)
elif
'head'
in
key
:
convert_head
(
key
,
weight
,
state_dict
,
converted_names
)
elif
key
.
startswith
(
's'
):
convert_reslayer
(
key
,
weight
,
state_dict
,
converted_names
)
# check if all layers are converted
for
key
in
blobs
:
if
key
not
in
converted_names
:
print
(
f
'not converted:
{
key
}
'
)
# save checkpoint
checkpoint
=
dict
()
checkpoint
[
'state_dict'
]
=
state_dict
torch
.
save
(
checkpoint
,
dst
)
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Convert model keys'
)
parser
.
add_argument
(
'src'
,
help
=
'src detectron model path'
)
parser
.
add_argument
(
'dst'
,
help
=
'save path'
)
args
=
parser
.
parse_args
()
convert
(
args
.
src
,
args
.
dst
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/slurm_train.sh
0 → 100644
View file @
df3c64a9
#!/usr/bin/env bash
set
-x
PARTITION
=
$1
JOB_NAME
=
$2
CONFIG
=
$3
GPUS
=
${
GPUS
:-
8
}
GPUS_PER_NODE
=
${
GPUS_PER_NODE
:-
8
}
CPUS_PER_TASK
=
${
CPUS_PER_TASK
:-
5
}
SRUN_ARGS
=
${
SRUN_ARGS
:-
""
}
PY_ARGS
=
${
@
:5
}
PYTHONPATH
=
"
$(
dirname
$0
)
/.."
:
$PYTHONPATH
\
srun
-p
${
PARTITION
}
\
--job-name
=
${
JOB_NAME
}
\
--gres
=
gpu:
${
GPUS_PER_NODE
}
\
--ntasks
=
${
GPUS
}
\
--ntasks-per-node
=
${
GPUS_PER_NODE
}
\
--cpus-per-task
=
${
CPUS_PER_TASK
}
\
--kill-on-bad-exit
=
1
\
${
SRUN_ARGS
}
\
python
-u
tools/train.py
${
CONFIG
}
--launcher
=
"slurm"
${
PY_ARGS
}
autonomous_driving/occupancy_prediction/tools/test.py
0 → 100644
View file @
df3c64a9
# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------
# Modified by Xiaoyu Tian
# ---------------------------------------------
import
argparse
import
mmcv
import
os
import
sys
import
torch
import
warnings
from
mmcv
import
Config
,
DictAction
from
mmcv.cnn
import
fuse_conv_bn
from
mmcv.parallel
import
MMDataParallel
,
MMDistributedDataParallel
from
mmcv.runner
import
(
get_dist_info
,
init_dist
,
load_checkpoint
,
wrap_fp16_model
)
from
mmdet3d.datasets
import
build_dataset
from
projects.mmdet3d_plugin.datasets.builder
import
build_dataloader
from
mmdet3d.models
import
build_model
from
mmdet.apis
import
set_random_seed
from
projects.mmdet3d_plugin.bevformer.apis.test
import
custom_multi_gpu_test
from
mmdet.datasets
import
replace_ImageToTensor
import
time
import
os.path
as
osp
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'MMDet test (and eval) a model'
)
parser
.
add_argument
(
'config'
,
help
=
'test config file path'
)
parser
.
add_argument
(
'checkpoint'
,
help
=
'checkpoint file'
)
parser
.
add_argument
(
'--out'
,
help
=
'output result file in pickle format'
)
parser
.
add_argument
(
'--eval_fscore'
,
action
=
'store_true'
,
help
=
'Evaluate f score'
)
parser
.
add_argument
(
'--fuse-conv-bn'
,
action
=
'store_true'
,
help
=
'Whether to fuse conv and bn, this will slightly increase'
'the inference speed'
)
parser
.
add_argument
(
'--format-only'
,
action
=
'store_true'
,
help
=
'Format the output results without perform evaluation. It is'
'useful when you want to format the result to a specific format and '
'submit it to the test server'
)
parser
.
add_argument
(
'--eval'
,
type
=
str
,
nargs
=
'+'
,
help
=
'evaluation metrics, which depends on the dataset, e.g., "bbox",'
' "segm", "proposal" for COCO, and "mAP", "recall" for PASCAL VOC'
)
parser
.
add_argument
(
'--show'
,
action
=
'store_true'
,
help
=
'show results'
)
parser
.
add_argument
(
'--show-dir'
,
help
=
'directory where results will be saved'
)
parser
.
add_argument
(
'--gpu-collect'
,
action
=
'store_true'
,
help
=
'whether to use gpu to collect results.'
)
parser
.
add_argument
(
'--tmpdir'
,
help
=
'tmp directory used for collecting results from multiple '
'workers, available when gpu-collect is not specified'
)
parser
.
add_argument
(
'--seed'
,
type
=
int
,
default
=
0
,
help
=
'random seed'
)
parser
.
add_argument
(
'--deterministic'
,
action
=
'store_true'
,
help
=
'whether to set deterministic options for CUDNN backend.'
)
parser
.
add_argument
(
'--cfg-options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.'
)
parser
.
add_argument
(
'--options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'custom options for evaluation, the key-value pair in xxx=yyy '
'format will be kwargs for dataset.evaluate() function (deprecate), '
'change to --eval-options instead.'
)
parser
.
add_argument
(
'--eval-options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'custom options for evaluation, the key-value pair in xxx=yyy '
'format will be kwargs for dataset.evaluate() function'
)
parser
.
add_argument
(
'--launcher'
,
choices
=
[
'none'
,
'pytorch'
,
'slurm'
,
'mpi'
],
default
=
'none'
,
help
=
'job launcher'
)
parser
.
add_argument
(
'--local_rank'
,
type
=
int
,
default
=
0
)
args
=
parser
.
parse_args
()
if
'LOCAL_RANK'
not
in
os
.
environ
:
os
.
environ
[
'LOCAL_RANK'
]
=
str
(
args
.
local_rank
)
if
args
.
options
and
args
.
eval_options
:
raise
ValueError
(
'--options and --eval-options cannot be both specified, '
'--options is deprecated in favor of --eval-options'
)
if
args
.
options
:
warnings
.
warn
(
'--options is deprecated in favor of --eval-options'
)
args
.
eval_options
=
args
.
options
return
args
def
main
():
args
=
parse_args
()
# assert args.out or args.eval or args.format_only or args.show \
# or args.show_dir, \
# ('Please specify at least one operation (save/eval/format/show the '
# 'results / save the results) with the argument "--out", "--eval"'
# ', "--format-only", "--show" or "--show-dir"')
if
args
.
eval
and
args
.
format_only
:
raise
ValueError
(
'--eval and --format_only cannot be both specified'
)
if
args
.
out
is
not
None
and
not
args
.
out
.
endswith
((
'.pkl'
,
'.pickle'
)):
raise
ValueError
(
'The output file must be a pkl file.'
)
cfg
=
Config
.
fromfile
(
args
.
config
)
if
args
.
cfg_options
is
not
None
:
cfg
.
merge_from_dict
(
args
.
cfg_options
)
# import modules from string list.
if
cfg
.
get
(
'custom_imports'
,
None
):
from
mmcv.utils
import
import_modules_from_strings
import_modules_from_strings
(
**
cfg
[
'custom_imports'
])
# import modules from plguin/xx, registry will be updated
if
hasattr
(
cfg
,
'plugin'
):
if
cfg
.
plugin
:
import
importlib
if
hasattr
(
cfg
,
'plugin_dir'
):
plugin_dir
=
cfg
.
plugin_dir
_module_dir
=
os
.
path
.
dirname
(
plugin_dir
)
_module_dir
=
_module_dir
.
split
(
'/'
)
_module_path
=
_module_dir
[
0
]
for
m
in
_module_dir
[
1
:]:
_module_path
=
_module_path
+
'.'
+
m
print
(
_module_path
)
plg_lib
=
importlib
.
import_module
(
_module_path
)
else
:
# import dir is the dirpath for the config file
_module_dir
=
os
.
path
.
dirname
(
args
.
config
)
_module_dir
=
_module_dir
.
split
(
'/'
)
_module_path
=
_module_dir
[
0
]
for
m
in
_module_dir
[
1
:]:
_module_path
=
_module_path
+
'.'
+
m
print
(
_module_path
)
plg_lib
=
importlib
.
import_module
(
_module_path
)
# set cudnn_benchmark
if
cfg
.
get
(
'cudnn_benchmark'
,
False
):
torch
.
backends
.
cudnn
.
benchmark
=
True
cfg
.
model
.
pretrained
=
None
# in case the test dataset is concatenated
samples_per_gpu
=
1
if
isinstance
(
cfg
.
data
.
test
,
dict
):
cfg
.
data
.
test
.
test_mode
=
True
samples_per_gpu
=
cfg
.
data
.
test
.
pop
(
'samples_per_gpu'
,
1
)
if
samples_per_gpu
>
1
:
# Replace 'ImageToTensor' to 'DefaultFormatBundle'
cfg
.
data
.
test
.
pipeline
=
replace_ImageToTensor
(
cfg
.
data
.
test
.
pipeline
)
elif
isinstance
(
cfg
.
data
.
test
,
list
):
for
ds_cfg
in
cfg
.
data
.
test
:
ds_cfg
.
test_mode
=
True
samples_per_gpu
=
max
(
[
ds_cfg
.
pop
(
'samples_per_gpu'
,
1
)
for
ds_cfg
in
cfg
.
data
.
test
])
if
samples_per_gpu
>
1
:
for
ds_cfg
in
cfg
.
data
.
test
:
ds_cfg
.
pipeline
=
replace_ImageToTensor
(
ds_cfg
.
pipeline
)
# init distributed env first, since logger depends on the dist info.
if
args
.
launcher
==
'none'
:
distributed
=
False
else
:
distributed
=
True
init_dist
(
args
.
launcher
,
**
cfg
.
dist_params
)
# set random seeds
if
args
.
seed
is
not
None
:
set_random_seed
(
args
.
seed
,
deterministic
=
args
.
deterministic
)
# build the dataloader
dataset
=
build_dataset
(
cfg
.
data
.
test
)
if
args
.
eval_fscore
:
dataset
.
eval_fscore
=
True
data_loader
=
build_dataloader
(
dataset
,
samples_per_gpu
=
samples_per_gpu
,
workers_per_gpu
=
cfg
.
data
.
workers_per_gpu
,
dist
=
distributed
,
shuffle
=
False
,
nonshuffler_sampler
=
cfg
.
data
.
nonshuffler_sampler
,
)
# build the model and load checkpoint
cfg
.
model
.
train_cfg
=
None
model
=
build_model
(
cfg
.
model
,
test_cfg
=
cfg
.
get
(
'test_cfg'
))
fp16_cfg
=
cfg
.
get
(
'fp16'
,
None
)
if
fp16_cfg
is
not
None
:
wrap_fp16_model
(
model
)
checkpoint
=
load_checkpoint
(
model
,
args
.
checkpoint
,
map_location
=
'cpu'
)
if
args
.
fuse_conv_bn
:
model
=
fuse_conv_bn
(
model
)
# old versions did not save class info in checkpoints, this walkaround is
# for backward compatibility
if
'CLASSES'
in
checkpoint
.
get
(
'meta'
,
{}):
model
.
CLASSES
=
checkpoint
[
'meta'
][
'CLASSES'
]
else
:
model
.
CLASSES
=
dataset
.
CLASSES
# palette for visualization in segmentation tasks
if
'PALETTE'
in
checkpoint
.
get
(
'meta'
,
{}):
model
.
PALETTE
=
checkpoint
[
'meta'
][
'PALETTE'
]
elif
hasattr
(
dataset
,
'PALETTE'
):
# segmentation dataset has `PALETTE` attribute
model
.
PALETTE
=
dataset
.
PALETTE
if
not
distributed
:
assert
False
# model = MMDataParallel(model, device_ids=[0])
# outputs = single_gpu_test(model, data_loader, args.show, args.show_dir)
else
:
model
=
MMDistributedDataParallel
(
model
.
cuda
(),
device_ids
=
[
torch
.
cuda
.
current_device
()],
broadcast_buffers
=
False
)
outputs
=
custom_multi_gpu_test
(
model
,
data_loader
,
args
.
tmpdir
,
args
.
gpu_collect
)
rank
,
_
=
get_dist_info
()
if
rank
==
0
:
if
args
.
out
:
print
(
f
'
\n
writing results to
{
args
.
out
}
'
)
assert
False
#mmcv.dump(outputs['bbox_results'], args.out)
kwargs
=
{}
if
args
.
eval_options
is
None
else
args
.
eval_options
kwargs
[
'jsonfile_prefix'
]
=
osp
.
join
(
'test'
,
args
.
config
.
split
(
'/'
)[
-
1
].
split
(
'.'
)[
-
2
],
time
.
ctime
().
replace
(
' '
,
'_'
).
replace
(
':'
,
'_'
))
if
args
.
format_only
:
dataset
.
format_results
(
outputs
,
**
kwargs
)
if
args
.
eval
:
eval_kwargs
=
cfg
.
get
(
'evaluation'
,
{}).
copy
()
# hard-code way to remove EvalHook args
for
key
in
[
'interval'
,
'tmpdir'
,
'start'
,
'gpu_collect'
,
'save_best'
,
'rule'
,
'begin'
,
'end'
]:
eval_kwargs
.
pop
(
key
,
None
)
eval_kwargs
.
update
(
dict
(
metric
=
args
.
eval
,
**
kwargs
))
dataset
.
evaluate_miou
(
outputs
,
show_dir
=
args
.
show_dir
,
**
eval_kwargs
)
if
__name__
==
'__main__'
:
main
()
autonomous_driving/occupancy_prediction/tools/train.py
0 → 100644
View file @
df3c64a9
# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------
# Modified by Zhiqi Li
# ---------------------------------------------
from
__future__
import
division
import
argparse
import
copy
import
mmcv
import
os
import
time
import
torch
import
warnings
from
mmcv
import
Config
,
DictAction
from
mmcv.runner
import
get_dist_info
,
init_dist
from
os
import
path
as
osp
from
mmdet
import
__version__
as
mmdet_version
from
mmdet3d
import
__version__
as
mmdet3d_version
#from mmdet3d.apis import train_model
from
mmdet3d.datasets
import
build_dataset
from
mmdet3d.models
import
build_model
from
mmdet3d.utils
import
collect_env
,
get_root_logger
from
mmdet.apis
import
set_random_seed
from
mmseg
import
__version__
as
mmseg_version
from
mmcv.utils
import
TORCH_VERSION
,
digit_version
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Train a detector'
)
parser
.
add_argument
(
'config'
,
help
=
'train config file path'
)
parser
.
add_argument
(
'--work-dir'
,
help
=
'the dir to save logs and models'
)
parser
.
add_argument
(
'--resume-from'
,
help
=
'the checkpoint file to resume from'
)
parser
.
add_argument
(
'--no-validate'
,
action
=
'store_true'
,
help
=
'whether not to evaluate the checkpoint during training'
)
group_gpus
=
parser
.
add_mutually_exclusive_group
()
group_gpus
.
add_argument
(
'--gpus'
,
type
=
int
,
help
=
'number of gpus to use '
'(only applicable to non-distributed training)'
)
group_gpus
.
add_argument
(
'--gpu-ids'
,
type
=
int
,
nargs
=
'+'
,
help
=
'ids of gpus to use '
'(only applicable to non-distributed training)'
)
parser
.
add_argument
(
'--seed'
,
type
=
int
,
default
=
0
,
help
=
'random seed'
)
parser
.
add_argument
(
'--deterministic'
,
action
=
'store_true'
,
help
=
'whether to set deterministic options for CUDNN backend.'
)
parser
.
add_argument
(
'--options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file (deprecate), '
'change to --cfg-options instead.'
)
parser
.
add_argument
(
'--cfg-options'
,
nargs
=
'+'
,
action
=
DictAction
,
help
=
'override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.'
)
parser
.
add_argument
(
'--launcher'
,
choices
=
[
'none'
,
'pytorch'
,
'slurm'
,
'mpi'
],
default
=
'none'
,
help
=
'job launcher'
)
parser
.
add_argument
(
'--local_rank'
,
type
=
int
,
default
=
0
)
parser
.
add_argument
(
'--autoscale-lr'
,
action
=
'store_true'
,
help
=
'automatically scale lr with the number of gpus'
)
args
=
parser
.
parse_args
()
if
'LOCAL_RANK'
not
in
os
.
environ
:
os
.
environ
[
'LOCAL_RANK'
]
=
str
(
args
.
local_rank
)
if
args
.
options
and
args
.
cfg_options
:
raise
ValueError
(
'--options and --cfg-options cannot be both specified, '
'--options is deprecated in favor of --cfg-options'
)
if
args
.
options
:
warnings
.
warn
(
'--options is deprecated in favor of --cfg-options'
)
args
.
cfg_options
=
args
.
options
return
args
def
main
():
args
=
parse_args
()
cfg
=
Config
.
fromfile
(
args
.
config
)
if
args
.
cfg_options
is
not
None
:
cfg
.
merge_from_dict
(
args
.
cfg_options
)
# import modules from string list.
if
cfg
.
get
(
'custom_imports'
,
None
):
from
mmcv.utils
import
import_modules_from_strings
import_modules_from_strings
(
**
cfg
[
'custom_imports'
])
# import modules from plguin/xx, registry will be updated
if
hasattr
(
cfg
,
'plugin'
):
if
cfg
.
plugin
:
import
importlib
if
hasattr
(
cfg
,
'plugin_dir'
):
plugin_dir
=
cfg
.
plugin_dir
_module_dir
=
os
.
path
.
dirname
(
plugin_dir
)
_module_dir
=
_module_dir
.
split
(
'/'
)
_module_path
=
_module_dir
[
0
]
for
m
in
_module_dir
[
1
:]:
_module_path
=
_module_path
+
'.'
+
m
print
(
_module_path
)
plg_lib
=
importlib
.
import_module
(
_module_path
)
else
:
# import dir is the dirpath for the config file
_module_dir
=
os
.
path
.
dirname
(
args
.
config
)
_module_dir
=
_module_dir
.
split
(
'/'
)
_module_path
=
_module_dir
[
0
]
for
m
in
_module_dir
[
1
:]:
_module_path
=
_module_path
+
'.'
+
m
print
(
_module_path
)
plg_lib
=
importlib
.
import_module
(
_module_path
)
from
projects.mmdet3d_plugin.bevformer.apis.train
import
custom_train_model
# set cudnn_benchmark
if
cfg
.
get
(
'cudnn_benchmark'
,
False
):
torch
.
backends
.
cudnn
.
benchmark
=
True
# work_dir is determined in this priority: CLI > segment in file > filename
if
args
.
work_dir
is
not
None
:
# update configs according to CLI args if args.work_dir is not None
cfg
.
work_dir
=
args
.
work_dir
elif
cfg
.
get
(
'work_dir'
,
None
)
is
None
:
# use config filename as default work_dir if cfg.work_dir is None
cfg
.
work_dir
=
osp
.
join
(
'./work_dirs'
,
osp
.
splitext
(
osp
.
basename
(
args
.
config
))[
0
])
# if args.resume_from is not None:
if
args
.
resume_from
is
not
None
and
osp
.
isfile
(
args
.
resume_from
):
cfg
.
resume_from
=
args
.
resume_from
if
args
.
gpu_ids
is
not
None
:
cfg
.
gpu_ids
=
args
.
gpu_ids
else
:
cfg
.
gpu_ids
=
range
(
1
)
if
args
.
gpus
is
None
else
range
(
args
.
gpus
)
if
digit_version
(
TORCH_VERSION
)
==
digit_version
(
'1.8.1'
)
and
cfg
.
optimizer
[
'type'
]
==
'AdamW'
:
cfg
.
optimizer
[
'type'
]
=
'AdamW2'
# fix bug in Adamw
if
args
.
autoscale_lr
:
# apply the linear scaling rule (https://arxiv.org/abs/1706.02677)
cfg
.
optimizer
[
'lr'
]
=
cfg
.
optimizer
[
'lr'
]
*
len
(
cfg
.
gpu_ids
)
/
8
# init distributed env first, since logger depends on the dist info.
if
args
.
launcher
==
'none'
:
distributed
=
False
else
:
distributed
=
True
init_dist
(
args
.
launcher
,
**
cfg
.
dist_params
)
# re-set gpu_ids with distributed training mode
_
,
world_size
=
get_dist_info
()
cfg
.
gpu_ids
=
range
(
world_size
)
# create work_dir
mmcv
.
mkdir_or_exist
(
osp
.
abspath
(
cfg
.
work_dir
))
# dump config
cfg
.
dump
(
osp
.
join
(
cfg
.
work_dir
,
osp
.
basename
(
args
.
config
)))
# init the logger before other steps
timestamp
=
time
.
strftime
(
'%Y%m%d_%H%M%S'
,
time
.
localtime
())
log_file
=
osp
.
join
(
cfg
.
work_dir
,
f
'
{
timestamp
}
.log'
)
# specify logger name, if we still use 'mmdet', the output info will be
# filtered and won't be saved in the log_file
# TODO: ugly workaround to judge whether we are training det or seg model
if
cfg
.
model
.
type
in
[
'EncoderDecoder3D'
]:
logger_name
=
'mmseg'
else
:
logger_name
=
'mmdet'
logger
=
get_root_logger
(
log_file
=
log_file
,
log_level
=
cfg
.
log_level
,
name
=
logger_name
)
# init the meta dict to record some important information such as
# environment info and seed, which will be logged
meta
=
dict
()
# log env info
env_info_dict
=
collect_env
()
env_info
=
'
\n
'
.
join
([(
f
'
{
k
}
:
{
v
}
'
)
for
k
,
v
in
env_info_dict
.
items
()])
dash_line
=
'-'
*
60
+
'
\n
'
logger
.
info
(
'Environment info:
\n
'
+
dash_line
+
env_info
+
'
\n
'
+
dash_line
)
meta
[
'env_info'
]
=
env_info
meta
[
'config'
]
=
cfg
.
pretty_text
# log some basic info
logger
.
info
(
f
'Distributed training:
{
distributed
}
'
)
logger
.
info
(
f
'Config:
\n
{
cfg
.
pretty_text
}
'
)
# set random seeds
if
args
.
seed
is
not
None
:
logger
.
info
(
f
'Set random seed to
{
args
.
seed
}
, '
f
'deterministic:
{
args
.
deterministic
}
'
)
set_random_seed
(
args
.
seed
,
deterministic
=
args
.
deterministic
)
cfg
.
seed
=
args
.
seed
meta
[
'seed'
]
=
args
.
seed
meta
[
'exp_name'
]
=
osp
.
basename
(
args
.
config
)
model
=
build_model
(
cfg
.
model
,
train_cfg
=
cfg
.
get
(
'train_cfg'
),
test_cfg
=
cfg
.
get
(
'test_cfg'
))
model
.
init_weights
()
logger
.
info
(
f
'Model:
\n
{
model
}
'
)
datasets
=
[
build_dataset
(
cfg
.
data
.
train
)]
if
len
(
cfg
.
workflow
)
==
2
:
val_dataset
=
copy
.
deepcopy
(
cfg
.
data
.
val
)
# in case we use a dataset wrapper
if
'dataset'
in
cfg
.
data
.
train
:
val_dataset
.
pipeline
=
cfg
.
data
.
train
.
dataset
.
pipeline
else
:
val_dataset
.
pipeline
=
cfg
.
data
.
train
.
pipeline
# set test_mode=False here in deep copied config
# which do not affect AP/AR calculation later
# refer to https://mmdetection3d.readthedocs.io/en/latest/tutorials/customize_runtime.html#customize-workflow # noqa
val_dataset
.
test_mode
=
False
datasets
.
append
(
build_dataset
(
val_dataset
))
if
cfg
.
checkpoint_config
is
not
None
:
# save mmdet version, config file content and class names in
# checkpoints as meta data
cfg
.
checkpoint_config
.
meta
=
dict
(
mmdet_version
=
mmdet_version
,
mmseg_version
=
mmseg_version
,
mmdet3d_version
=
mmdet3d_version
,
config
=
cfg
.
pretty_text
,
CLASSES
=
datasets
[
0
].
CLASSES
,
PALETTE
=
datasets
[
0
].
PALETTE
# for segmentors
if
hasattr
(
datasets
[
0
],
'PALETTE'
)
else
None
)
# add an attribute for visualization convenience
model
.
CLASSES
=
datasets
[
0
].
CLASSES
custom_train_model
(
model
,
datasets
,
cfg
,
distributed
=
distributed
,
validate
=
(
not
args
.
no_validate
),
timestamp
=
timestamp
,
meta
=
meta
)
if
__name__
==
'__main__'
:
torch
.
multiprocessing
.
set_start_method
(
'fork'
)
main
()
autonomous_driving/occupancy_prediction/utils/vis.py
0 → 100644
View file @
df3c64a9
import
open3d
as
o3d
import
pickle
import
numpy
as
np
import
torch
import
math
from
pathlib
import
Path
import
os
from
glob
import
glob
LINE_SEGMENTS
=
[
[
4
,
0
],
[
3
,
7
],
[
5
,
1
],
[
6
,
2
],
# lines along x-axis
[
5
,
4
],
[
5
,
6
],
[
6
,
7
],
[
7
,
4
],
# lines along x-axis
[
0
,
1
],
[
1
,
2
],
[
2
,
3
],
[
3
,
0
]]
# lines along y-axis
colors_map
=
np
.
array
(
[
# [0, 0, 0, 255], # 0 undefined
[
255
,
158
,
0
,
255
],
# 1 car orange
[
0
,
0
,
230
,
255
],
# 2 pedestrian Blue
[
47
,
79
,
79
,
255
],
# 3 sign Darkslategrey
[
220
,
20
,
60
,
255
],
# 4 CYCLIST Crimson
[
255
,
69
,
0
,
255
],
# 5 traiffic_light Orangered
[
255
,
140
,
0
,
255
],
# 6 pole Darkorange
[
233
,
150
,
70
,
255
],
# 7 construction_cone Darksalmon
[
255
,
61
,
99
,
255
],
# 8 bycycle Red
[
112
,
128
,
144
,
255
],
# 9 motorcycle Slategrey
[
222
,
184
,
135
,
255
],
# 10 building Burlywood
[
0
,
175
,
0
,
255
],
# 11 vegetation Green
[
165
,
42
,
42
,
255
],
# 12 trunk nuTonomy green
[
0
,
207
,
191
,
255
],
# 13 curb, road, lane_marker, other_ground
[
75
,
0
,
75
,
255
],
# 14 walkable, sidewalk
[
255
,
0
,
0
,
255
],
# 15 unobsrvd
])
color
=
colors_map
[:,
:
3
]
/
255
def
voxel2points
(
voxel
,
voxelSize
,
range
=
[
-
40.0
,
-
40.0
,
-
1.0
,
40.0
,
40.0
,
5.4
],
ignore_labels
=
[
17
,
255
]):
if
isinstance
(
voxel
,
np
.
ndarray
):
voxel
=
torch
.
from_numpy
(
voxel
)
mask
=
torch
.
zeros_like
(
voxel
,
dtype
=
torch
.
bool
)
for
ignore_label
in
ignore_labels
:
mask
=
torch
.
logical_or
(
voxel
==
ignore_label
,
mask
)
mask
=
torch
.
logical_not
(
mask
)
occIdx
=
torch
.
where
(
mask
)
# points = torch.concatenate((np.expand_dims(occIdx[0], axis=1) * voxelSize[0], \
# np.expand_dims(occIdx[1], axis=1) * voxelSize[1], \
# np.expand_dims(occIdx[2], axis=1) * voxelSize[2]), axis=1)
points
=
torch
.
cat
((
occIdx
[
0
][:,
None
]
*
voxelSize
[
0
]
+
voxelSize
[
0
]
/
2
+
range
[
0
],
\
occIdx
[
1
][:,
None
]
*
voxelSize
[
1
]
+
voxelSize
[
1
]
/
2
+
range
[
1
],
\
occIdx
[
2
][:,
None
]
*
voxelSize
[
2
]
+
voxelSize
[
2
]
/
2
+
range
[
2
]),
dim
=
1
)
return
points
,
voxel
[
occIdx
]
def
voxel_profile
(
voxel
,
voxel_size
):
centers
=
torch
.
cat
((
voxel
[:,
:
2
],
voxel
[:,
2
][:,
None
]
-
voxel_size
[
2
]
/
2
),
dim
=
1
)
# centers = voxel
wlh
=
torch
.
cat
((
torch
.
tensor
(
voxel_size
[
0
]).
repeat
(
centers
.
shape
[
0
])[:,
None
],
torch
.
tensor
(
voxel_size
[
1
]).
repeat
(
centers
.
shape
[
0
])[:,
None
],
torch
.
tensor
(
voxel_size
[
2
]).
repeat
(
centers
.
shape
[
0
])[:,
None
]),
dim
=
1
)
yaw
=
torch
.
full_like
(
centers
[:,
0
:
1
],
0
)
return
torch
.
cat
((
centers
,
wlh
,
yaw
),
dim
=
1
)
def
rotz
(
t
):
"""Rotation about the z-axis."""
c
=
torch
.
cos
(
t
)
s
=
torch
.
sin
(
t
)
return
torch
.
tensor
([[
c
,
-
s
,
0
],
[
s
,
c
,
0
],
[
0
,
0
,
1
]])
def
my_compute_box_3d
(
center
,
size
,
heading_angle
):
h
,
w
,
l
=
size
[:,
2
],
size
[:,
0
],
size
[:,
1
]
heading_angle
=
-
heading_angle
-
math
.
pi
/
2
center
[:,
2
]
=
center
[:,
2
]
+
h
/
2
#R = rotz(1 * heading_angle)
l
,
w
,
h
=
(
l
/
2
).
unsqueeze
(
1
),
(
w
/
2
).
unsqueeze
(
1
),
(
h
/
2
).
unsqueeze
(
1
)
x_corners
=
torch
.
cat
([
-
l
,
l
,
l
,
-
l
,
-
l
,
l
,
l
,
-
l
],
dim
=
1
)[...,
None
]
y_corners
=
torch
.
cat
([
w
,
w
,
-
w
,
-
w
,
w
,
w
,
-
w
,
-
w
],
dim
=
1
)[...,
None
]
z_corners
=
torch
.
cat
([
h
,
h
,
h
,
h
,
-
h
,
-
h
,
-
h
,
-
h
],
dim
=
1
)[...,
None
]
#corners_3d = R @ torch.vstack([x_corners, y_corners, z_corners])
corners_3d
=
torch
.
cat
([
x_corners
,
y_corners
,
z_corners
],
dim
=
2
)
corners_3d
[...,
0
]
+=
center
[:,
0
:
1
]
corners_3d
[...,
1
]
+=
center
[:,
1
:
2
]
corners_3d
[...,
2
]
+=
center
[:,
2
:
3
]
return
corners_3d
def
generate_the_ego_car
():
ego_range
=
[
-
2
,
-
1
,
0
,
2
,
1
,
1.5
]
ego_voxel_size
=
[
0.1
,
0.1
,
0.1
]
ego_xdim
=
int
((
ego_range
[
3
]
-
ego_range
[
0
])
/
ego_voxel_size
[
0
])
ego_ydim
=
int
((
ego_range
[
4
]
-
ego_range
[
1
])
/
ego_voxel_size
[
1
])
ego_zdim
=
int
((
ego_range
[
5
]
-
ego_range
[
2
])
/
ego_voxel_size
[
2
])
ego_voxel_num
=
ego_xdim
*
ego_ydim
*
ego_zdim
temp_x
=
np
.
arange
(
ego_xdim
)
temp_y
=
np
.
arange
(
ego_ydim
)
temp_z
=
np
.
arange
(
ego_zdim
)
ego_xyz
=
np
.
stack
(
np
.
meshgrid
(
temp_y
,
temp_x
,
temp_z
),
axis
=-
1
).
reshape
(
-
1
,
3
)
ego_point_x
=
(
ego_xyz
[:,
0
:
1
]
+
0.5
)
/
ego_xdim
*
(
ego_range
[
3
]
-
ego_range
[
0
])
+
ego_range
[
0
]
ego_point_y
=
(
ego_xyz
[:,
1
:
2
]
+
0.5
)
/
ego_ydim
*
(
ego_range
[
4
]
-
ego_range
[
1
])
+
ego_range
[
1
]
ego_point_z
=
(
ego_xyz
[:,
2
:
3
]
+
0.5
)
/
ego_zdim
*
(
ego_range
[
5
]
-
ego_range
[
2
])
+
ego_range
[
2
]
ego_point_xyz
=
np
.
concatenate
((
ego_point_y
,
ego_point_x
,
ego_point_z
),
axis
=-
1
)
ego_points_label
=
(
np
.
ones
((
ego_point_xyz
.
shape
[
0
]))
*
16
).
astype
(
np
.
uint8
)
ego_dict
=
{}
ego_dict
[
'point'
]
=
ego_point_xyz
ego_dict
[
'label'
]
=
ego_points_label
return
ego_point_xyz
def
show_point_cloud
(
points
:
np
.
ndarray
,
colors
=
True
,
points_colors
=
None
,
obj_bboxes
=
None
,
voxelize
=
False
,
bbox_corners
=
None
,
linesets
=
None
,
ego_pcd
=
None
,
scene_idx
=
0
,
frame_idx
=
0
,
large_voxel
=
True
,
voxel_size
=
0.4
)
->
None
:
vis
=
o3d
.
visualization
.
VisualizerWithKeyCallback
()
vis
.
create_window
(
str
(
scene_idx
))
opt
=
vis
.
get_render_option
()
opt
.
background_color
=
np
.
asarray
([
1
,
1
,
1
])
pcd
=
o3d
.
geometry
.
PointCloud
()
pcd
.
points
=
o3d
.
utility
.
Vector3dVector
(
points
)
if
colors
:
pcd
.
colors
=
o3d
.
utility
.
Vector3dVector
(
points_colors
[:,
:
3
])
mesh_frame
=
o3d
.
geometry
.
TriangleMesh
.
create_coordinate_frame
(
size
=
1.6
,
origin
=
[
0
,
0
,
0
])
pcd
.
points
=
o3d
.
utility
.
Vector3dVector
(
points
)
voxelGrid
=
o3d
.
geometry
.
VoxelGrid
.
create_from_point_cloud
(
pcd
,
voxel_size
=
voxel_size
)
if
large_voxel
:
vis
.
add_geometry
(
voxelGrid
)
else
:
vis
.
add_geometry
(
pcd
)
if
voxelize
:
line_sets
=
o3d
.
geometry
.
LineSet
()
line_sets
.
points
=
o3d
.
open3d
.
utility
.
Vector3dVector
(
bbox_corners
.
reshape
((
-
1
,
3
)))
line_sets
.
lines
=
o3d
.
open3d
.
utility
.
Vector2iVector
(
linesets
.
reshape
((
-
1
,
2
)))
line_sets
.
paint_uniform_color
((
0
,
0
,
0
))
vis
.
add_geometry
(
mesh_frame
)
vis
.
add_geometry
(
pcd
)
view_control
=
vis
.
get_view_control
()
view_control
.
set_lookat
(
np
.
array
([
0
,
0
,
0
]))
vis
.
add_geometry
(
line_sets
)
vis
.
poll_events
()
vis
.
update_renderer
()
return
vis
def
vis_nuscene
():
voxelSize
=
[
0.4
,
0.4
,
0.4
]
point_cloud_range
=
[
-
40.0
,
-
40.0
,
-
1.0
,
40.0
,
40.0
,
5.4
]
ignore_labels
=
[
17
,
255
]
vis_voxel_size
=
0.4
file
=
"data/29796060110c4163b07f06eff4af0753/labels.npz"
data
=
np
.
load
(
file
)
semantics
,
mask_lidar
,
mask_camera
=
data
[
'semantics'
],
data
[
'mask_lidar'
],
data
[
'mask_camera'
]
voxels
=
semantics
points
,
labels
=
voxel2points
(
voxels
,
voxelSize
,
range
=
point_cloud_range
,
ignore_labels
=
ignore_labels
)
points
=
points
.
numpy
()
labels
=
labels
.
numpy
()
pcd_colors
=
color
[
labels
.
astype
(
int
)
%
len
(
color
)]
bboxes
=
voxel_profile
(
torch
.
tensor
(
points
),
voxelSize
)
ego_pcd
=
o3d
.
geometry
.
PointCloud
()
ego_points
=
generate_the_ego_car
()
ego_pcd
.
points
=
o3d
.
utility
.
Vector3dVector
(
ego_points
)
bboxes_corners
=
my_compute_box_3d
(
bboxes
[:,
0
:
3
],
bboxes
[:,
3
:
6
],
bboxes
[:,
6
:
7
])
bases_
=
torch
.
arange
(
0
,
bboxes_corners
.
shape
[
0
]
*
8
,
8
)
edges
=
torch
.
tensor
([[
0
,
1
],
[
1
,
2
],
[
2
,
3
],
[
3
,
0
],
[
4
,
5
],
[
5
,
6
],
[
6
,
7
],
[
7
,
4
],
[
0
,
4
],
[
1
,
5
],
[
2
,
6
],
[
3
,
7
]])
# lines along y-axis
edges
=
edges
.
reshape
((
1
,
12
,
2
)).
repeat
(
bboxes_corners
.
shape
[
0
],
1
,
1
)
edges
=
edges
+
bases_
[:,
None
,
None
]
vis
=
show_point_cloud
(
points
=
points
,
colors
=
True
,
points_colors
=
pcd_colors
,
voxelize
=
True
,
obj_bboxes
=
None
,
bbox_corners
=
bboxes_corners
.
numpy
(),
linesets
=
edges
.
numpy
(),
ego_pcd
=
ego_pcd
,
large_voxel
=
True
,
voxel_size
=
vis_voxel_size
)
# control view
# view_control = vis.get_view_control()
# view_control.set_zoom(args.zoom)
# view_control.set_up(args.up_vec)
# view_control.set_front(args.front_vec)
# view_control.set_lookat(np.array([points.mean(axis=0)[0], 0, 0]))
# vis.poll_events()
# vis.update_renderer()
vis
.
run
()
# vis.capture_screen_image(os.path.join(images_outdir, "{}.png".format(file_name)))
vis
.
destroy_window
()
del
vis
if
__name__
==
'__main__'
:
vis_nuscene
()
\ No newline at end of file
Prev
1
…
4
5
6
7
8
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment