keras模型结构可视化报错('`pydot` failed to call GraphViz.')

@[keras]keras模型结构可视化报错(’pydot failed to call GraphViz.’)

问题:输入plot_model(model,to_file=‘model.png’),报错
OSError: pydot failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.
参考了很多大神的博客,最后针对个人遇到的问题总结如下:

  1. 在python中安装graphviz:pip install graphviz
  2. 在graphviz官网下载界面http://www.graphviz.org/download/选择自己操作系统对应的版本的.msi文件,Windows的下载链接是https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi,安装完成后将安装的Graphviz2.38/bin添加环境变量。
  3. 在python中安装pydot:pip install pydot==1.1.0,注意一定是pydot==1.1.0,不能只是pip install pydot,最新版本的pydot不适用于这里介绍的解决方法,会影响下面的操作。
  4. 在C:\Anaconda2\Lib\site-packages中找到pydot.py文件,按照下面的方式修改代码,注释掉Method1,注意Method3中添加的代码,Method3中添加的路径即graphviz的安装路径,修改之前最好将原版本备份。
 # 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, win32con
#            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 )
#
#                res = 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 :
#                            #print "Used Windows registry"
#                            return progs
#
#                except Exception, excp:
#                    #raise excp
#                    pass
#                else:
#                    break



    # Method 2 (Linux, Windows etc)
    #
    if os.environ.has_key('PATH'):

        for path in os.environ['PATH'].split(os.pathsep):
            progs = __find_executables(path)
            if progs is not None :
                #print "Used path"
                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 os.environ.has_key('PROGRAMFILES'):

            # 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"

        progs = __find_executables(path)

        if progs is not None :

            #print "Used default install location"
            return progs

    #添加这部分代码,path直接指向graphviz安装路径
    path = r"D:\Graphviz2.38\bin"

    progs = __find_executables(path)
    
    if progs is not None :
        
        #print "Used default install location"
        return progs        
    
    for path in (
        '/usr/bin', '/usr/local/bin',
        '/opt/local/bin',
        '/opt/bin', '/sw/bin', '/usr/share',
        '/Applications/Graphviz.app/Contents/MacOS/' ):

        progs = __find_executables(path)
        if progs is not None :
            #print "Used path"
            return progs

    # Failed to find GraphViz
    #
    return None
  1. 加载模型,输入
from keras.utils import plot_model
plot_model(model, to_file='model.png',show_shapes=True)

即可得
在这里插入图片描述


参考博客:
https://blog.csdn.net/lwy_520/article/details/81479486
https://blog.csdn.net/da_kao_la/article/details/80025124
https://stackoverflow.com/questions/38446771/importing-theano-attributeerror-module-object-has-no-attribute-find-graphvi
https://blog.csdn.net/sinat_37998852/article/details/80507536
https://blog.csdn.net/wangjian1204/article/details/50346457

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值