Mmdetection RuntimeError(‘Invalid DISPLAY variable’)
from mmdet.apis import init_detector, inference_detector
import mmcv
# Specify the path to model config and checkpoint file
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results
img = 'test.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
#model.show_result(img, result)
# or save the visualization results to image files
model.show_result(img, result, out_file='result.jpg')
Traceback (most recent call last):
File "1.py", line 34, in <module>
img2 = show_result_pyplot(model, img, result, score_thr=0.8)
File "1.py", line 18, in show_result_pyplot
img = model.show_result(img, result, score_thr=score_thr, show=False)
File "/root/anaconda3/lib/python3.6/site-packages/mmdet-2.19.0-py3.6.egg/mmdet/models/detectors/base.py", line 353, in show_result
out_file=out_file)
File "/root/anaconda3/lib/python3.6/site-packages/mmdet-2.19.0-py3.6.egg/mmdet/core/visualization/image.py", line 119, in imshow_det_bboxes
fig = plt.figure(win_name, frameon=False)
File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 548, in figure
**kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 161, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 167, in new_figure_manager_given_figure
canvas = cls.FigureCanvas(figure)
File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 24, in __init__
super(FigureCanvasQTAgg, self).__init__(figure=figure)
File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 234, in __init__
_create_qApp()
File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 125, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
问题原因
matplotlib 在 windows 下的默认 backend 是 TkAgg;在 Linux 下的默认 backend 是 Qt5Agg。但是这两个 backend 都要求有 GUI 图形界面,所以在 Linux 服务器上运行时会报错。
解决方案
vi mmdet/core/visualization/image.py
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
cd mmdetection
python setup.py develop #修改参数和代码需要重新编译