简体   繁体   English

将Tensorflow对象检测与keras CNN分类器集成

[英]Integrating Tensorflow object detection with keras cnn classifier

My problem is to detect object using mobilenet SSD and then to read data's from the bounding boxes using CNN classifier trained in Keras.everytime after the bounding box is obtained it has to be evaluated with CNN classifier. 我的问题是使用mobilenet SSD检测对象,然后使用在Keras中训练的CNN分类器从边界框中读取数据。每次获得边界框后,都必须使用CNN分类器对其进行评估。 On referring number of questions in git and stack i started write my own coding.however after resetting tf graph using tf.reset_default_graph() and on loading .h5 from keras it throws an error as 在引用git和stack中的问题数量时,我开始编写自己的编码。但是使用tf.reset_default_graph()重置tf图并从keras加载.h5后,它会抛出错误

"ValueError: 
 Tensor("loss/dense_6_loss/Const:0", shape=(), 
 dtype=float32) must be from the same graph as 
 Tensor("truediv_19:0", shape=(?, 36), 
 dtype=float32)."

I am doing instance detect and image retrieval task by Keras and Tensorflow as backend. 我正在通过Keras和Tensorflow作为后端来执行实例检测和图像检索任务。

show:ValueError: tensor a must be from the same graph as tensor b. show:ValueError:张量a必须与张量b来自同一图。

The code is as below: 代码如下:

Merge.py
from keras import backend as K

g1=tf.Graph()
g2=Graph()
sess1=tf.Session(graph=g1)
sess2=Session(graph=g2)

def intiMaskrcnn():
     with g1.as_default():
          with sess1.as_default():
               Model1=........
tf.rest_defaut_graph()
def instanceDetect():
     K.set_session(sess1)
     with g1.as_default():
           Model1.predit()
            ............
k.clear_session()

def intiMobilenet():
    with g2.as_default():
         with sess2.as_default():
              Model2=........

def Retrieval():
    K.set_session(sess2)
     with g2.as_default():
         Model2.predit()
           ............

i need to know is it possible to integrate the tf and Keras in one pipeline simultaneously..if possible how?Thaks in advance 我想知道是否可以将tf和Keras同时集成在一个管道中..如果可能的话,如何?

This helps me to run the keras model inside the tensorflow SSD object Detection. 这有助于我在tensorflow SSD对象检测中运行keras模型。

* Use Seperate Graph.
* Use Seperate Session.
* reset the default Graph using tf.reset_default_graph()
* for keras Use K.clear

from keras.models import load_model
with tf.Session(graph=K.get_session().graph) as session:
    session.run(tf.global_variables_initializer())
    model = load_model('model.h5')
    predictions = model.predict(input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM