Matplotlib backend shifts when using object detection API, QtAgg to Agg

Viewed 493

The figure doesn't show up when using matplotlib with object detection API, But it works fine before I import some test method.

I'm using Anaconda virtual envionment, python 3.6 and the google object detection API.

import matplotlib.pyplot as plt

import tensorflow as tf

from matplotlib import patches

from object_detection.anchor_generators.multiple_grid_anchor_generator import create_ssd_anchors

from object_detection.models.ssd_mobilenet_v2_feature_extractor_test import SsdMobilenetV2FeatureExtractorTest

from object_detection.models.ssd_mobilenet_v2_feature_extractor_test import SsdMobilenetV2FeatureExtractorTest

The change happens when it comes to the last line

from object_detection.models.ssd_mobilenet_v2_feature_extractor_test import SsdMobilenetV2FeatureExtractorTest"

Before, I can show the figure, like plt.subplot(2,2), I get the figure pop up and the following:

(<Figure size 640x480 with 4 Axes>,
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f4615eb8ba8>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f4614d320f0>],
    [<matplotlib.axes._subplots.AxesSubplot object at 0x7f4614ce36a0>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f4614c92c50>]],
   dtype=object))
WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons

If you depend on functionality not listed there, please file an issue.

and "plt.get_backend()" shows "Qt5Agg". but after the last line, the figure doesn't pop up, although I do get results from "plt.subplots(2,2)":

(<Figure size 640x480 with 4 Axes>,
 array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f460b8a5f60>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f460b86b5c0>],
    [<matplotlib.axes._subplots.AxesSubplot object at 0x7f460a7b2fd0>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x7f460a7cb630>]],
   dtype=object))

Now when I type "plt.get_backend()", it shows "Agg", not the previous "Qt5Agg". and "plt.show()" throws an error:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  #!/usr/bin/env python2

the interpreter I was using is

~/anaconda3/envs/py36/bin/python

Is there a shift of environment here?

The script for the last line can be found here

Thanks a lot for your time!

2 Answers

Solved. Have found this line in ~/models/research/object_detecion/utils/visualization_utils.py :

import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements

just comment it and works fine.

Solved. I am running locally on a virtual enironment and tried several suggested solution but with no success. I tried what Yunfei Fang was suggesting above but then realized that by, commenting out the line of code import matplotlib; matplotlib.use('Agg') in the ~/models/research/object_detecion/utils/visualization_utils.py folder wasn't enough. I found out by putting this code in the body of my script: print('matplotlib backend= ',matplotlib.get_backend()) as this was still returning "agg" so I had to add this matplotlib.use('MacOSX') at the end of the import section of the script. I am running on a MacBook Pro with macOS Big Sur

Related