MMdetection3D 单目图像推理(3D目标检测)
梳理了MMdetection3D 单目图像推理的过程。由于MMdetection3D 具体的安装方式,在官方文档和大部分文章都有,这里不介绍。
单目推理的完整代码。
import mmcv
import mmengine
from mmdet3d.apis import init_model, inference_mono_3d_detector
from mmdet3d.visualization import Det3DLocalVisualizer
def main():
# 1. 构建并导入模型
config_file = 'configs/fcos3d/fcos3d_r101-caffe-dcn_fpn_head-gn_8xb2-1x_nus-mono3d.py'
checkpoint_file = 'checkpoint/fcos3d_r101_caffe_fpn_gn-head_dcn_2x8_1x_nus-mono3d_20210715_235813-4bed5239.pth'
model = init_model(config_file, checkpoint_file, device='cuda:0')
# 2. 设置图片路径和相机内参矩阵路径
#img_ = 'demo/data/kitti/000008.png' # G:/Data/KITTI/test/02/image_0/000091.png demo/data/kitti/000008.png
#ann_ = 'demo/data/kitti/000008.pkl' # data/kitti/KITTI00-02.json demo/data/kitti/000008.pkl
img = 'G:/Data/KITTI/test/02/image_0/000091.png' # G:/Data/KITTI/test/02/image_0/000091.png demo/data/kitti/000008.png
ann = 'data/kitti/KITTI00-02.json' # data/kitti/KITTI00-02.json demo/data/kitti/000008.pkl
# 3. 打印.pkl文件的内容
data_list = mmengine.load(ann)
print(data_list)
# 4. 推理并打印结果
result = inference_mono_3d_detector(model, img, ann, 'CAM2')
print(result)
# 5. 显示初始化
visualizer = Det3DLocalVisualizer() #VISUALIZERS.build(model.cfg.visualizer)
visualizer.dataser_meta = model.dataset_meta
# 6. 加载图像并修改数据格式
img = mmcv.imread(img)
img = mmcv.imconvert(img, 'bgr', 'rgb')
# 7. 显示推理结果
data_input = dict(img=img)
visualizer.add_datasample(
'result',
data_input,
data_sample=result,
draw_gt=False,
show=True,
wait_time=0,
out_file='./outputs/fcos3d.png',
pred_score_thr=0.2,
vis_task='mono_det')
if __name__ == '__main__':
main()
步骤1:加载模型文件和权重。
模型文件在MMdetection3D项目的configs/fcos3d文件夹中,

对应权重文件可以在Github下载。https://github.com/open-mmlab/mmdetection3d/tree/main/configs,转到该链接后,选择并点击fcos3d模型,网页跳转后,拉到下面,下载预训练权重。

步骤2:设置图片路径和内参文件路径。
官方文档中给的demo文件路径为已经注释掉的img_和ann_,其中,ann_包含了很多信息,如相机内参矩阵、雷达和相机的标定矩阵、推理结果等,内容可以使用步骤3中的代码查看,具体如下,
{
'metainfo': {'DATASET': 'KITTI'},
'data_list':
[
{
'sample_id': 8,
'images': {
'CAM0': {
'cam2img': [
[721.5377, 0.0, 609.5593, 0.0],
[0.0, 721.5377, 172.854, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]],
'lidar2img': [
[609.6954175209152, -721.4215943316945, -1.2512579994207245, -167.89907838168253],
[180.38420408453626, 7.644797969406144, -719.6515015339527, -101.2330630658172],
[0.9999454021453857, 0.00012436544056981802, 0.010451302863657475, -0.2721327841281891],
[0.0, 0.0, 0.0, 1.0]]
},
'CAM1': {
'cam2img': [
[721.5377, 0.0, 609.5593, -387.5744],
[0.0, 721.5377, 172.854, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]],
'lidar2img': [
[609.6954175209152, -721.4215943316945, -1.2512579994207245, -555.4734783816825],
[180.38420408453626, 7.644797969406144, -719.6515015339527, -101.2330630658172],
[0.9999454021453857, 0.00012436544056981802, 0.010451302863657475, -0.2721327841281891],
[0.0, 0.0, 0.0, 1.0]]
},
'CAM2': {
'img_path': '000008.png',
'height': 375,
'width': 1242,
'cam2img': [
[721.5377, 0.0, 609.5593, 44.85728],
[0.0, 721.5377, 172.854, 0.2163791],
[0.0, 0.0, 1.0, 0.002745884],
[0.0, 0.0, 0.0, 1.0]],
'lidar2cam': [
[0.00023477380455005914, -0.9999441504478455, -0.01056347694247961, -0.0027968171052634716],
[0.010449407622218132, 0.01056535355746746, -0.999889612197876, -0.07510878890752792],
[0.9999454021453857, 0.00012436544056981802, 0.010451302863657475, -0.2721327841281891],
[0.0, 0.0, 0.0, 1.0]],
'lidar2img': [
[609.6954175209152, -721.4215943316945, -1.2512579994207245, -123.04179838168253],
[180.38420408453626, 7.644797969406144, -719.6515015339527, -101.01668396581721],
[0.9999454021453857, 0.00012436544056981802, 0.010451302863657475, -0.2693869001281891],
[0.0, 0.0, 0.0, 1.0]]
},
'CAM3': {
'cam2img': [
[721.5377, 0.0, 609.5593, -339.5242],
[0.0, 721.5377, 172.854, 2.199936],
[0.0, 0.0, 1.0, 0.002729905],
[0.0, 0.0, 0.0, 1.0]],
'lidar2img': [
[609.6954175209152, -721.4215943316945, -1.2512579994207245, -507.4232783816825],
[180.38420408453626, 7.644797969406144, -719.6515015339527, -99.03312706581721],
[0.9999454021453857, 0.00012436544056981802, 0.010451302863657475, -0.2694028791281891],
[0.0, 0.0, 0.0, 1.0]]
},
'R0_rect': [
[0.9999238848686218, 0.009837759658694267, -0.007445048075169325, 0.0],
[-0.00986979529261589, 0.9999421238899231, -0.004278459120541811, 0.0],
[0.007402527146041393, 0.0043516140431165695, 0.999963104724884, 0.0],
[0.0, 0.0, 0.0, 1.0]]
},
'lidar_points': {
'num_pts_feats': 4,
'lidar_path': '000008.bin',
'Tr_velo_to_cam': [
[0.0075337449088692665, -0.9999713897705078, -0.00061660201754421, -0.004069766029715538],
[0.01480249036103487, 0.0007280732970684767, -0.9998902082443237, -0.07631617784500122],
[0.9998620748519897, 0.007523790001869202, 0.014807550236582756, -0.2717806100845337],
[0.0, 0.0, 0.0, 1.0]],
'Tr_imu_to_velo': [
[0.999997615814209, 0.0007553070900030434, -0.002035825978964567, -0.8086758852005005],
[-0.0007854027207940817, 0.9998897910118103, -0.014822980388998985, 0.3195559084415436],
[0.002024406101554632, 0.014824540354311466, 0.9998881220817566, -0.7997230887413025],
[0.0, 0.0, 0.0, 1.0]]
},
'instances': [
{
'bbox': [0.0, 192.37, 402.31, 374.0],
'bbox_label': 2,
'bbox_3d': [-2.7, 1.74, 3.68, 3.23, 1.6, 1.57, -1.29],
'bbox_label_3d': 2,
'depth': 3.682745933532715,
'center_2d': [92.29086303710938, 356.9522705078125],
'num_lidar_pts': 1325,
'difficulty': -1,
'truncated': 0.88,
'occluded': 3,
'alpha': -0.69,
'score': 0.0,
'index': 0,
'group_id': 0},
{
'bbox': [334.85, 178.94, 624.5, 372.04],
'bbox_label': 2,
'bbox_3d': [-1.17, 1.65, 7.86, 3.68, 1.57, 1.5, 1.9],
'bbox_label_3d': 2,
'depth': 7.862746238708496,
'center_2d': [507.68450927734375, 252.1992950439453],
'num_lidar_pts': 1900,
'difficulty': 1,
'truncated': 0.0,
'occluded': 1,
'alpha': 2.04,
'score': 0.0,
'index': 1,
'group_id': 1},
{
'bbox':
[937.29, 197.39, 1241.0, 374.0],
'bbox_label': 2,
'bbox_3d': [3.81, 1.64, 6.15, 3.08, 1.39, 1.44, -1.31],
'bbox_label_3d': 2,
'depth': 6.152746200561523,
'center_2d': [1063.3797607421875, 283.6329650878906],
'num_lidar_pts': 881,
'difficulty': -1,
'truncated': 0.34,
'occluded': 3,
'alpha': -1.84,
'score': 0.0,
'index': 2,
'group_id': 2},
{
'bbox': [597.59, 176.18, 720.9, 261.14],
'bbox_label': 2,
'bbox_3d': [1.07, 1.55, 14.44, 3.66, 1.47, 1.6, -1.25],
'bbox_label_3d': 2,
'depth': 14.442745208740234,
'center_2d': [666.0049438476562, 213.5522918701172],
'num_lidar_pts': 659,
'difficulty': 1,
'truncated': 0.0,
'occluded': 1,
'alpha': -1.33,
'score': 0.0,
'index': 3,
'group_id': 3},
{
'bbox': [741.18, 168.83, 792.25, 208.43],
'bbox_label': 2,
'bbox_3d': [7.24, 1.55, 33.2, 4.08, 1.7, 1.63, 1.95],
'bbox_label_3d': 2,
'depth': 33.2027473449707,
'center_2d': [768.1942749023438, 188.05809020996094],
'num_lidar_pts': 55,
'difficulty': 1,
'truncated': 0.0,
'occluded': 0,
'alpha': 1.74,
'score': 0.0,
'index': 4,
'group_id': 4},
{
'bbox': [884.52, 178.31, 956.41, 240.18],
'bbox_label': 2,
'bbox_3d': [8.48, 1.75, 19.96, 2.47, 1.59, 1.59, -1.25],
'bbox_label_3d': 2,
'depth': 19.962745666503906,
'center_2d': [918.2254028320312, 207.35877990722656],
'num_lidar_pts': 162,
'difficulty': 0,
'truncated': 0.0,
'occluded': 0,
'alpha': -1.65,
'score': 0.0,
'index': 5,
'group_id': 5},
{
'bbox': [800.38, 163.67, 825.45, 184.07],
'bbox_label': -1,
'bbox_3d': [-1000.0, -1000.0, -1000.0, -1.0, -1.0, -1.0, -10.0],
'bbox_label_3d': -1,
'depth': -999.9972534179688,
'center_2d': [1331.055908203125, 894.033203125],
'num_lidar_pts': -1,
'difficulty': -1,
'truncated': -1.0,
'occluded': -1,
'alpha': -10.0,
'score': 0.0,
'index': -1,
'group_id': 6},
{
'bb4179688',
'center_2d': [1331.055908203125, 894.033203125],
'num_lidar_pts': -1,
'difficulty': -1,
'truncated': -1.0,
'occluded': -1,
'alpha': -10.0,
'score': 0.0,
'index': -1,
'group_id': 7},
{
'bbox': [801.81, 163.96, 825.2, 183.59],
'bbox_label': -1,
'bbox_3d': [-1000.0, -1000.0, -1000.0, -1.0, -1.0, -1.0, -10.0],
'bbox_label_3d': -1,
'depth': -999.9972534179688,
'center_2d': [1331.055908203125, 894.033203125],
'num_lidar_pts': -1,
'difficulty': -1,
'truncated': -1.0,
'occluded': -1,
'alpha': -10.0,
'score': 0.0,
'index': -1,
'group_id': 8},
{
'bbox': [826.87, 162.28, 845.84, 178.86],
'bbox_label': -1,
'bbox_3d': [-1000.0, -1000.0, -1000.0, -1.0, -1.0, -1.0, -10.0],
'bbox_label_3d': -1,
'depth': -999.9972534179688,
'center_2d': [1331.055908203125, 894.033203125],
'num_lidar_pts': -1,
'difficulty': -1,
'truncated': -1.0,
'occluded': -1,
'alpha': -10.0,
'score': 0.0,
'index': -1,
'group_id': 9
}
]
}
]
}
然而在单目推理中,根据单目推理函数inference_mono_3d_detector(model, img, ann, 'CAM2'),仅需要彩色相机的内参矩阵信息即可('CAM2'),另外,推理过程中需要根据不同数据集修改相机的内参矩阵信息,原始的文件格式修改并不方便。因此可以将'data_list'中的'CAM2'单独拿出来,写入到.json文件中,对应代码中的data/kitti/KITTI00-02.json,里面的参数"img_path"、"height"、"width"、"cam2img"根据需要修改即可,注意,"img_path"需要和待推理的图像路径对应上。
{
"data_list":
[
{
"sample_id": 0,
"images":
{
"CAM2": {
"img_path": "G:/Data/KITTI/test/02/image_0/000091.png",
"height": 376,
"width": 1241,
"cam2img": [
[718.856, 0.0, 607.1928, 44.85728],
[0.0, 718.856, 185.2157, 0.2163791],
[0.0, 0.0, 1.0, 0.002745884],
[0.0, 0.0, 0.0, 1.0]]
}
}
}
]
}
步骤7:显示推理结果。
visualizer.add_datasample()里面的参数根据需求更改,个人认为后面两个比较重要,pred_score_thr和vis_task,这里的pred_score_thr就是目标检测中的分数阈值,vis_task这个就是任务模式,单目推理只能选择'mono_det'。多模态或者点云需要改成其他的参数,具体查看函数定义就好了。
至于中间的步骤就无脑复制就可以了......
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)