MMDetection(二):目标检测的示例运行

MMDetection(二):目标检测的示例运行

import mmcv
from mmcv.runner import load_checkpoint

from mmdet.apis import inference_detector, show_result_pyplot
from mmdet.models import build_detector

# Choose to use a config and initialize the detector
config = 'configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco.py'
# Setup a checkpoint file to load
checkpoint = 'checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth'

# Set the device to be used for evaluation
device='cuda:0'

# Load the config
config = mmcv.Config.fromfile(config)
# Set pretrained to be None since we do not need pretrained model here
config.model.pretrained = None

# Initialize the detector
model = build_detector(config.model)

# Load checkpoint
checkpoint = load_checkpoint(model, checkpoint, map_location=device)

# Set the classes of models for inference
model.CLASSES = checkpoint['meta']['CLASSES']

# We need to set the model's cfg for inference
model.cfg = config

# Convert the model to GPU
model.to(device)
# Convert the model into evaluation mode
model.eval()

# Use the detector to do inference
img = 'demo/demo.jpg'
result = inference_detector(model, img)

# Let's plot the result
show_result_pyplot(model, img, result, score_thr=0.3)
  • 注意:运行可能会报以下错误:
    UserWarning: “ImageToTensor” pipeline is replaced by “DefaultFormatBundle” for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.
    ‘data pipeline in your config file.’, UserWarning)
    官方对该警告的建议是不用搭理!不影响代码运行。
  • 运行结果:
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/gaoqing_dream163/article/details/126462891