Jupyter notebook 運行 tensorflow報告以下錯誤:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
參考stackoverflow解決:
https://stackoverflow.com/questions/3285193/how-to-switch-backends-in-matplotlib-python
其中的代碼用來檢測可用的agg環境:
import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
non_gui_backends = matplotlib.rcsetup.non_interactive_bk
print ("Non Gui backends are:", non_gui_backends)
print ("Gui backends I will test for", gui_env)
for gui in gui_env:
print ("testing", gui)
try:
matplotlib.use(gui,warn=False, force=True)
from matplotlib import pyplot as plt
print (" ",gui, "Is Available")
plt.plot([1.5,2.0,2.5])
fig = plt.gcf()
fig.suptitle(gui)
plt.show()
print ("Using ..... ",matplotlib.get_backend())
except:
print (" ",gui, "Not found")
立刻會彈出如下的圖片:
說明在本機有Qt5Agg可用,所以修改原代碼爲:
import numpy as np
import os
import tensorflow as tf
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib import pyplot as plt
from PIL import Image
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
注意其中的第4行和第5行爲新添加的。
參考環境:
Windows 7 7601 sp1,
Tensorflow1.10.0,
Jupyter notebook server is: 5.7.8
Python 3.6.8
<end>