-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Hackathon No.166】Paddle3D目标检测结果可视化 #272
Open
lizechng
wants to merge
6
commits into
PaddlePaddle:develop
Choose a base branch
from
lizechng:Chng
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import os | ||
import numpy as np | ||
|
||
from paddle3d.apis.infer import Infer | ||
from paddle3d.apis.config import Config | ||
from paddle3d.slim import get_qat_config | ||
from paddle3d.utils.checkpoint import load_pretrained_model | ||
|
||
|
||
def parse_args(): | ||
""" | ||
""" | ||
parser = argparse.ArgumentParser(description='Model evaluation') | ||
# params of training | ||
parser.add_argument( | ||
"--config", dest="cfg", help="The config file.", default=None, type=str) | ||
parser.add_argument( | ||
'--batch_size', | ||
dest='batch_size', | ||
help='Mini batch size of one gpu or cpu', | ||
type=int, | ||
default=None) | ||
parser.add_argument( | ||
'--model', | ||
dest='model', | ||
help='pretrained parameters of the model', | ||
type=str, | ||
default=None) | ||
parser.add_argument( | ||
'--num_workers', | ||
dest='num_workers', | ||
help='Num workers for data loader', | ||
type=int, | ||
default=2) | ||
parser.add_argument( | ||
'--quant_config', | ||
dest='quant_config', | ||
help='Config for quant model.', | ||
default=None, | ||
type=str) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def worker_init_fn(worker_id): | ||
np.random.seed(1024) | ||
|
||
|
||
def main(args): | ||
""" | ||
""" | ||
if args.cfg is None: | ||
raise RuntimeError("No configuration file specified!") | ||
|
||
if not os.path.exists(args.cfg): | ||
raise RuntimeError("Config file `{}` does not exist!".format(args.cfg)) | ||
|
||
cfg = Config(path=args.cfg, batch_size=args.batch_size) | ||
|
||
if cfg.val_dataset is None: | ||
raise RuntimeError( | ||
'The validation dataset is not specified in the configuration file!' | ||
) | ||
elif len(cfg.val_dataset) == 0: | ||
raise ValueError( | ||
'The length of validation dataset is 0. Please check if your dataset is valid!' | ||
) | ||
|
||
dic = cfg.to_dict() | ||
batch_size = dic.pop('batch_size') | ||
dic.update({ | ||
'dataloader_fn': { | ||
'batch_size': batch_size, | ||
'num_workers': args.num_workers, | ||
'worker_init_fn': worker_init_fn | ||
} | ||
}) | ||
|
||
if args.quant_config: | ||
quant_config = get_qat_config(args.quant_config) | ||
cfg.model.build_slim_model(quant_config['quant_config']) | ||
|
||
if args.model is not None: | ||
load_pretrained_model(cfg.model, args.model) | ||
dic['checkpoint'] = None | ||
dic['resume'] = False | ||
else: | ||
dic['resume'] = True | ||
|
||
infer = Infer(**dic) | ||
infer.infer('bev') | ||
|
||
|
||
if __name__ == '__main__': | ||
args = parse_args() | ||
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Copyright |
||
import numpy as np | ||
import paddle | ||
from paddle.inference import Config, create_predictor | ||
from paddle3d.ops.iou3d_nms_cuda import nms_gpu | ||
from .utils import preprocess, Calibration, show_bev_with_boxes | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--model_file", | ||
type=str, | ||
help="Model filename, Specify this when your model is a combined model.", | ||
required=True) | ||
parser.add_argument( | ||
"--params_file", | ||
type=str, | ||
help= | ||
"Parameter filename, Specify this when your model is a combined model.", | ||
required=True) | ||
parser.add_argument( | ||
'--lidar_file', type=str, help='The lidar path.', required=True) | ||
parser.add_argument( | ||
'--calib_file', type=str, help='The lidar path.', required=True) | ||
parser.add_argument( | ||
"--num_point_dim", | ||
type=int, | ||
default=4, | ||
help="Dimension of a point in the lidar file.") | ||
parser.add_argument( | ||
"--point_cloud_range", | ||
dest='point_cloud_range', | ||
nargs='+', | ||
help="Range of point cloud for voxelize operation.", | ||
type=float, | ||
default=None) | ||
parser.add_argument( | ||
"--voxel_size", | ||
dest='voxel_size', | ||
nargs='+', | ||
help="Size of voxels for voxelize operation.", | ||
type=float, | ||
default=None) | ||
parser.add_argument( | ||
"--max_points_in_voxel", | ||
type=int, | ||
default=100, | ||
help="Maximum number of points in a voxel.") | ||
parser.add_argument( | ||
"--max_voxel_num", | ||
type=int, | ||
default=12000, | ||
help="Maximum number of voxels.") | ||
parser.add_argument("--gpu_id", type=int, default=0, help="GPU card id.") | ||
parser.add_argument( | ||
"--use_trt", | ||
type=int, | ||
default=0, | ||
help="Whether to use tensorrt to accelerate when using gpu.") | ||
parser.add_argument( | ||
"--trt_precision", | ||
type=int, | ||
default=0, | ||
help="Precision type of tensorrt, 0: kFloat32, 1: kHalf.") | ||
parser.add_argument( | ||
"--trt_use_static", | ||
type=int, | ||
default=0, | ||
help="Whether to load the tensorrt graph optimization from a disk path." | ||
) | ||
parser.add_argument( | ||
"--trt_static_dir", | ||
type=str, | ||
help="Path of a tensorrt graph optimization directory.") | ||
parser.add_argument( | ||
"--collect_shape_info", | ||
type=int, | ||
default=0, | ||
help="Whether to collect dynamic shape before using tensorrt.") | ||
parser.add_argument( | ||
"--dynamic_shape_file", | ||
type=str, | ||
default="", | ||
help="Path of a dynamic shape file for tensorrt.") | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def init_predictor(model_file, | ||
params_file, | ||
gpu_id=0, | ||
use_trt=False, | ||
trt_precision=0, | ||
trt_use_static=False, | ||
trt_static_dir=None, | ||
collect_shape_info=False, | ||
dynamic_shape_file=None): | ||
config = Config(model_file, params_file) | ||
config.enable_memory_optim() | ||
config.enable_use_gpu(1000, gpu_id) | ||
if use_trt: | ||
precision_mode = paddle.inference.PrecisionType.Float32 | ||
if trt_precision == 1: | ||
precision_mode = paddle.inference.PrecisionType.Half | ||
config.enable_tensorrt_engine( | ||
workspace_size=1 << 30, | ||
max_batch_size=1, | ||
min_subgraph_size=10, | ||
precision_mode=precision_mode, | ||
use_static=trt_use_static, | ||
use_calib_mode=False) | ||
if collect_shape_info: | ||
config.collect_shape_range_info(dynamic_shape_file) | ||
else: | ||
config.enable_tuned_tensorrt_dynamic_shape(dynamic_shape_file, True) | ||
if trt_use_static: | ||
config.set_optim_cache_dir(trt_static_dir) | ||
|
||
predictor = create_predictor(config) | ||
return predictor | ||
|
||
|
||
def run(predictor, voxels, coords, num_points_per_voxel): | ||
input_names = predictor.get_input_names() | ||
for i, name in enumerate(input_names): | ||
input_tensor = predictor.get_input_handle(name) | ||
if name == "voxels": | ||
input_tensor.reshape(voxels.shape) | ||
input_tensor.copy_from_cpu(voxels.copy()) | ||
elif name == "coords": | ||
input_tensor.reshape(coords.shape) | ||
input_tensor.copy_from_cpu(coords.copy()) | ||
elif name == "num_points_per_voxel": | ||
input_tensor.reshape(num_points_per_voxel.shape) | ||
input_tensor.copy_from_cpu(num_points_per_voxel.copy()) | ||
|
||
# do the inference | ||
predictor.run() | ||
|
||
# get out data from output tensor | ||
output_names = predictor.get_output_names() | ||
for i, name in enumerate(output_names): | ||
output_tensor = predictor.get_output_handle(name) | ||
if i == 0: | ||
box3d_lidar = output_tensor.copy_to_cpu() | ||
elif i == 1: | ||
label_preds = output_tensor.copy_to_cpu() | ||
elif i == 2: | ||
scores = output_tensor.copy_to_cpu() | ||
return box3d_lidar, label_preds, scores | ||
|
||
|
||
if __name__ == '__main__': | ||
args = parse_args() | ||
|
||
predictor = init_predictor(args.model_file, args.params_file, args.gpu_id, | ||
args.use_trt, args.trt_precision, | ||
args.trt_use_static, args.trt_static_dir, | ||
args.collect_shape_info, args.dynamic_shape_file) | ||
voxels, coords, num_points_per_voxel = preprocess( | ||
args.lidar_file, args.num_point_dim, args.point_cloud_range, | ||
args.voxel_size, args.max_points_in_voxel, args.max_voxel_num) | ||
box3d_lidar, label_preds, scores = run(predictor, voxels, coords, | ||
num_points_per_voxel) | ||
|
||
scan = np.fromfile(args.lidar_file, dtype=np.float32) | ||
pc_velo = scan.reshape((-1, 4)) | ||
|
||
# Obtain calibration information about Kitti | ||
calib = Calibration(args.calib_file) | ||
|
||
# Plot box in lidar cloud | ||
show_bev_with_boxes(pc_velo, box3d_lidar, scores, calib) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add Copyright