• Django整合Keras报错:ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph.解决方法


    本人在写Django RESful API时,碰到一个难题,老出现,整合Keras,报如下错误;很纠结,探索找资料近一个星期,皇天不负有心人,解决了

    Internal Server Error: /pic/analysis/
    Traceback (most recent call last):
      File "D:AIPython35libsite-packages	ensorflowpythonclientsession.py", line 1064, in _run
        allow_operation=False)
      File "D:AIPython35libsite-packages	ensorflowpythonframeworkops.py", line 3035, in as_graph_element
        return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
      File "D:AIPython35libsite-packages	ensorflowpythonframeworkops.py", line 3114, in _as_graph_element_locked
        raise ValueError("Tensor %s is not an element of this graph." % obj)
    ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph.
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "D:AIPython35libsite-packagesdjangocorehandlersexception.py", line 35, in inner
        response = get_response(request)
      File "D:AIPython35libsite-packagesdjangocorehandlersase.py", line 128, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "D:AIPython35libsite-packagesdjangocorehandlersase.py", line 126, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "D:AIPython35libsite-packagesdjango	estutils.py", line 371, in inner
        return func(*args, **kwargs)
      File "E:PyCharmWorkPythonWebAppApiPicDjangoSitePicWebviews.py", line 25, in picAnalysis
        predictBank(im, name)  #对图片进行预测,并返回预测结果
      File "E:PyCharmWorkPythonWebAppApiPicDjangoSitePicWebPreDictBankFunc.py", line 42, in predictBank
        model = load_model('staticCnnBankUp.h5', compile=False)
      File "D:AIPython35libsite-packageskerasmodels.py", line 243, in load_model
        topology.load_weights_from_hdf5_group(f['model_weights'], model.layers)
      File "D:AIPython35libsite-packageskerasengine	opology.py", line 3142, in load_weights_from_hdf5_group
        K.batch_set_value(weight_value_tuples)
      File "D:AIPython35libsite-packageskerasackend	ensorflow_backend.py", line 2247, in batch_set_value
        get_session().run(assign_ops, feed_dict=feed_dict)
      File "D:AIPython35libsite-packages	ensorflowpythonclientsession.py", line 889, in run
        run_metadata_ptr)
      File "D:AIPython35libsite-packages	ensorflowpythonclientsession.py", line 1067, in _run
        + e.args[0])
    TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) is not an element of t
    his graph.
    [08/Jan/2018 08:53:15] "POST /pic/analysis/ HTTP/1.1" 500 159200

    个人理解:在初始化Django时,把keras中 model先初始化,免得后面不断调用,产生莫名其妙的问题

    Django初始化代码写在__init__.py中:

    from keras.models import load_model
    import numpy as np
    
    #参考:https://zhuanlan.zhihu.com/p/27101000
    
    print('load model...')
    model = load_model('static\CnnBankUp.h5', compile=False)
    print('load done.')
    
    #一定要添加这段代码,先测试一下,可以避免ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 1, 32), dtype=float32) 
    #is not an element of this graph.的错误
    print('test model...')
    #根据自己传入图片格式定义np.zeros()
    print(model.predict(np.zeros((2, 200,200,1)))) print('test done.') # 使用模型,在得到用户输入时会调用以下两个函数进行实时文本分类 # 输入参数 comment 为经过了分词与向量化处理后的模型输入 def picType_class(comment): global model result_vec=None result_vec = model.predict(comment) return result_vec

    很有用的参考,非常感谢

    参考:https://zhuanlan.zhihu.com/p/27101000

  • 相关阅读:
    no module name cx_oracle 的解决方法
    开通博客
    普通用户启动Hadoop格式化namenode出现无法创建目录的问题
    改写文件权限时出现问题___2
    suse添加普通用户赋予root所有权限时出现问题___1
    suse系统vim未正常退出产生的问题(can't write viminfo file /home/zhaoy/.viminfo)
    intellij idea根据mvn仓库添加或改变scala-sdk
    git拉项目和上传项目时遇到的一些问题
    简单的clone项目fromGitHub
    初始机器学习
  • 原文地址:https://www.cnblogs.com/yanjj/p/8242595.html
Copyright © 2020-2023  润新知