使用keras的函数plot_model可视化模型
from keras.utils import plot_model
plot_model(model, to_file='model.png')
运行结果出错
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
in
43
44 # Plot model graph
---> 45 plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')
46 Image(retina=True, filename='model.png')
E:\Software\Anaconda3\envs\tensorkeras\lib\site-packages\keras\utils\vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
130 'LR' creates a horizontal plot.
131 """
--> 132 dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
133 _, extension = os.path.splitext(to_file)
134 if not extension:
E:\Software\Anaconda3\envs\tensorkeras\lib\site-packages\keras\utils\vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
53 from ..models import Sequential
54
---> 55 _check_pydot()
56 dot = pydot.Dot()
57 dot.set('rankdir', rankdir)
E:\Software\Anaconda3\envs\tensorkeras\lib\site-packages\keras\utils\vis_utils.py in _check_pydot()
18 if pydot is None:
19 raise ImportError(
---> 20 'Failed to import `pydot`. '
21 'Please install `pydot`. '
22 'For example with `pip install pydot`.')
ImportError: Failed to import `pydot`. Please install `pydot`. For example with `pip install pydot`.
解决步骤:(如果项目文件使用的是自己创建的虚拟环境请切换到相应环境安装)
1.anaconda安装graphviz
conda install graphviz
2.windows需安装软件(软件下载地址:https://graphviz.gitlab.io/_pages/Download/Download_windows.html)
安装完后将软件安装目录如( E:\Software\Graphviz2.38\ )后加上bin 如(E:\Software\Graphviz2.38\bin)添加到path变量中
3.安装pydot-ng
conda下安装不了pydot-ng说冲突,使用 pip 安装
pip install pydot-ng
4.修改配置
import pydot_ng 按住Ctrl键点击pydot_ng 进入到__intit__.py中,首先找到find_graphviz()的三个method。
将第一个method注释掉,第二个method不用管,第三个method(pydot-ng1.0.0版本大约在581行左右,可自行搜索)要修改,添加一句代码即可,path='安装graphviz的bin目录',也就是第二步设置的环境变量,修改部分的完整代码如下(!!!下面中文括号内是需要修改的path)
# # Method 1 (Windows only)
# if os.sys.platform == 'win32':
#
# HKEY_LOCAL_MACHINE = 0x80000002
# KEY_QUERY_VALUE = 0x0001
#
# RegOpenKeyEx = None
# RegQueryValueEx = None
# RegCloseKey = None
#
# try:
# import win32api
# RegOpenKeyEx = win32api.RegOpenKeyEx
# RegQueryValueEx = win32api.RegQueryValueEx
# RegCloseKey = win32api.RegCloseKey
#
# except ImportError:
# # Print a messaged suggesting they install these?
# pass
#
# try:
# import ctypes
#
# def RegOpenKeyEx(key, subkey, opt, sam):
# result = ctypes.c_uint(0)
# ctypes.windll.advapi32.RegOpenKeyExA(key, subkey, opt, sam,
# ctypes.byref(result))
# return result.value
#
# def RegQueryValueEx(hkey, valuename):
# data_type = ctypes.c_uint(0)
# data_len = ctypes.c_uint(1024)
# data = ctypes.create_string_buffer(1024)
#
# # this has a return value, which we should probably check
# ctypes.windll.advapi32.RegQueryValueExA(
# hkey, valuename, 0, ctypes.byref(data_type),
# data, ctypes.byref(data_len))
#
# return data.value
#
# RegCloseKey = ctypes.windll.advapi32.RegCloseKey
#
# except ImportError:
# # Print a messaged suggesting they install these?
# pass
#
# if RegOpenKeyEx is not None:
# # Get the GraphViz install path from the registry
# hkey = None
# potentialKeys = [
# "SOFTWARE\\ATT\\Graphviz",
# "SOFTWARE\\AT&T Research Labs\\Graphviz"]
# for potentialKey in potentialKeys:
#
# try:
# hkey = RegOpenKeyEx(
# HKEY_LOCAL_MACHINE,
# potentialKey, 0, KEY_QUERY_VALUE)
#
# if hkey is not None:
# path = RegQueryValueEx(hkey, "InstallPath")
# RegCloseKey(hkey)
#
# # The regitry variable might exist, left by
# # old installations but with no value, in those cases
# # we keep searching...
# if not path:
# continue
#
# # Now append the "bin" subdirectory:
# path = os.path.join(path, "bin")
# progs = __find_executables(path)
# if progs is not None:
# return progs
#
# except Exception:
# pass
# else:
# break
# Method 2 (Linux, Windows etc)
if 'PATH' in os.environ:
for path in os.environ['PATH'].split(os.pathsep):
progs = __find_executables(path)
if progs is not None:
return progs
# Method 3 (Windows only)
if os.sys.platform == 'win32':
# Try and work out the equivalent of "C:\Program Files" on this
# machine (might be on drive D:, or in a different language)
if 'PROGRAMFILES' in os.environ:
# Note, we could also use the win32api to get this
# information, but win32api may not be installed.
path = os.path.join(os.environ['PROGRAMFILES'], 'ATT',
'GraphViz', 'bin')
else:
# Just in case, try the default...
path = r"C:\Program Files\att\Graphviz\bin"
path = (这里需要自行修改路径) #如path = r"E:\Software\Graphviz2.38\bin"
progs = __find_executables(path)
接下来运行plot_model还是会报错,所以进入报错路径"E:\Software\Anaconda3\envs\tensorkeras\lib\site-packages\keras\utils\vis_utils.py"(这里路径不唯一,自行点开)
在vis_util文件的10~13行左右有
try:
import pydot
except ImportError:
pydot = None
我们只安装了pydot_ng所以会报错,可以将 import pydot 改成import pydot_ng as pydot
如
try:
import pydot_ng as pydot
except ImportError:
pydot = None
这样就不会报错了。